post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength()));
} finally {
fis.close();
}
} else if (request.getBodyGenerator() != null) {
Body body = request.getBodyGenerator().createBody();
try {
int length = (int) body.getContentLength();
if (length < 0) {
length = (int) request.getContentLength();
}
// TODO: This is suboptimal
if (length >= 0) {
post.setRequestHeader("Content-Length", String.valueOf(length));
// This is totally sub optimal
byte[] bytes = new byte[length];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
for (; ;) {
buffer.clear();
if (body.read(buffer) < 0) {
break;
}
}
post.setRequestEntity(new ByteArrayRequestEntity(bytes));
}
} finally {
try {
body.close();
} catch (IOException e) {
logger.warn("Failed to close request body: {}", e.getMessage(), e);
}
}
}