Class Multipart

java.lang.Object
dev.relism.flash.api.multipart.Multipart

public final class Multipart extends Object
Lazy streaming multipart/form-data parser.

Reads from body.stream() — the request body is never fully materialised. Text fields are buffered eagerly on first encounter (they are small by definition). File part bodies are exposed as zero-copy InputStreams backed directly by the socket stream and must be consumed before the next call to any scan method.


 Multipart mp = Multipart.of(req);

 // Text fields — buffered eagerly, accessible in any call order
 String userId = mp.field("userId");
 String width  = mp.field("width");

 // File parts — zero-copy socket stream; consume before requesting the next file
 Part avatar = mp.file("avatar");
 Files.copy(avatar.stream(), destination);

 // Explicit materialization of a file body (opt-in heap allocation)
 byte[] data = mp.file("doc").materialize();

 // Collect everything at once — eagerly buffers all bodies (accept the memory cost)
 List<Part> files = mp.parts("files");
 

Scan ordering: field(java.lang.String) and file(java.lang.String) scan forward through the stream. Parts already passed cannot be re-read. Text fields encountered while scanning toward a file are buffered silently; file bodies encountered while scanning toward a text field are drained silently. Use parts() only if you need everything and accept full materialisation.

Thread safety: not thread-safe; one instance per request.

  • Method Details

    • of

      public static Multipart of(Request req) throws IOException
      Creates a parser for request. Uses body.stream() — zero heap allocation.
      Throws:
      IllegalArgumentException - if the request is not multipart or boundary is missing
      IOException - if the initial stream read fails
    • field

      public String field(String name) throws IOException
      Returns the text value of the first field named name, or null. Scans forward; text parts encountered along the way are buffered, file bodies are drained.
      Throws:
      IOException
    • file

      public Part file(String name) throws IOException
      Returns the first file part named name, or null. Scans forward; text parts encountered along the way are buffered, earlier file bodies are drained. The returned part's stream must be consumed before the next scan call.
      Throws:
      IOException
    • parts

      public List<Part> parts(String name) throws IOException
      Returns all parts named name in declaration order. Forces a full scan; all file bodies are materialised into heap.
      Throws:
      IOException
    • parts

      public List<Part> parts() throws IOException
      Returns all parts in declaration order. Forces a full scan; all file bodies are materialised into heap.
      Throws:
      IOException