This is an automated email from the ASF dual-hosted git repository. jerrick pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new d173e84 [Dubbo-2413] Fix StreamUtils resource leak (#2414) d173e84 is described below commit d173e841c41704d13e7357eb5704764353384963 Author: Yuhao Bi <byh0...@gmail.com> AuthorDate: Thu Sep 6 19:22:51 2018 +0800 [Dubbo-2413] Fix StreamUtils resource leak (#2414) --- .../org/apache/dubbo/common/io/StreamUtils.java | 6 +++ .../apache/dubbo/common/io/StreamUtilsTest.java | 47 ++++++++++++++++------ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java index 9b52286..5798f02 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java @@ -98,6 +98,7 @@ public class StreamUtils { @Override public void close() throws IOException { + is.close(); } }; } @@ -199,6 +200,11 @@ public class StreamUtils { return available; } + + @Override + public void close() throws IOException { + is.close(); + } }; } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java index b4dc1b6..2f8595f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java @@ -79,6 +79,8 @@ public class StreamUtilsTest { is.reset(); assertEquals(-1, is.read()); assertEquals(-1, is.read()); + + is.close(); } @Test @@ -118,35 +120,54 @@ public class StreamUtilsTest { @Test(expected = IOException.class) public void testMarkInputSupport() throws IOException { InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - is = StreamUtils.markSupportedInputStream(new PushbackInputStream(is), 1); - - is.mark(1); - int read = is.read(); - assertThat(read, is((int) '0')); - - is.skip(1); - is.read(); + try { + is = StreamUtils.markSupportedInputStream(new PushbackInputStream(is), 1); + + is.mark(1); + int read = is.read(); + assertThat(read, is((int) '0')); + + is.skip(1); + is.read(); + } finally { + if (is != null) { + is.close(); + } + } } @Test - public void testSkipForOriginMarkSupportInput() { + public void testSkipForOriginMarkSupportInput() throws IOException { InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); InputStream newIs = StreamUtils.markSupportedInputStream(is, 1); assertThat(newIs, is(is)); + is.close(); } @Test(expected = NullPointerException.class) public void testReadEmptyByteArray() throws IOException { InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - is = StreamUtils.limitedInputStream(is, 2); - is.read(null, 0, 1); + try { + is = StreamUtils.limitedInputStream(is, 2); + is.read(null, 0, 1); + } finally { + if (is != null) { + is.close(); + } + } } @Test(expected = IndexOutOfBoundsException.class) public void testReadWithWrongOffset() throws IOException { InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - is = StreamUtils.limitedInputStream(is, 2); - is.read(new byte[1], -1, 1); + try { + is = StreamUtils.limitedInputStream(is, 2); + is.read(new byte[1], -1, 1); + } finally { + if (is != null) { + is.close(); + } + } } } \ No newline at end of file