public static String genBoundary(List<MultipartEntity> entities) {
return "----HttpKitFormBoundary" + System.currentTimeMillis();
}
public static ByteBuffer encode(String boundary, List<MultipartEntity> entities) throws IOException {
DynamicBytes bytes = new DynamicBytes(entities.size() * 1024);
for (MultipartEntity e : entities) {
bytes.append("--").append(boundary).append(HttpUtils.CR, HttpUtils.LF);
bytes.append("Content-Disposition: form-data; name=\"");
bytes.append(e.name, HttpUtils.UTF_8);
if (e.filename != null) {
bytes.append("\"; filename=\"").append(e.filename).append("\"\r\n");
} else {
bytes.append("\"\r\n");
}
if (e.content instanceof File || e.content instanceof InputStream) {
// TODO configurable
bytes.append("Content-Type: application/octet-stream\r\n\r\n");
} else {
bytes.append("\r\n");
}
if (e.content instanceof String) {
bytes.append((String) e.content, HttpUtils.UTF_8);
} else if (e.content instanceof InputStream) {
DynamicBytes b = HttpUtils.readAll((InputStream) e.content);
bytes.append(b.get(), b.length());
} else if (e.content instanceof File) {
byte[] b = HttpUtils.readContent((File) e.content, (int) ((File) e.content).length());
bytes.append(b, b.length);
} else if (e.content instanceof ByteBuffer) {
while (((ByteBuffer) e.content).hasRemaining()) {