Stop Boundary I/O Streams Released!

The library for boundary I/O streams brought the possibility to work with multiple stream written in a single stream.
But what if the stream continues further without containing any more sub-stream? In this case we can use the new released Stop Boundary Stream feature from the same library to ignore the rest of the stream after a boundary was reached.


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

To stop consuming a stream after a boundary was reached it is possible to use the StopBoundaryInputStream class and the convenience class StopBoundaryOutputStream to generate such a stream. The stop boundary streaming is still using a boundary to separate sub-streams, but when a stop boundary occurs the rest of the input stream is ignored.

Let's take a look at a simple example: 

String[] values = {
    "abcde", "ABCDE", "12345"
};

byte[] boundary = "|".getBytes();
byte[] stopBoundary = "#".getBytes();

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

StopBoundaryOutputStream out = new StopBoundaryOutputStream(bytes, boundary, stopBoundary);

for (String s : values) {
    out.write(s.getBytes());
    out.boundary();
}
out.stopBoundary();
out.write("xyz".getBytes());    // some junk at the end

//System.out.println(bytes);    // prints `abcde|ABCDE|12345|#xyz`

StopBoundaryInputStream in = new StopBoundaryInputStream(
        new ByteArrayInputStream(bytes.toByteArray()), boundary, stopBoundary);

for (InputStream is : in) {
    int b;
    while ((b = is.read()) != -1) {
        System.out.print((char) b);
    }
    System.out.println();
}

// close streams...

The code above prints:

abcde
ABCDE
12345

Previous post about the library: Boundary I/O Streams Released!

All about: https://github.com/ttulka/boundary-io-streams

Happy streaming!