diff --git a/src/main/java/teetime/stage/io/AbstractTcpReader.java b/src/main/java/teetime/stage/io/AbstractTcpReader.java index 5df93077510670c9ddbcd1efd93af6cb70ffdcd4..2b31e029caefce07a6a99eaa0aa37d22ae2c5d6e 100644 --- a/src/main/java/teetime/stage/io/AbstractTcpReader.java +++ b/src/main/java/teetime/stage/io/AbstractTcpReader.java @@ -57,21 +57,28 @@ public abstract class AbstractTcpReader<T> extends AbstractProducerStage<T> { try { while (buffer.hasRemaining()) { buffer.mark(); - this.read(buffer); + boolean success = this.read(buffer); + if (!success) { + buffer.reset(); + buffer.compact(); + return; + } } buffer.clear(); } catch (final BufferUnderflowException ex) { + logger.warn("Unexpected exception. Resetting and compacting buffer.", ex); buffer.reset(); buffer.compact(); } } /** - * Important note: Do not catch {@link BufferUnderflowException}s since they are caught by the caller to automatically fill the buffer with new content. - * * @param buffer * to be read from + * @return <ul> + * <li><code>true</code> when there were enough bytes to perform the read operation + * <li><code>false</code> otherwise. In this case, the buffer is reset, compacted, and filled with new content. */ - protected abstract void read(final ByteBuffer buffer); + protected abstract boolean read(final ByteBuffer buffer); }