}
}
public Object readObject (Connection connection) throws IOException {
SocketChannel socketChannel = this.socketChannel;
if (socketChannel == null) throw new SocketException("Connection is closed.");
if (currentObjectLength == 0) {
// Read the length of the next object from the socket.
if (!IntSerializer.canRead(readBuffer, true)) {
readBuffer.compact();
int bytesRead = socketChannel.read(readBuffer);
readBuffer.flip();
if (bytesRead == -1) throw new SocketException("Connection is closed.");
lastReadTime = System.currentTimeMillis();
if (!IntSerializer.canRead(readBuffer, true)) return null;
}
currentObjectLength = IntSerializer.get(readBuffer, true);
if (currentObjectLength <= 0) throw new SerializationException("Invalid object length: " + currentObjectLength);
if (currentObjectLength > readBuffer.capacity())
throw new SerializationException("Unable to read object larger than read buffer: " + currentObjectLength);
}
int length = currentObjectLength;
if (readBuffer.remaining() < length) {
// Read the bytes for the next object from the socket.
readBuffer.compact();
int bytesRead = socketChannel.read(readBuffer);
readBuffer.flip();
if (bytesRead == -1) throw new SocketException("Connection is closed.");
lastReadTime = System.currentTimeMillis();
if (readBuffer.remaining() < length) return null;
}
currentObjectLength = 0;