From 5e2b6ea4d84532b32464e1698131946c60e732ce Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Tue, 20 Jan 2015 06:01:35 +0100 Subject: [PATCH] changed buffer underflow behavior --- .../java/teetime/stage/io/AbstractTcpReader.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/teetime/stage/io/AbstractTcpReader.java b/src/main/java/teetime/stage/io/AbstractTcpReader.java index 5df93077..2b31e029 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); } -- GitLab