Example usage for io.netty.buffer ByteBuf getBytes

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

Introduction

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

Prototype

public abstract ByteBuf getBytes(int index, ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit.

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 {/*w w  w . j  a v a  2s .  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.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 w w  w.j  ava 2 s .co m*/
    }
    byte[] array = new byte[readable];
    buf.getBytes(readerIndex, array);
    return array;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static byte[] extractByteArray(JSObject object) {
    ByteBuf buf = extract(object);
    byte[] bytes = new byte[bufLen(object)];
    buf.getBytes(buf.readerIndex(), bytes);
    return bytes;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static String hexSlice(JSObject object, int start, int end) {
    ByteBuf b = extract(object);
    byte[] bytes = new byte[end - start];
    b.getBytes(start, bytes);
    return Hex.toHexString(bytes);
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static String base64Slice(JSObject object, int start, int end) {
    ByteBuf b = extract(object);
    byte[] bytes = new byte[end - start];
    b.getBytes(start, bytes);

    return Base64.toBase64String(bytes);
}

From source file:io.nodyn.crypto.Hash.java

License:Apache License

public void update(ByteBuf buf) {
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), bytes);
    this.digest.update(bytes, 0, bytes.length);
}

From source file:io.nodyn.crypto.Hmac.java

License:Apache License

public void update(ByteBuf buf) {
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), bytes);
    this.hmac.update(bytes, 0, bytes.length);
}

From source file:io.reactivesocket.netty.TestUtil.java

License:Apache License

public static String byteBufToString(ByteBuf buf) {
    byte[] bytes = new byte[buf.readableBytes()];
    int readerIndex = buf.readerIndex();
    buf.getBytes(readerIndex, bytes);
    buf.readerIndex(readerIndex);//from  ww w  .  j a  va 2  s . co  m

    StringBuilder result = new StringBuilder();
    StringBuilder ascii = new StringBuilder();
    int i = 0;
    for (i = 0; i < bytes.length; i++) {
        byte b = bytes[i];
        result.append(String.format("%02X ", b));

        if (32 <= b && b < 127) {
            ascii.append((char) b);
        } else {
            ascii.append('.');
        }

        if ((i + 1) % 16 == 0) {
            result.append("   ");
            result.append(ascii);
            result.append('\n');
            ascii = new StringBuilder();
        }
    }
    if ((bytes.length - 1) % 16 != 0) {
        int x = 16 - ((bytes.length - 1) % 16);
        StringBuilder padding = new StringBuilder();
        for (int j = 0; j < x; j++) {
            result.append("   ");
        }
        result.append(ascii);
    }
    return result.toString();
}

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

License:Apache License

public static byte[] getBytesFast(ByteBuf byteBuf) {
    if (byteBuf.hasArray()) {
        return byteBuf.array();
    }/*  w  w w .ja  va  2 s.  c  o  m*/

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