Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

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

License:Open Source License

@Override
public void read(MysqlClientHandler handler, ByteBuf in) {
    // header//w  ww .j  a va2s .  c  o  m
    int begin = in.readerIndex();

    tableId = BufferUtils.readPackedInteger(in, 6);
    flags = BufferUtils.readInt(in);

    // payload
    schemaName = BufferUtils.readStringWithLength(in);
    // seperated by 0
    in.readByte();
    tableName = BufferUtils.readStringWithLength(in);
    // seperated by 0
    in.readByte();
    colCount = (int) BufferUtils.readLength(in);
    colTypeDef = BufferUtils.readBytes(in, colCount);

    metaCount = (int) BufferUtils.readLength(in);
    colMetaDef = BufferUtils.readBytes(in, metaCount);

    int nullCount = (colCount + 7) / 8;
    nullBitMap = new byte[nullCount];
    in.readBytes(nullBitMap);

    int end = in.readerIndex();

    if (_log.isTraceEnabled()) {
        in.readerIndex(begin);
        byte[] bytes = new byte[end - begin];
        in.readBytes(bytes);
        String dump = '\n' + UberUtil.hexDump(bytes);
        _log.trace("Packet Info:\n" + this.toString() + "\nTableMapPacket packet:\n" + dump);
    }

}

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

License:Open Source License

