Package org.mariadb.jdbc.client.socket
Class Writer
- java.lang.Object
-
- org.mariadb.jdbc.client.socket.Writer
-
public class Writer extends java.lang.ObjectPacket writer
-
-
Field Summary
Fields Modifier and Type Field Description private static byteBACKSLASHprotected byte[]bufinternal bufferprivate booleanbufContainDataAfterMarkprivate longcmdLengthprotected MutableBytecompressSequencecompressed packet sequenceprivate static byteDBL_QUOTEprivate static intLARGE_BUFFER_SIZEprivate static Loggerloggerprivate intmarkprivate static intMAX_PACKET_LENGTHprivate java.lang.IntegermaxAllowedPacketprivate intmaxPacketLengthprivate intmaxQuerySizeToLogprivate static intMEDIUM_BUFFER_SIZEprivate java.io.OutputStreamoutprivate booleanpermitTraceprotected intposbuffer positionprivate static byteQUOTEprotected MutableBytesequencepacket sequenceprivate java.lang.StringserverThreadLogstatic intSMALL_BUFFER_SIZEinitial buffer sizeprivate static byteZERO_BYTE
-
Constructor Summary
Constructors Constructor Description Writer(java.io.OutputStream out, int maxQuerySizeToLog, java.lang.Integer maxAllowedPacket, MutableByte sequence, MutableByte compressSequence)Common feature to write data into socket, creating MariaDB Packet.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]buf()Current bufferbooleanbufIsDataAfterMark()private voidcheckMaxAllowedLength(int length)Count query size.voidclose()voidflush()Send packet to socket.voidflushBufferStopAtMark()Flush to last mark.voidflushPipeline()longgetCmdLength()get current command lengthbytegetSequence()private voidgrowBuffer(int len)buf growing use 4 size only to avoid creating/copying that are expensive operations.booleanhasFlushed()voidinitPacket()booleanisMarked()voidmark()voidpermitTrace(boolean permitTrace)intpos()get current positionvoidpos(int pos)position setterbyte[]resetMark()Reset mark flag and send bytes after mark flag.voidsetMaxAllowedPacket(java.lang.Integer maxAllowedPacket)Set the maximum size (in bytes) of a packet the writer will send.voidsetServerThreadId(java.lang.Long serverThreadId, HostAddress hostAddress)Set server thread id.booleanthrowMaxAllowedLength(int length)booleanthrowMaxAllowedLengthOr16M(int length)voidwriteAscii(java.lang.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)voidwriteEmptyPacket()Send 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(java.lang.String str)voidwriteStringEscaped(java.lang.String str, boolean noBackslashEscapes)Write string to socket.
-
-
-
Field Detail
-
SMALL_BUFFER_SIZE
public static final int SMALL_BUFFER_SIZE
initial buffer size- See Also:
- Constant Field Values
-
logger
private static final Logger logger
-
QUOTE
private static final byte QUOTE
- See Also:
- Constant Field Values
-
DBL_QUOTE
private static final byte DBL_QUOTE
- See Also:
- Constant Field Values
-
ZERO_BYTE
private static final byte ZERO_BYTE
- See Also:
- Constant Field Values
-
BACKSLASH
private static final byte BACKSLASH
- See Also:
- Constant Field Values
-
MEDIUM_BUFFER_SIZE
private static final int MEDIUM_BUFFER_SIZE
- See Also:
- Constant Field Values
-
LARGE_BUFFER_SIZE
private static final int LARGE_BUFFER_SIZE
- See Also:
- Constant Field Values
-
MAX_PACKET_LENGTH
private static final int MAX_PACKET_LENGTH
- See Also:
- Constant Field Values
-
sequence
protected final MutableByte sequence
packet sequence
-
compressSequence
protected final MutableByte compressSequence
compressed packet sequence
-
maxQuerySizeToLog
private final int maxQuerySizeToLog
-
out
private final java.io.OutputStream out
-
maxPacketLength
private final int maxPacketLength
- See Also:
- Constant Field Values
-
maxAllowedPacket
private java.lang.Integer maxAllowedPacket
-
buf
protected byte[] buf
internal buffer
-
pos
protected int pos
buffer position
-
cmdLength
private long cmdLength
-
permitTrace
private boolean permitTrace
-
serverThreadLog
private java.lang.String serverThreadLog
-
mark
private int mark
-
bufContainDataAfterMark
private boolean bufContainDataAfterMark
-
-
Constructor Detail
-
Writer
public Writer(java.io.OutputStream out, int maxQuerySizeToLog, java.lang.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 Detail
-
pos
public int pos()
get current position- Returns:
- current position
-
pos
public void pos(int pos) throws java.io.IOExceptionposition setter- Parameters:
pos- new position- Throws:
java.io.IOException- if buffer is not big enough to contains new position
-
getCmdLength
public long getCmdLength()
get current command length- Returns:
- current command length
-
writeByte
public void writeByte(int value) throws java.io.IOExceptionWrite byte into buf, flush buf to socket if needed.- Parameters:
value- byte to send- Throws:
java.io.IOException- if socket error occur.
-
writeShort
public void writeShort(short value) throws java.io.IOExceptionWrite short value into buf. flush buf if too small.- Parameters:
value- short value- Throws:
java.io.IOException- if socket error occur
-
writeInt
public void writeInt(int value) throws java.io.IOExceptionWrite int value into buf. flush buf if too small.- Parameters:
value- int value- Throws:
java.io.IOException- if socket error occur
-
writeLong
public void writeLong(long value) throws java.io.IOExceptionWrite long value into buf. flush buf if too small.- Parameters:
value- long value- Throws:
java.io.IOException- if socket error occur
-
writeDouble
public void writeDouble(double value) throws java.io.IOException- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float value) throws java.io.IOException- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(byte[] arr) throws java.io.IOException- Throws:
java.io.IOException
-
writeBytesAtPos
public void writeBytesAtPos(byte[] arr, int pos)
-
writeBytes
public void writeBytes(byte[] arr, int off, int len) throws java.io.IOExceptionWrite byte array to buf. If buf is full, flush socket.- Parameters:
arr- byte arrayoff- offsetlen- byte length to write- Throws:
java.io.IOException- if socket error occur
-
writeLength
public void writeLength(long length) throws java.io.IOExceptionWrite field length into buf, flush socket if needed.- Parameters:
length- field length- Throws:
java.io.IOException- if socket error occur.
-
writeAscii
public void writeAscii(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-
writeString
public void writeString(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-
buf
public byte[] buf()
Current buffer- Returns:
- current buffer
-
writeStringEscaped
public void writeStringEscaped(java.lang.String str, boolean noBackslashEscapes) throws java.io.IOExceptionWrite string to socket.- Parameters:
str- stringnoBackslashEscapes- escape method- Throws:
java.io.IOException- if socket error occur
-
writeBytesEscaped
public void writeBytesEscaped(byte[] bytes, int len, boolean noBackslashEscapes) throws java.io.IOExceptionWrite escape bytes to socket.- Parameters:
bytes- byteslen- len to writenoBackslashEscapes- escape method- Throws:
java.io.IOException- if socket error occur
-
growBuffer
private void growBuffer(int len) throws java.io.IOExceptionbuf 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:
java.io.IOException
-
writeEmptyPacket
public void writeEmptyPacket() throws java.io.IOExceptionSend empty packet.- Throws:
java.io.IOException- if socket error occur.
-
flush
public void flush() throws java.io.IOExceptionSend packet to socket.- Throws:
java.io.IOException- if socket error occur.
-
flushPipeline
public void flushPipeline() throws java.io.IOException- Throws:
java.io.IOException
-
checkMaxAllowedLength
private void checkMaxAllowedLength(int length) throws MaxAllowedPacketExceptionCount 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
public void setMaxAllowedPacket(java.lang.Integer maxAllowedPacket)
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
public void setServerThreadId(java.lang.Long serverThreadId, HostAddress hostAddress)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
public void flushBufferStopAtMark() throws java.io.IOExceptionFlush to last mark.- Throws:
java.io.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
protected void writeSocket(boolean commandEnd) throws java.io.IOExceptionFlush the internal buf.- Parameters:
commandEnd- command end- Throws:
java.io.IOException- id connection error occur.
-
close
public void close() throws java.io.IOException- Throws:
java.io.IOException
-
getSequence
public byte getSequence()
-
-