Class PacketWriter


  • public class PacketWriter
    extends java.lang.Object
    • Field Detail

      • logger

        private static final Logger logger
      • compressSequence

        protected final MutableInt compressSequence
      • maxQuerySizeToLog

        private final int maxQuerySizeToLog
      • out

        private final java.io.OutputStream out
      • buf

        protected byte[] buf
      • pos

        protected int pos
      • maxPacketLength

        private int maxPacketLength
      • maxAllowedPacket

        private int maxAllowedPacket
      • cmdLength

        private long cmdLength
      • permitTrace

        private boolean permitTrace
      • serverThreadLog

        private java.lang.String serverThreadLog
      • mark

        private int mark
      • bufContainDataAfterMark

        private boolean bufContainDataAfterMark
    • Constructor Detail

      • PacketWriter

        public PacketWriter​(java.io.OutputStream out,
                            int maxQuerySizeToLog,
                            MutableInt sequence,
                            MutableInt compressSequence)
        Common feature to write data into socket, creating MariaDB Packet.
        Parameters:
        out - output stream
        maxQuerySizeToLog - maximum query size to log
        sequence - packet sequence
        compressSequence - compressed packet sequence
    • Method Detail

      • pos

        public int pos()
      • pos

        public void pos​(int pos)
                 throws java.io.IOException
        Throws:
        java.io.IOException
      • writeByte

        public void writeByte​(int value)
                       throws java.io.IOException
        Write byte into buf, flush buf to socket if needed.
        Parameters:
        value - byte to send
        Throws:
        java.io.IOException - if socket error occur.
      • writeShort

        public void writeShort​(short value)
                        throws java.io.IOException
        Write short value into buf. flush buf if too small.
        Parameters:
        value - short value
        Throws:
        java.io.IOException - if socket error occur
      • writeInt

        public void writeInt​(int value)
                      throws java.io.IOException
        Write int value into buf. flush buf if too small.
        Parameters:
        value - int value
        Throws:
        java.io.IOException - if socket error occur
      • writeLong

        public void writeLong​(long value)
                       throws java.io.IOException
        Write long value into buf. flush buf if too small.
        Parameters:
        value - long value
        Throws:
        java.io.IOException - if socket error occur
      • writeDouble

        public void writeDouble​(double value)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFloat

        public void writeFloat​(float value)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBytes

        public void writeBytes​(byte[] arr)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBytesAtPos

        public void writeBytesAtPos​(byte[] arr,
                                    int pos)
      • writeBytes

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

        public void writeLength​(long length)
                         throws java.io.IOException
        Write field length into buf, flush socket if needed.
        Parameters:
        length - field length
        Throws:
        java.io.IOException - if socket error occur.
      • writeAscii

        public void writeAscii​(java.lang.String str)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeString

        public void writeString​(java.lang.String str)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeStringEscaped

        public void writeStringEscaped​(java.lang.String str,
                                       boolean noBackslashEscapes)
                                throws java.io.IOException
        Write string to socket.
        Parameters:
        str - string
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • writeBytesEscaped

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

        private void growBuffer​(int len)
                         throws java.io.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:
        java.io.IOException
      • writeEmptyPacket

        public void writeEmptyPacket()
                              throws java.io.IOException
        Send empty packet.
        Throws:
        java.io.IOException - if socket error occur.
      • flush

        public void flush()
                   throws java.io.IOException
        Send packet to socket.
        Throws:
        java.io.IOException - if socket error occur.
      • checkMaxAllowedLength

        public 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.
      • setMaxAllowedPacket

        public void setMaxAllowedPacket​(int maxAllowedPacket)
      • permitTrace

        public void permitTrace​(boolean permitTrace)
      • setServerThreadId

        public void setServerThreadId​(java.lang.Long serverThreadId,
                                      HostAddress hostAddress)
        Set server thread id.
        Parameters:
        serverThreadId - current server thread id.
        hostAddress - host information
      • mark

        public void mark()
      • isMarked

        public boolean isMarked()
      • hasFlushed

        public boolean hasFlushed()
      • flushBufferStopAtMark

        public void flushBufferStopAtMark()
                                   throws java.io.IOException
        Flush to last mark.
        Throws:
        java.io.IOException - if flush fail.
      • bufIsDataAfterMark

        public boolean bufIsDataAfterMark()
      • resetMark

        public byte[] resetMark()
        Reset mark flag and send bytes after mark flag.
        Returns:
        bytes after mark flag
      • initPacket

        public void initPacket()
      • writeSocket

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

        public void close()
                   throws java.io.IOException
        Throws:
        java.io.IOException