Package dev.relism.flash.api.multipart
Class Multipart
java.lang.Object
dev.relism.flash.api.multipart.Multipart
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 Summary
-
Method Details
-
of
Creates a parser forrequest. Usesbody.stream()— zero heap allocation.- Throws:
IllegalArgumentException- if the request is not multipart or boundary is missingIOException- if the initial stream read fails
-
field
Returns the text value of the first field namedname, ornull. Scans forward; text parts encountered along the way are buffered, file bodies are drained.- Throws:
IOException
-
file
Returns the first file part namedname, ornull. 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
Returns all parts namednamein declaration order. Forces a full scan; all file bodies are materialised into heap.- Throws:
IOException
-
parts
Returns all parts in declaration order. Forces a full scan; all file bodies are materialised into heap.- Throws:
IOException
-