public void completed(final ClientExchange exchange) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
exchange.setResponseListener(new ClientCallback<ClientExchange>() {
@Override
public void completed(final ClientExchange exchange) {
StreamSourceChannel source = exchange.getResponseChannel();
final ByteBuffer buffer = ByteBuffer.wrap(new byte[1024]);
for(;;) {
try {
int res = source.read(buffer);
if(res == -1) {
handleDone(exchange, out);
return;
} else if(res == 0) {
source.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
@Override
public void handleEvent(StreamSourceChannel channel) {
for(;;) {
try {
int res = channel.read(buffer);
if (res == -1) {
handleDone(exchange, out);
return;
} else if (res == 0) {
return;
} else {
buffer.flip();
out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.arrayOffset() + buffer.limit());
buffer.clear();
}
} catch (IOException e) {
result.setException(e);
}
}
}
});
source.resumeReads();
return;
} else {
buffer.flip();
out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.arrayOffset() + buffer.limit());
buffer.clear();