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

The following examples show how to use io.netty.buffer.ByteBuf#writeMediumLE() . 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: CodecUtils.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
public static void writeLengthEncodedInt(ByteBuf buf, Long n) {
	if (n == null) {
		buf.writeByte(NULL_VALUE);
	} else if (n < 0) {
		throw new IllegalArgumentException("Cannot encode a negative length: " + n);
	} else if (n < NULL_VALUE) {
		buf.writeByte(n.intValue());
	} else if (n < 0xffff) {
		buf.writeByte(SHORT_VALUE);
		buf.writeShortLE(n.intValue());
	} else if (n < 0xffffff) {
		buf.writeByte(MEDIUM_VALUE);
		buf.writeMediumLE(n.intValue());
	} else {
		buf.writeByte(LONG_VALUE);
		buf.writeLongLE(n);
	}
}
 
Example 2
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 3
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 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: 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 6
Source File: BufferUtils.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public static void writeLengthEncodedInteger(ByteBuf buffer, long value) {
  if (value < 251) {
    // 1-byte integer
    buffer.writeByte((byte) value);
  } else if (value <= 0xFFFF) {
    // 0xFC + 2-byte integer
    buffer.writeByte(0xFC);
    buffer.writeShortLE((int) value);
  } else if (value < 0xFFFFFF) {
    // 0xFD + 3-byte integer
    buffer.writeByte(0xFD);
    buffer.writeMediumLE((int) value);
  } else {
    // 0xFE + 8-byte integer
    buffer.writeByte(0xFE);
    buffer.writeLongLE(value);
  }
}
 
Example 7
Source File: CloseStatementCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendCloseStatementCommand(MySQLPreparedStatement statement) {
  ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
  // encode packet header
  packet.writeMediumLE(PAYLOAD_LENGTH);
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_CLOSE);
  packet.writeIntLE((int) statement.statementId);

  sendNonSplitPacket(packet);
}
 
Example 8
Source File: InitialHandshakeCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendSslRequest() {
  ByteBuf packet = allocateBuffer(36);
  // encode packet header
  packet.writeMediumLE(32);
  packet.writeByte(sequenceId);

  // encode SSLRequest payload
  packet.writeIntLE(encoder.clientCapabilitiesFlag);
  packet.writeIntLE(PACKET_PAYLOAD_LENGTH_LIMIT);
  packet.writeByte(cmd.collation().collationId());
  packet.writeZero(23); // filler

  sendNonSplitPacket(packet);
}
 
Example 9
Source File: SimpleQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private Future<Void> sendFileInPacket(String filename, int offset, int length) {
  ByteBuf packetHeader = allocateBuffer(4);
  packetHeader.writeMediumLE(length);
  packetHeader.writeByte(sequenceId++);
  encoder.chctx.write(packetHeader);
  return encoder.socketConnection.socket().sendFile(filename, offset, length);
}
 
Example 10
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 11
Source File: CommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
final void sendBytesAsPacket(byte[] payload) {
  int payloadLength = payload.length;
  ByteBuf packet = allocateBuffer(payloadLength + 4);
  // encode packet header
  packet.writeMediumLE(payloadLength);
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeBytes(payload);

  sendNonSplitPacket(packet);
}
 
Example 12
Source File: CloseConnectionCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendQuitCommand() {
  ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
  // encode packet header
  packet.writeMediumLE(PAYLOAD_LENGTH);
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_QUIT);

  sendNonSplitPacket(packet);
}
 
Example 13
Source File: ResetConnectionCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendResetConnectionCommand() {
  ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
  // encode packet header
  packet.writeMediumLE(PAYLOAD_LENGTH);
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_RESET_CONNECTION);

  sendNonSplitPacket(packet);
}
 
Example 14
Source File: ResetStatementCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendStatementResetCommand(long statementId) {
  ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
  // encode packet header
  packet.writeMediumLE(PAYLOAD_LENGTH);
  packet.writeByte(sequenceId);

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

  sendNonSplitPacket(packet);
}
 
Example 15
Source File: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void encodeDateNParameter(ByteBuf payload, LocalDate date) {
  payload.writeByte(0x00);
  payload.writeByte(0x00);
  payload.writeByte(MSSQLDataTypeId.DATENTYPE_ID);
  if (date == null) {
    // null
    payload.writeByte(0);
  } else {
    payload.writeByte(3);
    long days = ChronoUnit.DAYS.between(MSSQLDataTypeCodec.START_DATE, date);
    payload.writeMediumLE((int) days);
  }
}
 
Example 16
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 17
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private static void binaryEncodeInt3(Number value, ByteBuf buffer) {
  buffer.writeMediumLE(value.intValue());
}
 
Example 18
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 19
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);
}
 
Example 20
Source File: SnappyFrameEncoder.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Writes the 2-byte chunk length to the output buffer.将2字节块长度写入输出缓冲区。
 *
 * @param out The buffer to write to
 * @param chunkLength The length to write
 */
private static void writeChunkLength(ByteBuf out, int chunkLength) {
    out.writeMediumLE(chunkLength);
}