ContentLengthInputStream extends InputStream { @Override public int available() throws IOException { final int len = this.buffer.length(); return Math.min(len, (int) (this.contentLength - this.pos)); } 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 testAvailable() throws IOException { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 3L); Assert.assertEquals(0, in.available()); in.read(); Assert.assertEquals(2, in.available()); in.close(); }