Example usage for io.netty.buffer ByteBuf array

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

Introduction

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

Prototype

public abstract byte[] array();

Source Link

Document

Returns the backing byte array of this buffer.

Usage

From source file:io.jsql.mysql.mysql.ResultSetHeaderPacket.java

License:Open Source License

@Override
public void write(Channel channel) {
    int size = calcPacketSize();
    ByteBuf buffer = Unpooled.buffer(size);
    //        buffer = c.checkWriteBuffer(buffer, c.getPacketHeaderSize() + size,writeSocketIfFull);
    MBufferUtil.writeUB3(buffer, size);//w  w  w.ja  v a2 s.  co m
    buffer.writeByte(packetId);
    MBufferUtil.writeLength(buffer, fieldCount);
    if (extra > 0) {
        MBufferUtil.writeLength(buffer, extra);
    }
    //        return buffer;
    channel.writeAndFlush(buffer.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;/*  w  w  w . j a  v a 2s .  com*/
        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.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

License:Apache License

private ExtendedCommunityPathAttribute decodeExtendedCommunityPathAttribute(final ByteBuf buffer) {
    final ExtendedCommunityPathAttribute attr = new ExtendedCommunityPathAttribute();

    if ((buffer.readableBytes() < 8) || ((buffer.readableBytes() % 8) != 0)) {
        throw new OptionalAttributeErrorException();
    }//from w w w. j a  v  a2s .  co  m

    while (buffer.isReadable()) {
        AbstractExtendedCommunityInterface extcomm;
        // we need to check whether this is a transitive or non-transitive value
        // and then determine whether we need to red the lower type byte
        byte higherType = buffer.readByte();
        byte higherTypeCompare = (byte) (higherType & ~(1 << 6));
        if (((higherType >> 6) & 1) == 0) {
            // bit 7 is not set in the byte, therefore this is a transitive type
            // clear bit 8, not interested in this value
            TransitiveExtendedCommunityType transCommType = TransitiveExtendedCommunityType
                    .fromCode((byte) (higherType & (~(3 << 6))));

            switch (transCommType) {
            case TWO_OCTET_AS_SPECIFIC:
                TransitiveTwoOctetASSpecificExtCommSubTypes twoOctASNLowerType = TransitiveTwoOctetASSpecificExtCommSubTypes
                        .fromCode(buffer.readByte());
                switch (twoOctASNLowerType) {
                case ROUTE_TARGET:
                    extcomm = new TransitiveTwoByteASNFourByteAdministratorRT((int) buffer.readShort(),
                            (long) buffer.readInt());
                    break;
                default:
                    // all non-RT types are currently unimplemented
                    extcomm = new UnknownTransitiveTwoByteASNSpecificExtendedCommunity(transCommType,
                            twoOctASNLowerType, buffer.readBytes(6).array());
                }
                break;

            case TWO_OCTET_IPv4_ADDRESS_SPECIFIC:

                TransitiveIPv4AddressSpecificExtCommSubTypes ipv4LowerType = TransitiveIPv4AddressSpecificExtCommSubTypes
                        .fromCode(buffer.readByte());

                switch (ipv4LowerType) {
                case ROUTE_TARGET:
                    try {
                        extcomm = new TransitiveIPv4AddressTwoByteAdministratorRT(
                                (Inet4Address) InetAddresses
                                        .fromLittleEndianByteArray(buffer.readBytes(4).array()),
                                (int) buffer.readShort());
                    } catch (UnknownHostException e) {
                        ByteBuf data = Unpooled.buffer();
                        data.getByte(ipv4LowerType.toCode());
                        data.readBytes(buffer.readBytes(6));
                        extcomm = new UnknownTransitiveExtendedCommunity(transCommType, data.array());
                    }
                    break;

                default:

                    // all non-RT types are currently unimplemented
                    extcomm = new UnknownTransitiveIPv4AddressSpecificExtendedCommunity(transCommType,
                            ipv4LowerType, buffer.readBytes(6).array());
                    break;

                }
                break;

            default:
                // by default, just create an unknown type, reading the subsequent
                // 7 bytes (we have already read byte 1)
                extcomm = new UnknownTransitiveExtendedCommunity(transCommType, buffer.readBytes(7).array());
            }
        } else {
            // bit 7 is set, these are non-transitive
            NonTransitiveExtendedCommunityType nonTransCommType = NonTransitiveExtendedCommunityType
                    .fromCode((byte) (higherType & (~(3 << 6))));
            // all non-transitive types are currently unimplemented
            extcomm = new UnknownNonTransitiveExtendedCommunity(nonTransCommType, buffer.readBytes(7).array());
        }
        attr.getMembers().add(extcomm);
    }
    return attr;
}

From source file:io.servicecomb.foundation.vertx.server.TcpParser.java

License:Apache License

protected void onParse(Buffer buffer) {
    switch (status) {
    case TCP_HEADER:
        ByteBuf buf = buffer.getByteBuf();
        if (!firstNEqual(TCP_MAGIC, buf.array(), TCP_MAGIC.length)) {
            reset();//from   w w  w .  ja  v a  2  s  . c  o  m
            return;
        }

        buf.skipBytes(TCP_MAGIC.length);
        msgId = buf.readLong();
        totalLen = buf.readInt();
        headerLen = buf.readInt();

        if (totalLen == 0) {
            onReadOnePackage(null, null);
            return;
        }

        parser.fixedSizeMode(totalLen);
        status = ParseStatus.TCP_PAYLOAD;
        break;

    case TCP_PAYLOAD:
        Buffer headerBuffer = buffer.slice(0, headerLen);
        Buffer bodyBuffer = buffer.slice(headerLen, buffer.length());
        onReadOnePackage(headerBuffer, bodyBuffer);
        break;

    default:
        break;
    }
}

From source file:io.servicecomb.foundation.vertx.VertxUtils.java

License:Apache License

public static byte[] getBytesFast(ByteBuf byteBuf) {
    if (byteBuf.hasArray()) {
        return byteBuf.array();
    }//from   ww w .  j av  a  2s  .  com

    byte[] arr = new byte[byteBuf.writerIndex()];
    byteBuf.getBytes(0, arr);
    return arr;
}

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();
    } else {/*from  w  ww.  j a v a  2s.c om*/
        bytes = new byte[buffer.readableBytes()];
        buffer.getBytes(buffer.readerIndex(), bytes);
    }
    return bytes;
}

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());/*  w w w . j  a v 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 testGetBytesToWritableBuffer() {
    byte[] data = new byte[] { 0, 1, 2, 3, 4 };
    ByteBuf byteBuffer = Unpooled.wrappedBuffer(data);
    ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);
    ByteBuf targetBuffer = Unpooled.buffer(data.length, data.length);
    ProtonWritableBufferImpl target = new ProtonWritableBufferImpl(targetBuffer);

    buffer.get(target);/*from   w w w .  j a  va2 s. c o  m*/
    assertFalse(buffer.hasRemaining());
    assertArrayEquals(targetBuffer.array(), data);
}

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());/*from   w w w.  j av  a2 s  .co  m*/
    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();
        offset = byteBuf.arrayOffset() + byteBuf.readerIndex();
        networkMessageWheel.pushToWheel(new NettyChannel(channelHandlerContext.channel()), array, offset,
                length);//from  ww w  .j ava 2s  . c  o m
    } 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();
    //        }
}