List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:org.apache.activemq.artemis.protocol.amqp.util.DeliveryUtil.java
License:Apache License
public static int readDelivery(Receiver receiver, ByteBuf buffer) { int initial = buffer.writerIndex(); // optimization by norman int count;/*from w w w. j a v a2s . co m*/ while ((count = receiver.recv(buffer.array(), buffer.arrayOffset() + buffer.writerIndex(), buffer.writableBytes())) > 0) { // Increment the writer index by the number of bytes written into it while calling recv. buffer.writerIndex(buffer.writerIndex() + count); buffer.ensureWritable(count); } return buffer.writerIndex() - initial; }
From source file:org.apache.activemq.artemis.protocol.amqp.util.DeliveryUtil.java
License:Apache License
public static MessageImpl decodeMessageImpl(ByteBuf buffer) { MessageImpl message = (MessageImpl) Message.Factory.create(); message.decode(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes()); return message; }
From source file:org.apache.activemq.artemis.protocol.amqp.util.NettyReadableTest.java
License:Apache License
@Test public void testArrayAccess() { ByteBuf byteBuffer = Unpooled.buffer(100, 100); NettyReadable buffer = new NettyReadable(byteBuffer); assertTrue(buffer.hasArray());//from w ww .j a v a 2 s . c om assertSame(buffer.array(), byteBuffer.array()); assertEquals(buffer.arrayOffset(), byteBuffer.arrayOffset()); }
From source file:org.apache.activemq.artemis.protocol.amqp.util.NettyReadableTest.java
License:Apache License
@Test public void testGetBytesToWritableBuffer() { byte[] data = new byte[] { 0, 1, 2, 3, 4 }; ByteBuf byteBuffer = Unpooled.wrappedBuffer(data); NettyReadable buffer = new NettyReadable(byteBuffer); ByteBuf targetBuffer = Unpooled.buffer(data.length, data.length); NettyWritable target = new NettyWritable(targetBuffer); buffer.get(target);/*from w w w .j a va2s . c o m*/ assertFalse(buffer.hasRemaining()); assertArrayEquals(targetBuffer.array(), data); }
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. *//*from www . java2s .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.ByteUtil.java
License:Apache License
public static byte[] longToBytes(long x) { ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.heapBuffer(8, 8); buffer.writeLong(x);//from w ww . j a v a 2s . c o m return buffer.array(); }
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()); }/*from w w w . j a v 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.activemq.artemis.utils.UTF8Util.java
License:Apache License
public static String readUTF(final ActiveMQBuffer input) { StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer(); final int size = input.readUnsignedShort(); if (UTF8Util.isTrace) { // This message is too verbose for debug, that's why we are using trace here ActiveMQUtilLogger.LOGGER.trace("Reading string with utfSize=" + size); }//from w ww .j a va 2 s .co m if (PlatformDependent.hasUnsafe() && input.byteBuf() != null && input.byteBuf().hasMemoryAddress()) { final ByteBuf byteBuf = input.byteBuf(); final long addressBytes = byteBuf.memoryAddress(); final int index = byteBuf.readerIndex(); byteBuf.skipBytes(size); final char[] chars = buffer.borrowCharBuffer(size); return unsafeOffHeapReadUTF(addressBytes, index, chars, size); } final byte[] bytes; final int index; if (input.byteBuf() != null && input.byteBuf().hasArray()) { final ByteBuf byteBuf = input.byteBuf(); bytes = byteBuf.array(); index = byteBuf.arrayOffset() + byteBuf.readerIndex(); byteBuf.skipBytes(size); } else { bytes = buffer.borrowByteBuffer(size); index = 0; input.readBytes(bytes, 0, size); } final char[] chars = buffer.borrowCharBuffer(size); if (PlatformDependent.hasUnsafe()) { return unsafeOnHeapReadUTF(bytes, index, chars, size); } else { return readUTF(bytes, index, chars, size); } }
From source file:org.apache.bookkeeper.proto.BookieProtoEncoding.java
License:Apache License
private static ByteBuf serializeProtobuf(MessageLite msg, ByteBufAllocator allocator) { int size = msg.getSerializedSize(); ByteBuf buf = allocator.heapBuffer(size, size); try {//from w w w . j a v a 2s. c o m msg.writeTo(CodedOutputStream.newInstance(buf.array(), buf.arrayOffset() + buf.writerIndex(), size)); } catch (IOException e) { // This is in-memory serialization, should not fail throw new RuntimeException(e); } // Advance writer idx buf.writerIndex(buf.capacity()); return buf; }
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 {// w w w . j a va 2 s. co 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); } }