org.mariadb.jdbc.internal.stream

## Class PacketOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - org.mariadb.jdbc.internal.stream.PacketOutputStream

All Implemented Interfaces:
  Closeable, Flushable, AutoCloseable

    public class PacketOutputStream
    extends OutputStream

### Field Summary

Fields
`ByteBuffer` `buffer`
`ByteBuffer` `firstBuffer`
`OutputStream` `outputStream`

### Constructor Summary

Constructors
Constructor and Description
`PacketOutputStream(OutputStream outputStream, boolean logQuery)` Initialization with server outputStream.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `assureBufferCapacity(int len)` Ensure that the buffer remaining size permit to write a data with a size len.
`boolean` `checkRewritableLength(int length)` Check that current buffer + length will not be superior to max_allowed_packet + header size.
`void` `close()`
`void` `compressedAndSend(int notCompressPosition, byte[] bufferBytes)` Compress datas and send them to database.
`void` `finishPacket()` Ending command that tell to send buffer to server.
`void` `finishPacketWithoutRelease()` Ending command that tell to send buffer to server.
`void` `forceCleanupBuffer()` Force buffer cleanup.
`int` `getMaxAllowedPacket()`
`int` `getMaxPacketSize()`
`OutputStream` `getOutputStream()`
`protected void` `increase(int newCapacity)`
`boolean` `isClosed()`
`boolean` `isUseCompression()`
`void` `releaseBuffer()` Reinitialized buffer to smaller size if needed to avoid memory consumption.
`void` `releaseBufferIfNotLogging()` If logging is not active, release buffer.
`void` `send(byte[] packetBuffer, int packetSize)` Send bytes according to compression.
`void` `send(String sql, byte commandType)` Send SQL string to outputStream.
`void` `sendDirect(byte[] sqlBytes, int offset, int sqlLength, byte commandType)` Send buffer to outputStream.
`void` `sendFile(InputStream is, int seq)` Used to send LOAD DATA INFILE.
`void` `sendStream(InputStream is)` Send stream to server.
`void` `sendStream(InputStream is, long readLength)` Send stream to server.
`void` `sendStream(Reader reader)` Send reader stream to server.
`void` `sendStream(Reader reader, long readLength)` Send reader stream to server.
`void` `setCompressSeqNo(int compressSeqNo)`
`void` `setMaxAllowedPacket(int maxAllowedPacket)` Initialize maximal send size (can be send in multiple stream).
`void` `setUseCompression(boolean useCompression)`
`void` `startPacket(int seqNo)` Initialize stream sequence.
`void` `startPacket(int seqNo, boolean checkPacketLength)` Initialize stream sequence.
`void` `write(byte[] bytes)`
`void` `write(byte[] bytes, int off, int len)`
`void` `write(int byteInt)`
`PacketOutputStream` `writeByte(byte theByte)` Write a byte data to buffer.
`PacketOutputStream` `writeByteArray(byte[] bytes)` Write byte array to buffer.
`PacketOutputStream` `writeByteArrayLength(byte[] bytes)` Write byte array data to binary data.
`PacketOutputStream` `writeBytes(byte theByte, int count)` Write count time the byte value.
`PacketOutputStream` `writeDateLength(Calendar calendar)` Write date in binary format.
`void` `writeEmptyPacket(int seqNo)` Send an empty stream to server.
`PacketOutputStream` `writeFieldLength(long length)` Write field length to encode in binary format.
`PacketOutputStream` `writeInt(int theInt)` Write int data in binary format.
`PacketOutputStream` `writeLong(long theLong)` Write long data in binary format.
`PacketOutputStream` `writeShort(short theShort)` Write short data in binary format.
`PacketOutputStream` `writeString(String str)` Write string data in binary format.
`PacketOutputStream` `writeStringLength(String str)` Write string in binary format.
`PacketOutputStream` `writeTimeLength(Calendar calendar, boolean fractionalSeconds)` Write time in binary format.
`PacketOutputStream` `writeTimestampLength(Calendar calendar, Timestamp ts, boolean fractionalSeconds)` Write timestamp in binary format.
`void` `writeUnsafe(byte[] bytes)`
`void` `writeUnsafe(byte[] bytes, int off, int len)`
`void` `writeUnsafe(int value)` Write byte value directly to buffer.

### Methods inherited from class java.io.OutputStream

`flush`

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

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

### Field Detail

#### buffer

    public ByteBuffer buffer

#### firstBuffer

    public ByteBuffer firstBuffer

#### outputStream

    public OutputStream outputStream

### Constructor Detail

