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.tesora.dve.mysqlapi.repl.messages.MyLoadLogEvent.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    threadId = cb.readInt();//from w  w w.ja v  a2 s . c om
    time = cb.readInt();
    ignoreLines = cb.readInt();
    tableLen = cb.readByte();
    dbLen = cb.readByte();
    columns = cb.readInt();
    // TODO: need to parse out the variable part of the data
    variableData = Unpooled.buffer(cb.readableBytes());
    variableData.writeBytes(cb);
}

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

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) throws PEException {

    // read event header
    slaveProxyId = cb.readUnsignedInt();
    execTime = cb.readUnsignedInt();/*from   w ww. j  a  va 2s .  c  o  m*/
    dbNameLen = cb.readByte();
    errorCode = cb.readUnsignedShort();
    statusVarsLen = cb.readUnsignedShort();

    statusVars.parseStatusVariables(cb, statusVarsLen);

    dbName = MysqlAPIUtils.readBytesAsString(cb, dbNameLen, CharsetUtil.UTF_8);
    cb.skipBytes(1); //for trailing 0
    query = Unpooled.buffer(cb.readableBytes());
    query.writeBytes(cb);
    origQuery = query.toString(CharsetUtil.UTF_8);
}

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

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) throws PEException {
    //assumes the provided buffer is already scoped to payload boundary.

    rawPayload = cb.slice().copy();//from   w w w  .ja  v a  2 s  . co  m

    // 19 bytes for the common header
    commonHdr = new MyReplEventCommonHeader();
    commonHdr.setTimestamp(cb.readUnsignedInt());
    commonHdr.setType(cb.readByte());
    commonHdr.setServerId(cb.readUnsignedInt());
    commonHdr.setTotalSize(cb.readUnsignedInt());
    commonHdr.setMasterLogPosition(cb.readUnsignedInt());
    commonHdr.setFlags(cb.readShort());

    // unmarshall message based on common header type
    if (logger.isDebugEnabled())
        logger.debug("Unmarshalling event of type: " + MyLogEventType.fromByte(commonHdr.getType()).name());

    levp = MyLogEventPacket.MySqlLogEventFactory.newInstance(MyLogEventType.fromByte(commonHdr.getType()),
            commonHdr);

    levp.unmarshallMessage(cb);
}

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

License:Open Source License

