Class PacketWriter
java.lang.Object
org.mariadb.jdbc.client.socket.impl.PacketWriter
- All Implemented Interfaces:
Writer
Packet writer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byte
protected byte[]
internal bufferprivate boolean
private long
protected final MutableByte
compressed packet sequenceprivate static final byte
private static final int
private static final Logger
private int
private static final int
private final Integer
private final int
private final int
private static final int
private final OutputStream
private boolean
protected int
buffer positionprivate static final byte
protected final MutableByte
packet sequenceprivate String
static final int
initial buffer sizeprivate static final byte
-
Constructor Summary
ConstructorsConstructorDescriptionPacketWriter
(OutputStream out, int maxQuerySizeToLog, Integer maxAllowedPacket, MutableByte sequence, MutableByte compressSequence) Common feature to write data into socket, creating MariaDB Packet. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
buf()
Current bufferboolean
Buffer has data after marked positionprivate void
checkMaxAllowedLength
(int length) Count query size.void
close()
Close socket streamvoid
flush()
Send packet to socket.void
Flush to last mark.void
Send packet to buffered outputstream without flushinglong
get current command lengthbyte
Get current sequence objectprivate void
growBuffer
(int len) buf growing use 4 size only to avoid creating/copying that are expensive operations.boolean
Current command has flushed packet to socketvoid
reset sequences and position for sending a new packetboolean
isMarked()
has some position been markedvoid
mark()
mark positionvoid
permitTrace
(boolean permitTrace) Indicate if logging trace are permittedint
pos()
get current positionvoid
pos
(int pos) position setterbyte[]
Reset mark flag and send bytes after mark flag.void
setServerThreadId
(Long serverThreadId, HostAddress hostAddress) Set server thread id.boolean
throwMaxAllowedLength
(int length) must a max allowed length exception be thrownvoid
writeAscii
(String str) Write ascii string to buffervoid
writeByte
(int value) Write byte into buf, flush buf to socket if needed.void
writeBytes
(byte[] arr) Write byte array to buffervoid
writeBytes
(byte[] arr, int off, int len) Write byte array to buf.void
writeBytesAtPos
(byte[] arr, int pos) Write byte array to buffer at a specific positionvoid
writeBytesEscaped
(byte[] bytes, int len, boolean noBackslashEscapes) Write escape bytes to socket.void
writeDouble
(double value) Write Double binary value to buffervoid
Send empty packet.void
writeFloat
(float value) Write float binary value to buffervoid
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) Write utf8 string to buffervoid
writeStringEscaped
(String str, boolean noBackslashEscapes) Write string to socket.
-
Field Details
-
SMALL_BUFFER_SIZE
public static final int SMALL_BUFFER_SIZEinitial buffer size- See Also:
-
logger
-
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:
-
MEDIUM_BUFFER_SIZE
private static final int MEDIUM_BUFFER_SIZE- See Also:
-
LARGE_BUFFER_SIZE
private static final int LARGE_BUFFER_SIZE- See Also:
-
MAX_PACKET_LENGTH
private static final int MAX_PACKET_LENGTH- See Also:
-
sequence
packet sequence -
compressSequence
compressed packet sequence -
maxQuerySizeToLog
private final int maxQuerySizeToLog -
out
-
maxPacketLength
private final int maxPacketLength- See Also:
-
maxAllowedPacket
-
buf
protected byte[] bufinternal buffer -
pos
protected int posbuffer position -
cmdLength
private long cmdLength -
permitTrace
private boolean permitTrace -
serverThreadLog
-
mark
private int mark -
bufContainDataAfterMark
private boolean bufContainDataAfterMark
-
-
Constructor Details
-
PacketWriter
public PacketWriter(OutputStream out, int maxQuerySizeToLog, Integer maxAllowedPacket, MutableByte sequence, MutableByte compressSequence) Common feature to write data into socket, creating MariaDB Packet.- Parameters:
out
- output streammaxQuerySizeToLog
- maximum query size to logmaxAllowedPacket
- max allowed packet value if knownsequence
- packet sequencecompressSequence
- compressed packet sequence
-
-
Method Details
-
pos
public int pos()get current position -
pos
position setter- Specified by:
pos
in interfaceWriter
- Parameters:
pos
- new position- Throws:
IOException
- if buffer is not big enough to contains new position
-
getCmdLength
public long getCmdLength()get current command length- Specified by:
getCmdLength
in interfaceWriter
- Returns:
- current command length
-
writeByte
Write byte into buf, flush buf to socket if needed.- Specified by:
writeByte
in interfaceWriter
- Parameters:
value
- byte to send- Throws:
IOException
- if socket error occur.
-
writeShort
Write short value into buf. flush buf if too small.- Specified by:
writeShort
in interfaceWriter
- Parameters:
value
- short value- Throws:
IOException
- if socket error occur
-
writeInt
Write int value into buf. flush buf if too small.- Specified by:
writeInt
in interfaceWriter
- Parameters:
value
- int value- Throws:
IOException
- if socket error occur
-
writeLong
Write long value into buf. flush buf if too small.- Specified by:
writeLong
in interfaceWriter
- Parameters:
value
- long value- Throws:
IOException
- if socket error occur
-
writeDouble
Description copied from interface:Writer
Write Double binary value to buffer- Specified by:
writeDouble
in interfaceWriter
- Parameters:
value
- double value- Throws:
IOException
- if socket error occur
-
writeFloat
Description copied from interface:Writer
Write float binary value to buffer- Specified by:
writeFloat
in interfaceWriter
- Parameters:
value
- float value- Throws:
IOException
- if socket error occur
-
writeBytes
Description copied from interface:Writer
Write byte array to buffer- Specified by:
writeBytes
in interfaceWriter
- Parameters:
arr
- bytes- Throws:
IOException
- if socket error occur
-
writeBytesAtPos
public void writeBytesAtPos(byte[] arr, int pos) Description copied from interface:Writer
Write byte array to buffer at a specific position- Specified by:
writeBytesAtPos
in interfaceWriter
- Parameters:
arr
- bytespos
- position
-
writeBytes
Write byte array to buf. If buf is full, flush socket.- Specified by:
writeBytes
in interfaceWriter
- Parameters:
arr
- byte arrayoff
- offsetlen
- byte length to write- Throws:
IOException
- if socket error occur
-
writeLength
Write field length into buf, flush socket if needed.- Specified by:
writeLength
in interfaceWriter
- Parameters:
length
- field length- Throws:
IOException
- if socket error occur.
-
writeAscii
Description copied from interface:Writer
Write ascii string to buffer- Specified by:
writeAscii
in interfaceWriter
- Parameters:
str
- string- Throws:
IOException
- if socket error occurs
-
writeString
Description copied from interface:Writer
Write utf8 string to buffer- Specified by:
writeString
in interfaceWriter
- Parameters:
str
- string- Throws:
IOException
- if socket error occurs
-
buf
public byte[] buf()Current buffer -
writeStringEscaped
Write string to socket.- Specified by:
writeStringEscaped
in interfaceWriter
- Parameters:
str
- stringnoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
writeBytesEscaped
Write escape bytes to socket.- Specified by:
writeBytesEscaped
in interfaceWriter
- Parameters:
bytes
- byteslen
- len to writenoBackslashEscapes
- escape method- Throws:
IOException
- if socket error occur
-
growBuffer
buf growing use 4 size only to avoid creating/copying that are expensive operations. possible size- SMALL_buf_SIZE = 8k (default)
- MEDIUM_buf_SIZE = 128k
- LARGE_buf_SIZE = 1M
- getMaxPacketLength = 16M (+ 4 if using no compression)
- Parameters:
len
- length to add- Throws:
IOException
-
writeEmptyPacket
Send empty packet.- Specified by:
writeEmptyPacket
in interfaceWriter
- Throws:
IOException
- if socket error occur.
-
flush
Send packet to socket.- Specified by:
flush
in interfaceWriter
- Throws:
IOException
- if socket error occur.
-
flushPipeline
Description copied from interface:Writer
Send packet to buffered outputstream without flushing- Specified by:
flushPipeline
in interfaceWriter
- Throws:
IOException
- if socket error occur.
-
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.- Parameters:
length
- additional length to query size- Throws:
MaxAllowedPacketException
- if query has not to be sent.
-
throwMaxAllowedLength
public boolean throwMaxAllowedLength(int length) Description copied from interface:Writer
must a max allowed length exception be thrown- Specified by:
throwMaxAllowedLength
in interfaceWriter
- Parameters:
length
- command length- Returns:
- true if too big
-
permitTrace
public void permitTrace(boolean permitTrace) Description copied from interface:Writer
Indicate if logging trace are permitted- Specified by:
permitTrace
in interfaceWriter
- Parameters:
permitTrace
- permits trace to be logged
-
setServerThreadId
Set server thread id.- Specified by:
setServerThreadId
in interfaceWriter
- Parameters:
serverThreadId
- current server thread id.hostAddress
- host information
-
mark
public void mark()Description copied from interface:Writer
mark position -
isMarked
public boolean isMarked()Description copied from interface:Writer
has some position been marked -
hasFlushed
public boolean hasFlushed()Description copied from interface:Writer
Current command has flushed packet to socket- Specified by:
hasFlushed
in interfaceWriter
- Returns:
- indicate if some packet have been flushed
-
flushBufferStopAtMark
Flush to last mark.- Specified by:
flushBufferStopAtMark
in interfaceWriter
- Throws:
IOException
- if flush fail.
-
bufIsDataAfterMark
public boolean bufIsDataAfterMark()Description copied from interface:Writer
Buffer has data after marked position- Specified by:
bufIsDataAfterMark
in interfaceWriter
- Returns:
- indicate if there is data after marked position
-
resetMark
public byte[] resetMark()Reset mark flag and send bytes after mark flag. -
initPacket
public void initPacket()Description copied from interface:Writer
reset sequences and position for sending a new packet- Specified by:
initPacket
in interfaceWriter
-
writeSocket
Flush the internal buf.- Parameters:
commandEnd
- command end- Throws:
IOException
- id connection error occur.
-
close
Description copied from interface:Writer
Close socket stream- Specified by:
close
in interfaceWriter
- Throws:
IOException
- if any error occurs
-
getSequence
public byte getSequence()Description copied from interface:Writer
Get current sequence object- Specified by:
getSequence
in interfaceWriter
- Returns:
- current sequence
-