marți, 28 aprilie 2015

[OmniFaces utilities (2.0)] Stream the given input to the given output by NIO ByteBuffer


[OmniFaces utilities] The stream() method stream the given input to the given output by NIO ByteBuffer. Both the input and output streams will implicitly be closed after streaming, regardless of whether an exception is been thrown or not.

Method:
Usage:

Suppose we have a file as an input stream and we want to pass it to an output stream for further processing. For this, we can use the Utils#stream() method:

import java.io.InputStream;
import java.io.OutputStream;
import org.omnifaces.util.Utils;
...
InputStream inputStream;
OutputStream outputStream;
try {
    inputStream = new FileInputStream("input_file");
    outputStream = new FileOutputStream("output_file");

    long length = Utils.stream(inputStream, outputStream);
} catch (IOException ex) {
   //
}

By default, the Utils#stream() method returns the length of the written bytes.

Niciun comentariu:

Trimiteți un comentariu