Skip to content

Commit 343e844

Browse files
committed
shyiko#14 - Make socket i/s buffering configurable
1 parent 24d2588 commit 343e844

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.github.shyiko.mysql.binlog.event.deserialization.GtidEventDataDeserializer;
3131
import com.github.shyiko.mysql.binlog.event.deserialization.QueryEventDataDeserializer;
3232
import com.github.shyiko.mysql.binlog.event.deserialization.RotateEventDataDeserializer;
33+
import com.github.shyiko.mysql.binlog.io.BufferedSocketInputStream;
3334
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
3435
import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientMXBean;
3536
import com.github.shyiko.mysql.binlog.network.AuthenticationException;
@@ -49,6 +50,7 @@
4950

5051
import java.io.EOFException;
5152
import java.io.IOException;
53+
import java.io.InputStream;
5254
import java.net.InetSocketAddress;
5355
import java.net.Socket;
5456
import java.net.SocketException;
@@ -76,6 +78,23 @@
7678
*/
7779
public class BinaryLogClient implements BinaryLogClientMXBean {
7880

81+
private static final SocketFactory DEFAULT_SOCKET_FACTORY = new SocketFactory() {
82+
83+
@Override
84+
public Socket createSocket() throws SocketException {
85+
return new Socket() {
86+
87+
private InputStream inputStream;
88+
89+
@Override
90+
public synchronized InputStream getInputStream() throws IOException {
91+
return inputStream != null ? inputStream :
92+
(inputStream = new BufferedSocketInputStream(super.getInputStream()));
93+
}
94+
};
95+
}
96+
};
97+
7998
private final Logger logger = Logger.getLogger(getClass().getName());
8099

81100
private final String hostname;
@@ -322,7 +341,7 @@ public void setEventDeserializer(EventDeserializer eventDeserializer) {
322341
}
323342

324343
/**
325-
* @param socketFactory custom socket factory. If not provided, socket will be created with "new Socket()".
344+
* @param socketFactory custom socket factory
326345
*/
327346
public void setSocketFactory(SocketFactory socketFactory) {
328347
this.socketFactory = socketFactory;
@@ -402,7 +421,8 @@ private void reset() {
402421

403422
private void establishConnection() throws IOException {
404423
try {
405-
Socket socket = socketFactory != null ? socketFactory.createSocket() : new Socket();
424+
SocketFactory socketFactory = this.socketFactory != null ? this.socketFactory : DEFAULT_SOCKET_FACTORY;
425+
Socket socket = socketFactory.createSocket();
406426
socket.connect(new InetSocketAddress(hostname, port));
407427
channel = new PacketChannel(socket);
408428
if (channel.getInputStream().peek() == -1) {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.github.shyiko.mysql.binlog.network.protocol;
1717

18-
import com.github.shyiko.mysql.binlog.io.BufferedSocketInputStream;
1918
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
2019
import com.github.shyiko.mysql.binlog.io.ByteArrayOutputStream;
2120
import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateCommand;
@@ -40,7 +39,7 @@ public PacketChannel(String hostname, int port) throws IOException {
4039

4140
public PacketChannel(Socket socket) throws IOException {
4241
this.socket = socket;
43-
this.inputStream = new ByteArrayInputStream(new BufferedSocketInputStream(socket.getInputStream()));
42+
this.inputStream = new ByteArrayInputStream(socket.getInputStream());
4443
this.outputStream = new ByteArrayOutputStream(socket.getOutputStream());
4544
}
4645

0 commit comments

Comments
 (0)