Skip to content

Commit 48d210c

Browse files
author
Stanley Shyiko
committed
Fixed shyiko#258 - checksum detection fails when custom FORMAT_DESCRIPTION event data deserializer is set
1 parent d024cb6 commit 48d210c

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/main/java/com/github/shyiko/mysql/binlog/event/FormatDescriptionEventData.java

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.github.shyiko.mysql.binlog.event;
1717

18+
import com.github.shyiko.mysql.binlog.event.deserialization.ChecksumType;
19+
1820
/**
1921
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
2022
*/
@@ -24,6 +26,7 @@ public class FormatDescriptionEventData implements EventData {
2426
private String serverVersion;
2527
private int headerLength;
2628
private int dataLength;
29+
private ChecksumType checksumType = ChecksumType.NONE;
2730

2831
public int getBinlogVersion() {
2932
return binlogVersion;
@@ -57,6 +60,14 @@ public int getDataLength() {
5760
return dataLength;
5861
}
5962

63+
public ChecksumType getChecksumType() {
64+
return checksumType;
65+
}
66+
67+
public void setChecksumType(ChecksumType checksumType) {
68+
this.checksumType = checksumType;
69+
}
70+
6071
@Override
6172
public String toString() {
6273
final StringBuilder sb = new StringBuilder();
@@ -65,6 +76,7 @@ public String toString() {
6576
sb.append(", serverVersion='").append(serverVersion).append('\'');
6677
sb.append(", headerLength=").append(headerLength);
6778
sb.append(", dataLength=").append(dataLength);
79+
sb.append(", checksumType=").append(checksumType);
6880
sb.append('}');
6981
return sb.toString();
7082
}

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,7 @@ private EventData deserializeFormatDescriptionEventData(ByteArrayInputStream inp
253253
} else {
254254
formatDescriptionEvent = (FormatDescriptionEventData) eventData;
255255
}
256-
int checksumBlockLength = eventBodyLength - formatDescriptionEvent.getDataLength();
257-
if (checksumBlockLength > 0) {
258-
inputStream.skip(inputStream.available() - checksumBlockLength);
259-
int checksumType = inputStream.read();
260-
checksumLength = ChecksumType.byOrdinal(checksumType).getLength();
261-
}
256+
checksumLength = formatDescriptionEvent.getChecksumType().getLength();
262257
} finally {
263258
inputStream.skipToTheEndOfTheBlock();
264259
}

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/FormatDescriptionEventDataDeserializer.java

+6
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ public class FormatDescriptionEventDataDeserializer implements EventDataDeserial
2929

3030
@Override
3131
public FormatDescriptionEventData deserialize(ByteArrayInputStream inputStream) throws IOException {
32+
int eventBodyLength = inputStream.available();
3233
FormatDescriptionEventData eventData = new FormatDescriptionEventData();
3334
eventData.setBinlogVersion(inputStream.readInteger(2));
3435
eventData.setServerVersion(inputStream.readString(50).trim());
3536
inputStream.skip(4); // redundant, present in a header
3637
eventData.setHeaderLength(inputStream.readInteger(1));
3738
inputStream.skip(EventType.FORMAT_DESCRIPTION.ordinal() - 1);
3839
eventData.setDataLength(inputStream.readInteger(1));
40+
int checksumBlockLength = eventBodyLength - eventData.getDataLength();
41+
if (checksumBlockLength > 0) {
42+
inputStream.skip(inputStream.available() - checksumBlockLength);
43+
eventData.setChecksumType(ChecksumType.byOrdinal(inputStream.read()));
44+
}
3945
return eventData;
4046
}
4147
}

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogFileReaderIntegrationTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public void testChecksumCRC32() throws Exception {
6161
readAll(reader, 303);
6262
}
6363

64+
@Test
65+
public void testChecksumCRC32WithCustomEventDataDeserializer() throws Exception {
66+
EventDeserializer eventDeserializer = new EventDeserializer();
67+
eventDeserializer.setEventDataDeserializer(EventType.FORMAT_DESCRIPTION, new NullEventDataDeserializer());
68+
BinaryLogFileReader reader = new BinaryLogFileReader(
69+
new FileInputStream("src/test/resources/mysql-bin.checksum-crc32"), eventDeserializer);
70+
readAll(reader, 303);
71+
}
72+
6473
private void readAll(BinaryLogFileReader reader, int expect) throws IOException {
6574
try {
6675
int numberOfEvents = 0;

0 commit comments

Comments
 (0)