Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Files.newInputStream instead of new FileInputStream #2931

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/io/mycat/backend/mysql/CharsetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
Expand Down Expand Up @@ -54,7 +55,7 @@ public class CharsetUtil {
+ "index_to_charset.properties";
Properties prop = new Properties();
try {
prop.load(new FileInputStream(filePath));
prop.load(Files.newInputStream(Paths.get(filePath)));
for (Object index : prop.keySet()){
INDEX_TO_CHARSET.put(Integer.parseInt((String) index), prop.getProperty((String) index));
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/mycat/backend/postgresql/utils/PacketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ public static ByteBuffer makeStartUpPacket(String user, String database)
paramList.add(new String[] { "extra_float_digits", "3" });
paramList.add(new String[] { "application_name", appName });
String[][] params = paramList.toArray(new String[0][]);
StringBuilder details = new StringBuilder();
for (int i = 0; i < params.length; ++i) {
if (i != 0) {
details.append(", ");
}
details.append(params[i][0]);
details.append("=");
details.append(params[i][1]);
}
// StringBuilder details = new StringBuilder();
// for (int i = 0; i < params.length; ++i) {
// if (i != 0) {
// details.append(", ");
// }
// details.append(params[i][0]);
// details.append("=");
// details.append(params[i][1]);
// }

/*
* Precalculate message length and encode params.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package io.mycat.route.sequence.handler;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -61,7 +61,7 @@ public Map<String, String> getParaValMap(String prefixName) {
Map<String, String> valMap = new HashMap<String, String>();
Properties prop = new Properties();
try {
prop.load(new FileInputStream(filePath));
prop.load(Files.newInputStream(Paths.get(filePath)));
valMap.put(prefixName + KEY_HIS_NAME,
prop.getProperty(prefixName + KEY_HIS_NAME));
valMap.put(prefixName + KEY_MIN_NAME,
Expand All @@ -82,7 +82,7 @@ public Map<String, String> getParaValMap(String prefixName) {
public Boolean fetchNextPeriod(String prefixName) {
Properties props = new Properties();
try {
props.load(new FileInputStream(filePath));
props.load(Files.newInputStream(Paths.get(filePath)));
String minStr = props.getProperty(prefixName + KEY_MIN_NAME);
String maxStr = props.getProperty(prefixName + KEY_MAX_NAME);
String hisIDS = props.getProperty(prefixName + KEY_HIS_NAME);
Expand All @@ -95,7 +95,7 @@ public Boolean fetchNextPeriod(String prefixName) {
props.setProperty(prefixName + KEY_MAX_NAME,
(maxId - minId + maxId + 1) + "");
props.setProperty(prefixName + KEY_CUR_NAME, maxStr);
OutputStream fos = new FileOutputStream(filePath);
OutputStream fos = Files.newOutputStream(Paths.get(filePath));
props.store(fos, "");
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
Expand All @@ -108,9 +108,9 @@ public Boolean fetchNextPeriod(String prefixName) {
public synchronized Boolean updateCURIDVal(String prefixName, Long val) {
Properties props = new Properties();
try {
props.load(new FileInputStream(filePath));
props.load(Files.newInputStream(Paths.get(filePath)));
props.setProperty(prefixName + KEY_CUR_NAME, val.longValue() + "");
OutputStream fos = new FileOutputStream(filePath);
OutputStream fos = Files.newOutputStream(Paths.get(filePath));
props.store(fos, "");
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,14 +774,14 @@ private static void deleteFile(String dirPath) {
return;
}
File[] fileList = fileDirToDel.listFiles();

for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if (file.isFile() && file.exists()) {
boolean delete = file.delete();
} else if (file.isDirectory()) {
deleteFile(file.getAbsolutePath());
file.delete();
if (fileList != null) {
for (File file : fileList) {
if (file.isFile() && file.exists()) {
file.delete();
} else if (file.isDirectory()) {
deleteFile(file.getAbsolutePath());
file.delete();
}
}
}
fileDirToDel.delete();
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/io/mycat/util/dataMigrator/DataMigratorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ public static long countLine(File file) throws IOException{
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
if (children != null) {
for (String child : children) {
boolean success = deleteDir(new File(dir, child));
if (!success) {
return false;
}
}
}
}
// 目录此时为空,可以删除
return dir.delete();
Expand Down