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

The following examples show how to use io.netty.buffer.ByteBuf#setMediumLE() . 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: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
private void sendStatementFetchCommand(long statementId, int count) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_FETCH);
  packet.writeIntLE((int) statementId);
  packet.writeIntLE(count);

  // set payload length
  int lenOfPayload = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, lenOfPayload);

  encoder.chctx.writeAndFlush(packet);
}
 
Example 2
Source File: SimpleQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
private void sendQueryCommand() {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_QUERY);
  packet.writeCharSequence(cmd.sql(), encoder.encodingCharset);

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}
 
Example 3
Source File: PrepareStatementCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
private void sendStatementPrepareCommand() {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_PREPARE);
  packet.writeCharSequence(cmd.sql(), encoder.encodingCharset);

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}
 
Example 4
Source File: InitDbCommandCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
private void sendInitDbCommand() {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_INIT_DB);
  packet.writeCharSequence(cmd.schemaName(), StandardCharsets.UTF_8);

  // set payload length
  int lenOfPayload = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, lenOfPayload);

  sendPacket(packet, lenOfPayload);
}
 
Example 5
Source File: SnappyFrameEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void setChunkLength(ByteBuf out, int lengthIdx) {
    int chunkLength = out.writerIndex() - lengthIdx - 3;
    if (chunkLength >>> 24 != 0) {
        throw new CompressionException("compressed data too large: " + chunkLength);
    }
    out.setMediumLE(lengthIdx, chunkLength);
}
 
Example 6
Source File: SnappyFrameEncoder.java    From teku with Apache License 2.0 5 votes vote down vote up
private static void setChunkLength(ByteBuf out, int lengthIdx) {
  int chunkLength = out.writerIndex() - lengthIdx - 3;
  if (chunkLength >>> 24 != 0) {
    throw new IllegalArgumentException("compressed data too large: " + chunkLength);
  }
  out.setMediumLE(lengthIdx, chunkLength);
}
 
Example 7
Source File: ChangeUserCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendChangeUserCommand() {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_CHANGE_USER);
  BufferUtils.writeNullTerminatedString(packet, cmd.username(), StandardCharsets.UTF_8);
  String password = cmd.password();
  if (password.isEmpty()) {
    packet.writeByte(0);
  } else {
    packet.writeByte(password.length());
    packet.writeCharSequence(password, StandardCharsets.UTF_8);
  }
  BufferUtils.writeNullTerminatedString(packet, cmd.database(), StandardCharsets.UTF_8);
  MySQLCollation collation = cmd.collation();
  int collationId = collation.collationId();
  packet.writeShortLE(collationId);

  if ((encoder.clientCapabilitiesFlag & CLIENT_PLUGIN_AUTH) != 0) {
    BufferUtils.writeNullTerminatedString(packet, "mysql_native_password", StandardCharsets.UTF_8);
  }
  if ((encoder.clientCapabilitiesFlag & CLIENT_CONNECT_ATTRS) != 0) {
    Map<String, String> clientConnectionAttributes = cmd.connectionAttributes();
    if (clientConnectionAttributes != null) {
      encodeConnectionAttributes(clientConnectionAttributes, packet);
    }
  }

  // set payload length
  int lenOfPayload = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, lenOfPayload);

  sendPacket(packet, lenOfPayload);
}
 
Example 8
Source File: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendStatementExecuteCommand(MySQLPreparedStatement statement, boolean sendTypesToServer, Tuple params, byte cursorType) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_EXECUTE);
  packet.writeIntLE((int) statement.statementId);
  packet.writeByte(cursorType);
  // iteration count, always 1
  packet.writeIntLE(1);

  int numOfParams = statement.bindingTypes().length;
  int bitmapLength = (numOfParams + 7) / 8;
  byte[] nullBitmap = new byte[bitmapLength];

  int pos = packet.writerIndex();

  if (numOfParams > 0) {
    // write a dummy bitmap first
    packet.writeBytes(nullBitmap);
    packet.writeBoolean(sendTypesToServer);

    if (sendTypesToServer) {
      for (DataType bindingType : statement.bindingTypes()) {
        packet.writeByte(bindingType.id);
        packet.writeByte(0); // parameter flag: signed
      }
    }

    for (int i = 0; i < numOfParams; i++) {
      Object value = params.getValue(i);
      if (value != null) {
        DataTypeCodec.encodeBinary(statement.bindingTypes()[i], value, encoder.encodingCharset, packet);
      } else {
        nullBitmap[i / 8] |= (1 << (i & 7));
      }
    }

    // padding null-bitmap content
    packet.setBytes(pos, nullBitmap);
  }

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}
 
Example 9
Source File: ExtendedBatchQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendBatchStatementExecuteCommand(MySQLPreparedStatement statement, Tuple params) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_EXECUTE);
  packet.writeIntLE((int) statement.statementId);
  packet.writeByte(CURSOR_TYPE_NO_CURSOR);
  // iteration count, always 1
  packet.writeIntLE(1);

  /*
   * Null-bit map and type should always be reconstructed for every batch of parameters here
   */
  int numOfParams = statement.bindingTypes().length;
  int bitmapLength = (numOfParams + 7) / 8;
  byte[] nullBitmap = new byte[bitmapLength];

  int pos = packet.writerIndex();

  if (numOfParams > 0) {
    // write a dummy bitmap first
    packet.writeBytes(nullBitmap);
    packet.writeByte(1);
    for (int i = 0; i < params.size(); i++) {
      Object param = params.getValue(i);
      DataType dataType = DataTypeCodec.inferDataTypeByEncodingValue(param);
      packet.writeByte(dataType.id);
      packet.writeByte(0); // parameter flag: signed
    }

    for (int i = 0; i < numOfParams; i++) {
      Object value = params.getValue(i);
      if (value != null) {
        DataTypeCodec.encodeBinary(DataTypeCodec.inferDataTypeByEncodingValue(value), value, encoder.encodingCharset, packet);
      } else {
        nullBitmap[i / 8] |= (1 << (i & 7));
      }
    }

    // padding null-bitmap content
    packet.setBytes(pos, nullBitmap);
  }

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}
 
Example 10
Source File: InitialHandshakeCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendHandshakeResponseMessage(String username, String password, String database, byte[] nonce, String authMethodName, Map<String, String> clientConnectionAttributes) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  int clientCapabilitiesFlags = encoder.clientCapabilitiesFlag;
  packet.writeIntLE(clientCapabilitiesFlags);
  packet.writeIntLE(PACKET_PAYLOAD_LENGTH_LIMIT);
  packet.writeByte(cmd.collation().collationId());
  packet.writeZero(23); // filler
  BufferUtils.writeNullTerminatedString(packet, username, StandardCharsets.UTF_8);
  if (password.isEmpty()) {
    packet.writeByte(0);
  } else {
    byte[] scrambledPassword;
    switch (authMethodName) {
      case "mysql_native_password":
        scrambledPassword = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
        break;
      case "caching_sha2_password":
        scrambledPassword = CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
        break;
      default:
        completionHandler.handle(CommandResponse.failure(new UnsupportedOperationException("Unsupported authentication method: " + authMethodName)));
        return;
    }
    if ((clientCapabilitiesFlags & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) != 0) {
      BufferUtils.writeLengthEncodedInteger(packet, scrambledPassword.length);
      packet.writeBytes(scrambledPassword);
    } else if ((clientCapabilitiesFlags & CLIENT_SECURE_CONNECTION) != 0) {
      packet.writeByte(scrambledPassword.length);
      packet.writeBytes(scrambledPassword);
    } else {
      packet.writeByte(0);
    }
  }
  if ((clientCapabilitiesFlags & CLIENT_CONNECT_WITH_DB) != 0) {
    BufferUtils.writeNullTerminatedString(packet, database, StandardCharsets.UTF_8);
  }
  if ((clientCapabilitiesFlags & CLIENT_PLUGIN_AUTH) != 0) {
    BufferUtils.writeNullTerminatedString(packet, authMethodName, StandardCharsets.UTF_8);
  }
  if ((clientCapabilitiesFlags & CLIENT_CONNECT_ATTRS) != 0) {
    encodeConnectionAttributes(clientConnectionAttributes, packet);
  }

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}