# Class AbstractPacketOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.mariadb.jdbc.internal.io.output.AbstractPacketOutputStream

All Implemented Interfaces:
  `Closeable, Flushable, AutoCloseable, PacketOutputStream`

Direct Known Subclasses:
  `CompressPacketOutputStream, StandardPacketOutputStream`

public abstract class AbstractPacketOutputStream extends FilterOutputStream implements PacketOutputStream

## Field Summary

Fields
Modifier and Type
Field
Description
`protected byte[]`
`buf`
`protected long`
`cmdLength`
`protected int`
`maxAllowedPacket`
`protected final int`
`maxQuerySizeToLog`
`protected boolean`
`permitTrace`
`protected int`
`pos`
`protected int`
`seqNo`
`protected String`
`serverThreadLog`
`protected long`
`threadId`
`protected LruTraceCache`
`traceCache`

### Fields inherited from class FilterOutputStream

`out`

## Constructor Summary

Constructors
Constructor
Description
`AbstractPacketOutputStream(OutputStream out, int maxQuerySizeToLog, long threadId)`
Common feature to write data into socket, creating MariaDB Packet.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`bufferIsDataAfterMark()`
`void`
`checkMaxAllowedLength(int length)`
Count query size.
`boolean`
`checkRemainingSize(int len)`
`boolean`
`exceedMaxLength()`
`void`
`flush()`
Send packet to socket.
`protected abstract void`
`flushBuffer(boolean commandEnd)`
`void`
`flushBufferStopAtMark()`
Flush to last mark.
`int`
`getMaxAllowedPacket()`
`abstract int`
`getMaxPacketLength()`
`OutputStream`
`getOutputStream()`
`boolean`
`isMarked()`
`void`
`mark()`
`void`
`permitTrace(boolean permitTrace)`
`byte[]`
`resetMark()`
Reset mark flag and send bytes after mark flag.
`abstract void`
`setMaxAllowedPacket(int maxAllowedPacket)`
`void`
`setServerThreadId(long serverThreadId, Boolean isMaster)`
Set server thread id.
`void`
`setTraceCache(LruTraceCache traceCache)`
`abstract void`
`startPacket(int seqNo)`
`void`
`write(byte[] arr)`
`void`
`write(byte[] arr, int off, int len)`
Write byte array to buffer.
`void`
`write(int value)`
Write byte into buffer, flush buffer to socket if needed.
`void`
`write(InputStream is, boolean escape, boolean noBackslashEscapes)`
Write stream into socket.
`void`
`write(InputStream is, long length, boolean escape, boolean noBackslashEscapes)`
Write stream into socket.
`void`
`write(Reader reader, boolean escape, boolean noBackslashEscapes)`
Write reader into socket.
`void`
`write(Reader reader, long length, boolean escape, boolean noBackslashEscapes)`
Write reader into socket.
`void`
`write(String str)`
`void`
`write(String str, boolean escape, boolean noBackslashEscapes)`
Write string to socket.
`void`
`writeBytes(byte value, int len)`
Write byte value, len times into buffer.
`void`
`writeBytesEscaped(byte[] bytes, int len, boolean noBackslashEscapes)`
Write escape bytes to socket.
`abstract void`
`writeEmptyPacket()`
`void`
`writeEmptyPacket(int seqNo)`
Send empty packet.
`void`
`writeFieldLength(long length)`
Write field length into buffer, flush socket if needed.
`void`
`writeInt(int value)`
Write int value into buffer.
`void`
`writeLong(long value)`
Write long value into buffer.
`void`
`writeShort(short value)`
Write short value into buffer.

### Methods inherited from class FilterOutputStream

`close`

### Methods inherited from class OutputStream

`nullOutputStream`

### Methods inherited from class Object

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

### Methods inherited from interface PacketOutputStream

`close, initialPacketPos`

## Field Details

### 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 String serverThreadLog

### traceCache

protected LruTraceCache traceCache

### threadId

protected long threadId

## Constructor Details

### AbstractPacketOutputStream

public AbstractPacketOutputStream(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 Details

### getMaxPacketLength

public abstract int getMaxPacketLength()

### startPacket

public abstract void startPacket(int seqNo)

Specified by:
  `startPacket` in interface `PacketOutputStream`

### flushBuffer

protected abstract void flushBuffer(boolean commandEnd) throws IOException

Throws:
  `IOException`

### writeEmptyPacket

public void writeEmptyPacket(int seqNo) throws IOException
Send empty packet.

Specified by:
  `writeEmptyPacket` in interface `PacketOutputStream`
Parameters:
  `seqNo` - packet sequence
Throws:
  `IOException` - if socket error occur.

### writeEmptyPacket

public abstract void writeEmptyPacket() throws IOException

Specified by:
  `writeEmptyPacket` in interface `PacketOutputStream`
Throws:
  `IOException`

### flush

public void flush() throws IOException
Send packet to socket.

Specified by:
  `flush` in interface `Flushable`
Specified by:
  `flush` in interface `PacketOutputStream`
Overrides:
  `flush` in class `FilterOutputStream`
Throws:
  `IOException` - if socket error occur.

### checkRemainingSize

public boolean checkRemainingSize(int len)

Specified by:
  `checkRemainingSize` in interface `PacketOutputStream`

### 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.

### exceedMaxLength

public boolean exceedMaxLength()

Specified by:
  `exceedMaxLength` in interface `PacketOutputStream`

### getOutputStream

public OutputStream getOutputStream()

Specified by:
  `getOutputStream` in interface `PacketOutputStream`

### writeShort

public void writeShort(short value) throws IOException
Write short value into buffer. flush buffer if too small.

Specified by:
  `writeShort` in interface `PacketOutputStream`
Parameters:
  `value` - short value
Throws:
  `IOException` - if socket error occur

### writeInt

public void writeInt(int value) throws IOException
Write int value into buffer. flush buffer if too small.

Specified by:
  `writeInt` in interface `PacketOutputStream`
Parameters:
  `value` - int value
Throws:
  `IOException` - if socket error occur

### writeLong

public void writeLong(long value) throws IOException
Write long value into buffer. flush buffer if too small.

Specified by:
  `writeLong` in interface `PacketOutputStream`
Parameters:
  `value` - long value
Throws:
  `IOException` - if socket error occur

### writeBytes

public void writeBytes(byte value, int len) throws 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:
  `IOException` - if socket error occur.

### writeFieldLength

public void writeFieldLength(long length) throws IOException
Write field length into buffer, flush socket if needed.

Specified by:
  `writeFieldLength` in interface `PacketOutputStream`
Parameters:
  `length` - field length
Throws:
  `IOException` - if socket error occur.

### write

public void write(int value) throws IOException
Write byte into buffer, flush buffer to socket if needed.

Specified by:
  `write` in interface `PacketOutputStream`
Overrides:
  `write` in class `FilterOutputStream`
Parameters:
  `value` - byte to send
Throws:
  `IOException` - if socket error occur.

### write

public void write(byte[] arr) throws IOException

Specified by:
  `write` in interface `PacketOutputStream`
Overrides:
  `write` in class `FilterOutputStream`
Throws:
  `IOException`

### write

public void write(byte[] arr, int off, int len) throws IOException
Write byte array to buffer. If buffer is full, flush socket.

Specified by:
  `write` in interface `PacketOutputStream`
Overrides:
  `write` in class `FilterOutputStream`
Parameters:
  `arr` - byte array
  `off` - offset
  `len` - byte length to write
Throws:
  `IOException` - if socket error occur

### write

public void write(String str) throws IOException

Specified by:
  `write` in interface `PacketOutputStream`
Throws:
  `IOException`

### write

public void write(String str, boolean escape, boolean noBackslashEscapes) throws IOException
Write string to socket.

Specified by:
  `write` in interface `PacketOutputStream`
Parameters:
  `str` - string
  `escape` - must be escape
  `noBackslashEscapes` - escape method
Throws:
  `IOException` - if socket error occur

### write

public void write(InputStream is, boolean escape, boolean noBackslashEscapes) throws IOException
Write stream into socket.

Specified by:
  `write` in interface `PacketOutputStream`
Parameters:
  `is` - inputStream
  `escape` - must be escape
  `noBackslashEscapes` - escape method
Throws:
  `IOException` - if socket error occur

### write

public void write(InputStream is, long length, boolean escape, boolean noBackslashEscapes) throws 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:
  `IOException` - if socket error occur

### write

public void write(Reader reader, boolean escape, boolean noBackslashEscapes) throws IOException
Write reader into socket.

Specified by:
  `write` in interface `PacketOutputStream`
Parameters:
  `reader` - reader
  `escape` - must be escape
  `noBackslashEscapes` - escape method
Throws:
  `IOException` - if socket error occur

### write

public void write(Reader reader, long length, boolean escape, boolean noBackslashEscapes) throws 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:
  `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 `PacketOutputStream`
Parameters:
  `bytes` - bytes
  `len` - len to write
  `noBackslashEscapes` - escape method
Throws:
  `IOException` - if socket error occur

### getMaxAllowedPacket

public int getMaxAllowedPacket()

Specified by:
  `getMaxAllowedPacket` in interface `PacketOutputStream`

### setMaxAllowedPacket

public abstract void setMaxAllowedPacket(int maxAllowedPacket)

Specified by:
  `setMaxAllowedPacket` in interface `PacketOutputStream`

### permitTrace

public void permitTrace(boolean permitTrace)

Specified by:
  `permitTrace` in interface `PacketOutputStream`

### setServerThreadId

public void setServerThreadId(long serverThreadId, Boolean isMaster)
Set server thread id.

Specified by:
  `setServerThreadId` in interface `PacketOutputStream`
Parameters:
  `serverThreadId` - current server thread id.
  `isMaster` - is server master

### setTraceCache

public void setTraceCache(LruTraceCache traceCache)

Specified by:
  `setTraceCache` in interface `PacketOutputStream`

### mark

public void mark()

Specified by:
  `mark` in interface `PacketOutputStream`

### isMarked

public boolean isMarked()

Specified by:
  `isMarked` in interface `PacketOutputStream`

### flushBufferStopAtMark

public void flushBufferStopAtMark() throws IOException
Flush to last mark.

Specified by:
  `flushBufferStopAtMark` in interface `PacketOutputStream`
Throws:
  `IOException` - if flush fail.

### bufferIsDataAfterMark

public boolean bufferIsDataAfterMark()

Specified by:
  `bufferIsDataAfterMark` in interface `PacketOutputStream`

### resetMark

public byte[] resetMark()
Reset mark flag and send bytes after mark flag.

Specified by:
  `resetMark` in interface `PacketOutputStream`
Returns:
  bytes after mark flag