Boundary I/O Streams Released!

New Java library for working with boundary I/O streams was released.
This library provides classes for processing multiple streams withing a single stream bounded by a specific boundary content.
Let's take a deeper look at its functionality...


First of all, put the Maven dependency into your pom.xml:

<dependency>
    <groupId>cz.net21.ttulka.io</groupId>
    <artifactId>boundary-io-streams</artifactId>
    <version>1.0.0</version>
</dependency> 

Consider a simple use-case: a files streaming service into the stdout.

public class FileStreamService {

    public static void main(String[] args) throws IOException {
        try (BoundaryOutputStream bos = new BoundaryOutputStream(System.out)) {

            for (String filename : args) {
                try (InputStream is = new FileInputStream(filename)) {

                    org.apache.commons.io.IOUtils.copy(is, bos);  // see https://commons.apache.org/io
                    bos.boundary();                               // separate the stream with the boundary
                }
            }
        }
    }
} 

When you run the service from the command line

java FileStreamService /data/text1.txt /data/text2.txt 

You get something similar like:

Content of the text1.txt-----StreamBoundary-----Content of the text2.txt-----StreamBoundary----- 

Notice that the class BoundaryOutputStream is nothing more than a convenient class, you don't need this class to create such a stream with standard Java I/O classes.

Reading a Multiple Stream 

For reading a multiple stream you can use the class BoundaryInputStream direct or wrap it into a convenient iterable class IterableBoundaryInputStream. We show the second approach:

InputStream in = callFileStreamingService();

try (BoundaryInputStream bis = new BoundaryInputStream(in)) {
    IterableBoundaryInputStream ibis = new IterableBoundaryInputStream(bis);

    for (InputStream is : ibis) {                
        // do something with the stream
    }
}

The method callFileStreamingService calls the service above and returns its stream as the input for the BoundaryInputStream.

More details at https://github.com/ttulka/boundary-io-streams.

Happy streaming!