FileDocCategorySizeDatePackage
ReaderWriter.javaAPI DocApache Poi 3.0.15824Mon Jan 01 12:39:44 GMT 2007org.apache.poi.poifs.filesystem

ReaderWriter

public class ReaderWriter extends Object implements POIFSWriterListener, POIFSReaderListener
Test (Proof of concept) program that employs the POIFSReaderListener and POIFSWriterListener interfaces
author
Marc Johnson (mjohnson at apache dot org)

Fields Summary
private POIFSFileSystem
filesystem
private DirectoryEntry
root
private Map
dataMap
Constructors Summary
ReaderWriter(POIFSFileSystem filesystem)
Constructor ReaderWriter

param
filesystem

        this.filesystem = filesystem;
        root            = this.filesystem.getRoot();
        dataMap         = new HashMap();
    
Methods Summary
public static voidmain(java.lang.String[] args)
Method main

param
args
exception
IOException

        if (args.length != 2)
        {
            System.err.println(
                "two arguments required: one input file name and one output file name");
        }
        else
        {
            POIFSReader     reader     = new POIFSReader();
            POIFSFileSystem filesystem = new POIFSFileSystem();

            reader.registerListener(new ReaderWriter(filesystem));
            FileInputStream istream = new FileInputStream(args[ 0 ]);

            reader.read(istream);
            istream.close();
            FileOutputStream ostream = new FileOutputStream(args[ 1 ]);

            filesystem.writeFilesystem(ostream);
            ostream.close();
        }
    
public voidprocessPOIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent event)
Process a POIFSReaderEvent that this listener had registered for

param
event the POIFSReaderEvent

        DocumentInputStream istream = event.getStream();
        POIFSDocumentPath   path    = event.getPath();
        String              name    = event.getName();

        try
        {
            int    size = istream.available();
            byte[] data = new byte[ istream.available() ];

            istream.read(data);
            DocumentDescriptor descriptor = new DocumentDescriptor(path,
                                                name);

            System.out.println("adding document: " + descriptor + " (" + size
                               + " bytes)");
            dataMap.put(descriptor, data);
            int            pathLength = path.length();
            DirectoryEntry entry      = root;

            for (int k = 0; k < path.length(); k++)
            {
                String componentName = path.getComponent(k);
                Entry  nextEntry     = null;

                try
                {
                    nextEntry = entry.getEntry(componentName);
                }
                catch (FileNotFoundException ignored)
                {
                    try
                    {
                        nextEntry = entry.createDirectory(componentName);
                    }
                    catch (IOException e)
                    {
                        System.out.println("Unable to create directory");
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
                entry = ( DirectoryEntry ) nextEntry;
            }
            entry.createDocument(name, size, this);
        }
        catch (IOException ignored)
        {
        }
    
public voidprocessPOIFSWriterEvent(org.apache.poi.poifs.filesystem.POIFSWriterEvent event)
Process a POIFSWriterEvent that this listener had registered for

param
event the POIFSWriterEvent

        try
        {
            DocumentDescriptor descriptor =
                new DocumentDescriptor(event.getPath(), event.getName());

            System.out.println("looking up document: " + descriptor + " ("
                               + event.getLimit() + " bytes)");
            event.getStream().write(( byte [] ) dataMap.get(descriptor));
        }
        catch (IOException e)
        {
            System.out.println("Unable to write document");
            e.printStackTrace();
            System.exit(1);
        }