FileDocCategorySizeDatePackage
Util.javaAPI DocApache Poi 3.0.19026Mon Jan 01 12:39:44 GMT 2007org.apache.poi.hpsf.basic

Util

public class Util extends Object

Static utility methods needed by the HPSF test cases.

author
Rainer Klute (klute@rainer-klute.de)
since
2002-07-20
version
$Id: Util.java 489730 2006-12-22 19:18:16Z bayard $

Fields Summary
Constructors Summary
Methods Summary
public static voidcopy(java.io.InputStream in, java.io.OutputStream out)

Reads bytes from an input stream and writes them to an output stream until end of file is encountered.

param
in the input stream to read from
param
out the output stream to write to
exception
IOException if an I/O exception occurs

        final int BUF_SIZE = 1000;
        byte[] b = new byte[BUF_SIZE];
        int read;
        boolean eof = false;
        while (!eof)
        {
            try
            {
                read = in.read(b, 0, BUF_SIZE);
                if (read > 0)
                    out.write(b, 0, read);
                else
                    eof = true;
            }
            catch (EOFException ex)
            {
                eof = true;
            }
        }
    
public static voidprintSystemProperties()

Prints the system properties to System.out.

        final Properties p = System.getProperties();
        final List names = new LinkedList();
        for (Iterator i = p.keySet().iterator(); i.hasNext();)
            names.add(i.next());
        Collections.sort(names);
        for (final Iterator i = names.iterator(); i.hasNext();)
        {
            String name = (String) i.next();
            String value = (String) p.get(name);
            System.out.println(name + ": " + value);
        }
        System.out.println("Current directory: " +
                           System.getProperty("user.dir"));
    
public static POIFile[]readPOIFiles(java.io.File poiFs)

Reads all files from a POI filesystem and returns them as an array of {@link POIFile} instances. This method loads all files into memory and thus does not cope well with large POI filessystems.

param
poiFs The name of the POI filesystem as seen by the operating system. (This is the "filename".)
return
The POI files. The elements are ordered in the same way as the files in the POI filesystem.
exception
FileNotFoundException if the file containing the POI filesystem does not exist
exception
IOException if an I/O exception occurs

        return readPOIFiles(poiFs, null);
    
public static POIFile[]readPOIFiles(java.io.File poiFs, java.lang.String[] poiFiles)

Reads a set of files from a POI filesystem and returns them as an array of {@link POIFile} instances. This method loads all files into memory and thus does not cope well with large POI filessystems.

param
poiFs The name of the POI filesystem as seen by the operating system. (This is the "filename".)
param
poiFiles The names of the POI files to be read.
return
The POI files. The elements are ordered in the same way as the files in the POI filesystem.
exception
FileNotFoundException if the file containing the POI filesystem does not exist
exception
IOException if an I/O exception occurs

        final List files = new ArrayList();
        POIFSReader r = new POIFSReader();
        POIFSReaderListener pfl = new POIFSReaderListener()
        {
            public void processPOIFSReaderEvent(final POIFSReaderEvent event)
            {
                try
                {
                    final POIFile f = new POIFile();
                    f.setName(event.getName());
                    f.setPath(event.getPath());
                    final InputStream in = event.getStream();
                    final ByteArrayOutputStream out =
                        new ByteArrayOutputStream();
                    Util.copy(in, out);
                    out.close();
                    f.setBytes(out.toByteArray());
                    files.add(f);
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                    throw new RuntimeException(ex.getMessage());
                }
            }
        };
        if (poiFiles == null)
            /* Register the listener for all POI files. */
            r.registerListener(pfl);
        else
            /* Register the listener for the specified POI files
             * only. */
            for (int i = 0; i < poiFiles.length; i++)
                r.registerListener(pfl, poiFiles[i]);

        /* Read the POI filesystem. */
        r.read(new FileInputStream(poiFs));
        POIFile[] result = new POIFile[files.size()];
        for (int i = 0; i < result.length; i++)
            result[i] = (POIFile) files.get(i);
        return result;
    
public static POIFile[]readPropertySets(java.io.File poiFs)

Read all files from a POI filesystem which are property set streams and returns them as an array of {@link org.apache.poi.hpsf.PropertySet} instances.

param
poiFs The name of the POI filesystem as seen by the operating system. (This is the "filename".)
return
The property sets. The elements are ordered in the same way as the files in the POI filesystem.
exception
FileNotFoundException if the file containing the POI filesystem does not exist
exception
IOException if an I/O exception occurs

        final List files = new ArrayList(7);
        final POIFSReader r = new POIFSReader();
        POIFSReaderListener pfl = new POIFSReaderListener()
        {
            public void processPOIFSReaderEvent(final POIFSReaderEvent event)
            {
                try
                {
                    final POIFile f = new POIFile();
                    f.setName(event.getName());
                    f.setPath(event.getPath());
                    final InputStream in = event.getStream();
                    if (PropertySet.isPropertySetStream(in))
                    {
                        final ByteArrayOutputStream out =
                            new ByteArrayOutputStream();
                        Util.copy(in, out);
                        out.close();
                        f.setBytes(out.toByteArray());
                        files.add(f);
                    }
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    throw new RuntimeException(ex.getMessage());
                }
            }
        };

        /* Register the listener for all POI files. */
        r.registerListener(pfl);

        /* Read the POI filesystem. */
        r.read(new FileInputStream(poiFs));
        POIFile[] result = new POIFile[files.size()];
        for (int i = 0; i < result.length; i++)
            result[i] = (POIFile) files.get(i);
        return result;