Skip to content

Commit 0c685a5

Browse files
committed
Fixed BufferedSocketInputStream::read(byte[], ...) reading "past the buffer"
1 parent 89d64eb commit 0c685a5

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/main/java/com/github/shyiko/mysql/binlog/io/BufferedSocketInputStream.java

+15
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ public int read() throws IOException {
5252
return limit != -1 ? buffer[offset++] & 0xff : -1;
5353
}
5454

55+
@Override
56+
public int read(byte[] b, int off, int len) throws IOException {
57+
if (offset >= limit) {
58+
if (len >= buffer.length) {
59+
return in.read(b, off, len);
60+
}
61+
offset = 0;
62+
limit = in.read(buffer, 0, buffer.length);
63+
}
64+
int bytesRemainingInBuffer = Math.min(len, limit - offset);
65+
System.arraycopy(buffer, offset, b, off, bytesRemainingInBuffer);
66+
offset += bytesRemainingInBuffer;
67+
return bytesRemainingInBuffer;
68+
}
69+
5570
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2015 Stanley Shyiko
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.shyiko.mysql.binlog.io;
17+
18+
import org.testng.annotations.Test;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.util.Arrays;
22+
23+
import static org.testng.Assert.assertEquals;
24+
25+
/**
26+
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
27+
*/
28+
public class BufferedSocketInputStreamTest {
29+
30+
@Test
31+
public void testCorrectness() throws Exception {
32+
BufferedSocketInputStream in = new BufferedSocketInputStream(new ByteArrayInputStream(new byte[]{
33+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), 5);
34+
assertEquals(in.read(), 0);
35+
assertEquals(in.read(), 1);
36+
byte[] buf = new byte[6];
37+
assertEquals(in.read(buf, 0, buf.length), 3); // data remaining in BSIS buffer
38+
assertEquals(Arrays.copyOf(buf, 3), new byte[] {2, 3, 4});
39+
assertEquals(in.read(buf, 0, buf.length), 6);
40+
assertEquals(buf, new byte[] {5, 6, 7, 8, 9, 10});
41+
assertEquals(in.read(buf, 0, 3), 3);
42+
assertEquals(Arrays.copyOf(buf, 3), new byte[] {11, 12, 13});
43+
assertEquals(in.read(buf, 0, 3), 2); // data remaining in BSIS buffer
44+
assertEquals(Arrays.copyOf(buf, 2), new byte[] {14, 15});
45+
assertEquals(in.read(), 16);
46+
assertEquals(in.read(), (byte) -1);
47+
}
48+
}

0 commit comments

Comments
 (0)