Package com.mysql.cj.protocol.a
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 Summary
Fields Modifier and Type Field Description static longNULL_LENGTHstatic shortTYPE_ID_AUTH_SWITCHIt has the same signature as EOF, but may be issued by server only during handshake phasestatic shortTYPE_ID_EOFstatic shortTYPE_ID_ERRORstatic shortTYPE_ID_LOCAL_INFILEstatic shortTYPE_ID_OK -
Constructor Summary
Constructors Constructor Description NativePacketPayload(byte[] buf)NativePacketPayload(int size) -
Method Summary
Modifier and Type Method Description voidensureCapacity(int additionalData)Checks that underlying buffer has enough space to store additionalData bytes starting from current position.static java.lang.StringextractSqlFromPacket(java.lang.String possibleSqlQuery, NativePacketPayload packet, int endOfQueryPacketPosition, int maxQuerySizeToLog)byte[]getByteBuffer()Returns the array of bytes this Buffer is using to read from.intgetCapacity()intgetPayloadLength()Get the actual length of payload the buffer contains.intgetPosition()Returns the current position to write to/ read frombooleanisAuthMethodSwitchRequestPacket()Is it a Protocol::AuthSwitchRequest packet.booleanisAuthMoreData()Is it a Protocol::AuthMoreData packet.booleanisEOFPacket()Is it a EOF packet.booleanisErrorPacket()Is it a ERROR packet.booleanisOKPacket()Is it an OK packet.booleanisResultSetOKPacket()Is it an OK packet for ResultSet.byte[]readBytes(NativeConstants.StringLengthDataType type, int len)Read len bytes from internal buffer starting from current position into the new byte array.byte[]readBytes(NativeConstants.StringSelfDataType type)Read bytes from internal buffer starting from current position into the new byte array.longreadInteger(NativeConstants.IntegerDataType type)Read data according to provided Integer type.java.lang.StringreadString(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.java.lang.StringreadString(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.voidsetByteBuffer(byte[] byteBufferToSet)Sets the array of bytes to use as a buffer to read from.voidsetPayloadLength(int bufLengthToSet)Set the actual length of payload written to buffer.voidsetPosition(int positionToSet)Set the current position to write to/ read fromvoidskipBytes(NativeConstants.StringSelfDataType type)Set position to next value in internal buffer skipping the current value according toNativeConstants.StringSelfDataType.java.lang.StringtoString()java.lang.StringtoSuperString()voidwriteBytes(NativeConstants.StringLengthDataType type, byte[] b)Write all bytes from given byte array into internal buffer starting with current buffer position.voidwriteBytes(NativeConstants.StringLengthDataType type, byte[] b, int offset, int len)Write len bytes from given byte array into internal buffer.voidwriteBytes(NativeConstants.StringSelfDataType type, byte[] b)Write all bytes from given byte array into internal buffer starting with current buffer position.voidwriteBytes(NativeConstants.StringSelfDataType type, byte[] b, int offset, int len)Write len bytes from given byte array into internal buffer.voidwriteInteger(NativeConstants.IntegerDataType type, long l)Write data according to provided Integer type.
-
Field Details
-
NULL_LENGTH
public static final long NULL_LENGTH- See Also:
- Constant Field Values
-
TYPE_ID_ERROR
public static final short TYPE_ID_ERROR- See Also:
- Constant Field Values
-
TYPE_ID_EOF
public static final short TYPE_ID_EOF- See Also:
- Constant Field Values
-
TYPE_ID_AUTH_SWITCH
public static final short TYPE_ID_AUTH_SWITCHIt has the same signature as EOF, but may be issued by server only during handshake phase- See Also:
- Constant Field Values
-
TYPE_ID_LOCAL_INFILE
public static final short TYPE_ID_LOCAL_INFILE- See Also:
- Constant Field Values
-
TYPE_ID_OK
public static final short TYPE_ID_OK- See Also:
- Constant Field Values
-
-
Constructor Details
-
NativePacketPayload
public NativePacketPayload(byte[] buf) -
NativePacketPayload
public NativePacketPayload(int size)
-
-
Method Details
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.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:MessageReturns the array of bytes this Buffer is using to read from.- Specified by:
getByteBufferin interfaceMessage- 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:MessageReturns the current position to write to/ read from- Specified by:
getPositionin interfaceMessage- 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
Write data according to provided Integer type.- Parameters:
type-NativeConstants.IntegerDataTypel- value
-
readInteger
Read data according to provided Integer type.- Parameters:
type-NativeConstants.IntegerDataType- Returns:
- long
-
writeBytes
Write all bytes from given byte array into internal buffer starting with current buffer position.- Parameters:
type- on-wire data typeb- from byte array
-
writeBytes
Write all bytes from given byte array into internal buffer starting with current buffer position.- Parameters:
type- on-wire data typeb- from byte array
-
writeBytes
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 typeb- from byte arrayoffset- starting index of blen- number of bytes to be written
-
writeBytes
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 typeb- from byte arrayoffset- starting index of blen- number of bytes to be written
-
readBytes
Read bytes from internal buffer starting from current position into the new byte array. The length of data to read depends onNativeConstants.StringSelfDataType.- Parameters:
type-NativeConstants.StringSelfDataType- Returns:
- bytes
-
skipBytes
Set position to next value in internal buffer skipping the current value according toNativeConstants.StringSelfDataType.- Parameters:
type-NativeConstants.StringSelfDataType
-
readBytes
Read len bytes from internal buffer starting from current position into the new byte array.- Parameters:
type-NativeConstants.StringLengthDataTypelen- 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 onNativeConstants.StringSelfDataType.- Parameters:
type-NativeConstants.StringSelfDataTypeencoding- 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.StringLengthDataTypeencoding- if null then platform default encoding is usedlen- length- Returns:
- string
-
extractSqlFromPacket
public static java.lang.String extractSqlFromPacket(java.lang.String possibleSqlQuery, NativePacketPayload packet, int endOfQueryPacketPosition, int maxQuerySizeToLog)
-