Example usage for io.netty.buffer ByteBuf arrayOffset

List of usage examples for io.netty.buffer ByteBuf arrayOffset

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf arrayOffset.

Prototype

public abstract int arrayOffset();

Source Link

Document

Returns the offset of the first byte within the backing byte array of this buffer.

Usage

From source file:io.datty.aerospike.support.AerospikeValueUtil.java

License:Apache License

public static Value toValue(ByteBuf bufferOrNull) {

    if (bufferOrNull == null) {
        return new NullValue();
    } else if (bufferOrNull.hasArray()) {
        int start = bufferOrNull.readerIndex();
        int length = bufferOrNull.readableBytes();
        if (start != 0 || length != bufferOrNull.capacity()) {
            int baseOffset = bufferOrNull.arrayOffset() + start;
            return new ByteSegmentValue(bufferOrNull.array(), baseOffset, baseOffset + length);
        } else {//from ww w.j  a v  a 2  s  . c  o  m
            return new BytesValue(bufferOrNull.array());
        }
    } else {
        byte[] bytes = new byte[bufferOrNull.readableBytes()];
        bufferOrNull.getBytes(bufferOrNull.readerIndex(), bytes);
        return new BytesValue(bytes);
    }

}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public String readString(ByteBuf buffer, int length, Charset charset) {
    if (length > buffer.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length);
    }//www  .  jav  a2 s  .  c  o  m
    if (buffer.hasArray()) {
        int start = buffer.readerIndex();
        int baseOffset = buffer.arrayOffset() + start;
        String str = new String(buffer.array(), baseOffset, length, charset);
        buffer.readerIndex(start + length);
        return str;
    } else {
        byte[] bytes = new byte[length];
        buffer.readBytes(bytes);
        return new String(bytes, charset);
    }
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

public static byte[] byteBuf2Bytes(ByteBuf buf) {
    int readable = buf.readableBytes();
    int readerIndex = buf.readerIndex();
    if (buf.hasArray()) {
        byte[] array = buf.array();
        if (buf.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
            return array;
        }//from  ww  w . j  a  v  a  2 s .  c o  m
    }
    byte[] array = new byte[readable];
    buf.getBytes(readerIndex, array);
    return array;
}

From source file:io.nebo.thrift.TNiftyTransport.java

License:Apache License

public TNiftyTransport(Channel channel, ByteBuf in, ThriftTransportType thriftTransportType) {
    this.channel = channel;
    this.in = in;
    this.out = Unpooled.buffer(DEFAULT_OUTPUT_BUFFER_SIZE);
    this.thriftTransportType = thriftTransportType;
    this.initialReaderIndex = in.readerIndex();

    if (!in.hasArray()) {
        buffer = null;/*from w  w  w  . ja v a2s.  c o  m*/
        bufferPosition = 0;
        initialBufferPosition = bufferEnd = -1;
    } else {
        buffer = in.array();
        initialBufferPosition = bufferPosition = in.arrayOffset() + in.readerIndex();
        bufferEnd = bufferPosition + in.readableBytes();
        // Without this, reading from a !in.hasArray() buffer will advance the readerIndex
        // of the buffer, while reading from a in.hasArray() buffer will not advance the
        // readerIndex, and this has led to subtle bugs. This should help to identify
        // those problems by making things more consistent.
        in.readerIndex(in.readerIndex() + in.readableBytes());
    }
}

From source file:io.vertx.proton.impl.ProtonReadableBufferImplTest.java

License:Apache License

@Test
public void testArrayAccess() {
    ByteBuf byteBuffer = Unpooled.buffer(100, 100);
    ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);

    assertTrue(buffer.hasArray());//  ww  w . jav  a  2 s . c  o m
    assertSame(buffer.array(), byteBuffer.array());
    assertEquals(buffer.arrayOffset(), byteBuffer.arrayOffset());
}

