public class PacketOutputStream extends OutputStream
Modifier and Type | Field and Description |
---|---|
ByteBuffer |
buffer |
ByteBuffer |
firstBuffer |
OutputStream |
outputStream |
Constructor and Description |
---|
PacketOutputStream(OutputStream outputStream,
boolean logQuery)
Initialization with server outputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
assureBufferCapacity(int len)
Ensure that the buffer remaining size permit to write a data with a size len.
|
boolean |
checkRewritableLength(int length)
Check that current buffer + length will not be superior to max_allowed_packet + header size.
|
void |
close() |
void |
compressedAndSend(int notCompressPosition,
byte[] bufferBytes)
Compress datas and send them to database.
|
void |
finishPacket()
Ending command that tell to send buffer to server.
|
void |
finishPacketWithoutRelease()
Ending command that tell to send buffer to server.
|
void |
forceCleanupBuffer()
Force buffer cleanup.
|
int |
getMaxAllowedPacket() |
int |
getMaxPacketSize() |
OutputStream |
getOutputStream() |
protected void |
increase(int newCapacity) |
boolean |
isClosed() |
boolean |
isUseCompression() |
void |
releaseBuffer()
Reinitialized buffer to smaller size if needed to avoid memory consumption.
|
void |
releaseBufferIfNotLogging()
If logging is not active, release buffer.
|
void |
send(byte[] packetBuffer,
int packetSize)
Send bytes according to compression.
|
void |
send(String sql,
byte commandType)
Send SQL string to outputStream.
|
void |
sendDirect(byte[] sqlBytes,
int offset,
int sqlLength,
byte commandType)
Send buffer to outputStream.
|
void |
sendFile(InputStream is,
int seq)
Used to send LOAD DATA INFILE.
|
void |
sendStream(InputStream is)
Send stream to server.
|
void |
sendStream(InputStream is,
long readLength)
Send stream to server.
|
void |
sendStream(Reader reader)
Send reader stream to server.
|
void |
sendStream(Reader reader,
long readLength)
Send reader stream to server.
|
void |
setCompressSeqNo(int compressSeqNo) |
void |
setMaxAllowedPacket(int maxAllowedPacket)
Initialize maximal send size (can be send in multiple stream).
|
void |
setUseCompression(boolean useCompression) |
void |
startPacket(int seqNo)
Initialize stream sequence.
|
void |
startPacket(int seqNo,
boolean checkPacketLength)
Initialize stream sequence.
|
void |
write(byte[] bytes) |
void |
write(byte[] bytes,
int off,
int len) |
void |
write(int byteInt) |
PacketOutputStream |
writeByte(byte theByte)
Write a byte data to buffer.
|
PacketOutputStream |
writeByteArray(byte[] bytes)
Write byte array to buffer.
|
PacketOutputStream |
writeByteArrayLength(byte[] bytes)
Write byte array data to binary data.
|
PacketOutputStream |
writeBytes(byte theByte,
int count)
Write count time the byte value.
|
PacketOutputStream |
writeDateLength(Calendar calendar)
Write date in binary format.
|
void |
writeEmptyPacket(int seqNo)
Send an empty stream to server.
|
PacketOutputStream |
writeFieldLength(long length)
Write field length to encode in binary format.
|
PacketOutputStream |
writeInt(int theInt)
Write int data in binary format.
|
PacketOutputStream |
writeLong(long theLong)
Write long data in binary format.
|
PacketOutputStream |
writeShort(short theShort)
Write short data in binary format.
|
PacketOutputStream |
writeString(String str)
Write string data in binary format.
|
PacketOutputStream |
writeStringLength(String str)
Write string in binary format.
|
PacketOutputStream |
writeTimeLength(Calendar calendar,
boolean fractionalSeconds)
Write time in binary format.
|
PacketOutputStream |
writeTimestampLength(Calendar calendar,
Timestamp ts,
boolean fractionalSeconds)
Write timestamp in binary format.
|
void |
writeUnsafe(byte[] bytes) |
void |
writeUnsafe(byte[] bytes,
int off,
int len) |
void |
writeUnsafe(int value)
Write byte value directly to buffer.
|
flush
public ByteBuffer buffer
public ByteBuffer firstBuffer
public OutputStream outputStream
public PacketOutputStream(OutputStream outputStream, boolean logQuery)
outputStream
- server outputStreamlogQuery
- is query logging enable ?protected void increase(int newCapacity)
public void setUseCompression(boolean useCompression)
public void startPacket(int seqNo, boolean checkPacketLength) throws IOException
seqNo
- stream sequence numbercheckPacketLength
- indication that max stream allowed size will be checked.IOException
- if any error occur during data send to serverpublic void startPacket(int seqNo) throws IOException
seqNo
- stream sequence numberIOException
- if any error occur during data send to serverpublic void writeEmptyPacket(int seqNo) throws IOException
seqNo
- stream sequence numberIOException
- if any error occur during data send to serverpublic void setCompressSeqNo(int compressSeqNo)
public void sendFile(InputStream is, int seq) throws IOException
is
- inputStream to sendseq
- stream sequence numberIOException
- if any error occur during data send to serverpublic void sendStream(InputStream is) throws IOException
is
- inputStream to sendIOException
- if any error occur during data send to serverpublic void sendStream(InputStream is, long readLength) throws IOException
is
- inputStream to sendreadLength
- max size to sendIOException
- if any error occur during data send to serverpublic void sendStream(Reader reader) throws IOException
reader
- reader to sendIOException
- if any error occur during data send to serverpublic void sendStream(Reader reader, long readLength) throws IOException
reader
- reader to sendreadLength
- max size to sendIOException
- if any error occur during data send to serverpublic void releaseBuffer()
public void releaseBufferIfNotLogging()
public void finishPacketWithoutRelease() throws IOException
IOException
- if any connection error occurpublic void finishPacket() throws IOException
IOException
- if any connection error occurpublic void forceCleanupBuffer()
public void write(byte[] bytes)
write
in class OutputStream
public void write(int byteInt)
write
in class OutputStream
public void write(byte[] bytes, int off, int len)
write
in class OutputStream
public void writeUnsafe(int value)
value
- byte valuepublic void writeUnsafe(byte[] bytes, int off, int len)
public void writeUnsafe(byte[] bytes)
public boolean checkRewritableLength(int length)
length
- additionnal lengthpublic void compressedAndSend(int notCompressPosition, byte[] bufferBytes) throws IOException
notCompressPosition
- notCompressPositionbufferBytes
- not compressed data bufferIOException
- if any compression or connection error occurpublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
public void setMaxAllowedPacket(int maxAllowedPacket)
maxAllowedPacket
- value of server maxAllowedPacketpublic void assureBufferCapacity(int len)
len
- size of the datapublic PacketOutputStream writeByte(byte theByte)
theByte
- byte to writepublic PacketOutputStream writeBytes(byte theByte, int count)
theByte
- byte to write to buffercount
- number of time the value will be put to bufferpublic PacketOutputStream writeByteArray(byte[] bytes)
bytes
- byte arraypublic PacketOutputStream writeByteArrayLength(byte[] bytes)
bytes
- byte array to encodepublic PacketOutputStream writeString(String str)
str
- string value to encodepublic PacketOutputStream writeShort(short theShort)
theShort
- short data to encodepublic PacketOutputStream writeInt(int theInt)
theInt
- int datapublic PacketOutputStream writeLong(long theLong)
theLong
- long datapublic PacketOutputStream writeFieldLength(long length)
length
- data length to encodepublic PacketOutputStream writeStringLength(String str)
str
- string to encodepublic PacketOutputStream writeTimestampLength(Calendar calendar, Timestamp ts, boolean fractionalSeconds)
calendar
- session calendarts
- timestamp to sendfractionalSeconds
- must fractionnal second be send to serverpublic PacketOutputStream writeDateLength(Calendar calendar)
calendar
- datepublic PacketOutputStream writeTimeLength(Calendar calendar, boolean fractionalSeconds)
calendar
- session calendar.fractionalSeconds
- fractional seconds must be sendpublic boolean isClosed()
public int getMaxAllowedPacket()
public void send(byte[] packetBuffer, int packetSize) throws IOException
packetBuffer
- data to writepacketSize
- packet sizeIOException
- if connection to server failpublic void send(String sql, byte commandType) throws IOException, QueryException
sql
- sqlcommandType
- command typeIOException
- if connection error occur.QueryException
- if query size is to big according to server max_allowed_sizepublic void sendDirect(byte[] sqlBytes, int offset, int sqlLength, byte commandType) throws IOException, QueryException
sqlBytes
- bufferoffset
- offsetsqlLength
- length to send to streamcommandType
- command typeIOException
- if connection error occurQueryException
- if query size is to big according to server max_allowed_sizepublic OutputStream getOutputStream()
public int getMaxPacketSize()
public boolean isUseCompression()
Copyright © 2016. All rights reserved.