public ChannelBuffer copy(int index, int length) { if (index < 0 || length < 0 || index + length > array.length) { throw new IndexOutOfBoundsException("Too many bytes to copy - Need " + (index + length) + ", maximum is " + array.length); } byte[] copiedArray = new byte[length]; System.arraycopy(array, index, copiedArray, 0, length); return new BigEndianHeapChannelBuffer(copiedArray); } }
/** * Creates a new buffer which wraps the specified {@code array} with the * specified {@code endianness}. A modification on the specified array's * content will be visible to the returned buffer. */ public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[] array) { if (endianness == BIG_ENDIAN) { if (array.length == 0) { return EMPTY_BUFFER; } return new BigEndianHeapChannelBuffer(array); } else if (endianness == LITTLE_ENDIAN) { if (array.length == 0) { return EMPTY_BUFFER; } return new LittleEndianHeapChannelBuffer(array); } else { throw new NullPointerException("endianness"); } }
/** * Creates a new Java heap buffer with the specified {@code endianness} * and {@code capacity}. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0}. */ public static ChannelBuffer buffer(ByteOrder endianness, int capacity) { if (endianness == BIG_ENDIAN) { if (capacity == 0) { return EMPTY_BUFFER; } return new BigEndianHeapChannelBuffer(capacity); } else if (endianness == LITTLE_ENDIAN) { if (capacity == 0) { return EMPTY_BUFFER; } return new LittleEndianHeapChannelBuffer(capacity); } else { throw new NullPointerException("endianness"); } }
public ChannelBuffer duplicate() { return new BigEndianHeapChannelBuffer(array, readerIndex(), writerIndex()); }
/** * Creates a new buffer with the specified {@code endianness} whose * content is a copy of the specified {@code array}. The new buffer's * {@code readerIndex} and {@code writerIndex} are {@code 0} and * {@code array.length} respectively. */ public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array) { if (endianness == BIG_ENDIAN) { if (array.length == 0) { return EMPTY_BUFFER; } return new BigEndianHeapChannelBuffer(array.clone()); } else if (endianness == LITTLE_ENDIAN) { if (array.length == 0) { return EMPTY_BUFFER; } return new LittleEndianHeapChannelBuffer(array.clone()); } else { throw new NullPointerException("endianness"); } }
@Test public void simpleMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); long chunkNum = 0; while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk, ++chunkNum); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void alignedMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); long chunkNum = 0; while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk, ++chunkNum); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); for (byte[] bytes : BYTE_LIST) { final byte[] expectedBytes = new byte[bytes.length]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); Assert.assertArrayEquals(expectedBytes, bytes); } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void simpleSingleStreamTest() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent(new BigEndianHeapChannelBuffer(allBytes)); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void simpleMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(Math.abs(RANDOM.nextInt()) % 128, allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void alignedMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); for(byte[] bytes : BYTE_LIST) { final byte[] expectedBytes = new byte[bytes.length]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); Assert.assertArrayEquals(expectedBytes, bytes); } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void simpleSingleStreamTest() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent(new BigEndianHeapChannelBuffer(allBytes)); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(Math.abs(RANDOM.nextInt()) % 128, allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
ChannelBuffer buffer = new BigEndianHeapChannelBuffer(pdu.getCommandLength());
ChannelBuffer buffer = new BigEndianHeapChannelBuffer(pdu.getCommandLength());
ChannelBuffer buffer = new BigEndianHeapChannelBuffer(pdu.getCommandLength());
ChannelBuffer buffer = new BigEndianHeapChannelBuffer(pdu.getCommandLength());