int numBytesRead = client.read(buffer);
if (numBytesRead == -1) {
// No more bytes can be read from the channel
logger.debug("channel has reached end-of-stream");
ClientContext context = getContext(key, client);
context.closeConnection();
} else {
logger.trace("Reading " + numBytesRead + " bytes from socket");
// To read the bytes, flip the buffer
buffer.flip();
ClientContext context = getContext(key, client);
StringBuilder stringBuilder = context.getBuffer();
while (buffer.hasRemaining()) {
boolean hasEnded = filterUntilEndNode();
CharBuffer decoded = decoder.decode(filteredBuffer);
logger.trace("Reading: \n" + decoded);
stringBuilder.append(decoded);
if (hasEnded) {
endNode(stringBuilder, context);
}
}
}
} catch (IOException e) {
logger.debug("Exception reading from socket, closing socket: "+e.getMessage());
ClientContext context = getContext(key, client);
context.closeConnection();
// Rethrow exception as declared
throw (e);
}
}