Package org.apache.avalon.excalibur.io

Utility code for IO operations.

See:
          Description

Class Summary
AndFileFilter Accepts a selection if it is acceptable to both of two FilenameFilters.
ClassLoaderObjectInputStream A special ObjectInputStream to handle highly transient classes hosted by Avalon components that are juggling many classloaders.
DirectoryFileFilter This filter accepts Files that are directories.
EndianUtil Utility code for dealing with different endian systems.
ExtensionFileFilter This filters files based on the extension (what the filename ends with).
FileUtil This class provides basic facilities for manipulating files and file paths.
InvertedFileFilter This takes a FilenameFilter as input and inverts the selection.
IOUtil General IO Stream manipulation.
OrFileFilter Accepts a selection if it is acceptable to either of two FilenameFilters.
PrefixFileFilter This filters filenames for a certain prefix.
SwappedDataInputStream DataInput for systems relying on little endian data formats.
 

Package org.apache.avalon.excalibur.io Description

Utility code for IO operations.

[Introduction] [FilenameFilters] [IO Utilities] [File Utilities] [Endian Utilities]

Introduction

The org.apache.avalon.excalibur.io package contains utility code for file- and stream-based IO operation. There are three main types of class:

FilenameFilters

The Java API defines an interface FilenameFilter, which is used to filter directory listings. This is commonly used in the File.list(java.io.FilenameFilter) method, and in java.awt.FileDialog.

There are three "primitive" FilenameFilters:

DirectoryFilterOnly accept directories
PrefixFileFilterFilter based on prefix
ExtensionFileFilterFilter based on extension

And there are three "boolean" FilenameFilters:

AndFileFilterAccept if two subfilters both accept
InvertedFileFilterAccept if a subfilter rejects
OrFileFilterAccept if either of two subfilters accepts

These boolean FilenameFilters can be nested, to allow arbitrary expressions. For example, here is how one could print all non-directory files in the current directory, starting with "A", and ending in ".java" or ".class":

File dir = new File(".");
String[] files = dir.list( new AndFileFilter(
      new AndFileFilter( 
        new PrefixFileFilter("A"),
        new OrFileFilter(
          new ExtensionFileFilter(".class"),
          new ExtensionFileFilter(".java")
          )
        ),
      new InvertedFileFilter(
        new DirectoryFileFilter()
        )
      )
    );
for ( int i=0; i<files.length; i++ )
{
  System.out.println(files[i]);
}
    

The org.apache.avalon.excalibur.io.IOUtil class

The IOUtil class contains a comprehensive set of static methods for copying from:

To:

As an example, consider the task of reading bytes from a URL, and printing them. This would typically done like this:

import java.net.URL;
import java.io.*;

public class ManualCopy {
  public static void main(String args[]) throws IOException {
    InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
    
      InputStreamReader inR = new InputStreamReader( in );
    BufferedReader buf = new BufferedReader( inR );
    String line;
    while ( ( line = buf.readLine() ) != null )
    {
      System.out.println( line );
    }
    
      in.close();
  }
}
   

With the IOUtil class, that could be done with:

import java.net.URL;
import java.io.*;
import org.apache.avalon.excalibur.io.IOUtil;

public class IOUtilCopy {
  public static void main(String args[]) throws IOException {
    InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
    System.out.println( IOUtil.toString( in ) );
      in.close();
  }   
}
    

In certain application domains, such IO operations are common, and this class can save a great deal of time.

For utility code such as this, flexibility and speed are of primary importance. In IOUtil, each kind of copy method has a variant which allows the buffer size to be set. For methods that convert bytes to chars, the encoding method may also be set.

The org.apache.avalon.excalibur.io.FileUtil class

The FileUtil class contains methods for retrieving different components of a file path (directory name, file base name, file extension), methods for copying Files to other files and directories, and methods for deleting and cleaning directories. For more information, see the class description

The 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:

For more information, see http://www.cs.umass.edu/~verts/cs32/endian.html.

Since:
4.0


Copyright © 2001 Apache Jakarta Project. All Rights Reserved.