package com.github.elopteryx.upload.internal.integration;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

import java.nio.ByteBuffer;

class RequestBuilder {

    private static final byte[] LINE_FEED = "\r\n".getBytes(ISO_8859_1);

    private final ByteBuffer buffer;

    private final byte[] boundary;

    static RequestBuilder newBuilder(String boundary) {
        return new RequestBuilder(boundary);
    }

    private RequestBuilder(String boundary) {
        this.buffer = ByteBuffer.allocate(300_000);
        this.boundary = boundary.getBytes(ISO_8859_1);
    }

    RequestBuilder addFormField(String name, String value) {
        buffer.put(LINE_FEED);
        buffer.put("--".getBytes(ISO_8859_1)).put(boundary).put(LINE_FEED);
        buffer.put("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1)).put(name.getBytes(ISO_8859_1)).put("\"".getBytes(ISO_8859_1)).put(LINE_FEED);
        buffer.put("Content-Type: text/plain; charset=".getBytes(ISO_8859_1)).put(ISO_8859_1.name().getBytes(ISO_8859_1)).put(LINE_FEED);
        buffer.put(LINE_FEED);
        buffer.put(value.getBytes(ISO_8859_1));

        return this;
    }

    RequestBuilder addFilePart(String fieldName, byte[] content, String contentType, String fileName) {
        buffer.put(LINE_FEED);
        buffer.put("--".getBytes(ISO_8859_1)).put(boundary).put(LINE_FEED);
        buffer.put("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1)).put(fieldName.getBytes(ISO_8859_1)).put("\"; filename=\"".getBytes(ISO_8859_1)).put(fileName.getBytes(ISO_8859_1)).put("\"".getBytes(ISO_8859_1)).put(LINE_FEED);
        buffer.put("Content-Type: ".getBytes(ISO_8859_1)).put(contentType.getBytes(ISO_8859_1)).put(LINE_FEED);
        buffer.put(LINE_FEED);
        buffer.put(content);

        return this;
    }

    ByteBuffer finish() {
        buffer.put(LINE_FEED);
        buffer.put("--".getBytes(ISO_8859_1)).put(boundary).put("--".getBytes(ISO_8859_1)).put(LINE_FEED);

        buffer.flip();
        return buffer;
    }

}

@@ -296,22 +296,22 @@
 e final 
-byte%5B%5D
+String
  boundar
@@ -552,37 +552,16 @@
 boundary
-.getBytes(ISO_8859_1)
 ;%0A    %7D%0A
@@ -664,36 +664,49 @@
      buffer.put(
+(
 %22--%22
+ + boundary)
 .getBytes(ISO_88
@@ -707,38 +707,40 @@
 ISO_8859_1))
-.put(boundary)
+;%0A        buffer
 .put(LINE_FE
@@ -755,32 +755,33 @@
      buffer.put(
+(
 %22Content-Disposi
@@ -809,70 +809,23 @@
 =%5C%22%22
-.getBytes(ISO_8859_1)).put(name.getBytes(ISO_8859_1)).put(
+ + name + 
 %22%5C%22%22
+)
 .get
@@ -834,32 +834,48 @@
 tes(ISO_8859_1))
+;%0A        buffer
 .put(LINE_FEED);
@@ -886,32 +886,33 @@
      buffer.put(
+(
 %22Content-Type: t
@@ -935,35 +935,11 @@
 et=%22
-.getBytes(ISO_8859_1)).put(
+ + 
 ISO_
@@ -951,16 +951,17 @@
 1.name()
+)
 .getByte
@@ -966,32 +966,48 @@
 tes(ISO_8859_1))
+;%0A        buffer
 .put(LINE_FEED);
@@ -1261,36 +1261,49 @@
      buffer.put(
+(
 %22--%22
+ + boundary)
 .getBytes(ISO_88
@@ -1304,38 +1304,40 @@
 ISO_8859_1))
-.put(boundary)
+;%0A        buffer
 .put(LINE_FE
@@ -1352,32 +1352,33 @@
      buffer.put(
+(
 %22Content-Disposi
@@ -1406,154 +1406,59 @@
 =%5C%22%22
-.getBytes(ISO_8859_1)).put(fieldName.getBytes(ISO_8859_1)).put(%22%5C%22; filename=%5C%22%22.getBytes(ISO_8859_1)).put(fileName.getBytes(ISO_8859_1)).put(
+ + fieldName + %22%5C%22; filename=%5C%22%22 + fileName + 
 %22%5C%22%22
+)
 .get
@@ -1467,32 +1467,48 @@
 tes(ISO_8859_1))
+;%0A        buffer
 .put(LINE_FEED);
@@ -1527,16 +1527,17 @@
 fer.put(
+(
 %22Content
@@ -1548,35 +1548,11 @@
 e: %22
-.getBytes(ISO_8859_1)).put(
+ + 
 cont
@@ -1558,16 +1558,17 @@
 tentType
+)
 .getByte
@@ -1573,32 +1573,48 @@
 tes(ISO_8859_1))
+;%0A        buffer
 .put(LINE_FEED);
@@ -1783,47 +1783,24 @@
 put(
+(
 %22--%22
-.getBytes(ISO_8859_1)).put(
+ + 
 boundary
 ).pu
@@ -1799,18 +1799,16 @@
 dary
-).put(
+ + 
 %22--%22
+)
 .get
@@ -1825,16 +1825,32 @@
 8859_1))
+;%0A        buffer
 .put(LIN

Simplify the multipart request builder.