Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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