#### PacketOutputStream

    public PacketOutputStream(OutputStream outputStream,
                              boolean logQuery)

Initialization with server outputStream.

Parameters:
  `outputStream` - server outputStream
  `logQuery` - is query logging enable ?

### Method Detail

#### increase

    protected void increase(int newCapacity)

#### setUseCompression

    public void setUseCompression(boolean useCompression)

#### startPacket

    public void startPacket(int seqNo,
                            boolean checkPacketLength)
                     throws IOException

Initialize stream sequence. Max stream allowed size will be checked.

Parameters:
  `seqNo` - stream sequence number
  `checkPacketLength` - indication that max stream allowed size will be checked.
Throws:
  `IOException` - if any error occur during data send to server

#### startPacket

    public void startPacket(int seqNo)
                     throws IOException

Initialize stream sequence.

Parameters:
  `seqNo` - stream sequence number
Throws:
  `IOException` - if any error occur during data send to server

#### writeEmptyPacket

    public void writeEmptyPacket(int seqNo)
                          throws IOException

Send an empty stream to server.

Parameters:
  `seqNo` - stream sequence number
Throws:
  `IOException` - if any error occur during data send to server

#### setCompressSeqNo

    public void setCompressSeqNo(int compressSeqNo)

#### sendFile

    public void sendFile(InputStream is,
                         int seq)
                  throws IOException

Used to send LOAD DATA INFILE. End of data is indicated by stream of length 0.

Parameters:
  `is` - inputStream to send
  `seq` - stream sequence number
Throws:
  `IOException` - if any error occur during data send to server

#### sendStream

    public void sendStream(InputStream is)
                    throws IOException

Send stream to server.

Parameters:
  `is` - inputStream to send
Throws:
  `IOException` - if any error occur during data send to server

#### sendStream

    public void sendStream(InputStream is,
                           long readLength)
                    throws IOException

Send stream to server.

Parameters:
  `is` - inputStream to send
  `readLength` - max size to send
Throws:
  `IOException` - if any error occur during data send to server

#### sendStream

    public void sendStream(Reader reader)
                    throws IOException

Send reader stream to server.

Parameters:
  `reader` - reader to send
Throws:
  `IOException` - if any error occur during data send to server

#### sendStream

    public void sendStream(Reader reader,
                           long readLength)
                    throws IOException

Send reader stream to server.

Parameters:
  `reader` - reader to send
  `readLength` - max size to send
Throws:
  `IOException` - if any error occur during data send to server

#### releaseBuffer

    public void releaseBuffer()

Reinitialized buffer to smaller size if needed to avoid memory consumption.

#### releaseBufferIfNotLogging

    public void releaseBufferIfNotLogging()

If logging is not active, release buffer. (if logging is active, it will use the buffer to know send query and release buffer after a while)

#### finishPacketWithoutRelease

    public void finishPacketWithoutRelease()
                                    throws IOException

Ending command that tell to send buffer to server.

Throws:
  `IOException` - if any connection error occur

#### finishPacket

    public void finishPacket()
                      throws IOException

Ending command that tell to send buffer to server.

Throws:
  `IOException` - if any connection error occur

#### forceCleanupBuffer

    public void forceCleanupBuffer()

Force buffer cleanup.

#### write

    public void write(byte[] bytes)

Overrides:
  `write` in class `OutputStream`

#### write

    public void write(int byteInt)

Specified by:
  `write` in class `OutputStream`

#### write

    public void write(byte[] bytes,
                      int off,
                      int len)

Overrides:
  `write` in class `OutputStream`

#### writeUnsafe

    public void writeUnsafe(int value)

Write byte value directly to buffer. (buffer size must have been checked beforeheand !)

Parameters:
  `value` - byte value

#### writeUnsafe

    public void writeUnsafe(byte[] bytes,
                            int off,
                            int len)

#### writeUnsafe

    public void writeUnsafe(byte[] bytes)

#### checkRewritableLength

    public boolean checkRewritableLength(int length)

Check that current buffer + length will not be superior to max_allowed_packet + header size. That permit to separate rewritable queries to be separate in multiple stream.

Parameters:
  `length` - additionnal length
Returns:
  true if with this additional length stream can be send in the same stream

#### compressedAndSend

    public void compressedAndSend(int notCompressPosition,
                                  byte[] bufferBytes)
                           throws IOException

Compress datas and send them to database.

Parameters:
  `notCompressPosition` - notCompressPosition
  `bufferBytes` - not compressed data buffer
Throws:
  `IOException` - if any compression or connection error occur

