My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 25 octombrie 2015

[OmniFaces utilities (2.2)] Stream a specified range of the given file to the given output via NIO


[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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors