ContentLengthInputStream extends InputStream { @Override public long skip(final long n) throws IOException { if (n <= 0) { return 0; } final byte[] buffer = new byte[BUFFER_SIZE]; long remaining = Math.min(n, this.contentLength - this.pos); long count = 0; while (remaining > 0) { final int readLen = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); if (readLen == -1) { break; } count += readLen; remaining -= readLen; } return count; } 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 testSkip() throws IOException { final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(new byte[20]); final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16); final InputStream in1 = new ContentLengthInputStream(inBuffer1, inputStream1, 10L); Assert.assertEquals(10, in1.skip(10)); Assert.assertTrue(in1.read() == -1); in1.close(); final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(new byte[20]); final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16); final InputStream in2 = new ContentLengthInputStream(inBuffer2, inputStream2, 10L); in2.read(); Assert.assertEquals(9, in2.skip(10)); Assert.assertTrue(in2.read() == -1); in2.close(); final ByteArrayInputStream inputStream3 = new ByteArrayInputStream(new byte[20]); final SessionInputBuffer inBuffer3 = new SessionInputBufferImpl(16); final InputStream in3 = new ContentLengthInputStream(inBuffer3, inputStream3, 2L); in3.read(); in3.read(); Assert.assertTrue(in3.skip(10) <= 0); Assert.assertTrue(in3.skip(-1) == 0); Assert.assertTrue(in3.read() == -1); in3.close(); final ByteArrayInputStream inputStream4 = new ByteArrayInputStream(new byte[20]); final SessionInputBuffer inBuffer4 = new SessionInputBufferImpl(16); final InputStream in4 = new ContentLengthInputStream(inBuffer4, inputStream4, 10L); Assert.assertEquals(5,in4.skip(5)); Assert.assertEquals(5, in4.read(new byte[20])); in4.close(); }