package org.xnio.conduits;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.xnio.channels.StreamSinkChannel;
public final class BlockingStreamSourceConduit extends AbstractStreamSourceConduit<StreamSourceConduit> {
private boolean resumed;
public BlockingStreamSourceConduit(final StreamSourceConduit next) {
super(next);
}
public long transferTo(final long position, final long count, final FileChannel target) throws IOException {
if (resumed) return next.transferTo(position, count, target);
long res;
while ((res = next.transferTo(position, count, target)) == 0L) {
next.awaitReadable();
}
return res;
}
public long transferTo(final long count, final ByteBuffer throughBuffer, final StreamSinkChannel target) throws IOException {
if (resumed) return next.transferTo(count, throughBuffer, target);
long res;
while ((res = next.transferTo(count, throughBuffer, target)) == 0L && ! throughBuffer.hasRemaining()) {
next.awaitReadable();
}
return res;
}
public int read(final ByteBuffer dst) throws IOException {
if (resumed) return next.read(dst);
int res;
while ((res = next.read(dst)) == 0) {
next.awaitReadable();
}
return res;
}
public long read(final ByteBuffer[] dsts, final int offs, final int len) throws IOException {
if (resumed) return next.read(dsts, offs, len);
long res;
while ((res = next.read(dsts, offs, len)) == 0L) {
next.awaitReadable();
}
return res;
}
public void resumeReads() {
resumed = true;
next.resumeReads();
}
public void wakeupReads() {
resumed = true;
next.wakeupReads();
}
public void suspendReads() {
resumed = false;
next.suspendReads();
}
public boolean isReadResumed() {
return resumed;
}
}