public void parseStatusVariables(ByteBuf cb, int svLen) throws PEException {
    if (svLen > 0) {
        ByteBuf statsVarsBuf = cb.readBytes(svLen);
        while (statsVarsBuf.isReadable()) {
            byte code = statsVarsBuf.readByte();
            MyQueryEventCode mqec = MyQueryEventCode.fromByte(code);
            if (mqec == null) {
                throw new PEException("Replication could not decode query event code: '" + code + "' (0x"
                        + Integer.toHexString(code) + ")");
            }/* w  ww. j  a  va 2  s .c o  m*/

            switch (mqec) {
            case Q_FLAGS2_CODE:
                int flags = statsVarsBuf.readInt();
                suppliedEventCodes.add(new QueryFlags2Event(flags));
                break;

            case Q_SQL_MODE_CODE:
                long sqlMode = statsVarsBuf.readLong();
                suppliedEventCodes.add(new QuerySQLModeEvent(sqlMode));
                break;

            case Q_CATALOG_CODE: {
                byte len = statsVarsBuf.readByte();
                String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8);
                statsVarsBuf.readByte(); // null terminated byte

                suppliedEventCodes.add(new QueryCatalogEvent(catalog));
                break;
            }
            case Q_AUTO_INCREMENT:
                int autoIncrementIncrement = statsVarsBuf.readUnsignedShort();
                int autoIncrementOffset = statsVarsBuf.readUnsignedShort();

                suppliedEventCodes
                        .add(new QueryAutoIncrementEvent(autoIncrementIncrement, autoIncrementOffset));
                break;

            case Q_CHARSET_CODE:
                int charSetClient = statsVarsBuf.readUnsignedShort();
                int collationConnection = statsVarsBuf.readUnsignedShort();
                int collationServer = statsVarsBuf.readUnsignedShort();

                suppliedEventCodes
                        .add(new QueryCharSetCodeEvent(charSetClient, collationConnection, collationServer));
                break;

            case Q_TIME_ZONE_CODE: {
                byte len = statsVarsBuf.readByte();
                String timeZone = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryTimeZoneCodeEvent(timeZone));
                break;
            }
            case Q_CATALOG_NZ_CODE: {
                byte catalogLen = statsVarsBuf.readByte();
                String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, catalogLen, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryCatalogNZEvent(catalog));
                break;
            }
            case Q_LC_TIME_NAMES_CODE:
                short monthDayNames = statsVarsBuf.readShort();

                suppliedEventCodes.add(new QueryTimeNamesEvent(monthDayNames));
                break;

            case Q_CHARSET_DATABASE_CODE:
                short collationDatabase = statsVarsBuf.readShort();

                suppliedEventCodes.add(new QueryCollationDatabaseEvent(collationDatabase));
                break;

            case Q_TABLE_MAP_FOR_UPDATE_CODE:
                long tableMapForUpdate = statsVarsBuf.readLong();

                suppliedEventCodes.add(new QueryTableMapEvent(tableMapForUpdate));
                break;

            case Q_MASTER_DATA_WRITTEN_CODE:
                int originalLength = statsVarsBuf.readInt();

                suppliedEventCodes.add(new QueryMasterDataWrittenEvent(originalLength));
                break;

            case Q_INVOKER:
                int userLen = statsVarsBuf.readByte();
                String user = MysqlAPIUtils.readBytesAsString(statsVarsBuf, userLen, CharsetUtil.UTF_8);
                int hostLen = statsVarsBuf.readByte();
                String host = MysqlAPIUtils.readBytesAsString(statsVarsBuf, hostLen, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryInvokerEvent(user, host));
                break;

            case Q_UPDATED_DB_NAMES:
                List<String> dbNames = new ArrayList<String>();
                int numDbs = statsVarsBuf.readByte();
                if (numDbs > 0) {
                    for (int i = 0; i < numDbs; i++) {
                        dbNames.add(statsVarsBuf.readSlice(statsVarsBuf.bytesBefore((byte) 0))
                                .toString(CharsetUtil.UTF_8));
                        statsVarsBuf.readByte(); //read null byte
                    }
                }
                suppliedEventCodes.add(new QueryUpdatedDBNamesEvent(dbNames));
                break;

            case Q_MICROSECONDS:
                int microseconds = statsVarsBuf.readMedium();

                suppliedEventCodes.add(new QueryMicrosecondsEvent(microseconds));
                break;

            case Q_HRNOW:
                //TODO: this was apparently added for MariaDB, but I can't find a lot of info on it. skip for now.
                suppliedEventCodes.add(new QueryMicrosecondsEvent(statsVarsBuf.readUnsignedMedium()));
                break;

            default:
                throw new PEException("Replication encountered an unknown query event code: '" + code + "' (0x"
                        + Integer.toHexString(code) + ")");
            }
        }
    }
}

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

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) throws PEException {
    variableNameLen = cb.readInt();/*w  w w. ja  v a2  s.  c  o m*/
    variableName = MysqlAPIUtils.readBytesAsString(cb, variableNameLen, CharsetUtil.UTF_8);
    nullByte = cb.readByte();
    if (nullByte != 1) {
        variableValue = processVariableValue(cb);
    } else {
        variableValue = "NULL";
    }
}

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

License:Open Source License

String processVariableValue(ByteBuf cb) throws PEException {
    String value = StringUtils.EMPTY;

    valueType = MyItemResultCode.fromByte(cb.readByte());
    valueCharSet = cb.readInt();/* ww w.  ja  v a2s . c o m*/
    valueLen = cb.readInt();
    valueBytes = Unpooled.buffer(cb.readableBytes()).order(ByteOrder.LITTLE_ENDIAN);
    valueBytes.writeBytes(cb);

    switch (valueType) {
    case DECIMAL_RESULT:
        value = processDecimalValue(valueBytes, valueLen);
        break;
    case INT_RESULT:
        value = processIntValue(valueBytes, valueLen);
        break;
    case REAL_RESULT:
        value = Double.toString(valueBytes.readDouble());
        break;
    case STRING_RESULT:
        value = "'" + StringUtils.replace(
                MysqlAPIUtils.readBytesAsString(valueBytes, valueLen, CharsetUtil.UTF_8), "'", "''") + "'";
        break;
    case ROW_RESULT:
    default:
        throw new PEException(
                "Unsupported variable type '" + valueType + "' for variable '" + variableName + "'");
    }
    return value;
}

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");
    }/*  w w w.  j  av  a 2  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.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

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

    switch (valueLen) {
    case 8:/*from   ww w .  j a  v a 2  s  .c o m*/
        value = Long.toString(cb.readLong());
        break;
    case 7:
    case 6:
    case 5:
        throw new PEException(
                "Cannot decode INT value of length '" + valueLen + "' for variable '" + variableName + "'");
    case 4:
        value = Long.toString(cb.readInt());
        break;
    case 3:
        value = Long.toString(cb.readMedium());
        break;
    case 2:
        value = Long.toString(cb.readShort());
        break;
    case 1:
        value = Byte.toString(cb.readByte());
        break;
    }
    return value;
}

From source file:com.tesora.dve.server.connectionmanager.loaddata.MSPLoadDataDecoder.java

License:Open Source License

private void processQueuedOutput(ChannelHandlerContext ctx) throws Exception {
    byte packetNumber = 0;
    for (;;) {/*  w  w  w.j  av a 2 s  .c  o m*/
        ByteBuf nextDecodedFrame = decodedFrameQueue.poll();

        if (nextDecodedFrame == null)
            break;

        try {
            if (encounteredError == null) {

                packetNumber = nextDecodedFrame.readByte();
                byte[] frameData = MysqlAPIUtils.readBytes(nextDecodedFrame);

                final List<List<byte[]>> parsedRows = LoadDataBlockExecutor.processDataBlock(ctx, ssCon,
                        frameData);

                insertRequest(ctx, parsedRows);

                waitingForInsert = true;

                if (waitingForInsert)
                    break; //OK, we sent out an insert. We'll wait for it to come back before sending another.
            }
        } catch (Throwable t) {
            failLoadData(t);
        } finally {
            ReferenceCountUtil.release(nextDecodedFrame);
        }
    }

    if (waitingForInsert) {
        pauseInput(ctx);
        return;
    }

    boolean loadDataIsFinished = decodedEOF || (encounteredError != null);

    if (loadDataIsFinished) {
        sendResponseAndRemove(ctx);
    } else {
        //not waiting for input, haven't seen input EOF or thrown an exception, must need more input data.
        resumeInput(ctx);
    }

}

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

License:Apache License

public BsonValue<?> readArrayEntry(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    BsonType bsonType = ParsingTools.getBsonType(byteBuf.readByte());
    stringReader.skipCString(byteBuf);//from  w w  w  .ja  va 2  s  .  c o m
    switch (bsonType) {
    case ARRAY:
        return readArray(byteBuf);
    case BINARY:
        return readBinary(byteBuf);
    case DATETIME:
        return readDateTime(byteBuf);
    case DB_POINTER:
        return readDbPointer(byteBuf);
    case DECIMAL128:
        return readDecimal128(byteBuf);
    case DEPRECATED:
        return readDeprecated(byteBuf);
    case DOCUMENT:
        return readDocument(byteBuf);
    case DOUBLE:
        return readDouble(byteBuf);
    case BOOLEAN:
        return readBoolean(byteBuf);
    case INT32:
        return readInt32(byteBuf);
    case INT64:
        return readInt64(byteBuf);
    case JAVA_SCRIPT:
        return readJavaScript(byteBuf);
    case JAVA_SCRIPT_WITH_SCOPE:
        return readJavaScriptWithScope(byteBuf);
    case MAX:
        return readMax(byteBuf);
    case MIN:
        return readMin(byteBuf);
    case NULL:
        return readNull(byteBuf);
    case OBJECT_ID:
        return readObjectId(byteBuf);
    case REGEX:
        return readRegex(byteBuf);
    case STRING:
        return readString(byteBuf);
    case TIMESTAMP:
        return readTimestamp(byteBuf);
    case UNDEFINED:
        return readUndefined(byteBuf);
    default:
        throw new NettyBsonReaderException("Unexpected bson type " + bsonType);
    }
}