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:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv4Prefix() {
    final byte[] result = { 123, 122, 4, 5, 8 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.IPV4_PREFIX_BYTE_LENGTH);
    writeIpv4Prefix(new Ipv4Prefix("123.122.4.5/8"), output);
    assertArrayEquals(result, output.array());

    output.clear();//ww w  .  j  a v  a2 s.  co m
    final byte[] zeroResult = { 0, 0, 0, 0, 0 };
    writeIpv4Prefix(null, output);
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv6Address() {
    final byte[] result = { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x01 };/*  www.  j  a v  a 2  s.com*/
    final ByteBuf output = Unpooled.buffer(Ipv6Util.IPV6_LENGTH);
    writeIpv6Address(new Ipv6Address("2001::1"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    writeIpv6Address(null, output);
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv6Prefix() {
    final byte[] result = { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x40 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.IPV6_PREFIX_BYTE_LENGTH);
    writeIpv6Prefix(new Ipv6Prefix("2001:db8:1:2::/64"), output);
    assertArrayEquals(result, output.array());

    output.clear();/*from w w  w  .j  a  va2 s.  c  o m*/
    final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    writeIpv6Prefix(null, output);
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteFloat32() {
    final byte[] result = { 0, 0, 0, 5 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.FLOAT32_BYTES_LENGTH);
    writeFloat32(new Float32(result), output);
    assertArrayEquals(result, output.array());

    output.clear();//from  w  w w  .j  a v  a 2 s . c o m
    writeFloat32(null, output);
    assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteBitSet() {
    final byte[] result = { 1 };
    final ByteBuf output = Unpooled.buffer(1);
    final BitSet bitSet = new BitSet(8);
    bitSet.set(7);//from  w w  w . ja v  a  2 s  .  c o m
    writeBitSet(bitSet, 1, output);
    assertArrayEquals(result, output.array());

    output.clear();
    writeBitSet(null, 1, output);
    assertArrayEquals(ONE_BYTE_ZERO, output.array());
}

From source file:org.proton.plug.context.AbstractProtonContextSender.java

License:Apache License

protected int performSend(ProtonJMessage serverMessage, Object context) {
    if (!creditsSemaphore.tryAcquire()) {
        try {/*from  www .  j a  va  2  s  .  c o m*/
            creditsSemaphore.acquire();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            // nothing to be done here.. we just keep going
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    //presettle means we can ack the message on the dealer side before we send it, i.e. for browsers
    boolean preSettle = sender.getRemoteSenderSettleMode() == SenderSettleMode.SETTLED;

    //we only need a tag if we are going to ack later
    byte[] tag = preSettle ? new byte[0] : protonSession.getTag();

    ByteBuf nettyBuffer = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
    try {
        serverMessage.encode(new NettyWritable(nettyBuffer));

        int size = nettyBuffer.writerIndex();

        synchronized (connection.getLock()) {
            final Delivery delivery;
            delivery = sender.delivery(tag, 0, tag.length);
            delivery.setContext(context);

            // this will avoid a copy.. patch provided by Norman using buffer.array()
            sender.send(nettyBuffer.array(), nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(),
                    nettyBuffer.readableBytes());

            if (preSettle) {
                delivery.settle();
            } else {
                sender.advance();
            }
        }

        connection.flush();

        return size;
    } finally {
        nettyBuffer.release();
    }
}

From source file:org.pumpkindb.Closure.java

License:Mozilla Public License

@Override
public void encode(ByteBuf buffer) {
    ByteBuf buf = Unpooled.buffer();
    encodables.forEach(encodable -> encodable.encode(buf));

    int index = buf.writerIndex();

    buf.capacity(index);//from w  w w. j a v a  2 s  .c o  m

    new Data(buf.array()).encode(buffer);
}

From source file:org.ratpackframework.http.internal.DefaultRequest.java

License:Apache License

@Override
public byte[] getBytes() {
    ByteBuf buffer = getBuffer();
    if (buffer.hasArray()) {
        return buffer.array();
    } else {//ww w  . j av a 2s  . c o m
        ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.writerIndex());
        try {
            writeBodyTo(baos);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return baos.toByteArray();
    }
}

From source file:org.redisson.misc.Hash.java

License:Apache License

public static byte[] hash(byte[] objectState) {
    long h1 = LongHashFunction.farmUo().hashBytes(objectState);
    long h2 = LongHashFunction.xx_r39().hashBytes(objectState);

    ByteBuf buf = Unpooled.buffer((2 * Long.SIZE) / Byte.SIZE).writeLong(h1).writeLong(h2);
    try {//  www  .j a  v a 2s  . co  m
        return buf.array();
    } finally {
        buf.release();
    }
}

From source file:org.restcomm.media.rtp.netty.RtpDemultiplexer.java

License:Open Source License

private void handleStunPacket(ChannelHandlerContext ctx, ByteBuf buffer) {
    // Retrieve data from network
    final byte[] data = buffer.array();
    final int length = buffer.readableBytes();
    final int offset = buffer.arrayOffset();

    // Wrap data into an STUN packet
    try {/*from   w ww  . j a v  a2s . c  om*/
        StunMessage stunPacket = StunMessage.decode(data, (char) offset, (char) length);
        ctx.fireChannelRead(stunPacket);
    } catch (StunException e) {
        // Drop packet as we could not decode it
        ReferenceCountUtil.release(buffer);
        log.warn("Channel " + ctx.channel().localAddress() + "could not decode incoming STUN packet", e);
    }
}