Java Code Examples for io.netty.buffer.ByteBuf#readUnsignedShortLE()

The following examples show how to use io.netty.buffer.ByteBuf#readUnsignedShortLE() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorMessage.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
public static ErrorMessage decode(ByteBuf buf) {
    buf.skipBytes(1); // 0xFF, error message header
    int errorCode = buf.readUnsignedShortLE(); // error code should be unsigned

    String sqlState;

    // capabilities & PROTOCOL_41, only exists under protocol 4.1
    if ('#' == buf.getByte(buf.readerIndex())) {
        buf.skipBytes(1); // constant '#'
        sqlState = buf.toString(buf.readerIndex(), SQL_STATE_SIZE, StandardCharsets.US_ASCII);
        buf.skipBytes(SQL_STATE_SIZE); // skip fixed string length by read
    } else {
        sqlState = null;
    }

    return new ErrorMessage(errorCode, sqlState, buf.toString(StandardCharsets.US_ASCII));
}
 
Example 2
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Note: it will change {@code buf} readerIndex.
 *
 * @param buf a readable buffer include a var integer.
 * @return A var integer read from buffer.
 */
public static long readVarInt(ByteBuf buf) {
    requireNonNull(buf, "buf must not be null");

    short firstByte = buf.readUnsignedByte();

    if (firstByte < VAR_INT_2_BYTE_CODE) {
        return firstByte;
    } else if (firstByte == VAR_INT_2_BYTE_CODE) {
        return buf.readUnsignedShortLE();
    } else if (firstByte == VAR_INT_3_BYTE_CODE) {
        return buf.readUnsignedMediumLE();
    } else {
        return buf.readLongLE();
    }
}
 
Example 3
Source File: PreparedOkMessage.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
static PreparedOkMessage decode(ByteBuf buf) {
    buf.skipBytes(1); // constant 0x00
    int statementId = buf.readIntLE();
    int totalColumns = buf.readUnsignedShortLE();
    int totalParameters = buf.readUnsignedShortLE();
    buf.skipBytes(1); // constant filler, 0x00
    int warnings;

    if (buf.isReadable(2)) {
        warnings = buf.readUnsignedShortLE();
    } else {
        warnings = 0;
    }

    return new PreparedOkMessage(statementId, totalColumns, totalParameters, warnings);
}
 
Example 4
Source File: IntegerCodec.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
private static int decodeBinary(ByteBuf buf, short type, boolean isUnsigned) {
    switch (type) {
        case DataTypes.INT: // Already check overflow in `doCanDecode`
        case DataTypes.MEDIUMINT:
            return buf.readIntLE();
        case DataTypes.SMALLINT:
            if (isUnsigned) {
                return buf.readUnsignedShortLE();
            } else {
                return buf.readShortLE();
            }
        case DataTypes.YEAR:
            return buf.readShortLE();
        default: // TINYINT
            if (isUnsigned) {
                return buf.readUnsignedByte();
            } else {
                return buf.readByte();
            }
    }
}
 
Example 5
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static List<ResourcePacksInfoPacket.Entry> readPacksInfoEntries(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    List<ResourcePacksInfoPacket.Entry> entries = new ObjectArrayList<>();
    int length = buffer.readUnsignedShortLE();
    for (int i = 0; i < length; i++) {
        String packId = readString(buffer);
        String packVersion = readString(buffer);
        long packSize = buffer.readLongLE();
        String encryptionKey = readString(buffer);
        String subpackName = readString(buffer);
        String contentId = readString(buffer);
        boolean scripting = buffer.readBoolean();
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, scripting));
    }
    return entries;
}
 
Example 6
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Note: it will change {@code buf} readerIndex.
 *
 * @param buf a readable buffer include a var integer.
 * @return A var integer read from buffer.
 */
public static long readVarInt(ByteBuf buf) {
    requireNonNull(buf, "buf must not be null");

    short firstByte = buf.readUnsignedByte();

    if (firstByte < VAR_INT_2_BYTE_CODE) {
        return firstByte;
    } else if (firstByte == VAR_INT_2_BYTE_CODE) {
        return buf.readUnsignedShortLE();
    } else if (firstByte == VAR_INT_3_BYTE_CODE) {
        return buf.readUnsignedMediumLE();
    } else {
        return buf.readLongLE();
    }
}
 
