public class DataSocket extends Object
Socket WrapperThis class is a simple Socket wrapper that exposes the native Socket.getInputStream() and
Socket.getOutputStream() as a DataInputStream and DataOutputStream respectively.
This is useful when wanting to send data over a socket at a low-level (i.e. serialising / de-serialising Google Protobuf messages).
(c) 2014 - 2017 Sport Trades Ltd| Modifier and Type | Field and Description |
|---|---|
private static org.slf4j.Logger |
log |
private DataInputStream |
receiveStream |
private InetSocketAddress |
remoteSocketAddress |
private DataOutputStream |
sendStream |
private Socket |
socket
The socket that is wrapped by this class.
|
| Constructor and Description |
|---|
DataSocket(Socket socket)
Instantiates the wrapper object based on an existing
Socket |
| Modifier and Type | Method and Description |
|---|---|
void |
disconnect()
Disconnects the socket.
|
DataInputStream |
getReceiveStream() |
SocketAddress |
getRemoteSocketAddress() |
DataOutputStream |
getSendStream() |
private void |
initialiseIOStreams() |
Boolean |
isClosed() |
void |
reconnect()
This method will only attempt a reconnect on a disconnected socket (i.e.
|
private static final org.slf4j.Logger log
private final InetSocketAddress remoteSocketAddress
private Socket socket
private DataInputStream receiveStream
Socket.getInputStream()private DataOutputStream sendStream
Socket.getOutputStream()public DataSocket(Socket socket) throws DataSocketFailedToInitialiseException
SocketDataSocketFailedToInitialiseException - If the socket provided is null or if there
is any error when attempting to create the DataInputStream or DataOutputStream.initialiseIOStreams()public SocketAddress getRemoteSocketAddress()
Socket.getRemoteSocketAddress()public DataInputStream getReceiveStream()
Socket.getInputStream(),
DataInputStreampublic DataOutputStream getSendStream()
Socket.getOutputStream(),
DataOutputStreampublic Boolean isClosed()
true if the socket has been closed. This will only occur when the disconnect()
method is called (which in turn should be called when an IOException occurs on one of the
data streams). Unfortunately the state change of the socket isn't accurately reflected in the
Socket object.public void disconnect()
public void reconnect()
throws DataSocketFailedToInitialiseException
isClosed() returns true).
The socket that is created has the following properties set:
Socket.setKeepAlive(boolean) = trueDataSocketFailedToInitialiseException - If the socket cannot be connectedisClosed(),
initialiseIOStreams()private void initialiseIOStreams()
throws DataSocketFailedToInitialiseException
Copyright © 2017. All rights reserved.