if (GrizzlyAsyncHttpProvider.LOGGER.isDebugEnabled()) {
GrizzlyAsyncHttpProvider.LOGGER.debug("REQUEST(modified): contentLength={}, contentType={}",
new Object[] { requestPacket.getContentLength(), requestPacket.getContentType() });
}
final FeedableBodyGenerator generator = new FeedableBodyGenerator() {
@Override
public Body createBody() throws IOException {
return multipartBody;
}
};
generator.setFeeder(new FeedableBodyGenerator.BaseFeeder(generator) {
@Override
public void flush() throws IOException {
final Body bodyLocal = feedableBodyGenerator.createBody();
try {
final MemoryManager mm = ctx.getMemoryManager();
boolean last = false;
while (!last) {
Buffer buffer = mm.allocate(BodyHandler.MAX_CHUNK_SIZE);
buffer.allowBufferDispose(true);
final long readBytes = bodyLocal.read(buffer.toByteBuffer());
if (readBytes > 0) {
buffer.position((int) readBytes);
buffer.trim();
} else {
buffer.dispose();
if (readBytes < 0) {
last = true;
buffer = Buffers.EMPTY_BUFFER;
} else {
throw new IllegalStateException("MultipartBody unexpectedly returned 0 bytes available");
}
}
feed(buffer, last);
}
} finally {
if (bodyLocal != null) {
try {
bodyLocal.close();
} catch (IOException ignore) {
}
}
}
}
});
generator.initializeAsynchronousTransfer(ctx, requestPacket);
return false;
}