Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.addthis.basis.chars.ReadOnlyAsciiBuf.java

License:Apache License

@Override
public String toString() {
    // TODO: if our ByteBuf has a backing array, then we can use the deprecated, ascii-only
    // String constructor : new String(byte[], int, int, int)

    // Can't find a good way around String's stupid always-copy constructor, but by
    // not using content().toString(UTF8), we can at least prevent one extra allocation.
    ////www . j a  v a2s. c o m
    // ((CharBuffer alloc, CharBuffer toString, new String) -> (char[] alloc, new String))
    //
    // If desperate, _might_ be able to hack it with a dummy CharacterEncoder if there is
    // no security manager. Otherwise have to class path boot etc to get into the lang
    // package. I suppose annoyances like these are why I made this package.
    ByteBuf slice = content().slice();
    char[] values = new char[slice.capacity()];
    if (slice.readableBytes() > 0) {
        for (int i = 0; i < slice.capacity(); i++) {
            values[i] = (char) slice.getByte(i);
        }
    } else {
        return "";
    }
    return new String(values);
}

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

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();/*from w ww  .  j av a  2  s  .co  m*/
    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.DataKeyTop.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] bytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//  ww  w.j a v a 2 s .co  m
        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.DataMap.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//from  w  ww  . j a v a 2 s  . c o  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

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buf = Unpooled.wrappedBuffer(b);
    try {/*  w w  w .  j a va2s  .c  o  m*/
        int length = Varint.readUnsignedVarInt(buf);
        keys = new String[length];
        vals = new String[length];
        try {
            for (int i = 0; i < length; i++) {
                keys[i] = readString(buf);
            }
            for (int i = 0; i < length; i++) {
                vals[i] = readString(buf);
            }
            if (buf.readableBytes() > 0) {
                size = Varint.readUnsignedVarInt(buf);
            } else {
                if (!IGNORE_DESERIALIZATION_ERROR) {
                    throw new RuntimeException("Tried to deserialize a corrupted DataMap attachment. "
                            + "set the system property hydra.tree.data.map=true to ignore (Map Attachment will be empty on old nodes)");
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } finally {
        buf.release();
    }
    postDecode();
}

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  ww.  j a  v  a 2s .co  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 {/*from  w  ww .  ja v a  2  s. c om*/
        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 w w. 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.KeyTopper.java

License:Apache License

/**
 * Encode the data structure into a serialized representation.
 * Encode the number of elements followed by each (key, value)
 * pair. If the error estimation is used then encode the special
 * byte value 0 (since we will never encode 0 as the size
 * of a non-empty map) at the head of the byte array.
 * @param version// w  w w  . ja  v a2 s . c  om
 * @return
 */
@Override
public byte[] bytesEncode(long version) {
    if (map.size() == 0) {
        return EMPTY;
    }
    byte[] retBytes = null;
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        if (hasErrors()) {
            byteBuf.writeByte(0);
        }
        Varint.writeUnsignedVarInt(map.size(), byteBuf);
        for (Map.Entry<String, Long> mapEntry : map.entrySet()) {
            String key = mapEntry.getKey();
            if (key == null) {
                throw new NullPointerException("KeyTopper decoded null key");
            }
            byte[] keyBytes = key.getBytes("UTF-8");
            Varint.writeUnsignedVarInt(keyBytes.length, byteBuf);
            byteBuf.writeBytes(keyBytes);
            Varint.writeUnsignedVarLong(mapEntry.getValue(), byteBuf);
            if (hasErrors()) {
                Long error = errors.get(key);
                if (error != null) {
                    Varint.writeUnsignedVarLong(error, byteBuf);
                } else {
                    Varint.writeUnsignedVarLong(0, byteBuf);
                }
            }
        }
        retBytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(retBytes);
    } catch (UnsupportedEncodingException e) {
        throw Throwables.propagate(e);
    } finally {
        byteBuf.release();
    }
    return retBytes;
}

From source file:com.addthis.hydra.query.tracker.DetailedStatusHandler.java

License:Apache License

private void onSuccess(QueryEntryInfo queryEntryInfo) {
    try {/*ww w .j  a v  a 2  s  .com*/
        JSONObject entryJSON = CodecJSON.encodeJSON(queryEntryInfo);
        writer.write(entryJSON.toString());
        ByteBuf textResponse = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(writer.getBuilder()),
                CharsetUtil.UTF_8);
        HttpContent content = new DefaultHttpContent(textResponse);
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, textResponse.readableBytes());
        if (HttpHeaders.isKeepAlive(request)) {
            response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        ctx.write(response);
        ctx.write(content);
        ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        if (!HttpHeaders.isKeepAlive(request)) {
            lastContentFuture.addListener(ChannelFutureListener.CLOSE);
        }
    } catch (Throwable t) {
        onFailure(t);
    }
}