Menu

Event Streams

If you want to use streams you often need observe the number of transferred bits. For example if you upload a file you can show a progress bar, thus a user can observe the upload process.
Happy Java Libraries implements EventOutputStream and EventInputStreams decorators, which can be used to decorate observed Stremas.
For example to decorate an InputStream and observe transferred bits you can use the code above:

ByteArrayInputStream is = createInputStream(length);
EventInputStream_1x2 eis = EventInputStream_1x2.of(is);

//register onRead event
eis.getOnReadEvent().add(new ActionListener_1x0<ActionEventAfter_1x0<Integer>>() {
    @Override
    public void actionPerformedImpl(ActionEventAfter_1x0<Integer> event) {
        Integer bitsNumber= event.getData();
        System.out.println(bitsNumber);
    }
} );

The EventInputStream is an alternative for ProgressMonitorInputStream

The observation of an OutputStream happens in almost the same manner, you need to decorate the OutputStream and register an ActionListener, where you can react on events which will be fired if any bits are transferred.

OutputStream os = new ByteArrayOutputStream();

int fireAfterByteNumber = 50;
int timeDalay = 5;

EventOutputStream_1x2 eos = EventOutputStream_1x2.of(os, fireAfterByteNumber, timeDalay);
eos.getOnWriteEvent().add(new ActionListener_1x0<ActionEventAfter_1x0<Integer>>() {
    @Override
    public void actionPerformedImpl(ActionEventAfter_1x0<Integer> event) {
        int totalNumberOfBits = event.getData();
        System.out.println(totalNumberOfBits);
    }
} );

See for examples in Example setion.