Class Writer
java.lang.Object
org.mariadb.jdbc.client.socket.Writer
Packet writer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byteprotected byte[]internal bufferprivate booleanprivate longprotected final MutableBytecompressed packet sequenceprivate static final byteprivate static final intprivate static final Loggerprivate intprivate static final intprivate Integerprivate final intprivate final intprivate static final intprivate final OutputStreamprivate booleanprotected intbuffer positionprivate static final byteprotected final MutableBytepacket sequenceprivate Stringstatic final intinitial buffer sizeprivate static final byte -
Constructor Summary
ConstructorsConstructorDescriptionWriter(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 bufferbooleanprivate voidcheckMaxAllowedLength(int length) Count query size.voidclose()voidflush()Send packet to socket.voidFlush to last mark.voidlongget current command lengthbyteprivate voidgrowBuffer(int len) buf growing use 4 size only to avoid creating/copying that are expensive operations.booleanvoidbooleanisMarked()voidmark()voidpermitTrace(boolean permitTrace) intpos()get current positionvoidpos(int pos) position setterbyte[]Reset mark flag and send bytes after mark flag.voidsetMaxAllowedPacket(Integer maxAllowedPacket) Set the maximum size (in bytes) of a packet the writer will send.voidsetServerThreadId(Long serverThreadId, HostAddress hostAddress) Set server thread id.booleanthrowMaxAllowedLength(int length) booleanthrowMaxAllowedLengthOr16M(int length) voidwriteAscii(String str) voidwriteByte(int value) Write byte into buf, flush buf to socket if needed.voidwriteBytes(byte[] arr) voidwriteBytes(byte[] arr, int off, int len) Write byte array to buf.voidwriteBytesAtPos(byte[] arr, int pos) voidwriteBytesEscaped(byte[] bytes, int len, boolean noBackslashEscapes) Write escape bytes to socket.voidwriteDouble(double value) voidSend empty packet.voidwriteFloat(float value) voidwriteInt(int value) Write int value into buf.voidwriteLength(long length) Write field length into buf, flush socket if needed.voidwriteLong(long value) Write long value into buf.voidwriteShort(short value) Write short value into buf.protected voidwriteSocket(boolean commandEnd) Flush the internal buf.voidwriteString(String str) voidwriteStringEscaped(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
-
Writer
public Writer(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- Returns:
- current position
-
pos
position setter- Parameters:
pos- new position- Throws:
IOException- if buffer is not big enough to contains new position
-
getCmdLength
public long getCmdLength()get current command length- Returns:
- current command length
-
writeByte
Write byte into buf, flush buf to socket if needed.- Parameters:
value- byte to send- Throws:
IOException- if socket error occur.
-
writeShort
Write short value into buf. flush buf if too small.- Parameters:
value- short value- Throws:
IOException- if socket error occur
-
writeInt
Write int value into buf. flush buf if too small.- Parameters:
value- int value- Throws:
IOException- if socket error occur
-
writeLong
Write long value into buf. flush buf if too small.- Parameters:
value- long value- Throws:
IOException- if socket error occur
-
writeDouble
- Throws:
IOException
-
writeFloat
- Throws:
IOException
-
writeBytes
- Throws:
IOException
-
writeBytesAtPos
public void writeBytesAtPos(byte[] arr, int pos) -
writeBytes
Write byte array to buf. If buf is full, flush socket.- 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.- Parameters:
length- field length- Throws:
IOException- if socket error occur.
-
writeAscii
- Throws:
IOException
-
writeString
- Throws:
IOException
-
buf
public byte[] buf()Current buffer- Returns:
- current buffer
-
writeStringEscaped
Write string to socket.- Parameters:
str- stringnoBackslashEscapes- escape method- Throws:
IOException- if socket error occur
-
writeBytesEscaped
Write escape bytes to socket.- 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.- Throws:
IOException- if socket error occur.
-
flush
Send packet to socket.- Throws:
IOException- if socket error occur.
-
flushPipeline
- Throws:
IOException
-
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) -
throwMaxAllowedLengthOr16M
public boolean throwMaxAllowedLengthOr16M(int length) -
setMaxAllowedPacket
Set the maximum size (in bytes) of a packet the writer will send. Called once the handshake/authentication phase is over to raise the limit from the 16Mb connection-phase cap to the configuredmaxAllowedPacket.- Parameters:
maxAllowedPacket- maximum packet size to send, ornullfor no explicit limit
-
permitTrace
public void permitTrace(boolean permitTrace) -
setServerThreadId
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
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
Flush the internal buf.- Parameters:
commandEnd- command end- Throws:
IOException- id connection error occur.
-
close
- Throws:
IOException
-
getSequence
public byte getSequence()
-