FileDocCategorySizeDatePackage
POIFSDocument.javaAPI DocApache Poi 3.0.118553Mon Jan 01 12:39:34 GMT 2007org.apache.poi.poifs.filesystem

POIFSDocument

public class POIFSDocument extends Object implements BlockWritable, POIFSViewable, BATManaged
This class manages a document in the POIFS filesystem.
author
Marc Johnson (mjohnson at apache dot org)

Fields Summary
private DocumentProperty
_property
private int
_size
private SmallBlockStore
_small_store
private BigBlockStore
_big_store
Constructors Summary
public POIFSDocument(String name, RawDataBlock[] blocks, int length)
Constructor from large blocks

param
name the name of the POIFSDocument
param
blocks the big blocks making up the POIFSDocument
param
length the actual length of the POIFSDocument
exception
IOException

        _size        = length;
        _big_store   = new BigBlockStore(blocks);
        _property    = new DocumentProperty(name, _size);
        _small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
        _property.setDocument(this);
    
public POIFSDocument(String name, SmallDocumentBlock[] blocks, int length)
Constructor from small blocks

param
name the name of the POIFSDocument
param
blocks the small blocks making up the POIFSDocument
param
length the actual length of the POIFSDocument

        _size = length;
        try
        {
            _big_store = new BigBlockStore(new RawDataBlock[ 0 ]);
        }
        catch (IOException ignored)
        {

            // can't happen with that constructor
        }
        _property    = new DocumentProperty(name, _size);
        _small_store = new SmallBlockStore(blocks);
        _property.setDocument(this);
    
public POIFSDocument(String name, ListManagedBlock[] blocks, int length)
Constructor from small blocks

param
name the name of the POIFSDocument
param
blocks the small blocks making up the POIFSDocument
param
length the actual length of the POIFSDocument
exception
IOException

        _size     = length;
        _property = new DocumentProperty(name, _size);
        _property.setDocument(this);
        if (Property.isSmall(_size))
        {
            _big_store   = new BigBlockStore(new RawDataBlock[ 0 ]);
            _small_store = new SmallBlockStore(blocks);
        }
        else
        {
            _big_store   = new BigBlockStore(blocks);
            _small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
        }
    
public POIFSDocument(String name, InputStream stream)
Constructor

param
name the name of the POIFSDocument
param
stream the InputStream we read data from
exception
IOException thrown on read errors

        List blocks = new ArrayList();

        _size = 0;
        while (true)
        {
            DocumentBlock block     = new DocumentBlock(stream);
            int           blockSize = block.size();

            if (blockSize > 0)
            {
                blocks.add(block);
                _size += blockSize;
            }
            if (block.partiallyRead())
            {
                break;
            }
        }
        DocumentBlock[] bigBlocks =
            ( DocumentBlock [] ) blocks.toArray(new DocumentBlock[ 0 ]);

        _big_store = new BigBlockStore(bigBlocks);
        _property  = new DocumentProperty(name, _size);
        _property.setDocument(this);
        if (_property.shouldUseSmallBlocks())
        {
            _small_store =
                new SmallBlockStore(SmallDocumentBlock.convert(bigBlocks,
                    _size));
            _big_store   = new BigBlockStore(new DocumentBlock[ 0 ]);
        }
        else
        {
            _small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
        }
    
public POIFSDocument(String name, int size, POIFSDocumentPath path, POIFSWriterListener writer)
Constructor

param
name the name of the POIFSDocument
param
size the length of the POIFSDocument
param
path the path of the POIFSDocument
param
writer the writer who will eventually write the document contents
exception
IOException thrown on read errors

        _size     = size;
        _property = new DocumentProperty(name, _size);
        _property.setDocument(this);
        if (_property.shouldUseSmallBlocks())
        {
            _small_store = new SmallBlockStore(path, name, size, writer);
            _big_store   = new BigBlockStore(new Object[ 0 ]);
        }
        else
        {
            _small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
            _big_store   = new BigBlockStore(path, name, size, writer);
        }
    
Methods Summary
public intcountBlocks()
Return the number of BigBlock's this instance uses

return
count of BigBlock instances

        return _big_store.countBlocks();
    
org.apache.poi.poifs.property.DocumentPropertygetDocumentProperty()
Get the DocumentProperty

return
the instance's DocumentProperty

        return _property;
    
public java.lang.StringgetShortDescription()
Provides a short description of the object, to be used when a POIFSViewable object has not provided its contents.

return
short description

        StringBuffer buffer = new StringBuffer();

        buffer.append("Document: \"").append(_property.getName())
            .append("\"");
        buffer.append(" size = ").append(getSize());
        return buffer.toString();
    
public intgetSize()

return
size of the document

        return _size;
    
public org.apache.poi.poifs.storage.BlockWritable[]getSmallBlocks()
return the array of SmallDocumentBlocks used

return
array of SmallDocumentBlocks; may be empty, cannot be null

        return _small_store.getBlocks();
    
public java.lang.Object[]getViewableArray()
Get an array of objects, some of which may implement POIFSViewable

return
an array of Object; may not be null, but may be empty

        Object[] results = new Object[ 1 ];
        String   result;

        try
        {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            BlockWritable[]       blocks = null;

            if (_big_store.isValid())
            {
                blocks = _big_store.getBlocks();
            }
            else if (_small_store.isValid())
            {
                blocks = _small_store.getBlocks();
            }
            if (blocks != null)
            {
                for (int k = 0; k < blocks.length; k++)
                {
                    blocks[ k ].writeBlocks(output);
                }
                byte[] data = output.toByteArray();

                if (data.length > _property.getSize())
                {
                    byte[] tmp = new byte[ _property.getSize() ];

                    System.arraycopy(data, 0, tmp, 0, tmp.length);
                    data = tmp;
                }
                output = new ByteArrayOutputStream();
                HexDump.dump(data, 0, output, 0);
                result = output.toString();
            }
            else
            {
                result = "<NO DATA>";
            }
        }
        catch (IOException e)
        {
            result = e.getMessage();
        }
        results[ 0 ] = result;
        return results;
    
public java.util.IteratorgetViewableIterator()
Get an Iterator of objects, some of which may implement POIFSViewable

return
an Iterator; may not be null, but may have an empty back end store

        return Collections.EMPTY_LIST.iterator();
    
public booleanpreferArray()
Give viewers a hint as to whether to call getViewableArray or getViewableIterator

return
true if a viewer should call getViewableArray, false if a viewer should call getViewableIterator

        return true;
    
voidread(byte[] buffer, int offset)
read data from the internal stores

param
buffer the buffer to write to
param
offset the offset into our storage to read from

        if (_property.shouldUseSmallBlocks())
        {
            SmallDocumentBlock.read(_small_store.getBlocks(), buffer, offset);
        }
        else
        {
            DocumentBlock.read(_big_store.getBlocks(), buffer, offset);
        }
    
public voidsetStartBlock(int index)
Set the start block for this instance

param
index index into the array of blocks making up the filesystem

        _property.setStartBlock(index);
    
public voidwriteBlocks(java.io.OutputStream stream)
Write the storage to an OutputStream

param
stream the OutputStream to which the stored data should be written
exception
IOException on problems writing to the specified stream

        _big_store.writeBlocks(stream);