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

This commit is contained in:
xu tao
2023-09-20 11:39:03 +08:00
committed by GitHub
parent 848290d8a8
commit b7ca4fcc8d
9 changed files with 37 additions and 45 deletions

View File

@ -450,7 +450,7 @@ public class Repository implements Writable {
// Preconditions.checkArgument(remoteFilePath.startsWith(location), remoteFilePath);
// get md5usm of local file
File file = new File(localFilePath);
String md5sum = null;
String md5sum;
try (FileInputStream fis = new FileInputStream(file)) {
md5sum = DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {

View File

@ -418,10 +418,10 @@ public class SmallFileMgr implements Writable {
}
file.createNewFile();
byte[] decoded = Base64.getDecoder().decode(smallFile.content);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(decoded);
outputStream.flush();
outputStream.close();
try (FileOutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(decoded);
outputStream.flush();
}
if (!checkMd5(file, smallFile.md5)) {
throw new DdlException("write file " + fileName
@ -436,7 +436,7 @@ public class SmallFileMgr implements Writable {
}
private boolean checkMd5(File file, String expectedMd5) throws DdlException {
String md5sum = null;
String md5sum;
try (FileInputStream fis = new FileInputStream(file)) {
md5sum = DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {

View File

@ -252,7 +252,7 @@ public class SparkRepository {
public String getMd5String(String filePath) throws LoadException {
File file = new File(filePath);
String md5sum = null;
String md5sum;
try (FileInputStream fis = new FileInputStream(file)) {
md5sum = DigestUtils.md5Hex(fis);
Preconditions.checkNotNull(md5sum);

View File

@ -126,8 +126,7 @@ public class MetaHelper {
public static String getResponse(HttpURLConnection conn) throws IOException {
String response;
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String line;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {

View File

@ -95,7 +95,6 @@ import org.apache.doris.transaction.TransactionStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.List;
@ -1157,12 +1156,6 @@ public class EditLog {
journal.close();
}
public synchronized void createEditLogFile(File name) throws IOException {
EditLogOutputStream editLogOutputStream = new EditLogFileOutputStream(name);
editLogOutputStream.create();
editLogOutputStream.close();
}
public void open() {
journal.open();
}

View File

@ -96,9 +96,9 @@ public class Storage {
Properties prop = new Properties();
File versionFile = getVersionFile();
if (versionFile.isFile()) {
FileInputStream in = new FileInputStream(versionFile);
prop.load(in);
in.close();
try (FileInputStream in = new FileInputStream(versionFile)) {
prop.load(in);
}
clusterID = Integer.parseInt(prop.getProperty(CLUSTER_ID));
if (prop.getProperty(TOKEN) != null) {
token = prop.getProperty(TOKEN);
@ -107,9 +107,9 @@ public class Storage {
File roleFile = getRoleFile();
if (roleFile.isFile()) {
FileInputStream in = new FileInputStream(roleFile);
prop.load(in);
in.close();
try (FileInputStream in = new FileInputStream(roleFile)) {
prop.load(in);
}
role = FrontendNodeType.valueOf(prop.getProperty(FRONTEND_ROLE));
// For compatibility, NODE_NAME may not exist in ROLE file, set nodeName to null
nodeName = prop.getProperty(NODE_NAME, null);

View File

@ -104,12 +104,12 @@ public class HelpModule {
String line;
List<String> lines = Lists.newArrayList();
if (size > 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(zf.getInputStream(entry),
CHARSET_UTF_8));
while ((line = reader.readLine()) != null) {
lines.add(line);
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(zf.getInputStream(entry), CHARSET_UTF_8))) {
while ((line = reader.readLine()) != null) {
lines.add(line);
}
}
reader.close();
// note that we only need basename
String parentPathStr = null;

View File

@ -145,9 +145,9 @@ public class DorisStreamLoader {
// build request and send to new be location
beConn = getConnection(location, label);
// send data to be
BufferedOutputStream bos = new BufferedOutputStream(beConn.getOutputStream());
bos.write(sb.toString().getBytes());
bos.close();
try (BufferedOutputStream bos = new BufferedOutputStream(beConn.getOutputStream())) {
bos.write(sb.toString().getBytes());
}
// get respond
status = beConn.getResponseCode();

View File

@ -343,15 +343,15 @@ public class FileSystemManager {
// different kerberos account has different file
tmpFilePath ="/tmp/." +
principal.replace('/', '_') +
"_" + Long.toString(currentTime) +
"_" + Integer.toString(randNumber) +
"_" + currentTime +
"_" + randNumber +
"_" + Thread.currentThread().getId();
logger.info("create kerberos tmp file" + tmpFilePath);
FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath);
FileLock lock = fileOutputStream.getChannel().lock();
fileOutputStream.write(base64decodedBytes);
lock.release();
fileOutputStream.close();
try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath)) {
FileLock lock = fileOutputStream.getChannel().lock();
fileOutputStream.write(base64decodedBytes);
lock.release();
}
keytab = tmpFilePath;
} else {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_ARGUMENT,
@ -730,10 +730,10 @@ public class FileSystemManager {
long currentTime = System.currentTimeMillis();
Random random = new Random(currentTime);
int randNumber = random.nextInt(10000);
tmpFilePath = "/tmp/." + Long.toString(currentTime) + "_" + Integer.toString(randNumber);
FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath);
fileOutputStream.write(base64decodedBytes);
fileOutputStream.close();
tmpFilePath = "/tmp/." + currentTime + "_" + randNumber;
try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath)) {
fileOutputStream.write(base64decodedBytes);
}
keytab = tmpFilePath;
} else {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_ARGUMENT,
@ -948,10 +948,10 @@ public class FileSystemManager {
long currentTime = System.currentTimeMillis();
Random random = new Random(currentTime);
int randNumber = random.nextInt(10000);
tmpFilePath = "/tmp/." + Long.toString(currentTime) + "_" + Integer.toString(randNumber);
FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath);
fileOutputStream.write(base64decodedBytes);
fileOutputStream.close();
tmpFilePath = "/tmp/." + currentTime + "_" + randNumber;
try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFilePath)) {
fileOutputStream.write(base64decodedBytes);
}
keytab = tmpFilePath;
} else {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_ARGUMENT,