[fix](io): use try with resource make io stream close automatically to avoid resource leak (#24297)

This commit is contained in:
xu tao
2023-09-14 11:51:30 +08:00
committed by GitHub
parent 25763d0ed9
commit f8692bef4b
10 changed files with 37 additions and 43 deletions

View File

@ -133,7 +133,9 @@ public class ConfigBase {
private void initConf(String confFile) throws Exception {
Properties props = new Properties();
props.load(new FileReader(confFile));
try (FileReader fr = new FileReader(confFile)) {
props.load(fr);
}
replacedByEnv(props);
setFields(props, isLdapConfig);
}
@ -398,7 +400,9 @@ public class ConfigBase {
}
Properties props = new Properties();
props.load(new FileReader(customConfFile));
try (FileReader fr = new FileReader(customConfFile)) {
props.load(fr);
}
for (Map.Entry<String, String> entry : customConf.entrySet()) {
props.setProperty(entry.getKey(), entry.getValue());

View File

@ -44,18 +44,15 @@ public class DeepCopy {
metaContext.setMetaVersion(metaVersion);
metaContext.setThreadLocalInfo();
FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteArrayOutputStream);
try {
try (FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteArrayOutputStream)) {
orig.write(out);
out.flush();
out.close();
DataInputStream in = new DataInputStream(byteArrayOutputStream.getInputStream());
Method readMethod = c.getDeclaredMethod(READ_FIELDS_METHOD_NAME, DataInput.class);
readMethod.invoke(dest, in);
in.close();
try (DataInputStream in = new DataInputStream(byteArrayOutputStream.getInputStream())) {
Method readMethod = c.getDeclaredMethod(READ_FIELDS_METHOD_NAME, DataInput.class);
readMethod.invoke(dest, in);
}
} catch (Exception e) {
LOG.warn("failed to copy object.", e);
return false;
@ -74,19 +71,16 @@ public class DeepCopy {
metaContext.setMetaVersion(metaVersion);
metaContext.setThreadLocalInfo();
FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteArrayOutputStream);
try {
try (FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteArrayOutputStream)) {
orig.write(out);
out.flush();
out.close();
DataInputStream in = new DataInputStream(byteArrayOutputStream.getInputStream());
Method readMethod = c.getDeclaredMethod(READ_METHOD_NAME, DataInput.class);
T result = (T) readMethod.invoke(orig, in);
in.close();
return result;
try (DataInputStream in = new DataInputStream(byteArrayOutputStream.getInputStream())) {
Method readMethod = c.getDeclaredMethod(READ_METHOD_NAME, DataInput.class);
T result = (T) readMethod.invoke(orig, in);
return result;
}
} catch (Exception e) {
LOG.warn("failed to copy object.", e);
return null;