List of usage examples for io.netty.buffer ByteBuf hasArray
public abstract boolean hasArray();
From source file:io.soliton.protobuf.quartz.ByteBufUtil.java
License:Apache License
public static byte[] getBytes(ByteBuf buffer) { byte[] bytes; // Not all ByteBuf implementation is backed by a byte array, so check if (buffer.hasArray()) { bytes = buffer.array();/* w w w . j ava 2 s.co m*/ } else { bytes = new byte[buffer.readableBytes()]; buffer.getBytes(buffer.readerIndex(), bytes); } return bytes; }
From source file:io.viewserver.network.netty.IncomingMessageHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects) throws Exception { final byte[] array; final int offset; final int length = byteBuf.readableBytes(); // try { if (byteBuf.hasArray()) { array = byteBuf.array();//from w w w . ja v a 2 s . c om offset = byteBuf.arrayOffset() + byteBuf.readerIndex(); networkMessageWheel.pushToWheel(new NettyChannel(channelHandlerContext.channel()), array, offset, length); } else { // array = new byte[length]; // byteBuf.getBytes(byteBuf.readerIndex(), array, 0, length); // offset = 0; networkMessageWheel.pushToWheel(new NettyChannel(channelHandlerContext.channel()), new ByteBufInputStream(byteBuf)); } // } finally { // byteBuf.release(); // } }
From source file:net.openhft.fix.transport.test.NettyFrameDecoderTest.java
License:Apache License
@Test public void testDecode() { ByteBuf message = newLogonMessage(); ByteBuf msgcopy = message.copy(); EmbeddedChannel channel = new EmbeddedChannel(new NettyFrameDecoder()); // raw write write Assert.assertTrue(channel.writeInbound(message)); Assert.assertTrue(channel.finish()); // read/*from www . j a v a 2s. co m*/ byte[] result = (byte[]) channel.readInbound(); Assert.assertNotNull(result); if (msgcopy.hasArray()) { Assert.assertArrayEquals(msgcopy.array(), result); } }
From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java
License:Apache License
/** * Writes a sequence of bytes to this file from the given buffer. * <p>//from w w w .jav a 2 s .com * <p> Bytes are written starting at this file's current position, */ public void write(ByteBuf src, int srcStart, int srcLength) throws IOException { final int nextPosition = this.position + srcLength; checkCapacity(nextPosition); final long destAddress = this.address + this.position; if (src.hasMemoryAddress()) { final long srcAddress = src.memoryAddress() + srcStart; PlatformDependent.copyMemory(srcAddress, destAddress, srcLength); } else if (src.hasArray()) { final byte[] srcArray = src.array(); PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength); } else { throw new IllegalArgumentException("unsupported byte buffer"); } rawMovePositionAndLength(nextPosition); }
From source file:org.apache.activemq.artemis.utils.AbstractByteBufPool.java
License:Apache License
/** * Batch hash code implementation that works at its best if {@code bytes} * contains a {@link org.apache.activemq.artemis.api.core.SimpleString} encoded. */// ww w. j a v a 2s. c om private static int hashCode(final ByteBuf bytes, final int offset, final int length) { if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) { //if the platform allows it, the hash code could be computed without bounds checking if (bytes.hasArray()) { return onHeapHashCode(bytes.array(), bytes.arrayOffset() + offset, length); } else if (bytes.hasMemoryAddress()) { return offHeapHashCode(bytes.memoryAddress(), offset, length); } } return byteBufHashCode(bytes, offset, length); }
From source file:org.apache.activemq.artemis.utils.UTF8Util.java
License:Apache License
public static void saveUTF(final ByteBuf out, final String str) { if (str.length() > 0xffff) { throw ActiveMQUtilBundle.BUNDLE.stringTooLong(str.length()); }// w ww . jav a 2 s . c o m final int len = UTF8Util.calculateUTFSize(str); if (len > 0xffff) { throw ActiveMQUtilBundle.BUNDLE.stringTooLong(len); } out.writeShort((short) len); final int stringLength = str.length(); if (UTF8Util.isTrace) { // This message is too verbose for debug, that's why we are using trace here ActiveMQUtilLogger.LOGGER.trace("Saving string with utfSize=" + len + " stringSize=" + stringLength); } if (out.hasArray()) { out.ensureWritable(len); final byte[] bytes = out.array(); final int writerIndex = out.writerIndex(); final int index = out.arrayOffset() + writerIndex; if (PlatformDependent.hasUnsafe()) { unsafeOnHeapWriteUTF(str, bytes, index, stringLength); } else { writeUTF(str, bytes, index, stringLength); } out.writerIndex(writerIndex + len); } else { if (PlatformDependent.hasUnsafe() && out.hasMemoryAddress()) { out.ensureWritable(len); final long addressBytes = out.memoryAddress(); final int writerIndex = out.writerIndex(); unsafeOffHeapWriteUTF(str, addressBytes, writerIndex, stringLength); out.writerIndex(writerIndex + len); } else { final StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer(); final byte[] bytes = buffer.borrowByteBuffer(len); writeUTF(str, bytes, 0, stringLength); out.writeBytes(bytes, 0, len); } } }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testUnpooled() { ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.UnpooledHeap).build(); ByteBuf buf = alloc.buffer(); assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc()); assertTrue(buf.hasArray()); ByteBuf buf2 = alloc.directBuffer(); assertEquals(UnpooledByteBufAllocator.DEFAULT, buf2.alloc()); assertFalse(buf2.hasArray());/*from w ww. j a v a 2 s . c o m*/ }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testPooled() { PooledByteBufAllocator pooledAlloc = new PooledByteBufAllocator(true); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.PooledDirect) .pooledAllocator(pooledAlloc).build(); assertTrue(alloc.isDirectBufferPooled()); ByteBuf buf1 = alloc.buffer(); assertEquals(pooledAlloc, buf1.alloc()); assertFalse(buf1.hasArray()); buf1.release();//from ww w . j av a2s . com ByteBuf buf2 = alloc.directBuffer(); assertEquals(pooledAlloc, buf2.alloc()); assertFalse(buf2.hasArray()); buf2.release(); ByteBuf buf3 = alloc.heapBuffer(); assertEquals(pooledAlloc, buf3.alloc()); assertTrue(buf3.hasArray()); buf3.release(); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testPooledWithDefaultAllocator() { ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.PooledDirect) .poolingConcurrency(3).build(); assertTrue(alloc.isDirectBufferPooled()); ByteBuf buf1 = alloc.buffer(); assertEquals(PooledByteBufAllocator.class, buf1.alloc().getClass()); assertEquals(3, ((PooledByteBufAllocator) buf1.alloc()).metric().numDirectArenas()); assertFalse(buf1.hasArray()); buf1.release();/* www.j av a 2 s . c o m*/ ByteBuf buf2 = alloc.directBuffer(); assertFalse(buf2.hasArray()); buf2.release(); ByteBuf buf3 = alloc.heapBuffer(); assertTrue(buf3.hasArray()); buf3.release(); }
From source file:org.apache.bookkeeper.proto.checksum.DirectMemoryCRC32Digest.java
License:Apache License
@Override public void update(ByteBuf buf) { int index = buf.readerIndex(); int length = buf.readableBytes(); try {/*ww w . j a va 2 s. c o m*/ if (buf.hasMemoryAddress()) { // Calculate CRC directly from the direct memory pointer crcValue = (int) updateByteBuffer.invoke(null, crcValue, buf.memoryAddress(), index, length); } else if (buf.hasArray()) { // Use the internal method to update from array based crcValue = (int) updateBytes.invoke(null, crcValue, buf.array(), buf.arrayOffset() + index, length); } else { // Fallback to data copy if buffer is not contiguous byte[] b = new byte[length]; buf.getBytes(index, b, 0, length); crcValue = (int) updateBytes.invoke(null, crcValue, b, 0, b.length); } } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }