Class NativePacketPayload

java.lang.Object
com.mysql.cj.protocol.a.NativePacketPayload
All Implemented Interfaces:
Message

public class NativePacketPayload
extends java.lang.Object
implements Message
PacketPayload is the content of a full single packet (independent from on-wire splitting) communicated with the server. We can manipulate the packet's underlying buffer when sending commands with writeInteger(), writeBytes(), etc. We can check the packet type with isEOFPacket(), etc predicates. A position is maintained for reading/writing data. A payload length is maintained allowing the PacketPayload to be decoupled from the size of the underlying buffer.
  • Field Details

  • Constructor Details

  • Method Details

    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • toSuperString

      public java.lang.String toSuperString()
    • getCapacity

      public int getCapacity()
    • ensureCapacity

      public final void ensureCapacity​(int additionalData)
      Checks that underlying buffer has enough space to store additionalData bytes starting from current position. If buffer size is smaller than required then it is re-allocated with bigger size.
      Parameters:
      additionalData - additional data size in bytes
    • getByteBuffer

      public byte[] getByteBuffer()
      Description copied from interface: Message
      Returns the array of bytes this Buffer is using to read from.
      Specified by:
      getByteBuffer in interface Message
      Returns:
      byte array being read from
    • setByteBuffer

      public void setByteBuffer​(byte[] byteBufferToSet)
      Sets the array of bytes to use as a buffer to read from.
      Parameters:
      byteBufferToSet - the array of bytes to use as a buffer
    • getPayloadLength

      public int getPayloadLength()
      Get the actual length of payload the buffer contains. It can be smaller than underlying buffer size because it can be reused after a big packet.
      Returns:
      payload length
    • setPayloadLength

      public void setPayloadLength​(int bufLengthToSet)
      Set the actual length of payload written to buffer. It can be smaller or equal to underlying buffer size.
      Parameters:
      bufLengthToSet - length
    • getPosition

      public int getPosition()
      Description copied from interface: Message
      Returns the current position to write to/ read from
      Specified by:
      getPosition in interface Message
      Returns:
      the current position to write to/ read from
    • setPosition

      public void setPosition​(int positionToSet)
      Set the current position to write to/ read from
      Parameters:
      positionToSet - the position (0-based index)
    • isErrorPacket

      public boolean isErrorPacket()
      Is it a ERROR packet.
      Returns:
      true if it is a ERROR packet
    • isEOFPacket

      public final boolean isEOFPacket()
      Is it a EOF packet. See http://dev.mysql.com/doc/internals/en/packet-EOF_Packet.html
      Returns:
      true if it is a EOF packet
    • isAuthMethodSwitchRequestPacket

      public final boolean isAuthMethodSwitchRequestPacket()
      Is it a Protocol::AuthSwitchRequest packet. See http://dev.mysql.com/doc/internals/en/connection-phase-packets.html
      Returns:
      true if it is a Protocol::AuthSwitchRequest packet
    • isOKPacket

      public final boolean isOKPacket()
      Is it an OK packet. See http://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
      Returns:
      true if it is an OK packet
    • isResultSetOKPacket

      public final boolean isResultSetOKPacket()
      Is it an OK packet for ResultSet. Unlike usual 0x00 signature it has 0xfe signature. See http://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
      Returns:
      true if it is an OK packet for ResultSet
    • isAuthMoreData

      public final boolean isAuthMoreData()
      Is it a Protocol::AuthMoreData packet. See http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthMoreData
      Returns:
      true if it is a Protocol::AuthMoreData packet
    • writeInteger

      public void writeInteger​(NativeConstants.IntegerDataType type, long l)
      Write data according to provided Integer type.
      Parameters:
      type - NativeConstants.IntegerDataType
      l - value
    • readInteger

      public final long readInteger​(NativeConstants.IntegerDataType type)
      Read data according to provided Integer type.
      Parameters:
      type - NativeConstants.IntegerDataType
      Returns:
      long
    • writeBytes

      public final void writeBytes​(NativeConstants.StringSelfDataType type, byte[] b)
      Write all bytes from given byte array into internal buffer starting with current buffer position.
      Parameters:
      type - on-wire data type
      b - from byte array
    • writeBytes

      public final void writeBytes​(NativeConstants.StringLengthDataType type, byte[] b)
      Write all bytes from given byte array into internal buffer starting with current buffer position.
      Parameters:
      type - on-wire data type
      b - from byte array
    • writeBytes

      public void writeBytes​(NativeConstants.StringSelfDataType type, byte[] b, int offset, int len)
      Write len bytes from given byte array into internal buffer. Read starts from given offset, write starts with current buffer position.
      Parameters:
      type - on-wire data type
      b - from byte array
      offset - starting index of b
      len - number of bytes to be written
    • writeBytes

      public void writeBytes​(NativeConstants.StringLengthDataType type, byte[] b, int offset, int len)
      Write len bytes from given byte array into internal buffer. Read starts from given offset, write starts with current buffer position.
      Parameters:
      type - on-wire data type
      b - from byte array
      offset - starting index of b
      len - number of bytes to be written
    • readBytes

      public byte[] readBytes​(NativeConstants.StringSelfDataType type)
      Read bytes from internal buffer starting from current position into the new byte array. The length of data to read depends on NativeConstants.StringSelfDataType.
      Parameters:
      type - NativeConstants.StringSelfDataType
      Returns:
      bytes
    • skipBytes

      public void skipBytes​(NativeConstants.StringSelfDataType type)
      Set position to next value in internal buffer skipping the current value according to NativeConstants.StringSelfDataType.
      Parameters:
      type - NativeConstants.StringSelfDataType
    • readBytes

      public byte[] readBytes​(NativeConstants.StringLengthDataType type, int len)
      Read len bytes from internal buffer starting from current position into the new byte array.
      Parameters:
      type - NativeConstants.StringLengthDataType
      len - length
      Returns:
      bytes
    • readString

      public java.lang.String readString​(NativeConstants.StringSelfDataType type, java.lang.String encoding)
      Read bytes from internal buffer starting from current position decoding them into String using the specified character encoding. The length of data to read depends on NativeConstants.StringSelfDataType.
      Parameters:
      type - NativeConstants.StringSelfDataType
      encoding - if null then platform default encoding is used
      Returns:
      string
    • readString

      public java.lang.String readString​(NativeConstants.StringLengthDataType type, java.lang.String encoding, int len)
      Read len bytes from internal buffer starting from current position decoding them into String using the specified character encoding.
      Parameters:
      type - NativeConstants.StringLengthDataType
      encoding - if null then platform default encoding is used
      len - length
      Returns:
      string
    • extractSqlFromPacket

      public static java.lang.String extractSqlFromPacket​(java.lang.String possibleSqlQuery, NativePacketPayload packet, int endOfQueryPacketPosition, int maxQuerySizeToLog)