public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
public static int readCompositeBuffer(final TCPNIOConnection connection, final CompositeBuffer buffer) throws IOException { final SocketChannel socketChannel = (SocketChannel) connection.getChannel(); final int oldPos = buffer.position(); final ByteBufferArray array = buffer.toByteBufferArray(); final ByteBuffer byteBuffers[] = array.getArray(); final int size = array.size(); final int read = (int) socketChannel.read(byteBuffers, 0, size); array.restore(); array.recycle(); if (read > 0) { buffer.position(oldPos + read); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "TCPNIOConnection ({0}) (nonallocated, composite) read {1} bytes", new Object[]{ connection, read }); } return read; }
@Test public void testToByteBufferArray() { final byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; final List<Buffer> bufferList = new ArrayList<Buffer>(); for (byte b : bytes) { final Buffer buffer = mm.allocate(1); buffer.put(0, b); bufferList.add(buffer); } final Buffer[] buffers = bufferList.toArray(new Buffer[bufferList.size()]); final CompositeBuffer composite = CompositeBuffer.newBuffer(mm, buffers); for (int i = 0; i < bytes.length; i++) { for (int j = i; j < bytes.length; j++) { final ByteBufferArray bbArray = composite.toByteBufferArray(i, j); int bytesChecked = 0; final ByteBuffer[] bbs = bbArray.getArray(); for (int k = 0; k < bbArray.size(); k++) { final ByteBuffer bb = bbs[k]; while (bb.hasRemaining()) { final byte got = bb.get(); assertEquals("Testcase [pos=" + i + " lim=" + j + " bytenumber=" + bytesChecked + "]", bytes[i + bytesChecked], got); bytesChecked++; } } assertEquals(j - i, bytesChecked); bbArray.restore(); } } }
@Override public void run() { // Initialize memory manager mm.allocate(33); final int initialSize = mm.getReadyThreadBufferSize(); CompositeBuffer compositeBuffer = CompositeBuffer.newBuffer(mm); for (int i = 0; i < 11; i++) { Buffer b = mm.allocate(1228); b.allowBufferDispose(true); compositeBuffer.append(b); } compositeBuffer.toByteBufferArray(0, 12280); compositeBuffer.limit(1228); compositeBuffer.shrink(); assertEquals(initialSize - (1228 * 11 - 12280), mm.getReadyThreadBufferSize()); compositeBuffer.position(compositeBuffer.limit()); compositeBuffer.shrink(); assertEquals(initialSize, mm.getReadyThreadBufferSize()); } };