BoundedInputStreamBuffer extends BaseBuffer { @Override public Buffer readBytes(final int length) throws IndexOutOfBoundsException, IOException { if (!checkReadableBytesSafe(length)) { final int availableBytes = getReadableBytes(); final int read = internalReadBytes(length); if (read == -1) { return null; } else if (read + availableBytes < length) { throw new IndexOutOfBoundsException("Not enough bytes left in the stream. Wanted " + length + " but only read " + read); } } int index = 0; final byte[] buf = new byte[length]; while (index < length) { final int spaceLeft = getAvailableLocalReadingSpace(); final int readAtMost = Math.min(length - index, spaceLeft); final int localIndex = getLocalReaderIndex(); System.arraycopy(this.buffer, localIndex, buf, index, readAtMost); this.readerIndex += readAtMost; index += readAtMost; } return Buffers.wrap(buf); } BoundedInputStreamBuffer(final InputStream is);  BoundedInputStreamBuffer(final int bufferCapacity, final InputStream is); @Override Buffer slice(final int start, final int stop); @Override byte readByte(); @Override byte peekByte(); @Override Buffer readBytes(final int length); @Override int getReadableBytes(); @Override boolean hasReadableBytes(); @Override boolean isEmpty(); @Override byte[] getArray(); @Override byte getByte(final int index); byte getByte(final long index); @Override long readUnsignedInt(); @Override int readInt(); @Override int getInt(final int index); @Override short getShort(final int index); @Override int readUnsignedShort(); @Override int getUnsignedShort(final int index); @Override short readShort(); @Override short getUnsignedByte(final int index); @Override String dumpAsHex(); @Override void setByte(final int index, final byte value); @Override void setUnsignedByte(final int index, final short value); @Override void setUnsignedShort(final int index, final int value); @Override Buffer clone(); @Override boolean equals(final Object other); @Override boolean equalsIgnoreCase(final Object other); @Override int hashCode(); @Override String toString(); @Override int getWritableBytes(); @Override boolean hasWritableBytes(); @Override void write(byte[] bytes); @Override void getBytes(final Buffer dst); @Override void getBytes(final byte[] dst); @Override void getBytes(final int index, final Buffer dst); @Override void setInt(final int index, final int value); @Override void write(final int value); @Override void write(final long value); @Override void writeAsString(final int value); @Override void writeAsString(final long value); @Override void setUnsignedInt(final int index, final long value); @Override void write(final byte b); @Override void write(final String s); @Override void write(final String s, final String charset); @Override boolean hasWriteSupport(); @Override void setWriterIndex(final int index); @Override void setReaderIndex(final int index); @Override int getReaderIndex(); @Override int getWriterIndex(); @Override final short readUnsignedByte(); @Override int capacity(); @Override void resetReaderIndex(); @Override void markReaderIndex(); @Override int getLowerBoundary(); @Override int getUpperBoundary(); @Override Buffer slice(final int stop); @Override Buffer slice(); static final int DEFAULT_CAPACITY; }
@Test public void testReadingOnDemand() throws Exception { final byte[] content = RawData.rawEthernetFrame; final InputStream in = new ByteArrayInputStream(content); final Buffer outer = Buffers.wrap(in); final Buffer first = outer.readBytes(50); final Buffer second = outer.readBytes(150); assertContent(first, content, 0, 50); assertContent(second, content, 50, 150); }
@Test public void testSliceAndWrapAroundBuffer() throws Exception { final byte[] content = RawData.rawEthernetFrame; final InputStream in = new ByteArrayInputStream(content); final int bufferSize = 719; assertTrue("Buffer size not valid", bufferSize > content.length && content.length * 2 > bufferSize); final BoundedInputStreamBuffer buffer = new BoundedInputStreamBuffer(bufferSize, in); for (int counter = 0; counter < 20; counter++) { final Buffer data = buffer.readBytes(content.length); assertContent(data, content, 0, content.length); in.reset(); } }