Class PacketWriter

  • All Implemented Interfaces:
    Writer

    public class PacketWriter
    extends Object
    implements Writer
    • Field Detail

      • compressSequence

        protected final MutableInt compressSequence
      • buf

        protected byte[] buf
      • pos

        protected int pos
    • Constructor Detail

      • PacketWriter

        public PacketWriter​(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()
        Specified by:
        pos in interface Writer
      • getCmdLength

        public long getCmdLength()
        Specified by:
        getCmdLength in interface Writer
      • 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
      • writeBytesAtPos

        public void writeBytesAtPos​(byte[] arr,
                                    int pos)
        Specified by:
        writeBytesAtPos in interface Writer
      • 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.
      • 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
      • throwMaxAllowedLength

        public boolean throwMaxAllowedLength​(int length)
        Specified by:
        throwMaxAllowedLength in interface Writer
      • setMaxAllowedPacket

        public void setMaxAllowedPacket​(int maxAllowedPacket)
        Specified by:
        setMaxAllowedPacket in interface Writer
      • permitTrace

        public void permitTrace​(boolean permitTrace)
        Specified by:
        permitTrace in interface Writer
      • 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()
        Specified by:
        mark in interface Writer
      • isMarked

        public boolean isMarked()
        Specified by:
        isMarked in interface Writer
      • hasFlushed

        public boolean hasFlushed()
        Specified by:
        hasFlushed in interface Writer
      • 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()
        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.