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 int getBytes(int index, FileChannel out, long position, int length) throws IOException;

Source Link

Document

Transfers this buffer's data starting at the specified absolute index to the specified channel starting at the given file position.

Usage

From source file:com.antsdb.saltedfish.server.mysql.packet.AuthPacket.java

License:Open Source License

/**
 * Check if response is version 41/*from w w w .  j  a  va2  s .c  o m*/
 * @param in
 * @return
 */
private boolean checkResponseVer41(ByteBuf in) {
    // response V41 has at least 32 bytes
    if (packetLength >= 32) {
        in.markReaderIndex();
        byte[] reserved = new byte[23];

        // 23 bytes from index 9 (+4 head offset) are all zero for V41
        in.getBytes(9 + 4, reserved, 0, 23);
        in.resetReaderIndex();
        if (Arrays.equals(reserved, new byte[23])) {
            return true;
        }

    }
    return false;
}

From source file:com.beeswax.hexbid.parser.BidProtobufParser.java

License:Apache License

/**
 * Parse serialized protocol buffer Bytebuf to protobuf object.</br>
 * Preferencing implementation of {@link ProtobufDecoder}
 * /*from w  w w .  j  av a 2  s  .c  om*/
 * @param bytebuf
 * @return protocol buffer message
 * @throws InvalidProtocolBufferException
 */
public static <T extends Message.Builder> Message parseProtoBytebuf(ByteBuf bytebuf, T messageBuilder)
        throws InvalidProtocolBufferException {
    final byte[] array;
    final int offset;
    final int length = bytebuf.readableBytes();
    if (bytebuf.hasArray()) {
        array = bytebuf.array();
        offset = bytebuf.arrayOffset() + bytebuf.readerIndex();
    } else {
        array = new byte[length];
        bytebuf.getBytes(bytebuf.readerIndex(), array, 0, length);
        offset = 0;
    }
    return messageBuilder.mergeFrom(array, offset, length).buildPartial();
}

From source file:com.chat.common.netty.handler.decode.ProtobufDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();//from   www  .  j av  a2 s  .c  om
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    if (extensionRegistry == null) {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
        }
    } else {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build());
        }
    }
}

From source file:com.github.milenkovicm.kafka.CrcTest.java

License:Apache License

long calculateJCrc(ByteBuf messageSet) {
    jcrc.reset();/*  ww  w. j a  va2s .c  o  m*/

    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);
        jcrc.update(buffer, 0, length);

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

From source file:com.github.milenkovicm.kafka.CrcTest.java

License:Apache License

