Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:com.addthis.hydra.data.tree.prop.DataCounting.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();/*from  ww  w . j a v  a 2s. com*/
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        Varint.writeUnsignedVarInt(ver, buffer);
        Varint.writeUnsignedVarInt(M.length, buffer);
        buffer.writeBytes(M);
        byte[] bytes = new byte[buffer.readableBytes()];
        buffer.readBytes(bytes);
        return bytes;
    } finally {
        buffer.release();
    }
}

From source file:com.addthis.hydra.data.tree.prop.DataCounting.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buffer = Unpooled.wrappedBuffer(b);
    try {//  ww  w.  j a v  a2 s  . c o  m
        ver = Varint.readUnsignedVarInt(buffer);
        M = new byte[Varint.readUnsignedVarInt(buffer)];
        buffer.readBytes(M);
    } finally {
        buffer.release();
    }
    postDecode();
}

From source file:com.addthis.hydra.data.tree.prop.DataKeyTop.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] bytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {/*from   ww  w.j  a v a2s  .com*/
        byte[] topBytes = top.bytesEncode(version);
        Varint.writeUnsignedVarInt(topBytes.length, buf);
        buf.writeBytes(topBytes);
        Varint.writeUnsignedVarInt(size, buf);
        bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
    } finally {
        buf.release();
    }
    return bytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataKeyTop.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    top = new ConcurrentKeyTopper();
    ByteBuf buf = Unpooled.wrappedBuffer(b);
    try {//from  w  w w.  ja va  2s . co m
        int topBytesLength = Varint.readUnsignedVarInt(buf);
        if (topBytesLength > 0) {
            byte[] topBytes = new byte[topBytesLength];
            buf.readBytes(topBytes);
            top.bytesDecode(topBytes, version);
        } else {
            top.init();
        }
        size = Varint.readUnsignedVarInt(buf);
    } finally {
        buf.release();
    }
}

From source file:com.addthis.hydra.data.tree.prop.DataMap.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//from   w  w  w. j  a v a2  s .co m
        synchronized (map) {
            preEncode();
            Varint.writeUnsignedVarInt(keys.length, buf);
            for (String key : keys) {
                writeString(buf, key);
            }
            for (String val : vals) {
                writeString(buf, val);
            }
            Varint.writeUnsignedVarInt(size, buf);
        }
        encodedBytes = new byte[buf.readableBytes()];
        buf.readBytes(encodedBytes);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } finally {
        buf.release();
    }
    return encodedBytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataMap.java

License:Apache License

private String readString(ByteBuf buf) throws UnsupportedEncodingException {
    int kl = Varint.readUnsignedVarInt(buf);
    byte[] kb = new byte[kl];
    buf.readBytes(kb);
    return new String(kb, "UTF-8");
}

From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    if (reservoir == null) {
        return EMPTY_BYTES;
    }/*w  w w  .  ja  v  a 2s  .  c  o  m*/
    byte[] retBytes = null;
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        Varint.writeUnsignedVarLong(minEpoch, byteBuf);
        Varint.writeUnsignedVarInt(reservoir.length, byteBuf);
        for (int element : reservoir) {
            Varint.writeUnsignedVarInt(element, byteBuf);
        }
        retBytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(retBytes);
    } finally {
        byteBuf.release();
    }
    return retBytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataTime.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {/* w  w  w.j  a va2 s  . c  o  m*/
        long delta = last - first;
        Varint.writeUnsignedVarLong(first, byteBuf);
        Varint.writeUnsignedVarLong(delta, byteBuf);
        encodedBytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(encodedBytes);
    } finally {
        byteBuf.release();
    }
    return encodedBytes;
}

From source file:com.addthis.hydra.data.util.ConcurrentKeyTopper.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();/*from   w  ww.  j  a v a  2  s  . c om*/
    if (map.size() == 0) {
        return EMPTY;
    }
    byte[] retBytes = null;
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        Varint.writeUnsignedVarInt(map.size(), byteBuf);
        for (Map.Entry<String, Long> mapEntry : map.entrySet()) {
            String key = mapEntry.getKey();
            if (key == null) {
                throw new IllegalStateException("ConcurrentKeyTopper decoded null key");
            }
            byte[] keyBytes = key.getBytes("UTF-8");
            Varint.writeUnsignedVarInt(keyBytes.length, byteBuf);
            byteBuf.writeBytes(keyBytes);
            Varint.writeUnsignedVarLong(mapEntry.getValue(), byteBuf);
        }
        retBytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(retBytes);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        byteBuf.release();
    }
    return retBytes;

}

From source file:com.addthis.hydra.data.util.ConcurrentKeyTopper.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    if (b.length == 0) {
        map = new ConcurrentHashMapV8<>(16, 0.75f, 4);
        return;/*from  w  w  w.ja  v a2s.c  o  m*/
    }
    ByteBuf byteBuf = Unpooled.wrappedBuffer(b);
    try {
        int mapSize = Varint.readUnsignedVarInt(byteBuf);
        int i;
        try {
            if (mapSize > 0) {
                map = new ConcurrentHashMapV8<>(mapSize + 16, 0.75f, 4);
                for (i = 0; i < mapSize; i++) {
                    int keyLength = Varint.readUnsignedVarInt(byteBuf);
                    byte[] keybytes = new byte[keyLength];
                    byteBuf.readBytes(keybytes);
                    String k = new String(keybytes, "UTF-8");
                    long value = Varint.readUnsignedVarLong(byteBuf);
                    map.put(k, value);
                }
            } else {
                map = new ConcurrentHashMapV8<>(16, 0.75f, 4);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        byteBuf.release();
    }
    postDecode();

}