How to use EasyStream classes

This is a definition of the current problem you come here for:

  • A function (usually in an external library) produces some data, and wants to put them on an OutputStream
  • Another function wants to process the previous data, and expect to read them from an InputStream
   OutputStream out = ?; //what to put here?
   InputStream in = ?;

   LibraryClass1.writeDataToTheOutputStream(out);
   ??? // 'convert' out to in
   LibraryClass2.processDataFromInputStream(in);

This is the snippet of code that solves the problem using EasyStream:

final OutputStreamToInputStream<Void> out = new OutputStreamToInputStream<Void>() {
    @Override
    protected Void doRead(final InputStream istream) throws Exception {
          /*
           * Read the data from the InputStream "istream" passed as parameter. 
           */
           LibraryClass2.processDataFromInputStream(in);
           return null;
        }
    };
try {   
     LibraryClass1.writeDataToTheOutputStream(out);
} finally {
     // don't miss the close (or a thread would not terminate correctly).
     out.close();
}

For more options and use cases you can have a look to the reference guide.

Reference guide Next