Example usage for io.netty.buffer ByteBuf getByte

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

Introduction

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

Prototype

public abstract byte getByte(int index);

Source Link

Document

Gets a byte at the specified absolute index in this buffer.

Usage

From source file:com.eightkdata.mongowp.bson.netty.NettyBsonLowLevelReader.java

License:Open Source License

public boolean hasNext(@Tight @ConservesIndexes ByteBuf byteBuf) {
    return byteBuf.getByte(byteBuf.readerIndex()) != 0x00;
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4; //the final 0x00 must be included

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    assert byteBuf.getByte(byteBuf.readerIndex() - 1) == 0x00;

    return new IterableNettyBsonDocument(significantSlice, this);
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonArray readArray(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4; //the final 0x00 must be included

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    assert byteBuf.getByte(byteBuf.readerIndex() - 1) == 0x00;

    return new IterableNettyBsonArray(significantSlice, this);
}

From source file:com.github.lburgazzoli.quickfixj.transport.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (m_msgLength == -1) {
        if (in.readableBytes() >= FIXCodecHelper.MSG_MIN_BYTES) {
            int ridx = in.readerIndex();
            int bssohidx = in.indexOf(ridx, ridx + 12, FIXCodecHelper.BYTE_SOH);
            int blsohidx = in.indexOf(ridx + 12, ridx + 20, FIXCodecHelper.BYTE_SOH);

            // check the existence of:
            // - BeginString 8=
            // - BodyLength  9=
            if (in.getByte(ridx) == FIXCodecHelper.BYTE_BEGIN_STRING
                    && in.getByte(ridx + 1) == FIXCodecHelper.BYTE_EQUALS
                    && in.getByte(bssohidx + 1) == FIXCodecHelper.BYTE_BODY_LENGTH
                    && in.getByte(bssohidx + 2) == FIXCodecHelper.BYTE_EQUALS) {

                int bodyLength = 0;
                for (int i = bssohidx + 3; i < blsohidx; i++) {
                    bodyLength *= 10;//from   w w w  .j  ava  2s .  c  om
                    bodyLength += (in.getByte(i) - '0');
                }

                m_msgLength = 1 + bodyLength + (blsohidx - ridx) + FIXCodecHelper.MSG_CSUM_LEN;
            } else {
                throw new Error("Unexpected state (header)");
            }
        }
    }

    if (m_msgLength != -1 && in.readableBytes() >= m_msgLength) {
        if (in.readableBytes() >= m_msgLength) {
            byte[] rv = new byte[m_msgLength];
            in.readBytes(rv);

            //TODO: validate checksum
            out.add(rv);

            m_msgLength = -1;
        } else {
            throw new Error("Unexpected state (body)");
        }
    }
}

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

License:Apache License

long calculateCrc(ByteBuf messageSet) {
    crc.reset();/*from w  w  w  .  j  av  a  2s  . c o m*/
    for (int i = 0; i < messageSet.readableBytes(); i++) {
        crc.update(messageSet.getByte(i));
    }
    return crc.getValue();
}

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

License:Apache License

@Override
public int partition(ByteBuf key, int numberOfPartitions) {
    return Math.abs(key.getByte(0) % numberOfPartitions);
}

From source file:com.github.sadikovi.netflowlib.record.RecordMaterializer.java

License:Apache License

/** Read buffer bytes sequence for column offset */
public Object readField(Column column, ByteBuf buffer) {
    Class<?> type = column.getColumnType();
    if (type.equals(Byte.class)) {
        return buffer.getByte(column.getColumnOffset());
    } else if (type.equals(Short.class)) {
        return buffer.getUnsignedByte(column.getColumnOffset());
    } else if (type.equals(Integer.class)) {
        return buffer.getUnsignedShort(column.getColumnOffset());
    } else if (type.equals(Long.class)) {
        return buffer.getUnsignedInt(column.getColumnOffset());
    } else {/* w  w w.ja  va  2  s.  c  om*/
        throw new UnsupportedOperationException("Unsupported read type " + type);
    }
}

From source file:com.github.sadikovi.netflowlib.record.RecordMaterializer.java

License:Apache License

public void updateValueInspector(Column column, ByteBuf buffer, ValueInspector vi) {
    Class<?> type = column.getColumnType();
    if (type.equals(Byte.class)) {
        vi.update(buffer.getByte(column.getColumnOffset()));
    } else if (type.equals(Short.class)) {
        vi.update(buffer.getUnsignedByte(column.getColumnOffset()));
    } else if (type.equals(Integer.class)) {
        vi.update(buffer.getUnsignedShort(column.getColumnOffset()));
    } else if (type.equals(Long.class)) {
        vi.update(buffer.getUnsignedInt(column.getColumnOffset()));
    } else {//from   ww  w  .jav  a 2 s . co  m
        throw new UnsupportedOperationException("Unsupported read type " + type);
    }
}

From source file:com.growcontrol.common.netty.JsonObjectDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    if (this.state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;//from  w ww. j  a  v a  2  s . c o  m
    }
    // index of next byte to process.
    int idx = this.idx;
    final int wrtIdx = in.writerIndex();
    if (wrtIdx > this.maxObjectLength) {
        // buffer size exceeded maxObjectLength; discarding the complete buffer.
        in.skipBytes(in.readableBytes());
        reset();
        throw new TooLongFrameException(
                "object length exceeds " + this.maxObjectLength + ": " + wrtIdx + " bytes discarded");
    }
    for (/* use current idx */; idx < wrtIdx; idx++) {
        final byte c = in.getByte(idx);
        if (this.state == ST_DECODING_NORMAL) {
            decodeByte(c, in, idx);
            // All opening braces/brackets have been closed. That's enough to conclude
            // that the JSON object/array is complete.
            if (this.openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(idx + 1);
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
            }
        } else if (this.state == ST_DECODING_ARRAY_STREAM) {
            decodeByte(c, in, idx);
            if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']')) {
                // skip leading spaces. No range check is needed and the loop will terminate
                // because the byte at position idx is not a whitespace.
                for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
                    in.skipBytes(1);
                }
                // skip trailing spaces.
                int idxNoSpaces = idx - 1;
                while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
                    idxNoSpaces--;
                }
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                in.readerIndex(idx + 1);
                if (c == ']') {
                    reset();
                }
            }
            // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);
            // Discard the array bracket
            if (this.state == ST_DECODING_ARRAY_STREAM)
                in.skipBytes(1);
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
        } else {
            this.state = ST_CORRUPTED;
            throw new CorruptedFrameException(
                    "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in));
        }
    }
    if (in.readableBytes() == 0)
        this.idx = 0;
    else
        this.idx = idx;
}

From source file:com.growcontrol.common.netty.JsonObjectDecoder.java

License:Apache License

private void decodeByte(final byte c, final ByteBuf in, final int idx) {
    if ((c == '{' || c == '[') && !this.insideString) {
        this.openBraces++;
    } else if ((c == '}' || c == ']') && !this.insideString) {
        this.openBraces--;
    } else if (c == '"') {
        // start of a new JSON string. It's necessary to detect strings as they may
        // also contain braces/brackets and that could lead to incorrect results.
        if (!this.insideString) {
            this.insideString = true;
            // If the double quote wasn't escaped then this is the end of a string.
        } else if (in.getByte(idx - 1) != '\\') {
            this.insideString = false;
        }/*from  w  w  w.j  a v  a2  s .c o  m*/
    }
}