@SuppressWarnings("unused")
public void read__(MysqlServerHandler handler) {
    ByteBuf in = this.content;
    statementId = (int) BufferUtils.readUB4(in);
    byte flags = BufferUtils.readByte(in);
    long iterationCount = BufferUtils.readUB4(in);
    this.pstmt = handler.getPrepared().get(statementId);
    if (pstmt == null) {
        throw new OrcaException("prepared statement {} is not found", statementId);
    }//from   w ww. j a v a2s . c o  m

    // read null bitmap

    int nullCount = (pstmt.getParameterCount() + 7) / 8;
    byte[] bytes = new byte[nullCount];
    in.readBytes(bytes);
    BitSet nullBits = BitSet.valueOf(bytes);

    // type information

    int sentTypes = in.readByte();
    if (sentTypes != 0) {
        int[] types = new int[pstmt.getParameterCount()];
        for (int i = 0; i < pstmt.getParameterCount(); i++) {
            types[i] = BufferUtils.readInt(in);
        }
        pstmt.types = types;
    }

    // bind values

    if (pstmt.types == null) {
        // type information is supposed to be sent in the first execution. 
    }
    Heap heap = pstmt.getHeap();
    for (int i = 0; i < pstmt.getParameterCount(); i++) {
        if (nullBits.get(i)) {
            continue;
        }
        if (pstmt.types[i] == Fields.FIELD_TYPE_BLOB) {
            // BLOB values should be set with long data packet
            // and already in pstmt. use isSet to indicate using
            // long data packet in PreparedStmtHandler in execution
            continue;
        }
        long pValue = BindValueUtil.read(heap, in, pstmt.types[i]);
        pstmt.setParam(i, pValue);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.PacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // do we have length field in buffer ?

    if (!in.isReadable(4)) {
        return;//from ww w. j  a v a 2 s  .c o m
    }

    // do we have entire packet in the buffer?

    in.markReaderIndex();
    int size = BufferUtils.readLongInt(in);
    int sequence = in.readByte() & 0xff;
    if (size == 0) {
        out.add(new ShutdownPacket());
        return;
    }
    if (!in.isReadable(size)) {
        in.resetReaderIndex();
        return;
    }

    // is very large packet?

    this.handler.packetSequence = sequence;
    if (size == MAX_PACKET_SIZE) {
        if (this.largePacket == null) {
            this.largePacket = ctx.alloc().directBuffer();
        }
        this.largePacket.writeBytes(in, MAX_PACKET_SIZE);
        return;
    }
    if (this.largePacket != null) {
        this.largePacket.writeBytes(in, size);
    }

    // parse packet

    if (this.largePacket == null) {
        int pos = in.readerIndex();
        try {
            RecievePacket packet = readPacket(in, size);
            out.add(packet);
        } finally {
            in.readerIndex(pos + size);
        }
    } else {
        RecievePacket packet = readPacket(this.largePacket, size);
        out.add(packet);
        this.largePacket.release();
        this.largePacket = null;
    }
}

From source file:com.antsdb.saltedfish.server.mysql.PacketDecoder.java

License:Open Source License

private RecievePacket readPacket(ByteBuf in, int size) {
    // handshake//from   w  ww  . ja v  a  2  s. co  m

    RecievePacket packet = null;
    try {
        if (!this.isHandshaken) {
            this.isHandshaken = true;
            packet = new AuthPacket();
            packet.packetLength = size;
            packet.read(this.handler, in);
        } else {
            // command 

            byte command = in.readByte();
            size--;
            switch (command) {
            case MySQLPacket.COM_QUERY:
                packet = new QueryPacket(command);
                break;
            case MySQLPacket.COM_STMT_PREPARE:
                packet = new StmtPreparePacket(command);
                break;
            case MySQLPacket.COM_STMT_EXECUTE:
                packet = new StmtExecutePacket(command);
                break;
            case MySQLPacket.COM_PING:
                packet = new PingPacket(command);
                break;
            case MySQLPacket.COM_INIT_DB:
                // Handle init_db cmd as query. Conversion is in QueryPacket read()
                packet = new InitPacket(command);
                break;
            case MySQLPacket.COM_STMT_CLOSE:
                packet = new StmtClosePacket(command);
                break;
            case MySQLPacket.COM_QUIT:
                packet = new ClosePacket(command);
                break;
            case MySQLPacket.COM_STMT_SEND_LONG_DATA:
                packet = new LongDataPacket(command);
                break;
            case MySQLPacket.COM_FIELD_LIST:
                packet = new FieldListPacket(command);
                break;
            case MySQLPacket.COM_SET_OPTION:
                packet = new SetOptionPacket(command);
                break;
            case MySQLPacket.COM_STMT_RESET:
            default:
                throw new CodingError("unknown command: " + command);
            }
            packet.packetLength = size;
            packet.read(this.handler, in);
        }
    } catch (UnsupportedEncodingException e) {
        throw new CodingError("unknown command: " + e);
    }
    return packet;
}

From source file:com.antsdb.saltedfish.server.mysql.ReplicationPacketDecoder.java

License:Open Source License

@SuppressWarnings("unused")
private ReplicationPacket readPacket(ByteBuf in, int size) {
    // packet sequence number for multiple packets

    // byte packetSequence = in.readByte();
    byte seqId = in.readByte();

    // handshake/*  w w  w .  j  ava  2 s . c o  m*/

    ReplicationPacket packet = null;

    // using state to decide how to handle connecting messages.
    if (state == StateIndicator.INITIAL_STATE) {
        packet = new StateIndicator(StateIndicator.INITIAL_STATE);
        packet.packetLength = size;
        packet.packetId = seqId;
        packet.read(this.handler, in);
        state = StateIndicator.RESPONSED_STATE;
    } else if (state == StateIndicator.RESPONSED_STATE) {
        byte header = in.readByte();

        if (header == 0) {
            packet = new StateIndicator(StateIndicator.HANDSHAKEN_STATE);
            state = StateIndicator.HANDSHAKEN_STATE;
        } else {
            packet = new StateIndicator(StateIndicator.HANDSHAKE_FAIL_STATE);
            state = StateIndicator.HANDSHAKE_FAIL_STATE;
        }
        char[] bytes = new char[size];
        for (int i = 0; i < size; i++) {
            int ch = in.getByte(i);
            bytes[i] = (char) ch;
        }
        packet.packetId = seqId;
        packet.packetLength = size;
        packet.read(this.handler, in);
    } else if (state == StateIndicator.HANDSHAKEN_STATE) {
        // expecting response for registered slave
        byte header = in.readByte();

        if (header == 0) {
            packet = new StateIndicator(StateIndicator.REGISTERED_STATE);
            state = StateIndicator.REGISTERED_STATE;
        } else {
            packet = new StateIndicator(StateIndicator.REGISTER_FAIL_STATE);
            state = StateIndicator.REGISTER_FAIL_STATE;
        }
        packet.packetId = seqId;
        packet.packetLength = size;
        packet.read(this.handler, in);
    } else {

        // binlog stream started with 00 ok-byte
        byte okByte = in.readByte();

        if (okByte == 0) {
            // read event header

            // timestamp 4 bytes
            int timeStamp = in.readInt();
            // event type
            byte eventType = in.readByte();
            // server id, 4 bytes
            int serverId = (int) BufferUtils.readLong(in);
            // event length, 4 bytes
            long eventLength = BufferUtils.readLong(in);
            // next position, 4 bytes
            long nextPosition = BufferUtils.readLong(in);
            // flags
            int flags = in.readShort();

            // events

            switch (eventType) {
            case ReplicationPacket.ROTATE_EVENT:
                packet = new RotatePacket(eventType, eventLength, nextPosition);
                break;
            case ReplicationPacket.TABLE_MAP_EVENT:
                packet = new TableMapPacket(eventType, eventLength, nextPosition);
                break;
            case ReplicationPacket.WRITE_ROWS_EVENT:
            case ReplicationPacket.UPDATE_ROWS_EVENT:
            case ReplicationPacket.DELETE_ROWS_EVENT:
                packet = new RowsEventV2Packet(eventType, eventLength, nextPosition);
                break;
            case ReplicationPacket.STOP_EVENT:
                packet = new StopPacket(eventType, eventLength, nextPosition);
                break;
            case ReplicationPacket.XID_EVENT:
                packet = new XIDPacket(eventType, eventLength, nextPosition);
                break;
            case ReplicationPacket.QUERY_EVENT:
            case ReplicationPacket.FORMAT_DESCRIPTION_EVENT:
            case ReplicationPacket.START_EVENT_V3:
                // use GenericPacket to ignore unsupported events for now
                packet = new GenericPacket(eventType, eventLength, nextPosition);
                break;
            default:
                _log.error("unknown event: " + eventType);
                throw new CodingError("unknown event: " + eventType);
            }

            if (packet != null) {
                packet.packetId = seqId;
                packet.packetLength = size;
                packet.read(this.handler, in);
            }
        } else {
            ByteBuf pkt = (ByteBuf) in;
            ByteBuffer bbuf = pkt.nioBuffer();

            int i = bbuf.remaining();

            byte[] bytes = new byte[i];
            pkt.readBytes(bytes);
            String dump = '\n' + UberUtil.hexDump(bytes);
            _log.error("unknown packet: " + dump);
            throw new CodingError("unknown packet");
        }
    }
    return packet;
}

From source file:com.antsdb.saltedfish.server.mysql.util.BindValueUtil.java

License:Open Source License

public static long read(Heap heap, ByteBuf buf, int type) {
    long pValue = 0;
    switch (type & 0xff) {
    case Fields.FIELD_TYPE_TINY:
        pValue = Int4.allocSet(heap, buf.readByte());
        break;/* www.j ava 2  s. com*/
    case Fields.FIELD_TYPE_SHORT:
        pValue = Int4.allocSet(heap, (short) BufferUtils.readInt(buf));
        break;
    case Fields.FIELD_TYPE_LONG:
        pValue = Int4.allocSet(heap, BufferUtils.readLong(buf));
        break;
    case Fields.FIELD_TYPE_LONGLONG:
        pValue = Int8.allocSet(heap, BufferUtils.readLongLong(buf));
        break;
    case Fields.FIELD_TYPE_FLOAT:
        pValue = Float4.allocSet(heap, BufferUtils.readFloat(buf));
        break;
    case Fields.FIELD_TYPE_DOUBLE:
        pValue = Float8.allocSet(heap, BufferUtils.readDouble(buf));
        break;
    case Fields.FIELD_TYPE_TIME:
    case Fields.FIELD_TYPE_TIME2:
        pValue = FishTime.allocSet(heap, BufferUtils.readTime(buf));
        break;
    case Fields.FIELD_TYPE_DATE:
        pValue = FishDate.allocSet(heap, BufferUtils.readDate(buf));
        break;
    case Fields.FIELD_TYPE_DATETIME:
    case Fields.FIELD_TYPE_TIMESTAMP:
    case Fields.FIELD_TYPE_DATETIME2:
    case Fields.FIELD_TYPE_TIMESTAMP2:
        pValue = FishTimestamp.allocSet(heap, BufferUtils.readTimestamp(buf));
        break;
    case Fields.FIELD_TYPE_VAR_STRING:
    case Fields.FIELD_TYPE_STRING:
    case Fields.FIELD_TYPE_VARCHAR:
        int len = (int) BufferUtils.readLength(buf);
        long pData = buf.memoryAddress() + buf.readerIndex();
        pValue = FishUtf8.allocSet(heap, pData, len);
        buf.readerIndex(buf.readerIndex() + len);
        break;
    case Fields.FIELD_TYPE_DECIMAL:
    case Fields.FIELD_TYPE_NEW_DECIMAL:
        pValue = FishNumber.allocSet(heap, BufferUtils.readBigDecimal(buf));
        break;
    default:
        throw new IllegalArgumentException("bindValue error,unsupported type:" + type);
    }
    return pValue;
}

From source file:com.antsdb.saltedfish.server.mysql.util.BindValueUtil.java

License:Open Source License

public static final void read(ByteBuf buf, BindValue bv) {
    switch (bv.type & 0xff) {
    case Fields.FIELD_TYPE_TINY:
        bv.byteBinding = buf.readByte();
        break;//from w ww. j a v a  2s  .c  om
    case Fields.FIELD_TYPE_SHORT:
        bv.shortBinding = (short) BufferUtils.readInt(buf);
        break;
    case Fields.FIELD_TYPE_LONG:
        bv.intBinding = BufferUtils.readLong(buf);
        break;
    case Fields.FIELD_TYPE_LONGLONG:
        bv.longBinding = BufferUtils.readLongLong(buf);
        break;
    case Fields.FIELD_TYPE_FLOAT:
        bv.floatBinding = BufferUtils.readFloat(buf);
        break;
    case Fields.FIELD_TYPE_DOUBLE:
        bv.doubleBinding = BufferUtils.readDouble(buf);
        break;
    case Fields.FIELD_TYPE_TIME:
    case Fields.FIELD_TYPE_TIME2:
        bv.value = BufferUtils.readTime(buf);
        break;
    case Fields.FIELD_TYPE_DATE:
        bv.value = BufferUtils.readDate(buf);
        break;
    case Fields.FIELD_TYPE_DATETIME:
    case Fields.FIELD_TYPE_TIMESTAMP:
    case Fields.FIELD_TYPE_DATETIME2:
    case Fields.FIELD_TYPE_TIMESTAMP2:
        bv.value = BufferUtils.readTimestamp(buf);
        break;
    case Fields.FIELD_TYPE_VAR_STRING:
        //bv.value = BufferUtils.readBytesWithLength(buf);
        //break;
    case Fields.FIELD_TYPE_STRING:
    case Fields.FIELD_TYPE_VARCHAR:
        bv.value = BufferUtils.readStringWithLength(buf);
        if (bv.value == null) {
            bv.isNull = true;
        }
        break;
    case Fields.FIELD_TYPE_DECIMAL:
    case Fields.FIELD_TYPE_NEW_DECIMAL:
        bv.value = BufferUtils.readBigDecimal(buf);
        if (bv.value == null) {
            bv.isNull = true;
        }
        break;
    default:
        throw new IllegalArgumentException("bindValue error,unsupported type:" + bv.type);
    }
    bv.isSet = true;
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static int readLongInt(ByteBuf buf) {
    byte b0 = buf.readByte();
    byte b1 = buf.readByte();
    byte b2 = buf.readByte();
    int n = (b0 & 0xff) | ((b1 & 0xff) << 8) | ((b2 & 0xff) << 16);
    return n;//from  w  w  w .  j  a va 2s . com
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static int readLong(ByteBuf in) {
    int l = (int) (in.readByte() & 0xff);
    l |= (int) (in.readByte() & 0xff) << 8;
    l |= (int) (in.readByte() & 0xff) << 16;
    l |= (int) (in.readByte() & 0xff) << 24;
    return l;/*from  w  w w.j av  a2 s  . c  o  m*/
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static long readLongLong(ByteBuf in) {
    long l = (long) (in.readByte() & 0xff);
    l |= (long) (in.readByte() & 0xff) << 8;
    l |= (long) (in.readByte() & 0xff) << 16;
    l |= (long) (in.readByte() & 0xff) << 24;
    l |= (long) (in.readByte() & 0xff) << 32;
    l |= (long) (in.readByte() & 0xff) << 40;
    l |= (long) (in.readByte() & 0xff) << 48;
    l |= (long) (in.readByte() & 0xff) << 56;
    return l;/*from   w w w . ja  v a2  s  .c om*/
}