Skip to content

Commit e140c06

Browse files
Fix some potential resource leaks (#15454)
1 parent 9fc81b5 commit e140c06

4 files changed

Lines changed: 34 additions & 28 deletions

File tree

dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public class DubboProtocCompilerMojo extends AbstractMojo {
125125
public void execute() throws MojoExecutionException, MojoFailureException {
126126
Properties versionMatrix = new Properties();
127127
ClassLoader loader = Thread.currentThread().getContextClassLoader();
128-
InputStream stream = loader.getResourceAsStream("version-matrix.properties");
129-
try {
128+
try (InputStream stream = loader.getResourceAsStream("version-matrix.properties")) {
130129
versionMatrix.load(stream);
131130
} catch (IOException e) {
132131
getLog().warn("Unable to load default version matrix", e);

dubbo-plugin/dubbo-native/src/main/java/org/apache/dubbo/aot/generate/JarScanner.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ private void scanURL(String prefixName) {
7777
if ("file".equals(protocol)) {
7878
scanFile(resource.getPath());
7979
} else if ("jar".equals(protocol)) {
80-
JarFile jar = ((JarURLConnection) resource.openConnection()).getJarFile();
81-
scanJar(jar);
80+
try (JarFile jar = ((JarURLConnection) resource.openConnection()).getJarFile()) {
81+
scanJar(jar);
82+
}
8283
}
8384
}
8485
} catch (Throwable ex) {

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/codec/ExchangeCodec.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -327,38 +327,39 @@ protected void encodeResponse(Channel channel, ChannelBuffer buffer, Response re
327327
Bytes.long2bytes(res.getId(), header, 4);
328328

329329
buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
330-
ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
330+
int len;
331+
try (ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer)) {
331332

332-
// encode response data or error message.
333-
if (status == Response.OK) {
334-
if (res.isHeartbeat()) {
335-
// heartbeat response data is always null
336-
bos.write(CodecSupport.getNullBytesOf(serialization));
337-
} else {
338-
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
339-
if (res.isEvent()) {
340-
encodeEventData(channel, out, res.getResult());
333+
// encode response data or error message.
334+
if (status == Response.OK) {
335+
if (res.isHeartbeat()) {
336+
// heartbeat response data is always null
337+
bos.write(CodecSupport.getNullBytesOf(serialization));
341338
} else {
342-
encodeResponseData(channel, out, res.getResult(), res.getVersion());
339+
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
340+
if (res.isEvent()) {
341+
encodeEventData(channel, out, res.getResult());
342+
} else {
343+
encodeResponseData(channel, out, res.getResult(), res.getVersion());
344+
}
345+
out.flushBuffer();
346+
if (out instanceof Cleanable) {
347+
((Cleanable) out).cleanup();
348+
}
343349
}
350+
} else {
351+
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
352+
out.writeUTF(res.getErrorMessage());
344353
out.flushBuffer();
345354
if (out instanceof Cleanable) {
346355
((Cleanable) out).cleanup();
347356
}
348357
}
349-
} else {
350-
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
351-
out.writeUTF(res.getErrorMessage());
352-
out.flushBuffer();
353-
if (out instanceof Cleanable) {
354-
((Cleanable) out).cleanup();
355-
}
356-
}
357358

358-
bos.flush();
359-
bos.close();
359+
bos.flush();
360+
len = bos.writtenBytes();
361+
}
360362

361-
int len = bos.writtenBytes();
362363
checkPayload(channel, len);
363364
Bytes.int2bytes(len, header, 12);
364365
// write

dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.io.FileOutputStream;
2727
import java.io.IOException;
28+
import java.io.InputStream;
2829
import java.nio.file.Files;
2930
import java.nio.file.Path;
3031
import java.nio.file.Paths;
@@ -55,7 +56,9 @@ private void updateConfig(ZookeeperContext context, int clientPort, int adminSer
5556
Properties properties = new Properties();
5657
try {
5758
// use Files.newInputStream instead of new FileInputStream
58-
properties.load(Files.newInputStream(zooSample.toPath()));
59+
try (InputStream is = Files.newInputStream(zooSample.toPath())) {
60+
properties.load(is);
61+
}
5962
properties.setProperty("clientPort", String.valueOf(clientPort));
6063
properties.setProperty("admin.serverPort", String.valueOf(availableAdminServerPort));
6164
Path dataDir = Paths.get(zookeeperConf.getParent().toString(), "data");
@@ -93,7 +96,9 @@ private void updateConfig(ZookeeperContext context, int clientPort, int adminSer
9396
File log4j = Paths.get(zookeeperConf.toString(), "log4j.properties").toFile();
9497
try {
9598
// use Files.newInputStream instead of new FileInputStream
96-
properties.load(Files.newInputStream(log4j.toPath()));
99+
try (InputStream is = Files.newInputStream(log4j.toPath())) {
100+
properties.load(is);
101+
}
97102
Path logDir = Paths.get(zookeeperConf.getParent().toString(), "logs");
98103
if (!Files.exists(logDir)) {
99104
try {

0 commit comments

Comments
 (0)