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


duminică, 29 noiembrie 2015

[OmniFaces utilities 2.0/2.4] Format the given bytes to nearest 10^n with IEC binary unit (KiB, MiB, etc)

[OmniFaces utilities] The formatBytes() function formats the given bytes to nearest 10n with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction. For example:
  • 1023 bytes will appear as 1023 B
  • 1024 bytes will appear as 1.0 KiB
  • 500000 bytes will appear as 488.3 KiB
  • 1048576 bytes will appear as 1.0 MiB
. The format locale will be set to the one as obtained by Faces#getLocale().


OmniFaces 2.4OmniFaces 2.0
See also: Faces#getLocale()

Let's suppose that we have a map that contains several files names and their sizes in bytes, as below:

// in real cases, you may loop a folder to extract the files
private final Map<Long, String> fileMap
         = Collections.unmodifiableMap(new HashMap<Long, String>() {
  put(13523424L, "flowers.png");
  put(2343L, "script.txt");
  put(343L, "");
  put(4565652L, "paint.jpg");
  put(1024L, "fire.jpg");

public Map<Long, String> getFileMap() {
 return fileMap;

Now, we can display a table containing these files names and sizes. The sizes are formatted via of:formatBytes():

<h:dataTable value="#{fileBean.fileMap.entrySet()}" var="file">
  <f:facet name="header">File</f:facet>
  <f:facet name="header">Size</f:facet>


Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors