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.talent.mysql.packet.response.HandshakePacket.java

License:Open Source License

@Override
public HandshakePacket decodeBody(ByteBuf byteBuf, MysqlHeader mysqlHeader) throws DecodeException {
    this.setMysqlHeader(mysqlHeader);
    int _index = byteBuf.readerIndex();
    int index = _index;

    protocolVersion = byteBuf.getByte(index++);

    int len = 0;//from   www  .  j  av a  2 s . c o m
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    versionInfo = new byte[len];
    byteBuf.getBytes(index, versionInfo, 0, len);
    index += len;
    index++;

    threadId = byteBuf.getInt(index);
    index += 4;

    encrypt1 = new byte[8];
    byteBuf.getBytes(index, encrypt1, 0, 8);
    index += 8;

    fix1 = byteBuf.getByte(index++);

    serverProp1 = new byte[2];
    byteBuf.getBytes(index, serverProp1, 0, 2);
    index += 2;

    charset = byteBuf.getByte(index++);

    serverStatus = new byte[2];
    byteBuf.getBytes(index, serverStatus, 0, 2);
    index += 2;

    serverProp2 = new byte[2];
    byteBuf.getBytes(index, serverProp2, 0, 2);
    index += 2;

    fix2 = byteBuf.getByte(index++);

    //      byte10 = new byte[10];
    //      byteBuf.getBytes(index, byte10, 0, 10);
    index += 10;

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    encrypt2 = new byte[len];
    byteBuf.getBytes(index, encrypt2, 0, len);
    index += len;
    index++;

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    authPluginName = new byte[len];
    byteBuf.getBytes(index, authPluginName, 0, len);
    index += len;
    index++;

    byteBuf.readerIndex(index);
    return this;
}

From source file:com.talent.nio.utils.ByteUtils.java

License:Open Source License

/**
 * //  w  w  w .j av  a  2  s  . com
 * @param buffer
 * @return
 * @throws IOException
 */
public static List<String> toLinesList(ByteBuf buffer) throws IOException {
    List<String> retList = new ArrayList<String>(20);

    int lastPosition = 0;
    int byteCountInOneLine = 0; // 

    byte lastByte = 0; // 
    int length = buffer.capacity();
    for (int i = 0; i < length; i++) {
        byte b = buffer.getByte(i);// .get();
        boolean isLastByte = (length - 1 == i); // ??
        byteCountInOneLine++;

        if (b == '\n') {
            if ((i > 0 && lastByte == '\r')) {
                if (byteCountInOneLine == 2) // ?????
                {
                    retList.add("\r\n"); // 
                } else {
                    byte[] bs1 = new byte[byteCountInOneLine];
                    buffer.getBytes(lastPosition, bs1);
                    String line1 = new String(bs1, "utf-8");
                    retList.add(line1);
                }
            } else {
                if (byteCountInOneLine == 1) // ?????
                {
                    retList.add("\n"); // 
                } else {
                    byte[] bs1 = new byte[byteCountInOneLine];
                    buffer.getBytes(lastPosition, bs1);
                    String line1 = new String(bs1, "utf-8");
                    retList.add(line1);
                }
            }

            byteCountInOneLine = 0;
            lastPosition = i + 1;
        } else if (isLastByte) {
            byte[] bs1 = new byte[byteCountInOneLine];
            buffer.getBytes(lastPosition, bs1);
            String line1 = new String(bs1, "utf-8");
            retList.add(line1);
        }

        lastByte = b;
    }
    return retList;
}

From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRowTest.java

License:Open Source License

@Test
public void testProjection_Identity() throws Exception {
    MyBinaryResultRow fullProj = origRow.projection(new int[] { 0, 1, 2, 3, 4 });

    //check that the sizes are the same.
    assertEquals(origRow.size(), fullProj.size());

    ByteBuf marshallProj = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);
    fullProj.marshallMessage(marshallProj);
    //check that the raw bytes are byte for byte equal.
    for (int i = 0; i < rawRow.readableBytes(); i++) {
        assertEquals("byte at index=" + i, rawRow.getByte(i), marshallProj.getByte(i));
    }/*from   www  .j  av a  2s  . c om*/

    //check that all the slices are byte for byte equal.
    for (int i = 0; i < origRow.size(); i++) {
        assertEquals(origRow.getSlice(i), fullProj.getSlice(i));
    }

    //check that the decoded objects are equal
    for (int i = 0; i < origRow.size(); i++) {
        assertEquals(origRow.getValue(i), fullProj.getValue(i));
    }

}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPProtocolDecoder.java

License:Open Source License

