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 9b52286769a..5798f020fb5 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 void reset() throws IOException { @Override public void close() throws IOException { + is.close(); } }; } @@ -199,6 +200,11 @@ public int available() throws IOException { 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 b4dc1b61821..2f8595fef87 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 void testMarkSupportedInputStream() throws Exception { is.reset(); assertEquals(-1, is.read()); assertEquals(-1, is.read()); + + is.close(); } @Test @@ -118,35 +120,54 @@ public void testLimitedInputStream() throws Exception { @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