Class AbstractPacketOutputStream

    • Field Detail

      • maxQuerySizeToLog

        protected final int maxQuerySizeToLog
      • buf

        protected byte[] buf
      • pos

        protected int pos
      • maxAllowedPacket

        protected int maxAllowedPacket
      • cmdLength

        protected long cmdLength
      • permitTrace

        protected boolean permitTrace
      • seqNo

        protected int seqNo
      • serverThreadLog

        protected java.lang.String serverThreadLog
      • mark

        private int mark
      • bufferContainDataAfterMark

        private boolean bufferContainDataAfterMark
      • threadId

        protected long threadId
    • Constructor Detail

      • AbstractPacketOutputStream

        public AbstractPacketOutputStream​(java.io.OutputStream out,
                                          int maxQuerySizeToLog,
                                          long threadId)
        Common feature to write data into socket, creating MariaDB Packet.
        Parameters:
        out - socket outputStream
        maxQuerySizeToLog - maximum query size to log
        threadId - thread id
    • Method Detail

      • getMaxPacketLength

        public abstract int getMaxPacketLength()
      • flushBuffer

        protected abstract void flushBuffer​(boolean commandEnd)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • growBuffer

        private void growBuffer​(int len)
                         throws java.io.IOException
        Buffer growing use 4 size only to avoid creating/copying that are expensive operations. possible size
        1. SMALL_BUFFER_SIZE = 8k (default)
        2. MEDIUM_BUFFER_SIZE = 128k
        3. LARGE_BUFFER_SIZE = 1M
        4. getMaxPacketLength = 16M (+ 4 is using no compression)
        Parameters:
        len - length to add
        Throws:
        java.io.IOException
      • writeEmptyPacket

        public void writeEmptyPacket​(int seqNo)
                              throws java.io.IOException
        Send empty packet.
        Specified by:
        writeEmptyPacket in interface PacketOutputStream
        Parameters:
        seqNo - packet sequence
        Throws:
        java.io.IOException - if socket error occur.
      • writeEmptyPacket

        public abstract void writeEmptyPacket()
                                       throws java.io.IOException
        Specified by:
        writeEmptyPacket in interface PacketOutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Send packet to socket.
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in interface PacketOutputStream
        Overrides:
        flush in class java.io.FilterOutputStream
        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.
        Specified by:
        checkMaxAllowedLength in interface PacketOutputStream
        Parameters:
        length - additional length to query size
        Throws:
        MaxAllowedPacketException - if query has not to be send.
      • writeShort

        public void writeShort​(short value)
                        throws java.io.IOException
        Write short value into buffer. flush buffer if too small.
        Specified by:
        writeShort in interface PacketOutputStream
        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 buffer. flush buffer if too small.
        Specified by:
        writeInt in interface PacketOutputStream
        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 buffer. flush buffer if too small.
        Specified by:
        writeLong in interface PacketOutputStream
        Parameters:
        value - long value
        Throws:
        java.io.IOException - if socket error occur
      • writeBytes

        public void writeBytes​(byte value,
                               int len)
                        throws java.io.IOException
        Write byte value, len times into buffer. flush buffer if too small.
        Specified by:
        writeBytes in interface PacketOutputStream
        Parameters:
        value - byte value
        len - number of time to write value.
        Throws:
        java.io.IOException - if socket error occur.
      • writeFieldLength

        public void writeFieldLength​(long length)
                              throws java.io.IOException
        Write field length into buffer, flush socket if needed.
        Specified by:
        writeFieldLength in interface PacketOutputStream
        Parameters:
        length - field length
        Throws:
        java.io.IOException - if socket error occur.
      • write

        public void write​(int value)
                   throws java.io.IOException
        Write byte into buffer, flush buffer to socket if needed.
        Specified by:
        write in interface PacketOutputStream
        Overrides:
        write in class java.io.FilterOutputStream
        Parameters:
        value - byte to send
        Throws:
        java.io.IOException - if socket error occur.
      • write

        public void write​(byte[] arr)
                   throws java.io.IOException
        Specified by:
        write in interface PacketOutputStream
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] arr,
                          int off,
                          int len)
                   throws java.io.IOException
        Write byte array to buffer. If buffer is full, flush socket.
        Specified by:
        write in interface PacketOutputStream
        Overrides:
        write in class java.io.FilterOutputStream
        Parameters:
        arr - byte array
        off - offset
        len - byte length to write
        Throws:
        java.io.IOException - if socket error occur
      • write

        public void write​(java.lang.String str)
                   throws java.io.IOException
        Specified by:
        write in interface PacketOutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String str,
                          boolean escape,
                          boolean noBackslashEscapes)
                   throws java.io.IOException
        Write string to socket.
        Specified by:
        write in interface PacketOutputStream
        Parameters:
        str - string
        escape - must be escape
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • write

        public void write​(java.io.InputStream is,
                          boolean escape,
                          boolean noBackslashEscapes)
                   throws java.io.IOException
        Write stream into socket.
        Specified by:
        write in interface PacketOutputStream
        Parameters:
        is - inputStream
        escape - must be escape
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • write

        public void write​(java.io.InputStream is,
                          long length,
                          boolean escape,
                          boolean noBackslashEscapes)
                   throws java.io.IOException
        Write stream into socket.
        Specified by:
        write in interface PacketOutputStream
        Parameters:
        is - inputStream
        length - write length
        escape - must be escape
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • write

        public void write​(java.io.Reader reader,
                          boolean escape,
                          boolean noBackslashEscapes)
                   throws java.io.IOException
        Write reader into socket.
        Specified by:
        write in interface PacketOutputStream
        Parameters:
        reader - reader
        escape - must be escape
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • write

        public void write​(java.io.Reader reader,
                          long length,
                          boolean escape,
                          boolean noBackslashEscapes)
                   throws java.io.IOException
        Write reader into socket.
        Specified by:
        write in interface PacketOutputStream
        Parameters:
        reader - reader
        length - write length
        escape - must be escape
        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.
        Specified by:
        writeBytesEscaped in interface PacketOutputStream
        Parameters:
        bytes - bytes
        len - len to write
        noBackslashEscapes - escape method
        Throws:
        java.io.IOException - if socket error occur
      • setServerThreadId

        public void setServerThreadId​(long serverThreadId,
                                      java.lang.Boolean isMaster)
        Set server thread id.
        Specified by:
        setServerThreadId in interface PacketOutputStream
        Parameters:
        serverThreadId - current server thread id.
        isMaster - is server master
      • flushBufferStopAtMark

        public void flushBufferStopAtMark()
                                   throws java.io.IOException
        Flush to last mark.
        Specified by:
        flushBufferStopAtMark in interface PacketOutputStream
        Throws:
        java.io.IOException - if flush fail.
      • resetMark

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