After writing out some processed content to an output stream, I need to revisit the beginning of the stream and write out some content metadata. The data I’m writing is very large, as much as 4Gb, and may be written either directly to a file or to an in-memory buffer, depending on various environmental factors.
How can I implement an OutputStream that allows me to write out headers after completing the writing of content?
Here’s a random access file output stream.
Note that if using it for a large amount of streamed output you can temporarily wrap it in a BufferedOutputStream to avoid lots of small writes (just be very sure to flush it before discarding the wrapper or using the underlying stream directly).