https://github.com/pb-/streaming-multipart-parser
Java library for stream decoding multipart/form-data using constant memory and no disk I/O
https://github.com/pb-/streaming-multipart-parser
form-data multipart multipart-formdata streaming
Last synced: 7 months ago
JSON representation
Java library for stream decoding multipart/form-data using constant memory and no disk I/O
- Host: GitHub
- URL: https://github.com/pb-/streaming-multipart-parser
- Owner: pb-
- License: other
- Created: 2023-04-19T21:13:22.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-06T15:30:54.000Z (about 2 years ago)
- Last Synced: 2025-06-20T10:48:58.228Z (7 months ago)
- Topics: form-data, multipart, multipart-formdata, streaming
- Language: Java
- Homepage:
- Size: 69.3 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# streaming-multipart-parser
A tiny library for decoding `multipart/form-data` that uses constant memory and no disk I/O. No dependencies apart from JDK 11+.
Many libraries or frameworks to parse this content type will either put files into memory or store them on disk. In some cases neither is an acceptable option. This library effectively provides a standard `Iterator` of `InputStream`s so that each part can be (sequentially) processed as a stream.
## Getting it
* Group id: `dev.baecher.multipart`
* Artifact id: `streaming-multipart-parser`
* Version: `0.10.0`
## Release notes
### 0.10.0
* Added [BoundaryInputStream](src/main/java/dev/baecher/io/BoundaryInputStream.java) which gives low-level access to reading a stream until a boundary is hit. In a future release the multipart parser will use this primitive, but it is useful on its own. Includes some basic optimizations over the naive search algorithm.
### 0.9.7
Initial public release
## Usage example
Read all parts and print their file names, content types, and lengths.
```java
// The parser implements the `Iterator` interface and takes any InputStream
StreamingMultipartParser parser = new StreamingMultipartParser(someInputStream);
while (parser.hasNext()) {
StreamingMultipartParser.Part part = parser.next();
// A convenience method to parse the filename out of the Content-Disposition header
System.out.println(part.getHeaders().getFilename());
// Any header can be looked up case-insensitively by name, returning the raw value
System.out.println(part.getHeaders().getHeaderValue("content-type"));
// A standard java.io.InputStream of the body
System.out.println(part.getInputStream().transferTo(OutputStream.nullOutputStream()));
}
```
Note that you have to exhaust the stream of each part (until hitting EOF) before you can move on to the next part. If you want to ignore the body of the part, you can use a construction like in the example above.
## Limitations
* No thread safety guaranteed whatsoever.
* Searching for the part boundary (`Buffer.find()`) is using a very naive algorithm.