Package org.mariadb.jdbc.client.socket

## Class PacketWriter

- java.lang.Object
- - org.mariadb.jdbc.client.socket.PacketWriter

    public class PacketWriter
    extends Object

### Field Summary

Fields
`protected byte[]` `buf`
`protected org.mariadb.jdbc.util.MutableInt` `compressSequence`
`protected int` `pos`
`protected org.mariadb.jdbc.util.MutableInt` `sequence`
`static int` `SMALL_BUFFER_SIZE`

### Constructor Summary

Constructors
`PacketWriter​(OutputStream out, int maxQuerySizeToLog, org.mariadb.jdbc.util.MutableInt sequence, org.mariadb.jdbc.util.MutableInt compressSequence)` Common feature to write data into socket, creating MariaDB Packet.

### Method Summary

All Methods Instance Methods Concrete Methods
`boolean` `bufIsDataAfterMark()`
`void` `checkMaxAllowedLength​(int length)` Count query size.
`void` `close()`
`void` `flush()` Send packet to socket.
`void` `flushBufferStopAtMark()` Flush to last mark.
`boolean` `hasFlushed()`
`void` `initPacket()`
`boolean` `isMarked()`
`void` `mark()`
`void` `permitTrace​(boolean permitTrace)`
`int` `pos()`
`void` `pos​(int pos)`
`byte[]` `resetMark()` Reset mark flag and send bytes after mark flag.
`void` `setMaxAllowedPacket​(int maxAllowedPacket)`
`void` `setServerThreadId​(Long serverThreadId, HostAddress hostAddress)` Set server thread id.
`void` `writeAscii​(String str)`
`void` `writeByte​(int value)` Write byte into buf, flush buf to socket if needed.
`void` `writeBytes​(byte[] arr)`
`void` `writeBytes​(byte[] arr, int off, int len)` Write byte array to buf.
`void` `writeBytesAtPos​(byte[] arr, int pos)`
`void` `writeBytesEscaped​(byte[] bytes, int len, boolean noBackslashEscapes)` Write escape bytes to socket.
`void` `writeDouble​(double value)`
`void` `writeEmptyPacket()` Send empty packet.
`void` `writeFloat​(float value)`
`void` `writeInt​(int value)` Write int value into buf.
`void` `writeLength​(long length)` Write field length into buf, flush socket if needed.
`void` `writeLong​(long value)` Write long value into buf.
`void` `writeShort​(short value)` Write short value into buf.
`protected void` `writeSocket​(boolean commandEnd)` Flush the internal buf.
`void` `writeString​(String str)`
`void` `writeStringEscaped​(String str, boolean noBackslashEscapes)` Write string to socket.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### SMALL_BUFFER_SIZE

    public static final int SMALL_BUFFER_SIZE

See Also:
  Constant Field Values

#### sequence

    protected final org.mariadb.jdbc.util.MutableInt sequence

#### compressSequence

    protected final org.mariadb.jdbc.util.MutableInt compressSequence

#### buf

    protected byte[] buf

#### pos

    protected int pos

### Constructor Detail

#### PacketWriter

    public PacketWriter​(OutputStream out,
                        int maxQuerySizeToLog,
                        org.mariadb.jdbc.util.MutableInt sequence,
                        org.mariadb.jdbc.util.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 IOException

Throws:
  `IOException`

#### writeByte

    public void writeByte​(int value)
                   throws IOException

Write byte into buf, flush buf to socket if needed.

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.

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.

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.

Parameters:
  `value` - long value
Throws:
  `IOException` - if socket error occur

#### writeDouble

    public void writeDouble​(double value)
                     throws IOException

Throws:
  `IOException`

#### writeFloat

    public void writeFloat​(float value)
                    throws IOException

Throws:
  `IOException`

#### writeBytes

    public void writeBytes​(byte[] arr)
                    throws IOException

Throws:
  `IOException`

#### writeBytesAtPos

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

#### writeBytes

    public void writeBytes​(byte[] arr,
                           int off,
                           int len)
                    throws IOException

Write byte array to buf. If buf is full, flush socket.

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.

Parameters:
  `length` - field length
Throws:
  `IOException` - if socket error occur.

#### writeAscii

    public void writeAscii​(String str)
                    throws IOException

Throws:
  `IOException`

#### writeString

    public void writeString​(String str)
                     throws IOException

Throws:
  `IOException`

#### writeStringEscaped

    public void writeStringEscaped​(String str,
                                   boolean noBackslashEscapes)
                            throws IOException

Write string to socket.

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.

Parameters:
  `bytes` - bytes
  `len` - len to write
  `noBackslashEscapes` - escape method
Throws:
  `IOException` - if socket error occur

#### writeEmptyPacket

    public void writeEmptyPacket()
                          throws IOException

Send empty packet.

Throws:
  `IOException` - if socket error occur.

#### flush

    public void flush()
               throws IOException

Send packet to socket.

Throws:
  `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 send.

#### setMaxAllowedPacket

    public void setMaxAllowedPacket​(int maxAllowedPacket)

#### permitTrace

    public void permitTrace​(boolean permitTrace)

#### setServerThreadId

    public void setServerThreadId​(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 IOException

Flush to last mark.

Throws:
  `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 IOException

Flush the internal buf.

Parameters:
  `commandEnd` - command end
Throws:
  `IOException` - id connection error occur.

#### close

    public void close()
               throws IOException

Throws:
  `IOException`