List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testWrap() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); assertTrue(buffer.isReadable());//www . j a v a2 s . com assertFalse(buffer.isWriteable()); assertEquals(10, buffer.readableBytes()); assertEquals(0, buffer.writeableBytes()); assertEquals(10, buffer.capacity()); assertTrue(buffer.hasArray()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testDuplicateSlice() throws IOException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); Buffer slice = buffer.readerIndex(3).slice(2); ReadableBuffer duplicate = slice.duplicate(); assertEquals(2, duplicate.readableBytes()); assertEquals(0, duplicate.readerIndex()); assertEquals(0, duplicate.readByte()); assertEquals(0, duplicate.readByte()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testSliceWithIndex() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); Buffer slice = buffer.slice(3, 2); assertEquals(2, slice.readableBytes()); assertEquals(0, slice.readerIndex()); assertEquals(0, slice.readByte());/*from ww w. ja v a 2 s . c om*/ assertEquals(0, slice.readByte()); assertTrue(buffer.isReadable()); assertFalse(buffer.isWriteable()); assertEquals(10, buffer.readableBytes()); assertEquals(0, buffer.writeableBytes()); assertEquals(10, buffer.capacity()); assertEquals(2, buffer.readByte()); assertEquals(-120, buffer.readByte()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testSliceDuplicate() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); Buffer slice = (Buffer) buffer.slice(5).duplicate(); assertFalse(slice.isWriteable());/*from www . j a v a 2 s . c o m*/ assertTrue(slice.isReadable()); assertEquals(0, slice.writeableBytes()); assertEquals(5, slice.readableBytes()); slice = (Buffer) buffer.slice(3).duplicate(); assertFalse(slice.isWriteable()); assertTrue(slice.isReadable()); assertEquals(0, slice.writeableBytes()); assertEquals(3, slice.readableBytes()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testRead() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); assertEquals(2, buffer.readByte());//from ww w . j a v a 2 s . c om assertTrue(buffer.isReadable()); assertEquals(-120, buffer.readByte()); assertTrue(buffer.isReadable()); buffer.skipBytes(3); assertTrue(buffer.isReadable()); byte[] bytes = new byte[4]; buffer.readBytes(bytes); assertArrayEquals(new byte[] { 4, 5, 6, 7 }, bytes); assertTrue(buffer.isReadable()); buffer.readBytes(bytes, 0, 1); assertArrayEquals(new byte[] { 6, 5, 6, 7 }, bytes); assertFalse(buffer.isReadable()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testGet() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); assertEquals(2, buffer.getByte(0));// ww w . j av a 2s. c om assertEquals(-120, buffer.getByte(1)); assertEquals(2, buffer.readByte()); assertEquals(6, buffer.getByte(9)); assertTrue(buffer.isReadable()); try { buffer.getByte(10); fail(); } catch (IndexOutOfBoundsException e) { assertTrue(true); } }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testReadSlice() throws IOException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); ByteReader slice = buffer.slice(2);/* w w w . j a v a2 s .co m*/ assertEquals(2, slice.readByte()); assertTrue(slice.isReadable()); assertEquals(-120, slice.readByte()); assertFalse(slice.isReadable()); assertTrue(buffer.isReadable()); slice = buffer.slice(8); assertFalse(buffer.isReadable()); slice.skipBytes(3); assertTrue(slice.isReadable()); byte[] bytes = new byte[4]; slice.readBytes(bytes); assertArrayEquals(new byte[] { 4, 5, 6, 7 }, bytes); assertTrue(slice.isReadable()); slice.readBytes(bytes, 0, 1); assertArrayEquals(new byte[] { 6, 5, 6, 7 }, bytes); assertFalse(slice.isReadable()); }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testReaderIndexRead() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); assertEquals(2, buffer.readByte());//from w w w . jav a 2s .c om assertTrue(buffer.isReadable()); assertEquals(-120, buffer.readByte()); assertTrue(buffer.isReadable()); buffer.skipBytes(3); assertTrue(buffer.isReadable()); buffer.readerIndex(0); byte[] bytes = new byte[4]; buffer.readBytes(bytes); assertArrayEquals(new byte[] { 2, -120, 0, 0 }, bytes); assertTrue(buffer.isReadable()); buffer.readerIndex(8); buffer.readBytes(bytes, 0, 1); assertArrayEquals(new byte[] { 7, -120, 0, 0 }, bytes); assertTrue(buffer.isReadable()); try { buffer.readerIndex(12); fail(); } catch (IndexOutOfBoundsException e) { assertTrue(true); } }
From source file:io.horizondb.io.buffers.NettyBufferTest.java
License:Apache License
@Test public void testGetMultipleBytes() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 2, -120, 0, 0, 0, 4, 5, 6, 7, 6 }); NettyBuffer buffer = new NettyBuffer(byteBuf); byte[] bytes = new byte[4]; buffer.getBytes(0, bytes, 0, 4);//from www . j a v a 2 s . co m assertArrayEquals(new byte[] { 2, -120, 0, 0 }, bytes); buffer.getBytes(1, bytes, 0, 4); assertArrayEquals(new byte[] { -120, 0, 0, 0 }, bytes); assertEquals(2, buffer.readByte()); buffer.getBytes(9, bytes, 3, 1); assertArrayEquals(new byte[] { -120, 0, 0, 6 }, bytes); assertTrue(buffer.isReadable()); try { buffer.getBytes(9, bytes, 3, 2); fail(); } catch (IndexOutOfBoundsException e) { assertTrue(true); } }
From source file:io.hydramq.disk.DiskSegment.java
License:Open Source License
public Message read(final int messageOffset) throws HydraRuntimeException { if (!(messageOffset < size())) { throw outOfBoundsException(); }/*w w w . j a va 2 s . co m*/ try { // First, use the index to find the starting byte of the message in the data file ByteBuffer indexReadBuffer = ByteBuffer.allocate(4); indexReadBuffer.clear(); while (indexReadBuffer.hasRemaining()) { index.read(indexReadBuffer, messageOffset * INDEX_ENTRY_SIZE + indexReadBuffer.position()); } indexReadBuffer.flip(); int dataOffset = indexReadBuffer.getInt(); // Second, get the message size from the first integer of the message indexReadBuffer.clear(); while (indexReadBuffer.hasRemaining()) { data.read(indexReadBuffer, dataOffset + indexReadBuffer.position()); } indexReadBuffer.flip(); int dataSize = indexReadBuffer.getInt(); ByteBuffer buffer = ByteBuffer.allocateDirect(dataSize); while (buffer.hasRemaining()) { data.read(buffer, dataOffset + 4 + buffer.position()); } buffer.flip(); return conversionContext.read(Message.class, Unpooled.wrappedBuffer(buffer)); } catch (Exception ex) { throw new HydraRuntimeException("Error reading message in segment " + segmentDirectory.toString() + " for message offset " + messageOffset, ex); } }