long calculateJCrc(ByteBuf messageSet, int position) {
    crc.reset();/*from w  w  w  .  ja v  a2 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();//from w w  w .j  av  a  2s .  c om

    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.sadikovi.netflowlib.NetFlowReader.java

License:Apache License

/** Prepare header using provided input stream */
private NetFlowHeader prepareHeader() throws IOException {
    NetFlowHeader internalHeader;/*from w w  w  . j a  va2s .c  o  m*/
    int numBytesRead = 0;
    int lenRead = 0;
    ByteBuf buf;
    byte[] headerArray;

    // Read header depending on stream version (different from flow version)
    if (streamVersion == 1) {
        // Version 1 has static header
        // TODO: verify header size for stream version 1
        lenRead = NetFlowHeader.S1_HEADER_SIZE - METADATA_LENGTH;
        internalHeader = new NetFlowHeader(streamVersion, byteOrder);
    } else {
        // Version 3 with dynamic header size
        headerArray = new byte[HEADER_OFFSET_LENGTH];
        numBytesRead = in.read(headerArray, 0, HEADER_OFFSET_LENGTH);
        if (numBytesRead != HEADER_OFFSET_LENGTH) {
            throw new UnsupportedOperationException("Short read while loading header offset");
        }

        buf = Unpooled.wrappedBuffer(headerArray).order(byteOrder);
        int headerSize = (int) buf.getUnsignedInt(0);
        if (headerSize <= 0) {
            throw new UnsupportedOperationException("Failed to load header of size " + headerSize);
        }

        // Actual header length, determine how many bytes to read
        lenRead = headerSize - METADATA_LENGTH - HEADER_OFFSET_LENGTH;
        internalHeader = new NetFlowHeader(streamVersion, byteOrder, headerSize);
    }

    // allocate buffer for length to read
    headerArray = new byte[lenRead];
    numBytesRead = in.read(headerArray, 0, lenRead);
    if (numBytesRead != lenRead) {
        throw new UnsupportedOperationException("Short read while loading header data");
    }
    // build buffer
    buf = Unpooled.wrappedBuffer(headerArray).order(byteOrder);

    // resolve stream version (either 1 or 3)
    if (streamVersion == 1) {
        internalHeader.setFlowVersion((short) buf.getUnsignedShort(0));
        internalHeader.setStartCapture(buf.getUnsignedInt(2));
        internalHeader.setEndCapture(buf.getUnsignedInt(6));
        internalHeader.setHeaderFlags(buf.getUnsignedInt(10));
        internalHeader.setRotation(buf.getUnsignedInt(14));
        internalHeader.setNumFlows(buf.getUnsignedInt(18));
        internalHeader.setNumDropped(buf.getUnsignedInt(22));
        internalHeader.setNumMisordered(buf.getUnsignedInt(26));
        // Read hostname fixed bytes
        byte[] hostnameBytes = new byte[NetFlowHeader.S1_HEADER_HN_LEN];
        buf.getBytes(30, hostnameBytes, 0, hostnameBytes.length);
        internalHeader.setHostname(new String(hostnameBytes));
        // Read comments fixed bytes
        byte[] commentsBytes = new byte[NetFlowHeader.S1_HEADER_CMNT_LEN];
        buf.getBytes(30 + hostnameBytes.length, commentsBytes, 0, commentsBytes.length);
        internalHeader.setComments(new String(commentsBytes));

        // Dereference arrays
        hostnameBytes = null;
        commentsBytes = null;
    } else {
        // Resolve TLV (type-length value)
        // Set decode pointer to first tlv
        int dp = 0;
        int left = lenRead;
        // Smallest TLV is 2+2+0 (null TLV)
        // tlv_t - TLV type, tlv_l - TLV length, tlv_v - TLV value
        int tlv_t = 0;
        int tlv_l = 0;
        int tlv_v = 0;

        // Byte array for holding Strings
        byte[] pr;

        while (left >= 4) {
            // Parse type, store in host byte order
            tlv_t = buf.getUnsignedShort(dp);
            dp += 2;
            left -= 2;

            // Parse len, store in host byte order
            tlv_l = buf.getUnsignedShort(dp);
            dp += 2;
            left -= 2;

            // Parse val
            tlv_v = dp;

            // Point decode buffer at next tlv
            dp += tlv_l;
            left -= tlv_l;

            // TLV length check
            if (left < 0) {
                break;
            }

            switch (tlv_t) {
            // FT_TLV_VENDOR
            case 0x1:
                internalHeader.setVendor(buf.getUnsignedShort(tlv_v));
                break;
            // FT_TLV_EX_VER
            case 0x2:
                internalHeader.setFlowVersion((short) buf.getUnsignedShort(tlv_v));
                break;
            // FT_TLV_AGG_VER
            case 0x3:
                internalHeader.setAggVersion(buf.getUnsignedByte(tlv_v));
                break;
            // FT_TLV_AGG_METHOD
            case 0x4:
                internalHeader.setAggMethod(buf.getUnsignedByte(tlv_v));
                break;
            // FT_TLV_EXPORTER_IP
            case 0x5:
                internalHeader.setExporterIP(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_CAP_START
            case 0x6:
                internalHeader.setStartCapture(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_CAP_END
            case 0x7:
                internalHeader.setEndCapture(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_HEADER_FLAGS
            case 0x8:
                internalHeader.setHeaderFlags(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_ROT_SCHEDULE
            case 0x9:
                internalHeader.setRotation(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_FLOW_COUNT
            case 0xA:
                internalHeader.setNumFlows(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_FLOW_LOST
            case 0xB:
                internalHeader.setNumDropped(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_FLOW_MISORDERED
            case 0xC:
                internalHeader.setNumMisordered(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_PKT_CORRUPT
            case 0xD:
                internalHeader.setNumCorrupt(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_SEQ_RESET
            case 0xE:
                internalHeader.setSeqReset(buf.getUnsignedInt(tlv_v));
                break;
            // FT_TLV_CAP_HOSTNAME
            case 0xF:
                pr = new byte[tlv_l];
                buf.getBytes(tlv_v, pr, 0, pr.length);
                // Expected null-terminated string
                if (pr[pr.length - 1] != 0) {
                    throw new UnsupportedOperationException("Char sequence is not null-terminated");
                }

                internalHeader.setHostname(new String(pr, 0, pr.length - 1));
                break;
            // FT_TLV_COMMENTS
            case 0x10:
                pr = new byte[tlv_l];
                buf.getBytes(tlv_v, pr, 0, pr.length);
                // Expected null-terminated string
                if (pr[pr.length - 1] != 0) {
                    throw new UnsupportedOperationException("Char sequence is not null-terminated");
                }
                internalHeader.setComments(new String(pr, 0, pr.length - 1));
                break;
            // FT_TLV_IF_NAME
            case 0x11:
                // uint32_t, uint16_t, string:
                // - IP address of device
                // - ifIndex of interface
                // - interface name
                long ip = buf.getUnsignedInt(tlv_v);
                int ifIndex = buf.getUnsignedShort(tlv_v + 4);
                pr = new byte[tlv_l - 4 - 2];
                buf.getBytes(tlv_v + 4 + 2, pr, 0, pr.length);
                if (pr[pr.length - 1] != 0) {
                    throw new UnsupportedOperationException("Char sequence is not null-terminated");
                }
                internalHeader.setInterfaceName(ip, ifIndex, new String(pr, 0, pr.length - 1));
                break;
            // FT_TLV_IF_ALIAS
            case 0x12:
                // uint32_t, uint16_t, uint16_t, string:
                // - IP address of device
                // - ifIndex count
                // - ifIndex of interface (count times)
                // - alias name
                long aliasIP = buf.getUnsignedInt(tlv_v);
                int aliasIfIndexCnt = buf.getUnsignedShort(tlv_v + 4);
                int aliasIfIndex = buf.getUnsignedShort(tlv_v + 4 + 2);
                pr = new byte[tlv_l - 4 - 2 - 2];
                buf.getBytes(tlv_v + 4 + 2 + 2, pr, 0, pr.length);
                if (pr[pr.length - 1] != 0) {
                    throw new UnsupportedOperationException("Char sequence is not null-terminated");
                }

                internalHeader.setInterfaceAlias(aliasIP, aliasIfIndexCnt, aliasIfIndex,
                        new String(pr, 0, pr.length - 1));
                break;
            // Case 0x0
            default:
                break;
            }
        }

        if (buf != null && buf.refCnt() > 0) {
            buf.release(buf.refCnt());
        }

        buf = null;
        pr = null;
    }
    return internalHeader;
}

From source file:com.netflix.iep.http.NetflixJsonObjectDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;/*from w  ww .ja va  2 s.co m*/
    }

    if (LOGGER.isTraceEnabled()) {
        byte[] bytes = new byte[in.readableBytes()];
        in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes());
        LOGGER.trace("starting [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes));
    }

    // index of next byte to process.
    int len = this.len;
    int wrtIdx = in.writerIndex();

    for (; in.readerIndex() + len < wrtIdx; len++) {
        if (len > maxObjectLength) {
            // buffer size exceeded maxObjectLength; discarding the complete buffer.
            in.skipBytes(in.readableBytes());
            reset();
            throw new TooLongFrameException(
                    "object length exceeds " + maxObjectLength + ": " + len + " bytes discarded");
        }
        byte c = in.getByte(in.readerIndex() + len);
        if (state == ST_DECODING_NORMAL) {
            decodeByte(c, in, in.readerIndex() + len);

            // All opening braces/brackets have been closed. That's enough to conclude
            // that the JSON object/array is complete.
            if (openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), len + 1);
                if (json != null) {
                    out.add(json);
                }

                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(in.readerIndex() + len + 1);
                len = 0;
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
                break;
            }
        } else if (state == ST_DECODING_ARRAY_STREAM) {
            if (len == 0 && Character.isWhitespace(c)) {
                in.skipBytes(1);
                len--;
            }
            decodeByte(c, in, in.readerIndex() + len);
            if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), len);
                if (json != null) {
                    out.add(json);
                }

                in.readerIndex(in.readerIndex() + len + 1);
                len = 0;

                if (c == ']') {
                    reset();
                }
                break;
            }
            // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);

            if (state == ST_DECODING_ARRAY_STREAM) {
                // Discard the array bracket
                in.skipBytes(1);
                len--;
            }
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
            len--;
        } else {
            state = ST_CORRUPTED;
            throw new CorruptedFrameException("invalid JSON received at byte position "
                    + (in.readerIndex() + len) + ": " + ByteBufUtil.hexDump(in));
        }
    }

    this.len = len;

    if (LOGGER.isTraceEnabled()) {
        byte[] bytes = new byte[in.readableBytes()];
        in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes());
        LOGGER.trace("remainder [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes));
    }
}

From source file:com.netflix.iep.http.NetflixJsonObjectDecoder.java

License:Apache License

/**
 * Override this method if you want to filter the json objects/arrays that get passed through the pipeline.
 *///  ww w  .j a  v  a 2  s  .c o m
@SuppressWarnings("UnusedParameters")
protected ByteBuf extractObject(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
    if (length == 0)
        return null;
    ByteBuf buf = buffer.slice(index, length).retain();

    if (LOGGER.isTraceEnabled()) {
        byte[] bytes = new byte[buf.readableBytes()];
        buf.getBytes(buf.readerIndex(), bytes, 0, buf.readableBytes());
        LOGGER.trace("extracted [" + buf.readerIndex() + ":" + buf.readableBytes() + "]:" + new String(bytes));
    }

    return buf;
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private String parseString(int offset, int length, ByteBuf buf) {
    // N-bytes/*from  w  w w.ja v  a2  s .c o m*/
    byte[] bytes = new byte[length - 5];
    buf.getBytes(offset, bytes, 0, length - 5);
    return new String(bytes);
}