Package org.mariadb.jdbc.client.socket
Class PacketWriter
java.lang.Object
org.mariadb.jdbc.client.socket.PacketWriter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byte
protected byte[]
private boolean
private long
protected final MutableInt
private static final byte
private static final int
private static final Logger
private int
private static final int
private int
private int
private final int
private static final int
private final OutputStream
private boolean
protected int
private static final byte
protected final MutableInt
private String
static final int
private static final byte
-
Constructor Summary
ConstructorsConstructorDescriptionPacketWriter
(OutputStream out, int maxQuerySizeToLog, MutableInt sequence, MutableInt compressSequence) Common feature to write data into socket, creating MariaDB Packet. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
checkMaxAllowedLength
(int length) Count query size.void
close()
void
flush()
Send packet to socket.void
Flush to last mark.private void
growBuffer
(int len) buf growing use 4 size only to avoid creating/copying that are expensive operations.boolean
void
boolean
isMarked()
void
mark()
void
permitTrace
(boolean permitTrace) int
pos()
void
pos
(int pos) byte[]
Reset mark flag and send bytes after mark flag.void
setMaxAllowedPacket
(int maxAllowedPacket) void
setServerThreadId
(Long serverThreadId, HostAddress hostAddress) Set server thread id.void
writeAscii
(String str) void
writeByte
(int value) Write byte into buf, flush buf to socket if needed.void
writeBytes
(byte[] arr) void
writeBytes
(byte[] arr, int off, int len) Write byte array to buf.void
writeBytesAtPos
(byte[] arr, int pos) void
writeBytesEscaped
(byte[] bytes, int len, boolean noBackslashEscapes) Write escape bytes to socket.void
writeDouble
(double value) void
Send empty packet.void
writeFloat
(float value) void
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) void
writeStringEscaped
(String str, boolean noBackslashEscapes) Write string to socket.
-
Field Details
-
SMALL_BUFFER_SIZE
public static final int SMALL_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
-
compressSequence
-
maxQuerySizeToLog
private final int maxQuerySizeToLog -
out
-
buf
protected byte[] buf -
pos
protected int pos -
maxPacketLength
private int maxPacketLength -
maxAllowedPacket
private int maxAllowedPacket -
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, MutableInt sequence, MutableInt compressSequence) Common feature to write data into socket, creating MariaDB Packet.- Parameters:
out
- output streammaxQuerySizeToLog
- maximum query size to logsequence
- packet sequencecompressSequence
- compressed packet sequence
-
-
Method Details
-
pos
public int pos() -
pos
- Throws:
IOException
-
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
-
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.
-
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.
-
setMaxAllowedPacket
public void setMaxAllowedPacket(int maxAllowedPacket) -
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
-