Class PacketWriter

java.lang.Object
org.mariadb.jdbc.client.socket.impl.PacketWriter
All Implemented Interfaces:
Writer

public class PacketWriter extends Object implements Writer
Packet writer
  • Field Details

    • SMALL_BUFFER_SIZE

      public static final int SMALL_BUFFER_SIZE
      initial buffer size
      See Also:
    • logger

      private static final Logger logger
    • QUOTE

      private static final byte QUOTE
      See Also:
    • DBL_QUOTE

      private static final byte DBL_QUOTE
      See Also:
    • ZERO_BYTE

      private static final byte ZERO_BYTE
      See Also:
    • BACKSLASH

      private static final byte BACKSLASH
      See Also:
    • MEDIUM_BUFFER_SIZE

      private static final int MEDIUM_BUFFER_SIZE
      See Also:
    • LARGE_BUFFER_SIZE

      private static final int LARGE_BUFFER_SIZE
      See Also:
    • MAX_PACKET_LENGTH

      private static final int MAX_PACKET_LENGTH
      See Also:
    • sequence

      protected final MutableByte sequence
      packet sequence
    • compressSequence

      protected final MutableByte compressSequence
      compressed packet sequence
    • maxQuerySizeToLog

      private final int maxQuerySizeToLog
    • out

      private final OutputStream out
    • maxPacketLength

      private final int maxPacketLength
      See Also:
    • maxAllowedPacket

      private final Integer maxAllowedPacket
    • buf

      protected byte[] buf
      internal buffer
    • pos

      protected int pos
      buffer position
    • cmdLength

      private long cmdLength
    • permitTrace

      private boolean permitTrace
    • serverThreadLog

      private String serverThreadLog
    • mark

      private int mark
    • bufContainDataAfterMark

      private boolean bufContainDataAfterMark
  • Constructor Details

    • PacketWriter

      public PacketWriter(OutputStream out, int maxQuerySizeToLog, Integer maxAllowedPacket, MutableByte sequence, MutableByte compressSequence)
      Common feature to write data into socket, creating MariaDB Packet.
      Parameters:
      out - output stream
      maxQuerySizeToLog - maximum query size to log
      maxAllowedPacket - max allowed packet value if known
      sequence - packet sequence
      compressSequence - compressed packet sequence
  • Method Details

    • pos

      public int pos()
      get current position
      Specified by:
      pos in interface Writer
      Returns:
      current position
    • pos

      public void pos(int pos) throws IOException
      position setter
      Specified by:
      pos in interface Writer
      Parameters:
      pos - new position
      Throws:
      IOException - if buffer is not big enough to contains new position
    • getCmdLength

      public long getCmdLength()
      get current command length
      Specified by:
      getCmdLength in interface Writer
      Returns:
      current command length
    • writeByte

      public void writeByte(int value) throws IOException
      Write byte into buf, flush buf to socket if needed.
      Specified by:
      writeByte in interface Writer
      Parameters:
      value - byte to send
      Throws:
      IOException - if socket error occur.
    • writeShort

      public void writeShort(short value) throws IOException
      Write short value into buf. flush buf if too small.
      Specified by:
      writeShort in interface Writer
      Parameters:
      value - short value
      Throws:
      IOException - if socket error occur
    • writeInt

      public void writeInt(int value) throws IOException
      Write int value into buf. flush buf if too small.
      Specified by:
      writeInt in interface Writer
      Parameters:
      value - int value
      Throws:
      IOException - if socket error occur
    • writeLong

      public void writeLong(long value) throws IOException
      Write long value into buf. flush buf if too small.
      Specified by:
      writeLong in interface Writer
      Parameters:
      value - long value
      Throws:
      IOException - if socket error occur
    • writeDouble

      public void writeDouble(double value) throws IOException
      Description copied from interface: Writer
      Write Double binary value to buffer
      Specified by:
      writeDouble in interface Writer
      Parameters:
      value - double value
      Throws:
      IOException - if socket error occur
    • writeFloat

      public void writeFloat(float value) throws IOException
      Description copied from interface: Writer
      Write float binary value to buffer
      Specified by:
      writeFloat in interface Writer
      Parameters:
      value - float value
      Throws:
      IOException - if socket error occur
    • writeBytes

      public void writeBytes(byte[] arr) throws IOException
      Description copied from interface: Writer
      Write byte array to buffer
      Specified by:
      writeBytes in interface Writer
      Parameters:
      arr - bytes
      Throws:
      IOException - if socket error occur
    • writeBytesAtPos

      public void writeBytesAtPos(byte[] arr, int pos)
      Description copied from interface: Writer
      Write byte array to buffer at a specific position
      Specified by:
      writeBytesAtPos in interface Writer
      Parameters:
      arr - bytes
      pos - position
    • writeBytes

      public void writeBytes(byte[] arr, int off, int len) throws IOException
      Write byte array to buf. If buf is full, flush socket.
      Specified by:
      writeBytes in interface Writer
      Parameters:
      arr - byte array
      off - offset
      len - byte length to write
      Throws:
      IOException - if socket error occur
    • writeLength

      public void writeLength(long length) throws IOException
      Write field length into buf, flush socket if needed.
      Specified by:
      writeLength in interface Writer
      Parameters:
      length - field length
      Throws:
      IOException - if socket error occur.
    • writeAscii

      public void writeAscii(String str) throws IOException
      Description copied from interface: Writer
      Write ascii string to buffer
      Specified by:
      writeAscii in interface Writer
      Parameters:
      str - string
      Throws:
      IOException - if socket error occurs
    • writeString

      public void writeString(String str) throws IOException
      Description copied from interface: Writer
      Write utf8 string to buffer
      Specified by:
      writeString in interface Writer
      Parameters:
      str - string
      Throws:
      IOException - if socket error occurs
    • buf

      public byte[] buf()
      Current buffer
      Specified by:
      buf in interface Writer
      Returns:
      current buffer
    • writeStringEscaped

      public void writeStringEscaped(String str, boolean noBackslashEscapes) throws IOException
      Write string to socket.
      Specified by:
      writeStringEscaped in interface Writer
      Parameters:
      str - string
      noBackslashEscapes - escape method
      Throws:
      IOException - if socket error occur
    • writeBytesEscaped

      public void writeBytesEscaped(byte[] bytes, int len, boolean noBackslashEscapes) throws IOException
      Write escape bytes to socket.
      Specified by:
      writeBytesEscaped in interface Writer
      Parameters:
      bytes - bytes
      len - len to write
      noBackslashEscapes - escape method
      Throws:
      IOException - if socket error occur
    • growBuffer

      private void growBuffer(int len) throws IOException
      buf growing use 4 size only to avoid creating/copying that are expensive operations. possible size
      1. SMALL_buf_SIZE = 8k (default)
      2. MEDIUM_buf_SIZE = 128k
      3. LARGE_buf_SIZE = 1M
      4. getMaxPacketLength = 16M (+ 4 if using no compression)
      Parameters:
      len - length to add
      Throws:
      IOException
    • writeEmptyPacket

      public void writeEmptyPacket() throws IOException
      Send empty packet.
      Specified by:
      writeEmptyPacket in interface Writer
      Throws:
      IOException - if socket error occur.
    • flush

      public void flush() throws IOException
      Send packet to socket.
      Specified by:
      flush in interface Writer
      Throws:
      IOException - if socket error occur.
    • flushPipeline

      public void flushPipeline() throws IOException
      Description copied from interface: Writer
      Send packet to buffered outputstream without flushing
      Specified by:
      flushPipeline in interface Writer
      Throws:
      IOException - if socket error occur.
    • checkMaxAllowedLength

      private void checkMaxAllowedLength(int length) throws MaxAllowedPacketException
      Count query size. If query size is greater than max_allowed_packet and nothing has been already send, throw an exception to avoid having the connection closed.
      Parameters:
      length - additional length to query size
      Throws:
      MaxAllowedPacketException - if query has not to be sent.
    • throwMaxAllowedLength

      public boolean throwMaxAllowedLength(int length)
      Description copied from interface: Writer
      must a max allowed length exception be thrown
      Specified by:
      throwMaxAllowedLength in interface Writer
      Parameters:
      length - command length
      Returns:
      true if too big
    • permitTrace

      public void permitTrace(boolean permitTrace)
      Description copied from interface: Writer
      Indicate if logging trace are permitted
      Specified by:
      permitTrace in interface Writer
      Parameters:
      permitTrace - permits trace to be logged
    • setServerThreadId

      public void setServerThreadId(Long serverThreadId, HostAddress hostAddress)
      Set server thread id.
      Specified by:
      setServerThreadId in interface Writer
      Parameters:
      serverThreadId - current server thread id.
      hostAddress - host information
    • mark

      public void mark()
      Description copied from interface: Writer
      mark position
      Specified by:
      mark in interface Writer
    • isMarked

      public boolean isMarked()
      Description copied from interface: Writer
      has some position been marked
      Specified by:
      isMarked in interface Writer
      Returns:
      is marked
    • hasFlushed

      public boolean hasFlushed()
      Description copied from interface: Writer
      Current command has flushed packet to socket
      Specified by:
      hasFlushed in interface Writer
      Returns:
      indicate if some packet have been flushed
    • flushBufferStopAtMark

      public void flushBufferStopAtMark() throws IOException
      Flush to last mark.
      Specified by:
      flushBufferStopAtMark in interface Writer
      Throws:
      IOException - if flush fail.
    • bufIsDataAfterMark

      public boolean bufIsDataAfterMark()
      Description copied from interface: Writer
      Buffer has data after marked position
      Specified by:
      bufIsDataAfterMark in interface Writer
      Returns:
      indicate if there is data after marked position
    • resetMark

      public byte[] resetMark()
      Reset mark flag and send bytes after mark flag.
      Specified by:
      resetMark in interface Writer
      Returns:
      bytes after mark flag
    • initPacket

      public void initPacket()
      Description copied from interface: Writer
      reset sequences and position for sending a new packet
      Specified by:
      initPacket in interface Writer
    • writeSocket

      protected void writeSocket(boolean commandEnd) throws IOException
      Flush the internal buf.
      Parameters:
      commandEnd - command end
      Throws:
      IOException - id connection error occur.
    • close

      public void close() throws IOException
      Description copied from interface: Writer
      Close socket stream
      Specified by:
      close in interface Writer
      Throws:
      IOException - if any error occurs
    • getSequence

      public byte getSequence()
      Description copied from interface: Writer
      Get current sequence object
      Specified by:
      getSequence in interface Writer
      Returns:
      current sequence