From source file:io.vertx.proton.impl.ProtonReadableBufferImplTest.java

License:Apache License

@Test
public void testArrayOffset() {
    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    ByteBuf byteBuffer = Unpooled.wrappedBuffer(data, 5, 5);
    ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);

    assertTrue(buffer.hasArray());/*  www.  j  a v  a  2s  .c om*/
    assertSame(buffer.array(), byteBuffer.array());
    assertEquals(buffer.arrayOffset(), byteBuffer.arrayOffset());

    assertEquals(5, buffer.get());

    assertEquals(buffer.arrayOffset(), byteBuffer.arrayOffset());
}

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 . j a v  a  2 s  . c o m
        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:org.apache.activemq.artemis.core.protocol.proton.plug.ProtonSessionIntegrationCallback.java

License:Apache License

@Override
public void serverSend(final Receiver receiver, final Delivery delivery, String address, int messageFormat,
        ByteBuf messageEncoded) throws Exception {
    EncodedMessage encodedMessage = new EncodedMessage(messageFormat, messageEncoded.array(),
            messageEncoded.arrayOffset(), messageEncoded.writerIndex());

    ServerMessage message = manager.getConverter().inbound(encodedMessage);
    //use the address on the receiver if not null, if null let's hope it was set correctly on the message
    if (address != null) {
        message.setAddress(new SimpleString(address));
    }/*www .j  av  a2  s .co  m*/

    serverSession.send(message, false);

    manager.getServer().getStorageManager().afterCompleteOperations(new IOAsyncTask() {
        @Override
        public void done() {
            synchronized (connection.getLock()) {
                delivery.settle();
                connection.flush();
            }
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            synchronized (connection.getLock()) {
                receiver.setCondition(
                        new ErrorCondition(AmqpError.ILLEGAL_STATE, errorCode + ":" + errorMessage));
                connection.flush();
            }
        }
    });
}

From source file:org.apache.activemq.artemis.protocol.amqp.broker.AMQPSessionCallback.java

License:Apache License

public void serverSend(final Transaction transaction, final Receiver receiver, final Delivery delivery,
        String address, int messageFormat, ByteBuf messageEncoded) throws Exception {
    EncodedMessage encodedMessage = new EncodedMessage(messageFormat, messageEncoded.array(),
            messageEncoded.arrayOffset(), messageEncoded.writerIndex());

    ServerMessage message = manager.getConverter().inbound(encodedMessage);
    //use the address on the receiver if not null, if null let's hope it was set correctly on the message
    if (address != null) {
        message.setAddress(new SimpleString(address));
    }//from   w w  w.  j  a va 2s.com

    recoverContext();

    PagingStore store = manager.getServer().getPagingManager().getPageStore(message.getAddress());
    if (store.isRejectingMessages()) {
        // We drop pre-settled messages (and abort any associated Tx)
        if (delivery.remotelySettled()) {
            if (transaction != null) {
                String amqpAddress = delivery.getLink().getTarget().getAddress();
                ActiveMQException e = new ActiveMQAMQPResourceLimitExceededException(
                        "Address is full: " + amqpAddress);
                transaction.markAsRollbackOnly(e);
            }
        } else {
            rejectMessage(delivery);
        }
    } else {
        serverSend(transaction, message, delivery, receiver);
    }
}

From source file:org.apache.activemq.artemis.protocol.amqp.converter.message.JMSMappingOutboundTransformerTest.java

License:Apache License

public EncodedMessage transform(ServerJMSMessage message) throws Exception {
    // Useful for testing but not recommended for real life use.
    ByteBuf nettyBuffer = Unpooled.buffer(1024);
    NettyWritable buffer = new NettyWritable(nettyBuffer);

    long messageFormat = transformer.transform(message, buffer);

    EncodedMessage encoded = new EncodedMessage(messageFormat, nettyBuffer.array(),
            nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(), nettyBuffer.readableBytes());

    return encoded;
}