The Apache Commons Component Library

advertisement
The Apache Commons
Component Library
For Java
The Apache Commons components have gained worldwide notoriety for quality, simplicity and
foresight. The library leaves well alone what native JAVA does well. But when native JAVA drops the
ball – the Apache Commons Component library is right there ready to pick it up.
The three best facilities in areas of JAVA weaknesses are String Utilities, File Utilities and Executing
System Commands via a Command Line Interface.
Welcome to Apache Commons.
Components
Description
Latest
Version
Released
BCEL
Byte Code Engineering Library - analyze,
create, and manipulate Java class files
5.2
2007-0614
BeanUtils
Easy-to-use wrappers around the Java
reflection and introspection APIs.
1.9.2
2014-0529
BSF
Bean Scripting Framework - interface to
scripting languages, including JSR-223
3.1
2010-2406
Chain
Chain of Responsibility pattern
implemention.
1.2
2008-0602
CLI
Command Line arguments parser.
1.2
2009-03-
19
Codec
General encoding/decoding algorithms (for
example phonetic, base64, URL).
1.10
2014-1109
Collections
Extends or augments the Java Collections
Framework.
4.0
2013-1124
Compress
Defines an API for working with tar, zip
and bzip2 files.
1.9
2014-1009
Configuration
Reading of configuration/preferences files
in various formats.
2.0alpha2
2014-1220
CSV
Component for reading and writing comma
separated value files.
1.1
2014-1127
Daemon
Alternative invocation mechanism for unixdaemon-like java code.
1.0.15
2013-0304
DBCP
Database connection pooling services.
2.0.1
2014-0524
DbUtils
JDBC helper library.
1.6
2014-0720
Digester
XML-to-Java-object mapping utility.
3.2
2011-1213
Discovery
Tools for locating resources by mapping
service/reference names to resource names.
0.5
2011-0428
EL
Interpreter for the Expression Language
defined by the JSP 2.0 specification.
1.0
2003-0618
Email
Library for sending e-mail from Java.
1.3.3
2014-0711
Exec
API for dealing with external process
execution and environment management in
Java.
1.3
2014-1106
FileUpload
File upload capability for your servlets and
web applications.
1.3.1
2014-0207
Functor
A functor is a function that can be
manipulated as an object, or an object
representing a single, generic function.
N/A
N/A
Imaging
(previously
called
Sanselan)
A pure-Java image library.
N/A
N/A
IO
Collection of I/O utilities.
2.4
2012-0612
JCI
Java Compiler Interface
1.1
2013-1014
JCS
Java Caching System
1.3
2007-0605
Jelly
XML based scripting and processing
engine.
1.0
2005-0616
Jexl
Expression language which extends the
Expression Language of the JSTL.
2.1.1
2011-1224
JXPath
Utilities for manipulating Java Beans using
the XPath syntax.
1.3
2008-0814
Lang
Provides extra functionality for classes in
java.lang.
3.3.2
2014-0409
Launcher
Cross platform Java application launcher.
1.1
2004-0822
Logging
Wrapper around a variety of logging API
implementations.
1.2
2014-0711
Math
Lightweight, self-contained mathematics
and statistics components.
3.4
2014-1226
Modeler
Mechanisms to create Model MBeans
compatible with JMX specification.
2.0.1
2007-0625
Net
Collection of network utilities and protocol
implementations.
3.3
2013-0612
OGNL
An Object-Graph Navigation Language
N/A
N/A
Pool
Generic object pooling component.
2.3
2014-1230
Primitives
Smaller, faster and easier to work with
types supporting Java primitive types.
1.0
2003-1105
Proxy
Library for creating dynamic proxies.
1.0
2008-0228
SCXML
An implementation of the State Chart XML
specification aimed at creating and
maintaining a Java SCXML engine. It is
0.9
2008-1201
capable of executing a state machine
defined using a SCXML document, and
abstracts out the environment interfaces.
Validator
Framework to define validators and
validation rules in an xml file.
1.4.1
2014-0113
VFS
Virtual File System component for treating
files, FTP, SMB, ZIP and such like as a
single logical file system.
2.0
2011-0824
Weaver
Provides an easy way to enhance (weave)
compiled bytecode.
1.1
2014-0930
Utility classes
IOUtils
IOUtils contains utility methods dealing with reading, writing and copying. The methods work on InputStream,
OutputStream, Reader and Writer.
As an example, consider the task of reading bytes from a URL, and printing them. This would typically done like
this:
InputStream in = new URL( "http://commons.apache.org" ).openStream();
try {
InputStreamReader inR = new InputStreamReader( in );
BufferedReader buf = new BufferedReader( inR );
String line;
while ( ( line = buf.readLine() ) != null ) {
System.out.println( line );
}
} finally {
in.close();
}
With the IOUtils class, that could be done with:
InputStream in = new URL( "http://commons.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}
In certain application domains, such IO operations are common, and this class can save a great deal of time. And
you can rely on well-tested code.
For utility code such as this, flexibility and speed are of primary importance. However you should also understand
the limitations of this approach. Using the above technique to read a 1GB file would result in an attempt to create
a 1GB String object!
FileUtils
The FileUtils class contains utility methods for working with File objects. These include reading, writing, copying
and comparing files.
For example to read an entire file line by line you could use:
File file = new File("/commons/io/project.properties");
List lines = FileUtils.readLines(file, "UTF-8");
FilenameUtils
The FilenameUtils class contains utility methods for working with filenames without using File objects. The class
aims to be consistent between Unix and Windows, to aid transitions between these environments (such as
moving from development to production).
For example to normalize a filename removing double dot segments:
String filename = "C:/commons/io/../lang/project.xml";
String normalized = FilenameUtils.normalize(filename);
// result is "C:/commons/lang/project.xml"
FileSystemUtils
The FileSystemUtils class contains utility methods for working with the file system to access functionality not
supported by the JDK. Currently, the only method is to get the free space on a drive. Note that this uses the
command line, not native code.
For example to find the free space on a drive:
long freeSpace = FileSystemUtils.freeSpace("C:/");
Endian classes
Different computer architectures adopt different conventions for byte ordering. In so-called "Little Endian"
architectures (eg Intel), the low-order byte is stored in memory at the lowest address, and subsequent bytes at
higher addresses. For "Big Endian" architectures (eg Motorola), the situation is reversed.
There are two classes in this package of relevance:


The EndianUtils class contains static methods for swapping the Endian-ness of Java primitives and streams.
The SwappedDataInputStream class is an implementation of the DataInput interface. With this, one can
read data from files of non-native Endian-ness.
For more information, see http://www.cs.umass.edu/~verts/cs32/endian.html
Line iterator
The org.apache.commons.io.LineIterator class provides a flexible way for working with a line-based
file. An instance can be created directly, or via factory methods on FileUtils or IOUtils . The
recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
File filters
The org.apache.commons.io.filefilter package defines an interface (IOFileFilter) that combines
both java.io.FileFilter and java.io.FilenameFilter . Besides that the package offers a series of
ready-to-use implementations of the IOFileFilter interface including implementation that allow you to
combine other such filters. These filters can be used to list files or in FileDialog, for example.
See the filefilter package javadoc for more details.
File comparators
The org.apache.commons.io.comparator package provides a number
of java.util.Comparator implementations for java.io.File . These comparators can be used to sort
lists and arrays of files, for example.
See the comparator package javadoc for more details.
Streams
The org.apache.commons.io.input and org.apache.commons.io.output packages contain various
useful implementations of streams. These include:

Null output stream - that silently absorbs all data sent to it

Tee output stream - that sends output data to two streams instead of one

Byte array output stream - that is a faster version of the JDK class

Counting streams - that count the number of bytes passed


Proxy streams - that delegate to the correct method in the proxy
Lockable writer - that provides synchronization of writes using a lock file
See the input or output package javadoc for more details.
Download