#### close

    public void close()
               throws IOException

Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `AutoCloseable`
Overrides:
  `close` in class `OutputStream`
Throws:
  `IOException`

#### setMaxAllowedPacket

    public void setMaxAllowedPacket(int maxAllowedPacket)

Initialize maximal send size (can be send in multiple stream).

Parameters:
  `maxAllowedPacket` - value of server maxAllowedPacket

#### assureBufferCapacity

    public void assureBufferCapacity(int len)

Ensure that the buffer remaining size permit to write a data with a size len.

Parameters:
  `len` - size of the data

#### writeByte

    public PacketOutputStream writeByte(byte theByte)

Write a byte data to buffer.

Parameters:
  `theByte` - byte to write
Returns:
  this

#### writeBytes

    public PacketOutputStream writeBytes(byte theByte,
                                         int count)

Write count time the byte value.

Parameters:
  `theByte` - byte to write to buffer
  `count` - number of time the value will be put to buffer
Returns:
  this

#### writeByteArray

    public PacketOutputStream writeByteArray(byte[] bytes)

Write byte array to buffer.

Parameters:
  `bytes` - byte array
Returns:
  this.

#### writeByteArrayLength

    public PacketOutputStream writeByteArrayLength(byte[] bytes)

Write byte array data to binary data.

Parameters:
  `bytes` - byte array to encode
Returns:
  this.

#### writeString

    public PacketOutputStream writeString(String str)

Write string data in binary format.

Parameters:
  `str` - string value to encode
Returns:
  this.

#### writeShort

    public PacketOutputStream writeShort(short theShort)

Write short data in binary format.

Parameters:
  `theShort` - short data to encode
Returns:
  this

#### writeInt

    public PacketOutputStream writeInt(int theInt)

Write int data in binary format.

Parameters:
  `theInt` - int data
Returns:
  this.

#### writeLong

    public PacketOutputStream writeLong(long theLong)

Write long data in binary format.

Parameters:
  `theLong` - long data
Returns:
  this

#### writeFieldLength

    public PacketOutputStream writeFieldLength(long length)

Write field length to encode in binary format.

Parameters:
  `length` - data length to encode
Returns:
  this.

#### writeStringLength

    public PacketOutputStream writeStringLength(String str)

Write string in binary format.

Parameters:
  `str` - string to encode
Returns:
  this.

#### writeTimestampLength

    public PacketOutputStream writeTimestampLength(Calendar calendar,
                                                   Timestamp ts,
                                                   boolean fractionalSeconds)

Write timestamp in binary format.

Parameters:
  `calendar` - session calendar
  `ts` - timestamp to send
  `fractionalSeconds` - must fractionnal second be send to server
Returns:
  this

#### writeDateLength

    public PacketOutputStream writeDateLength(Calendar calendar)

Write date in binary format.

Parameters:
  `calendar` - date
Returns:
  this

#### writeTimeLength

    public PacketOutputStream writeTimeLength(Calendar calendar,
                                              boolean fractionalSeconds)

Write time in binary format.

Parameters:
  `calendar` - session calendar.
  `fractionalSeconds` - fractional seconds must be send
Returns:
  this

#### isClosed

    public boolean isClosed()

#### getMaxAllowedPacket

    public int getMaxAllowedPacket()

#### send

    public void send(byte[] packetBuffer,
                     int packetSize)
              throws IOException

Send bytes according to compression.

Parameters:
  `packetBuffer` - data to write
  `packetSize` - packet size
Throws:
  `IOException` - if connection to server fail

#### send

    public void send(String sql,
                     byte commandType)
              throws IOException,
                     QueryException

Send SQL string to outputStream. SQL will be transform to UTF-8 byte buffer, and if possible this buffer will be send to stream directly.

Parameters:
  `sql` - sql
  `commandType` - command type
Throws:
  `IOException` - if connection error occur.
  `QueryException` - if query size is to big according to server max_allowed_size

#### sendDirect

    public void sendDirect(byte[] sqlBytes,
                           int offset,
                           int sqlLength,
                           byte commandType)
                    throws IOException,
                           QueryException

Send buffer to outputStream.

Parameters:
  `sqlBytes` - buffer
  `offset` - offset
  `sqlLength` - length to send to stream
  `commandType` - command type
Throws:
  `IOException` - if connection error occur
  `QueryException` - if query size is to big according to server max_allowed_size

#### getOutputStream

    public OutputStream getOutputStream()

#### getMaxPacketSize

    public int getMaxPacketSize()

#### isUseCompression

    public boolean isUseCompression()