Skip to content

Commit

Permalink
[Dubbo-2413] Fix StreamUtils resource leak (apache#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
biyuhao authored and jerrick-zhu committed Sep 6, 2018
1 parent ff36fb0 commit d173e84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void reset() throws IOException {

@Override
public void close() throws IOException {
is.close();
}
};
}
Expand Down Expand Up @@ -199,6 +200,11 @@ public int available() throws IOException {

return available;
}

@Override
public void close() throws IOException {
is.close();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public void testMarkSupportedInputStream() throws Exception {
is.reset();
assertEquals(-1, is.read());
assertEquals(-1, is.read());

is.close();
}

@Test
Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit d173e84

Please sign in to comment.