Example 7
Source File: CodecUtils.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
public static long readLengthEncodedInteger(ByteBuf buf, int firstByte) {
	firstByte = firstByte & 0xff;
	if (firstByte < NULL_VALUE) {
		return firstByte;
	}
	if (firstByte == NULL_VALUE) {
		return -1;
	}
	if (firstByte == SHORT_VALUE) {
		return buf.readUnsignedShortLE();
	}
	if (firstByte == MEDIUM_VALUE) {
		return buf.readUnsignedMediumLE();
	}
	if (firstByte == LONG_VALUE) {
		long length = buf.readLongLE();
		if (length < 0) {
			throw new CodecException("Received a length value too large to handle: " + Long.toHexString(length));
		}
		return length;
	}
	throw new CodecException("Received an invalid length value " + firstByte);
}
 
Example 8
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static List<ResourcePacksInfoPacket.Entry> readPacksInfoEntries(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    List<ResourcePacksInfoPacket.Entry> entries = new ObjectArrayList<>();
    int length = buffer.readUnsignedShortLE();
    for (int i = 0; i < length; i++) {
        String packId = readString(buffer);
        String packVersion = readString(buffer);
        long packSize = buffer.readLongLE();
        String encryptionKey = readString(buffer);
        String subpackName = readString(buffer);
        String contentId = readString(buffer);
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, false));
    }
    return entries;
}
 
Example 9
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static List<ResourcePacksInfoPacket.Entry> readPacksInfoEntries(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    List<ResourcePacksInfoPacket.Entry> entries = new ObjectArrayList<>();
    int length = buffer.readUnsignedShortLE();
    for (int i = 0; i < length; i++) {
        String packId = readString(buffer);
        String packVersion = readString(buffer);
        long packSize = buffer.readLongLE();
        String encryptionKey = readString(buffer);
        String subpackName = readString(buffer);
        String contentId = readString(buffer);
        boolean unknownBool = buffer.readBoolean();
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, unknownBool));
    }
    return entries;
}
 
Example 10
Source File: LongCodec.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private static long decodeBinary(ByteBuf buf, short type, boolean isUnsigned) {
    switch (type) {
        case DataTypes.BIGINT:
            // Note: no check overflow for BIGINT UNSIGNED
            return buf.readLongLE();
        case DataTypes.INT:
            if (isUnsigned) {
                return buf.readUnsignedIntLE();
            } else {
                return buf.readIntLE();
            }
        case DataTypes.MEDIUMINT:
            // Note: MySQL return 32-bits two's complement for 24-bits integer
            return buf.readIntLE();
        case DataTypes.SMALLINT:
            if (isUnsigned) {
                return buf.readUnsignedShortLE();
            } else {
                return buf.readShortLE();
            }
        case DataTypes.YEAR:
            return buf.readShortLE();
        default: // TINYINT
            if (isUnsigned) {
                return buf.readUnsignedByte();
            } else {
                return buf.readByte();
            }
    }
}
 
Example 11
Source File: CommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
ColumnDefinition decodeColumnDefinitionPacketPayload(ByteBuf payload) {
  String catalog = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  String schema = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  String table = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  String orgTable = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  String name = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  String orgName = BufferUtils.readLengthEncodedString(payload, StandardCharsets.UTF_8);
  long lengthOfFixedLengthFields = BufferUtils.readLengthEncodedInteger(payload);
  int characterSet = payload.readUnsignedShortLE();
  long columnLength = payload.readUnsignedIntLE();
  DataType type = DataType.valueOf(payload.readUnsignedByte());
  int flags = payload.readUnsignedShortLE();
  byte decimals = payload.readByte();
  return new ColumnDefinition(catalog, schema, table, orgTable, name, orgName, characterSet, columnLength, type, flags, decimals);
}
 
Example 12
Source File: FecPacket.java    From java-Kcp with Apache License 2.0 5 votes vote down vote up
public static FecPacket newFecPacket(ByteBuf byteBuf){
    FecPacket pkt = FEC_PACKET_RECYCLER.get();
    pkt.seqid =byteBuf.readUnsignedIntLE();
    pkt.flag = byteBuf.readUnsignedShortLE();
    pkt.data = byteBuf.retainedSlice(byteBuf.readerIndex(),byteBuf.capacity()-byteBuf.readerIndex());
    pkt.data.writerIndex(byteBuf.readableBytes());
    return pkt;
}
 
Example 13
Source File: AbstractPacketDecoder.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
protected ServerEofPacket decodeEofResponse(int sequenceId, ByteBuf packet, Set<CapabilityFlags> capabilities) {
	if (capabilities.contains(CapabilityFlags.CLIENT_PROTOCOL_41)) {
		return new ServerEofPacket(
				sequenceId,
				packet.readUnsignedShortLE(),
				CodecUtils.readShortEnumSet(packet, ServerStatusFlag.class));
	} else {
		return new ServerEofPacket(sequenceId, 0);
	}
}
 
