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
FieldsModifier and TypeFieldDescriptionprivate static final byte
protected byte[]
private boolean
protected long
private static final byte
private static final int
private int
protected int
protected final int
private static final int
protected boolean
protected int
private static final byte
protected int
protected String
private static final int
protected LruTraceCache
private static final byte
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractPacketOutputStream
(OutputStream out, int maxQuerySizeToLog) Common feature to write data into socket, creating MariaDB Packet. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
checkMaxAllowedLength
(int length) Count query size.boolean
checkRemainingSize
(int len) boolean
void
flush()
Send packet to socket.protected abstract void
flushBuffer
(boolean commandEnd) void
Flush to last mark.int
abstract int
private void
growBuffer
(int len) Buffer growing use 4 size only to avoid creating/copying that are expensive operations.boolean
isMarked()
void
mark()
void
permitTrace
(boolean permitTrace) byte[]
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 into socket.void
Write reader into socket.void
void
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
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 java.io.FilterOutputStream
close
Methods inherited from class java.io.OutputStream
nullOutputStream
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.mariadb.jdbc.internal.io.output.PacketOutputStream
close, initialPacketPos
-
Field Details
-
QUOTE
private static final byte QUOTE- See Also:
-
DBL_QUOTE
private static final byte DBL_QUOTE- See Also:
-
ZERO_BYTE
private static final byte ZERO_BYTE- See Also:
-
BACKSLASH
private static final byte BACKSLASH- See Also:
-
SMALL_BUFFER_SIZE
private static final int SMALL_BUFFER_SIZE- See Also:
-
MEDIUM_BUFFER_SIZE
private static final int MEDIUM_BUFFER_SIZE- See Also:
-
LARGE_BUFFER_SIZE
private static final int LARGE_BUFFER_SIZE- See Also:
-
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
-
traceCache
-
mark
private int mark -
bufferContainDataAfterMark
private boolean bufferContainDataAfterMark
-
-
Constructor Details
-
AbstractPacketOutputStream
Common feature to write data into socket, creating MariaDB Packet.- Parameters:
out
- socket outputStreammaxQuerySizeToLog
- maximum query size to log
-
-
Method Details
-
getMaxPacketLength
public abstract int getMaxPacketLength() -
startPacket
public abstract void startPacket(int seqNo) - Specified by:
startPacket
in interfacePacketOutputStream
-
flushBuffer
- Throws:
IOException
-
growBuffer
Buffer growing use 4 size only to avoid creating/copying that are expensive operations. possible size- SMALL_BUFFER_SIZE = 8k (default)
- MEDIUM_BUFFER_SIZE = 128k
- LARGE_BUFFER_SIZE = 1M
- getMaxPacketLength = 16M (+ 4 is using no compression)
- Parameters:
len
- length to add- Throws:
IOException
-
writeEmptyPacket
Send empty packet.- Specified by:
writeEmptyPacket
in interfacePacketOutputStream
- Parameters:
seqNo
- packet sequence- Throws:
IOException
- if socket error occur.
-
writeEmptyPacket
- Specified by:
writeEmptyPacket
in interfacePacketOutputStream
- Throws:
IOException
-
flush
Send packet to socket.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in interfacePacketOutputStream
- Overrides:
flush
in classFilterOutputStream
- Throws:
IOException
- if socket error occur.
-
checkRemainingSize
public boolean checkRemainingSize(int len) - Specified by:
checkRemainingSize
in interfacePacketOutputStream
-
checkMaxAllowedLength
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 interfacePacketOutputStream
- Parameters:
length
- additional length to query size- Throws:
MaxAllowedPacketException
- if query has not to be send.
-
exceedMaxLength
public boolean exceedMaxLength()- Specified by:
exceedMaxLength
in interfacePacketOutputStream
-
getOutputStream
- Specified by:
getOutputStream
in interfacePacketOutputStream
-
writeShort
Write short value into buffer. flush buffer if too small.- Specified by:
writeShort
in interfacePacketOutputStream
- Parameters:
value
- short value- Throws:
IOException
- if socket error occur
-
writeInt
Write int value into buffer. flush buffer if too small.- Specified by:
writeInt
in interfacePacketOutputStream
- Parameters:
value
- int value- Throws:
IOException
- if socket error occur
-
writeLong
Write long value into buffer. flush buffer if too small.- Specified by:
writeLong
in interfacePacketOutputStream
- Parameters:
value
- long value- Throws:
IOException
- if socket error occur
-
writeBytes
Write byte value, len times into buffer. flush buffer if too small.- Specified by:
writeBytes
in interfacePacketOutputStream
- Parameters:
value
- byte valuelen
- number of time to write value.- Throws:
IOException
- if socket error occur.
-
writeFieldLength
Write field length into buffer, flush socket if needed.- Specified by:
writeFieldLength
in interfacePacketOutputStream
- Parameters:
length
- field length- Throws:
IOException
- if socket error occur.
-
write
Write byte into buffer, flush buffer to socket if needed.- Specified by:
write
in interfacePacketOutputStream
- Overrides:
write
in classFilterOutputStream
- Parameters:
value
- byte to send- Throws:
IOException
- if socket error occur.
-
write
- Specified by:
write
in interfacePacketOutputStream
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
Write byte array to buffer. If buffer is full, flush socket.- Specified by:
write
in interfacePacketOutputStream
- Overrides:
write
in classFilterOutputStream
- Parameters:
arr
- byte arrayoff
- offsetlen
- byte length to write- Throws:
IOException
- if socket error occur
-
write
- Specified by:
write
in interfacePacketOutputStream
- Throws:
IOException
-
write
Write string to socket.- Specified by:
write
in interfacePacketOutputStream
- Parameters:
str
- stringescape
- must be escapenoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
write
Write stream into socket.- Specified by:
write
in interfacePacketOutputStream
- Parameters:
is
- inputStreamescape
- must be escapenoBackslashEscapes
- 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 interfacePacketOutputStream
- Parameters:
is
- inputStreamlength
- write lengthescape
- must be escapenoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
write
Write reader into socket.- Specified by:
write
in interfacePacketOutputStream
- Parameters:
reader
- readerescape
- must be escapenoBackslashEscapes
- 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 interfacePacketOutputStream
- Parameters:
reader
- readerlength
- write lengthescape
- must be escapenoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
writeBytesEscaped
Write escape bytes to socket.- Specified by:
writeBytesEscaped
in interfacePacketOutputStream
- Parameters:
bytes
- byteslen
- len to writenoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
getMaxAllowedPacket
public int getMaxAllowedPacket()- Specified by:
getMaxAllowedPacket
in interfacePacketOutputStream
-
setMaxAllowedPacket
public abstract void setMaxAllowedPacket(int maxAllowedPacket) - Specified by:
setMaxAllowedPacket
in interfacePacketOutputStream
-
permitTrace
public void permitTrace(boolean permitTrace) - Specified by:
permitTrace
in interfacePacketOutputStream
-
setServerThreadId
Set server thread id.- Specified by:
setServerThreadId
in interfacePacketOutputStream
- Parameters:
serverThreadId
- current server thread id.isMaster
- is server master
-
setTraceCache
- Specified by:
setTraceCache
in interfacePacketOutputStream
-
mark
public void mark()- Specified by:
mark
in interfacePacketOutputStream
-
isMarked
public boolean isMarked()- Specified by:
isMarked
in interfacePacketOutputStream
-
flushBufferStopAtMark
Flush to last mark.- Specified by:
flushBufferStopAtMark
in interfacePacketOutputStream
- Throws:
IOException
- if flush fail.
-
bufferIsDataAfterMark
public boolean bufferIsDataAfterMark()- Specified by:
bufferIsDataAfterMark
in interfacePacketOutputStream
-
resetMark
public byte[] resetMark()Reset mark flag and send bytes after mark flag.- Specified by:
resetMark
in interfacePacketOutputStream
- Returns:
- bytes after mark flag
-