private MSPMessage buildMessage(final ByteBuf payload, final byte sequenceId) {
    final byte messageType = payload.getByte(0); //peek at the first byte in the payload to determine message type.
    try {/*  w  w  w. ja va2s.  c  o  m*/
        return this.messageExecutor[messageType].newPrototype(payload);
    } catch (final Exception e) {
        return new MSPUnknown(messageType, payload);
    }
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPServerGreetingRequestMessage.java

License:Open Source License

public static void write(ChannelHandlerContext ctx, int connectionId, String salt, int serverCapabilities,
        String serverVersion, byte serverCharSet, String pluginData) {
    ByteBuf out = ctx.channel().alloc().heapBuffer(50).order(ByteOrder.LITTLE_ENDIAN);

    String scrambleBuffer1st = salt.substring(0, 8);
    String scrambleBuffer2nd = salt.substring(8) + '\0';
    Integer scrambleBufferSize = scrambleBuffer1st.length() + scrambleBuffer2nd.length();

    ByteBuf serverCapabilitiesBuf = ctx.channel().alloc().heapBuffer(4).order(ByteOrder.LITTLE_ENDIAN);
    try {// w  w w .  j a va  2s . c  o m
        serverCapabilitiesBuf.writeInt(serverCapabilities);
        out.writeMedium(0);
        out.writeByte(0);
        out.writeByte(MYSQL_PROTOCOL_VERSION);

        out.writeBytes(serverVersion.getBytes());
        out.writeZero(1);
        out.writeInt(connectionId);
        out.writeBytes(scrambleBuffer1st.getBytes()); // Salt
        out.writeZero(1);
        out.writeByte(serverCapabilitiesBuf.getByte(0));
        out.writeByte(serverCapabilitiesBuf.getByte(1));

        out.writeByte(serverCharSet);
        out.writeShort(MyProtocolDefs.SERVER_STATUS_AUTOCOMMIT);
        out.writeByte(serverCapabilitiesBuf.getByte(2));
        out.writeByte(serverCapabilitiesBuf.getByte(3));
        out.writeByte(scrambleBufferSize.byteValue());
        out.writeZero(10); // write 10 unused bytes
        out.writeBytes(scrambleBuffer2nd.getBytes()); // Salt

        out.writeBytes(pluginData.getBytes()); // payload
        out.writeZero(1);

        out.setMedium(0, out.writerIndex() - 4);

        ctx.channel().writeAndFlush(out);
    } finally {
        serverCapabilitiesBuf.release();
    }
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MysqlClientAuthenticationHandler.java

License:Open Source License

private void processAcknowlegement(ChannelHandlerContext ctx, ByteBuf payload) throws Exception {
    byte fieldCount = payload.getByte(payload.readerIndex());
    if (fieldCount == MyErrorResponse.ERRORPKT_FIELD_COUNT) {
        processErrorPacket(ctx, payload);
    } else {/*from   w w w  . j  a  va  2  s.  c  om*/
        ctx.pipeline().remove(this);

        enterState(AuthenticationState.AUTHENTICATED);
    }
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

String processDecimalValue(ByteBuf cb, int valueLen) throws PEException {
    String value = StringUtils.EMPTY;

    byte precision = cb.readByte();
    byte scale = cb.readByte();

    int intg = (int) precision - (int) scale;
    int intg0 = intg / DIG_PER_DEC1;
    int frac0 = (int) scale / DIG_PER_DEC1;
    int intg0x = intg - intg0 * DIG_PER_DEC1;
    int frac0x = (int) scale - frac0 * DIG_PER_DEC1;

    int firstValue = intg0 * 4 + dig2bytes[intg0x];
    int secondValue = frac0 * 4 + dig2bytes[frac0x];

    int binSize = firstValue + secondValue;

    int readableBytes = cb.readableBytes();
    if ((firstValue < 1 && secondValue < 1) || readableBytes < binSize) {
        throw new PEException("Cannot decode binary decimal");
    }/*from  w  w w .  j  a v  a2 s  .c o m*/

    ByteBuf chunk = PooledByteBufAllocator.DEFAULT.heapBuffer(binSize);
    cb.readBytes(chunk);

    // 1st byte is special cause it determines the sign
    byte firstByte = chunk.getByte(0);
    int sign = (firstByte & 0x80) == 0x80 ? 1 : -1;
    // invert sign
    chunk.setByte(0, (firstByte ^ 0x80));

    if (sign == -1) {
        // invert all the bytes
        for (int i = 0; i < binSize; i++) {
            chunk.setByte(i, ~chunk.getByte(i));
        }
    }

    BigDecimal integerPortion = decodeBinDecimal(chunk, firstValue, true);
    BigDecimal fractionPortion = decodeBinDecimal(chunk, secondValue, false);

    value = ((sign == -1) ? "-" : StringUtils.EMPTY) + integerPortion.toPlainString() + "."
            + fractionPortion.toPlainString();

    return value;
}

From source file:com.tesora.dve.sql.LargeMaxPktTest.java

License:Open Source License

@Test
public void testComQueryMessageContinuationOverlap() throws Exception {
    int defaultMaxPacket = 0xFFFFFF;
    int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4.
    ByteBuf source = Unpooled.buffer(payloadSize, payloadSize);
    Random rand = new Random(239873L);
    while (source.isWritable())
        source.writeInt(rand.nextInt());
    Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer");

    ByteBuf dest = Unpooled.buffer(payloadSize);

    MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array());
    Packet.encodeFullMessage((byte) 0, outboundMessage, dest);

    int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;
    Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata,
            "Number of bytes in destination buffer is wrong");

    Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum");
    Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero");
    Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER,
            "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER");

    ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1));
    Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1));

}

From source file:com.tesora.dve.sql.LargeMaxPktTest.java

License:Open Source License

@Test
public void testComQueryMessageContinuationExact() throws Exception {
    int defaultMaxPacket = 0xFFFFFF;
    int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet.
    ByteBuf source = Unpooled.buffer(payloadSize, payloadSize);
    Random rand = new Random(239873L);
    while (source.isWritable())
        source.writeInt(rand.nextInt());
    Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer");

    ByteBuf dest = Unpooled.buffer(payloadSize);

    MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array());
    Packet.encodeFullMessage((byte) 0, outboundMessage, dest);

    int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload
    Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata,
            "Number of bytes in destination buffer is wrong");

    Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum");
    Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero");
    Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER,
            "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER");

    ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1));
    Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1));

}

From source file:com.twitter.http2.HttpCodecUtil.java

License:Apache License

/**
 * Reads a big-endian unsigned short integer from the buffer.
 *//*from www  . j  a  v a 2 s  .  com*/
static int getUnsignedShort(ByteBuf buf, int offset) {
    return (buf.getByte(offset) & 0xFF) << 8 | buf.getByte(offset + 1) & 0xFF;
}