Allow "release on close" for DataBuffer.asInputStream

This commit introduces DataBuffer.asInputStream(boolean), that can
release the underlying buffer when the stream is closed.

Furthermore, this commit adds additional javadoc.

Issue: SPR-16444
This commit is contained in:
Arjen Poutsma
2018-02-08 10:11:53 +01:00
parent 4318710b9b
commit fb61af1882
5 changed files with 60 additions and 3 deletions

View File

@@ -182,6 +182,27 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void inputStreamReleaseOnClose() throws IOException {
DataBuffer buffer = createDataBuffer(3);
byte[] bytes = {'a', 'b', 'c'};
buffer.write(bytes);
InputStream inputStream = buffer.asInputStream(true);
try {
byte[] result = new byte[3];
int len = inputStream.read(result);
assertEquals(3, len);
assertArrayEquals(bytes, result);
} finally {
inputStream.close();
}
// AbstractDataBufferAllocatingTestCase.LeakDetector will verify the buffer's release
}
@Test
public void outputStream() throws IOException {
DataBuffer buffer = createDataBuffer(4);