Skip to content

Commit 40c5f31

Browse files
authored
[Hotfix][MySQL-CDC] Fix ArrayIndexOutOfBoundsException in mysql binlog read (#7381)
1 parent 464da8f commit 40c5f31

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.github.shyiko.mysql.binlog.io;
18+
19+
import java.io.FilterInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
/**
24+
* Copied from https://github.com/osheroff/mysql-binlog-connector-java project to fix
25+
* https://github.com/apache/seatunnel/issues/7380
26+
*
27+
* <p>reference: - https://github.com/osheroff/mysql-binlog-connector-java/issues/66 -
28+
* https://github.com/apache/flink-cdc/issues/460
29+
*/
30+
public class BufferedSocketInputStream extends FilterInputStream {
31+
32+
private byte[] buffer;
33+
private int offset;
34+
private int limit;
35+
36+
public BufferedSocketInputStream(InputStream in) {
37+
this(in, 512 * 1024);
38+
}
39+
40+
public BufferedSocketInputStream(InputStream in, int bufferSize) {
41+
super(in);
42+
this.buffer = new byte[bufferSize];
43+
}
44+
45+
@Override
46+
public int available() throws IOException {
47+
return limit == -1 ? in.available() : limit - offset + in.available();
48+
}
49+
50+
@Override
51+
public int read() throws IOException {
52+
if (offset < limit) {
53+
return buffer[offset++] & 0xff;
54+
}
55+
offset = 0;
56+
limit = in.read(buffer, 0, buffer.length);
57+
return limit != -1 ? buffer[offset++] & 0xff : -1;
58+
}
59+
60+
@Override
61+
public int read(byte[] b, int off, int len) throws IOException {
62+
if (offset >= limit) {
63+
if (len >= buffer.length) {
64+
return in.read(b, off, len);
65+
}
66+
offset = 0;
67+
limit = in.read(buffer, 0, buffer.length);
68+
if (limit == -1) {
69+
return limit;
70+
}
71+
}
72+
int bytesRemainingInBuffer = Math.min(len, limit - offset);
73+
System.arraycopy(buffer, offset, b, off, bytesRemainingInBuffer);
74+
offset += bytesRemainingInBuffer;
75+
return bytesRemainingInBuffer;
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.github.shyiko.mysql.binlog.io;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Arrays;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
26+
public class BufferedSocketInputStreamTest {
27+
28+
@Test
29+
public void testReadFromBufferedSocketInputStream() throws Exception {
30+
BufferedSocketInputStream in =
31+
new BufferedSocketInputStream(
32+
new ByteArrayInputStream(
33+
new byte[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}));
34+
byte[] buf = new byte[3];
35+
assertEquals(3, in.read(buf, 0, buf.length));
36+
Arrays.equals(new byte[] {'A', 'B', 'C'}, buf);
37+
assertEquals(5, in.available());
38+
39+
assertEquals(3, in.read(buf, 0, buf.length));
40+
Arrays.equals(new byte[] {'D', 'E', 'F'}, buf);
41+
assertEquals(2, in.available());
42+
43+
assertEquals(2, in.read(buf, 0, buf.length));
44+
Arrays.equals(new byte[] {'G', 'H'}, buf);
45+
assertEquals(0, in.available());
46+
47+
// reach the end of stream normally
48+
assertEquals(-1, in.read(buf, 0, buf.length));
49+
assertEquals(0, in.available());
50+
}
51+
}

0 commit comments

Comments
 (0)