[OmniFaces utilities] The
stream()
method stream a specified range of the given file to the given output via NIO Channels
and a directly allocated NIO ByteBuffer
. The output stream will only implicitly be closed after streaming when the specified range
represents the whole file, regardless of whether an exception is been thrown or not.Method:
See also: Utils#stream()
Usage:
Let's suppose that we have in /resources/default/images folder the picture MasteringOmniFaces.jpg
image from the left, and we want to copy in a temporary file only the part highlighted
via the red rectangle:
For this we can use the Utils#stream()
method as below:
public void
cropPicture() throws IOException{
Path path =
Paths.get((Faces.getServletContext()).getRealPath("/resources/default/images/MasteringOmniFaces.jpg"));
Path tmp =
Files.createTempFile("copy_", null);
OutputStream outputStream =
Files.newOutputStream(tmp);
Utils.stream(path.toFile(), outputStream, 0,
16000);
// or, half of the picture size
// Utils.stream(path.toFile(), outputStream, 0,
(Files.readAttributes(path, BasicFileAttributes.class)).size()/2);
}
This will produce in your temporary folder (e.g. /temp) a file as copy_1935333494585132160.tmp.
Niciun comentariu :
Trimiteți un comentariu