Skip to content

Pipelined OutputStream implementation written in Java (splits producer and consumer into different threads)

License

Notifications You must be signed in to change notification settings

lukehutch/PipelinedOutputStream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

PipelinedOutputStream.java

Allows you to set up a producer/consumer relationship between the current thread (the producer / writer) and a new thread that writes data to an OutputStream (the consumer / writer). This is useful if you want to overlap (pipeline) two or more of the following:

  1. Data production / computation
  2. Data compression
  3. Writing data to disk

Data production and compression can be run on different cores; writing data to disk results in blocking waits, and the wait time can be overlapped with further data production and compression.

For example, the following allows you to create one new thread for gzipping data produced by produceAndWriteBuffer (with a 64MB buffer inserted between data production and the GZIPOutputStream), and another new thread for writing the gzipped data to disk (with a 32MB buffer inserted between the GZIPOutputStream and the FileOutputStream). This creates a chain of three threads, with producer-consumer relationships between the first and second thread, and between the second and third thread:

try (OutputStream os =
         new PipelinedOutputStream(64 * 1024 * 1024,
             new GZIPOutputStream(
                 new PipelinedOutputStream(32 * 1024 * 1024,
                     new FileOutputStream(outputFilename)))) {
    produceAndWriteData(os);
}

N.B. GZIPOutputStream is quite slow, so maybe consider using LZ4FrameOutputStream instead, if it's an option, otherwise the GZIPOutputStream stage of the pipeline can still end up being a major bottleneck.

See also: Producer

License

The MIT License (MIT)

Copyright (c) 2019 Luke Hutchison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Pipelined OutputStream implementation written in Java (splits producer and consumer into different threads)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages