/** * Read more bytes from the stream. * @param b The byte array to put the new data in. * @return The number of bytes read into the buffer. * @throws IOException If an IO problem occurs * @see java.io.InputStream#read(byte[]) */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
protected InputStream createContentInputStream( final long len, final SessionInputBuffer buffer, final InputStream inputStream) { if (len > 0) { return new ContentLengthInputStream(buffer, inputStream, len); } else if (len == 0) { return EmptyInputStream.INSTANCE; } else if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedInputStream(buffer, inputStream, this.h1Config); } else { return new IdentityInputStream(buffer, inputStream); } }
protected InputStream createContentInputStream( final long len, final SessionInputBuffer buffer, final InputStream inputStream) { if (len > 0) { return new ContentLengthInputStream(buffer, inputStream, len); } else if (len == 0) { return EmptyInputStream.INSTANCE; } else if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedInputStream(buffer, inputStream, this.h1Config); } else { return new IdentityInputStream(buffer, inputStream); } }
@Test public void testBasics() 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, 10L); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final byte[] buffer = new byte[50]; int len = in.read(buffer, 0, 2); outputStream.write(buffer, 0, len); len = in.read(buffer); outputStream.write(buffer, 0, len); final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1); Assert.assertEquals(result, "1234567890"); in.close(); }
/** * Read more bytes from the stream. * @param b The byte array to put the new data in. * @return The number of bytes read into the buffer. * @throws IOException If an IO problem occurs * @see java.io.InputStream#read(byte[]) */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@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(); }
/** * <p>Reads until the end of the known length of content.</p> * * <p>Does not close the underlying socket input, but instead leaves it * primed to parse the next response.</p> * @throws IOException If an IO problem occurs. */ @Override public void close() throws IOException { if (!closed) { try { if (pos < contentLength) { final byte[] buffer = new byte[BUFFER_SIZE]; while (read(buffer) >= 0) { // keep reading } } } finally { // close after above so that we don't throw an exception trying // to read after closed! closed = true; } } }
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); final InputStream in2 = new ContentLengthInputStream(inBuffer2, inputStream2, 10L); in2.read(); Assert.assertEquals(9, in2.skip(10)); final InputStream in3 = new ContentLengthInputStream(inBuffer3, inputStream3, 2L); in3.read(); in3.read(); final InputStream in4 = new ContentLengthInputStream(inBuffer4, inputStream4, 10L); Assert.assertEquals(5,in4.skip(5)); Assert.assertEquals(5, in4.read(new byte[20]));
/** * <p>Reads until the end of the known length of content.</p> * * <p>Does not close the underlying socket input, but instead leaves it * primed to parse the next response.</p> * @throws IOException If an IO problem occurs. */ @Override public void close() throws IOException { if (!closed) { try { if (pos < contentLength) { final byte[] buffer = new byte[BUFFER_SIZE]; while (read(buffer) >= 0) { // keep reading } } } finally { // close after above so that we don't throw an exception trying // to read after closed! closed = true; } } }
@Test public void testTruncatedContent() 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, 32L); final byte[] tmp = new byte[32]; final int byteRead = in.read(tmp); Assert.assertEquals(16, byteRead); try { in.read(tmp); Assert.fail("ConnectionClosedException should have been closed"); } catch (final ConnectionClosedException ex) { } try { in.read(); Assert.fail("ConnectionClosedException should have been closed"); } catch (final ConnectionClosedException ex) { } try { in.close(); Assert.fail("ConnectionClosedException should have been closed"); } catch (final ConnectionClosedException ex) { } }
/** * Skips and discards a number of bytes from the input stream. * @param n The number of bytes to skip. * @return The actual number of bytes skipped. ≤ 0 if no bytes * are skipped. * @throws IOException If an error occurs while skipping bytes. * @see InputStream#skip(long) */ @Override public long skip(final long n) throws IOException { if (n <= 0) { return 0; } final byte[] buffer = new byte[BUFFER_SIZE]; // make sure we don't skip more bytes than are // still available long remaining = Math.min(n, this.contentLength - this.pos); // skip and keep track of the bytes actually skipped 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; } }
@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)); }
/** * Skips and discards a number of bytes from the input stream. * @param n The number of bytes to skip. * @return The actual number of bytes skipped. ≤ 0 if no bytes * are skipped. * @throws IOException If an error occurs while skipping bytes. * @see InputStream#skip(long) */ @Override public long skip(final long n) throws IOException { if (n <= 0) { return 0; } final byte[] buffer = new byte[BUFFER_SIZE]; // make sure we don't skip more bytes than are // still available long remaining = Math.min(n, this.contentLength - this.pos); // skip and keep track of the bytes actually skipped 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; } }