Skip to content

Commit 8a26229

Browse files
committed
Fixed handling of packets exceeding 16mb in size
1 parent c15c226 commit 8a26229

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/com/github/shyiko/mysql/binlog/BinaryLogClient.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.github.shyiko.mysql.binlog.network.SocketFactory;
3636
import com.github.shyiko.mysql.binlog.network.protocol.ErrorPacket;
3737
import com.github.shyiko.mysql.binlog.network.protocol.GreetingPacket;
38+
import com.github.shyiko.mysql.binlog.network.protocol.Packet;
3839
import com.github.shyiko.mysql.binlog.network.protocol.PacketChannel;
3940
import com.github.shyiko.mysql.binlog.network.protocol.ResultSetRowPacket;
4041
import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateCommand;
@@ -597,7 +598,9 @@ private void listenForEventPackets() throws IOException {
597598
}
598599
Event event;
599600
try {
600-
event = eventDeserializer.nextEvent(inputStream);
601+
event = eventDeserializer.nextEvent(packetLength == Packet.MAX_LENGTH ?
602+
new ByteArrayInputStream(readPacketSplitInChunks(inputStream, packetLength - 1)) :
603+
inputStream);
601604
} catch (Exception e) {
602605
Throwable cause = e instanceof EventDataDeserializationException ? e.getCause() : e;
603606
if (cause instanceof EOFException || cause instanceof SocketException) {
@@ -633,6 +636,25 @@ private void listenForEventPackets() throws IOException {
633636
}
634637
}
635638

639+
private byte[] readPacketSplitInChunks(ByteArrayInputStream inputStream, int packetLength) throws IOException {
640+
List<byte[]> chunks = new LinkedList<byte[]>();
641+
chunks.add(inputStream.read(packetLength));
642+
int chunkLength;
643+
do {
644+
chunkLength = inputStream.readInteger(3);
645+
inputStream.skip(1); // 1 byte for sequence
646+
chunks.add(inputStream.read(chunkLength));
647+
packetLength += chunkLength;
648+
} while (chunkLength == Packet.MAX_LENGTH);
649+
byte[] buffer = new byte[packetLength];
650+
int offset = 0;
651+
for (byte[] chunk : chunks) {
652+
System.arraycopy(chunk, 0, buffer, offset, chunk.length);
653+
offset += chunk.length;
654+
}
655+
return buffer;
656+
}
657+
636658
private void updateClientBinlogFilenameAndPosition(Event event) {
637659
EventHeader eventHeader = event.getHeader();
638660
if (eventHeader.getEventType() == EventType.ROTATE) {

src/main/java/com/github/shyiko/mysql/binlog/network/protocol/Packet.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
*/
2121
public interface Packet {
2222

23+
// https://dev.mysql.com/doc/internals/en/sending-more-than-16mbyte.html
24+
int MAX_LENGTH = 16777215;
2325
}

0 commit comments

Comments
 (0)