ContentLengthInputStream extends InputStream { @Override public void close() throws IOException { if (!closed) { try { if (pos < contentLength) { final byte[] buffer = new byte[BUFFER_SIZE]; while (read(buffer) >= 0) { } } } finally { closed = true; } } } ContentLengthInputStream(final SessionInputBuffer buffer, final InputStream inputStream, final long contentLength); @Override void close(); @Override int available(); @Override int read(); @Override int read(final byte[] b, final int off, final int len); @Override int read(final byte[] b); @Override long skip(final long n);  }
@Test public void testClose() throws IOException { final String s = "1234567890123456-"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 16L); in.close(); in.close(); try { in.read(); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } final byte[] tmp = new byte[10]; try { in.read(tmp); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } try { in.read(tmp, 0, tmp.length); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } Assert.assertEquals('-', inBuffer.read(inputStream)); }