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.github.milenkovicm.kafka.CrcTest.java

License:Apache License

long calculateJCrc(ByteBuf messageSet, int position) {
    crc.reset();/*from   w w  w .j  a  v a 2 s. c  om*/

    int readableBytes = messageSet.readableBytes();
    int bufferLength = buffer.length;
    //int position = 0;

    while (position < readableBytes) {
        int length = readableBytes - position >= bufferLength ? bufferLength : readableBytes - position;
        messageSet.getBytes(position, buffer, 0, length);
        crc.update(buffer, 0, length);

        position += bufferLength;
    }
    return crc.getValue();
}

From source file:com.github.milenkovicm.kafka.handler.AbstractProducerHandler.java

License:Apache License

long calculateCrc(ByteBuf messageSet, int position) {
    crc.reset();// w ww.java2s  .c  o m

    int readableBytes = messageSet.readableBytes();
    int bufferLength = buffer.length;

    while (position < readableBytes) {
        int length = readableBytes - position >= bufferLength ? bufferLength : readableBytes - position;
        messageSet.getBytes(position, buffer, 0, length);
        crc.update(buffer, 0, length);

        position += bufferLength;
    }
    return crc.getValue();
}

From source file:com.github.milenkovicm.kafka.handler.CompositeProducerHandler.java

License:Apache License

ByteBuf creteProduceRequest(ByteBufAllocator allocator, ByteBuf messageSet, String topic) {
    final int messageSetSize = messageSet.readableBytes();

    // a bit hardcoded logic follows
    // total length is length of the all fields
    // i've pre-calculated sizes of arrays and strings in case of
    // topic and client id
    final int totalLength = 22 + messageSetSize + Convert.sizeOfString(topicNameEncoded)
            + Convert.sizeOfString(clientIdEncoded);
    // 18 + clientId.length
    ByteBuf header = allocator/*from  w  w w  .  jav a2s .  co  m*/
            .buffer(24 + Convert.sizeOfString(topicNameEncoded) + Convert.sizeOfString(clientIdEncoded));
    createMessageHeader(totalLength, header);

    updateCrc(messageSet);
    return Unpooled.wrappedBuffer(header, messageSet);

}

From source file:com.github.milenkovicm.kafka.handler.CopyProducerHandler.java

License:Apache License

ByteBuf creteProduceRequest(ByteBufAllocator allocator, ByteBuf messageSet, String topic) {
    final int messageSetSize = messageSet.readableBytes();
    // a bit hardcoded logic follows
    // total length is length of the all fields
    // i've pre-calculated sizes of arrays and strings in case of
    // topic and client id
    final int totalLength = 22 + messageSetSize + Convert.sizeOfString(topicNameEncoded)
            + Convert.sizeOfString(clientIdEncoded);

    ByteBuf buffer = allocator.buffer(totalLength + 4); // msg length + size of length field
    createMessageHeader(totalLength, buffer);
    updateCrc(messageSet);//w  w  w  .  ja  va2  s  . com
    buffer.writeBytes(messageSet);

    return buffer;
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeBytes(ByteBuf bytes, ByteBuf buf) {
    if (bytes != null) {
        int readable = bytes.readableBytes();
        encodeInteger(readable, buf);//from   ww  w .  j a  v  a  2s  .  co  m
        buf.writeBytes(bytes, 0, readable); // content
    } else {
        encodeInteger(NULL_VALUE_LENGTH, buf); // indicates null
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static int sizeOfBytes(ByteBuf bytes) {
    if (bytes != null) {
        return SIZE_OF_INT + bytes.readableBytes();
    } else {/*  w w  w.j  av a 2s.c  om*/
        return SIZE_OF_INT;
    }

}

From source file:com.github.mrstampy.gameboot.otp.websocket.client.WebSocketHandler.java

License:Open Source License

private byte[] extractBytes(Object msg) {
    BinaryWebSocketFrame frame = (BinaryWebSocketFrame) msg;

    ByteBuf buf = frame.content();

    byte[] b = new byte[buf.readableBytes()];

    buf.readBytes(b);/* www.  j av  a2 s  . c o  m*/

    return b;
}

From source file:com.github.mrstampy.kitchensync.netty.handler.AbstractKiSyNettyHandler.java

License:Open Source License

/**
 * Bytes.//  w  w  w.  j  a  v a2  s .  c o  m
 *
 * @param msg
 *          the msg
 * @return the byte[]
 */
protected byte[] bytes(DatagramPacket msg) {
    ByteBuf content = msg.content();

    int num = content.readableBytes();
    byte[] message = new byte[num];
    content.readBytes(message);

    return message;
}

From source file:com.github.nettybook.ch0.LoggingHandler.java

License:Apache License

/**
 * Generates the default log message of the specified event whose argument is a {@link ByteBuf}.
 *///  w  ww.  j av a2 s . c om
private static String formatByteBuf(ChannelHandlerContext ctx, String eventName, ByteBuf msg) {
    String chStr = ctx.channel().toString();
    int length = msg.readableBytes();
    if (length == 0) {
        StringBuilder buf = new StringBuilder(chStr.length() + 1 + eventName.length() + 4);
        buf.append(chStr).append(' ').append(eventName).append(": 0B");
        return buf.toString();
    } else {
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(
                chStr.length() + 1 + eventName.length() + 2 + 10 + 1 + 2 + rows * 80);

        buf.append(chStr).append(' ').append(eventName).append(": ").append(length).append('B');
        appendHexDump(buf, msg);

        return buf.toString();
    }
}

From source file:com.github.nettybook.ch0.LoggingHandler.java

License:Apache License

/**
 * Generates the default log message of the specified event whose argument is a {@link ByteBufHolder}.
 *///from   w  w  w.j  a v a 2s. com
private static String formatByteBufHolder(ChannelHandlerContext ctx, String eventName, ByteBufHolder msg) {
    String chStr = ctx.channel().toString();
    String msgStr = msg.toString();
    ByteBuf content = msg.content();
    int length = content.readableBytes();
    if (length == 0) {
        StringBuilder buf = new StringBuilder(
                chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 4);
        buf.append(chStr).append(' ').append(eventName).append(", ").append(msgStr).append(", 0B");
        return buf.toString();
    } else {
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(
                chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 2 + 10 + 1 + 2 + rows * 80);

        buf.append(chStr).append(' ').append(eventName).append(": ");
        buf.append(msgStr).append(", ").append(length).append('B');
        appendHexDump(buf, content);

        return buf.toString();
    }
}