use of org.xnio.FinishedIoFuture in project undertow by undertow-io.
the class BlockingWebSocketHttpServerExchange method readRequestData.
@Override
public IoFuture<byte[]> readRequestData() {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
byte[] buf = new byte[1024];
int r;
while ((r = in.read(buf)) != -1) {
data.write(buf, 0, r);
}
return new FinishedIoFuture<>(data.toByteArray());
} catch (IOException e) {
final FutureResult<byte[]> ioFuture = new FutureResult<>();
ioFuture.setException(e);
return ioFuture.getIoFuture();
}
}
use of org.xnio.FinishedIoFuture in project undertow by undertow-io.
the class AsyncWebSocketHttpServerExchange method readRequestData.
@Override
public IoFuture<byte[]> readRequestData() {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
final ByteBuffer buffer = pooled.getBuffer();
final StreamSourceChannel channel = exchange.getRequestChannel();
int res;
for (; ; ) {
try {
res = channel.read(buffer);
if (res == -1) {
return new FinishedIoFuture<>(data.toByteArray());
} else if (res == 0) {
// callback
final FutureResult<byte[]> future = new FutureResult<>();
channel.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
@Override
public void handleEvent(final StreamSourceChannel channel) {
int res;
try {
res = channel.read(buffer);
if (res == -1) {
future.setResult(data.toByteArray());
channel.suspendReads();
return;
} else if (res == 0) {
return;
} else {
buffer.flip();
while (buffer.hasRemaining()) {
data.write(buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
future.setException(e);
}
}
});
channel.resumeReads();
return future.getIoFuture();
} else {
buffer.flip();
while (buffer.hasRemaining()) {
data.write(buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
final FutureResult<byte[]> future = new FutureResult<>();
future.setException(e);
return future.getIoFuture();
}
}
}
use of org.xnio.FinishedIoFuture in project undertow by undertow-io.
the class ServletWebSocketHttpExchange method readRequestData.
@Override
public IoFuture<byte[]> readRequestData() {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
final ServletInputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int r;
while ((r = in.read(buf)) != -1) {
data.write(buf, 0, r);
}
return new FinishedIoFuture<>(data.toByteArray());
} catch (IOException e) {
final FutureResult<byte[]> ioFuture = new FutureResult<>();
ioFuture.setException(e);
return ioFuture.getIoFuture();
}
}
Aggregations