Example 14
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
private static long crossReadLong0(ByteBuf firstPart, ByteBuf secondPart) {
    int readable = firstPart.readableBytes();

    if (readable == 0) {
        return secondPart.readLongLE();
    }

    long low, middle, high;

    switch (readable) {
        case 1:
            low = firstPart.readUnsignedByte();
            middle = (secondPart.readUnsignedIntLE() << Byte.SIZE);
            high = ((long) secondPart.readUnsignedMediumLE()) << (Byte.SIZE + Integer.SIZE);

            return high | middle | low;
        case 2:
            low = firstPart.readUnsignedShortLE();
            middle = secondPart.readUnsignedIntLE() << Short.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Short.SIZE + Integer.SIZE);

            return high | middle | low;
        case 3:
            low = firstPart.readUnsignedMediumLE();
            middle = secondPart.readUnsignedIntLE() << MEDIUM_SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (MEDIUM_SIZE + Integer.SIZE);

            return high | middle | low;
        case 4:
            low = firstPart.readUnsignedIntLE();
            high = secondPart.readUnsignedIntLE() << Integer.SIZE;

            return high | low;
        case 5:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedByte()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedMediumLE()) << (Integer.SIZE + Byte.SIZE);

            return high | middle | low;
        case 6:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedShortLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Integer.SIZE + Short.SIZE);

            return high | middle | low;
        case 7:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedMediumLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (Integer.SIZE + MEDIUM_SIZE);

            return high | middle | low;
        default:
            return firstPart.readLongLE();
    }
}
 
Example 15
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@Override
public Object read(final ByteBuf in) {
    final int length = in.readUnsignedShortLE();
    Codec.checkReadableBytes(in, length, 0xFFFF);
    return in.readBytes(length);
}
 
Example 16
Source File: QueryCommandBaseCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private MSSQLDataType decodeDataTypeMetadata(ByteBuf payload) {
  int typeInfo = payload.readUnsignedByte();
  switch (typeInfo) {
    /*
     * FixedLen DataType
     */
    case INT1TYPE_ID:
      return FixedLenDataType.INT1TYPE;
    case INT2TYPE_ID:
      return FixedLenDataType.INT2TYPE;
    case INT4TYPE_ID:
      return FixedLenDataType.INT4TYPE;
    case INT8TYPE_ID:
      return FixedLenDataType.INT8TYPE;
    case FLT4TYPE_ID:
      return FixedLenDataType.FLT4TYPE;
    case FLT8TYPE_ID:
      return FixedLenDataType.FLT8TYPE;
    case BITTYPE_ID:
      return FixedLenDataType.BITTYPE;
    /*
     * Variable Length Data Type
     */
    case NUMERICNTYPE_ID:
    case DECIMALNTYPE_ID:
      short numericTypeSize = payload.readUnsignedByte();
      byte numericPrecision = payload.readByte();
      byte numericScale = payload.readByte();
      return new NumericDataType(NUMERICNTYPE_ID, Numeric.class, numericPrecision, numericScale);
    case INTNTYPE_ID:
      byte intNTypeLength = payload.readByte();
      return IntNDataType.valueOf(intNTypeLength);
    case FLTNTYPE_ID:
      byte fltNTypeLength = payload.readByte();
      return FloatNDataType.valueOf(fltNTypeLength);
    case BITNTYPE_ID:
      payload.skipBytes(1); // should only be 1
      return BitNDataType.BIT_1_DATA_TYPE;
    case DATENTYPE_ID:
      return FixedLenDataType.DATENTYPE;
    case TIMENTYPE_ID:
      byte scale = payload.readByte();
      return new TimeNDataType(scale);
    case BIGCHARTYPE_ID:
    case BIGVARCHRTYPE_ID:
      int size = payload.readUnsignedShortLE();
      short collateCodepage = payload.readShortLE();
      short collateFlags = payload.readShortLE();
      byte collateCharsetId = payload.readByte();
      return new TextWithCollationDataType(BIGVARCHRTYPE_ID, String.class, null);
    default:
      throw new UnsupportedOperationException("Unsupported type with typeinfo: " + typeInfo);
  }
}
 
