Skip to content

Commit

Permalink
Fix craigwblake#129 - fill buffer completely in read before calling c…
Browse files Browse the repository at this point in the history
…onsumers.
  • Loading branch information
danf committed Jan 17, 2018
1 parent 6b3fc4b commit c8db435
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/org/redline_rpm/ReadableChannelWrapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.redline_rpm;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

Expand All @@ -23,19 +24,28 @@ public ReadableChannelWrapper( final ReadableByteChannel channel) {
*
* @param buffer the buffer to read into
* @return the number of bytes read from the underlying channel
* @throws IOException if an IO error occurrs
* @throws IOException if an IO error occurs
*/
public int read( final ByteBuffer buffer) throws IOException {
final int read = channel.read( buffer);
for ( Consumer< ?> consumer : consumers.values()) consumer.consume(( ByteBuffer) buffer.duplicate().flip());
return read;
int total = 0;
while (buffer.hasRemaining()) {
int read;
if ((read = channel.read(buffer)) == -1) {
throw new BufferUnderflowException();
}
total += read;
}
for (Consumer consumer : consumers.values()) {
consumer.consume(( ByteBuffer) buffer.duplicate().flip());
}
return total;
}

/**
* Close the underlying read channel and complete any operations in the
* consumer.
*
* @throws IOException if an IO error occurrs
* @throws IOException if an IO error occurs
*/
public void close() throws IOException {
channel.close();
Expand Down

0 comments on commit c8db435

Please sign in to comment.