Skip to content

[ISSUE #9447] Avoiding possible resource leaks in InputStream #9459

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

Merged
merged 1 commit into from
Jun 30, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ public SystemConfigFileHelper() {
}

public Properties loadConfig() throws Exception {
InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(file)));
Properties properties = new Properties();
properties.load(in);
in.close();
try (InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(file)))) {
properties.load(in);
}
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,11 @@ public SystemConfigFileHelper() {
}

public Properties loadConfig() throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(file));
Properties properties = new Properties();
properties.load(in);
in.close();

try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
properties.load(in);
}
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public byte[] encode(boolean compress) {
}
long start = System.currentTimeMillis();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream outputStream = new DeflaterOutputStream(byteArrayOutputStream, new Deflater(Deflater.BEST_COMPRESSION));
DataVersion dataVersion = topicConfigSerializeWrapper.getDataVersion();
ConcurrentMap<String, TopicConfig> topicConfigTable = cloneTopicConfigTable(topicConfigSerializeWrapper.getTopicConfigTable());
assert topicConfigTable != null;
try {
try (DeflaterOutputStream outputStream = new DeflaterOutputStream(byteArrayOutputStream, new Deflater(Deflater.BEST_COMPRESSION))) {
DataVersion dataVersion = topicConfigSerializeWrapper.getDataVersion();
ConcurrentMap<String, TopicConfig> topicConfigTable = cloneTopicConfigTable(topicConfigSerializeWrapper.getTopicConfigTable());
assert topicConfigTable != null;
byte[] buffer = dataVersion.encode();

// write data version
Expand Down Expand Up @@ -117,58 +116,56 @@ public static RegisterBrokerBody decode(byte[] data, boolean compressed, MQVersi
return RegisterBrokerBody.decode(data, RegisterBrokerBody.class);
}
long start = System.currentTimeMillis();
InflaterInputStream inflaterInputStream = new InflaterInputStream(new ByteArrayInputStream(data));
int dataVersionLength = readInt(inflaterInputStream);
byte[] dataVersionBytes = readBytes(inflaterInputStream, dataVersionLength);
DataVersion dataVersion = DataVersion.decode(dataVersionBytes, DataVersion.class);

RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();
registerBrokerBody.getTopicConfigSerializeWrapper().setDataVersion(dataVersion);
ConcurrentMap<String, TopicConfig> topicConfigTable = registerBrokerBody.getTopicConfigSerializeWrapper().getTopicConfigTable();

int topicConfigNumber = readInt(inflaterInputStream);
LOGGER.debug("{} topic configs to extract", topicConfigNumber);

for (int i = 0; i < topicConfigNumber; i++) {
int topicConfigJsonLength = readInt(inflaterInputStream);

byte[] buffer = readBytes(inflaterInputStream, topicConfigJsonLength);
TopicConfig topicConfig = new TopicConfig();
String topicConfigJson = new String(buffer, MixAll.DEFAULT_CHARSET);
topicConfig.decode(topicConfigJson);
topicConfigTable.put(topicConfig.getTopicName(), topicConfig);
}

int filterServerListJsonLength = readInt(inflaterInputStream);

byte[] filterServerListBuffer = readBytes(inflaterInputStream, filterServerListJsonLength);
String filterServerListJson = new String(filterServerListBuffer, MixAll.DEFAULT_CHARSET);
List<String> filterServerList = new ArrayList<>();
try {
filterServerList = JSON.parseArray(filterServerListJson, String.class);
} catch (Exception e) {
LOGGER.error("Decompressing occur Exception {}", filterServerListJson);
}

registerBrokerBody.setFilterServerList(filterServerList);
try (InflaterInputStream inflaterInputStream = new InflaterInputStream(new ByteArrayInputStream(data))) {
int dataVersionLength = readInt(inflaterInputStream);
byte[] dataVersionBytes = readBytes(inflaterInputStream, dataVersionLength);
DataVersion dataVersion = DataVersion.decode(dataVersionBytes, DataVersion.class);

RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();
registerBrokerBody.getTopicConfigSerializeWrapper().setDataVersion(dataVersion);
ConcurrentMap<String, TopicConfig> topicConfigTable = registerBrokerBody.getTopicConfigSerializeWrapper().getTopicConfigTable();

int topicConfigNumber = readInt(inflaterInputStream);
LOGGER.debug("{} topic configs to extract", topicConfigNumber);

for (int i = 0; i < topicConfigNumber; i++) {
int topicConfigJsonLength = readInt(inflaterInputStream);
byte[] buffer = readBytes(inflaterInputStream, topicConfigJsonLength);
TopicConfig topicConfig = new TopicConfig();
String topicConfigJson = new String(buffer, MixAll.DEFAULT_CHARSET);
topicConfig.decode(topicConfigJson);
topicConfigTable.put(topicConfig.getTopicName(), topicConfig);
}

if (brokerVersion.ordinal() >= MQVersion.Version.V5_0_0.ordinal()) {
int topicQueueMappingNum = readInt(inflaterInputStream);
Map<String/* topic */, TopicQueueMappingInfo> topicQueueMappingInfoMap = new ConcurrentHashMap<>();
for (int i = 0; i < topicQueueMappingNum; i++) {
int mappingJsonLen = readInt(inflaterInputStream);
byte[] buffer = readBytes(inflaterInputStream, mappingJsonLen);
TopicQueueMappingInfo info = TopicQueueMappingInfo.decode(buffer, TopicQueueMappingInfo.class);
topicQueueMappingInfoMap.put(info.getTopic(), info);
int filterServerListJsonLength = readInt(inflaterInputStream);
byte[] filterServerListBuffer = readBytes(inflaterInputStream, filterServerListJsonLength);
String filterServerListJson = new String(filterServerListBuffer, MixAll.DEFAULT_CHARSET);
List<String> filterServerList = new ArrayList<>();
try {
filterServerList = JSON.parseArray(filterServerListJson, String.class);
} catch (Exception e) {
LOGGER.error("Decompressing occur Exception {}", filterServerListJson, e);
}
registerBrokerBody.setFilterServerList(filterServerList);

if (brokerVersion.ordinal() >= MQVersion.Version.V5_0_0.ordinal()) {
int topicQueueMappingNum = readInt(inflaterInputStream);
Map<String, TopicQueueMappingInfo> topicQueueMappingInfoMap = new ConcurrentHashMap<>();
for (int i = 0; i < topicQueueMappingNum; i++) {
int mappingJsonLen = readInt(inflaterInputStream);
byte[] buffer = readBytes(inflaterInputStream, mappingJsonLen);
TopicQueueMappingInfo info = TopicQueueMappingInfo.decode(buffer, TopicQueueMappingInfo.class);
topicQueueMappingInfoMap.put(info.getTopic(), info);
}
registerBrokerBody.getTopicConfigSerializeWrapper().setTopicQueueMappingInfoMap(topicQueueMappingInfoMap);
}
registerBrokerBody.getTopicConfigSerializeWrapper().setTopicQueueMappingInfoMap(topicQueueMappingInfoMap);
}

long takeTime = System.currentTimeMillis() - start;
if (takeTime > MINIMUM_TAKE_TIME_MILLISECOND) {
LOGGER.info("Decompressing takes {}ms", takeTime);
long takeTime = System.currentTimeMillis() - start;
if (takeTime > MINIMUM_TAKE_TIME_MILLISECOND) {
LOGGER.info("Decompressing takes {}ms", takeTime);
}
return registerBrokerBody;
}
return registerBrokerBody;
}

private static byte[] convertIntToByteArray(int n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ public void checkDuplicatedMessageInfo(boolean bPrintLog,
if (bPrintLog) {
String logFileNameStr = "D:" + File.separator + "checkDuplicatedMessageInfo.txt";
File logFileNameFile = new File(logFileNameStr);
OutputStream out = new FileOutputStream(logFileNameFile, true);
try (OutputStream out = new FileOutputStream(logFileNameFile, true)) {

String strToWrite;
byte[] byteToWrite;
strToWrite = strBuilder + titleString;
for (int i = 0; i < msgListSize; i++)
strToWrite += strBQueue.get(i).toString() + "\r\n";
String strToWrite;
byte[] byteToWrite;
strToWrite = strBuilder + titleString;
for (int i = 0; i < msgListSize; i++)
strToWrite += strBQueue.get(i).toString() + "\r\n";

byteToWrite = strToWrite.getBytes(StandardCharsets.UTF_8);
out.write(byteToWrite);
out.close();
byteToWrite = strToWrite.getBytes(StandardCharsets.UTF_8);
out.write(byteToWrite);
}
}
}

Expand Down
Loading