Example 17
Source File: PrepareStatementCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
@Override
void decodePayload(ByteBuf payload, int payloadLength) {
  switch (commandHandlerState) {
    case INIT:
      int firstByte = payload.getUnsignedByte(payload.readerIndex());
      if (firstByte == ERROR_PACKET_HEADER) {
        handleErrorPacketPayload(payload);
      } else {
        // handle COM_STMT_PREPARE response
        payload.readUnsignedByte(); // 0x00: OK
        long statementId = payload.readUnsignedIntLE();
        int numberOfColumns = payload.readUnsignedShortLE();
        int numberOfParameters = payload.readUnsignedShortLE();
        payload.readByte(); // [00] filler
        int numberOfWarnings = payload.readShortLE();

        // handle metadata here
        this.statementId = statementId;
        this.paramDescs = new ColumnDefinition[numberOfParameters];
        this.columnDescs = new ColumnDefinition[numberOfColumns];

        if (numberOfParameters != 0) {
          processingIndex = 0;
          this.commandHandlerState = CommandHandlerState.HANDLING_PARAM_COLUMN_DEFINITION;
        } else if (numberOfColumns != 0) {
          processingIndex = 0;
          this.commandHandlerState = CommandHandlerState.HANDLING_COLUMN_COLUMN_DEFINITION;
        } else {
          handleReadyForQuery();
          resetIntermediaryResult();
        }
      }
      break;
    case HANDLING_PARAM_COLUMN_DEFINITION:
      paramDescs[processingIndex++] = decodeColumnDefinitionPacketPayload(payload);
      if (processingIndex == paramDescs.length) {
        if (isDeprecatingEofFlagEnabled()) {
          // we enabled the DEPRECATED_EOF flag and don't need to accept an EOF_Packet
          handleParamDefinitionsDecodingCompleted();
        } else {
          // we need to decode an EOF_Packet before handling rows, to be compatible with MySQL version below 5.7.5
          commandHandlerState = CommandHandlerState.PARAM_DEFINITIONS_DECODING_COMPLETED;
        }
      }
      break;
    case PARAM_DEFINITIONS_DECODING_COMPLETED:
      skipEofPacketIfNeeded(payload);
      handleParamDefinitionsDecodingCompleted();
      break;
    case HANDLING_COLUMN_COLUMN_DEFINITION:
      columnDescs[processingIndex++] = decodeColumnDefinitionPacketPayload(payload);
      if (processingIndex == columnDescs.length) {
        if (isDeprecatingEofFlagEnabled()) {
          // we enabled the DEPRECATED_EOF flag and don't need to accept an EOF_Packet
          handleColumnDefinitionsDecodingCompleted();
        } else {
          // we need to decode an EOF_Packet before handling rows, to be compatible with MySQL version below 5.7.5
          commandHandlerState = CommandHandlerState.COLUMN_DEFINITIONS_DECODING_COMPLETED;
        }
      }
      break;
    case COLUMN_DEFINITIONS_DECODING_COMPLETED:
      handleColumnDefinitionsDecodingCompleted();
      break;
  }
}
 
Example 18
Source File: DefinitionMetadataMessage.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
private static DefinitionMetadataMessage decode41(ByteBuf buf, ConnectionContext context) {
    buf.skipBytes(4); // "def" which sized by var integer

    CharCollation collation = context.getClientCollation();
    Charset charset = collation.getCharset();
    String database = readVarIntSizedString(buf, charset);
    String table = readVarIntSizedString(buf, charset);
    String originTable = readVarIntSizedString(buf, charset);
    String column = readVarIntSizedString(buf, charset);
    String originColumn = readVarIntSizedString(buf, charset);

    VarIntUtils.readVarInt(buf); // skip constant 0x0c encoded by var integer

    int collationId = buf.readUnsignedShortLE();
    long size = buf.readUnsignedIntLE();
    short type = buf.readUnsignedByte();
    short definitions = buf.readShortLE();

    if (DataTypes.JSON == type && collationId == CharCollation.BINARY_ID) {
        collationId = collation.getId();
    }

    if ((definitions & ColumnDefinitions.SET) != 0) {
        // Maybe need to check if it is a string-like type?
        type = DataTypes.SET;
    } else if ((definitions & ColumnDefinitions.ENUMERABLE) != 0) {
        // Maybe need to check if it is a string-like type?
        type = DataTypes.ENUMERABLE;
    }

    return new DefinitionMetadataMessage(
        database,
        table,
        originTable,
        column,
        originColumn,
        collationId,
        size,
        type,
        definitions,
        buf.readUnsignedByte()
    );
}
 
Example 19
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@Override
public final Object read(final ByteBuf in) {
    return in.readUnsignedShortLE();
}
 
Example 20
Source File: MySQLJsonValueDecoder.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
private static int getIntBasedObjectSize(final ByteBuf byteBuf, final boolean isSmall) {
    return isSmall ? byteBuf.readUnsignedShortLE() : (int) byteBuf.readUnsignedIntLE();
}