Simplification and code clean-up
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@707551 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9d306f26d0
commit
f175e62e37
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
/* ====================================================================
|
/* ====================================================================
|
||||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
contributor license agreements. See the NOTICE file distributed with
|
contributor license agreements. See the NOTICE file distributed with
|
||||||
@ -16,20 +15,24 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
==================================================================== */
|
==================================================================== */
|
||||||
|
|
||||||
|
|
||||||
package org.apache.poi.poifs.filesystem;
|
package org.apache.poi.poifs.filesystem;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.poifs.common.POIFSConstants;
|
import org.apache.poi.poifs.common.POIFSConstants;
|
||||||
import org.apache.poi.poifs.dev.POIFSViewable;
|
import org.apache.poi.poifs.dev.POIFSViewable;
|
||||||
import org.apache.poi.poifs.property.DocumentProperty;
|
import org.apache.poi.poifs.property.DocumentProperty;
|
||||||
import org.apache.poi.poifs.property.Property;
|
import org.apache.poi.poifs.property.Property;
|
||||||
import org.apache.poi.poifs.storage.BlockWritable;
|
import org.apache.poi.poifs.storage.BlockWritable;
|
||||||
import org.apache.poi.poifs.storage.ListManagedBlock;
|
|
||||||
import org.apache.poi.poifs.storage.DocumentBlock;
|
import org.apache.poi.poifs.storage.DocumentBlock;
|
||||||
|
import org.apache.poi.poifs.storage.ListManagedBlock;
|
||||||
import org.apache.poi.poifs.storage.RawDataBlock;
|
import org.apache.poi.poifs.storage.RawDataBlock;
|
||||||
import org.apache.poi.poifs.storage.SmallDocumentBlock;
|
import org.apache.poi.poifs.storage.SmallDocumentBlock;
|
||||||
import org.apache.poi.util.HexDump;
|
import org.apache.poi.util.HexDump;
|
||||||
@ -39,10 +42,9 @@ import org.apache.poi.util.HexDump;
|
|||||||
*
|
*
|
||||||
* @author Marc Johnson (mjohnson at apache dot org)
|
* @author Marc Johnson (mjohnson at apache dot org)
|
||||||
*/
|
*/
|
||||||
|
public final class POIFSDocument implements BATManaged, BlockWritable, POIFSViewable {
|
||||||
public class POIFSDocument
|
private static final DocumentBlock[] EMPTY_BIG_BLOCK_ARRAY = { };
|
||||||
implements BATManaged, BlockWritable, POIFSViewable
|
private static final SmallDocumentBlock[] EMPTY_SMALL_BLOCK_ARRAY = { };
|
||||||
{
|
|
||||||
private DocumentProperty _property;
|
private DocumentProperty _property;
|
||||||
private int _size;
|
private int _size;
|
||||||
|
|
||||||
@ -56,21 +58,32 @@ public class POIFSDocument
|
|||||||
* @param name the name of the POIFSDocument
|
* @param name the name of the POIFSDocument
|
||||||
* @param blocks the big blocks making up the POIFSDocument
|
* @param blocks the big blocks making up the POIFSDocument
|
||||||
* @param length the actual length of the POIFSDocument
|
* @param length the actual length of the POIFSDocument
|
||||||
*
|
|
||||||
* @exception IOException
|
|
||||||
*/
|
*/
|
||||||
|
public POIFSDocument(String name, RawDataBlock[] blocks, int length) throws IOException {
|
||||||
public POIFSDocument(final String name, final RawDataBlock [] blocks,
|
|
||||||
final int length)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
_size = length;
|
_size = length;
|
||||||
_big_store = new BigBlockStore(blocks);
|
_big_store = new BigBlockStore(convertRawBlocksToBigBlocks(blocks));
|
||||||
_property = new DocumentProperty(name, _size);
|
_property = new DocumentProperty(name, _size);
|
||||||
_small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
|
_small_store = new SmallBlockStore(EMPTY_SMALL_BLOCK_ARRAY);
|
||||||
_property.setDocument(this);
|
_property.setDocument(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO - awkward typing going on here
|
||||||
|
private static DocumentBlock[] convertRawBlocksToBigBlocks(ListManagedBlock[] blocks) throws IOException {
|
||||||
|
DocumentBlock[] result = new DocumentBlock[blocks.length];
|
||||||
|
for (int i = 0; i < result.length; i++) {
|
||||||
|
result[i] = new DocumentBlock((RawDataBlock)blocks[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
private static SmallDocumentBlock[] convertRawBlocksToSmallBlocks(ListManagedBlock[] blocks) {
|
||||||
|
if (blocks instanceof SmallDocumentBlock[]) {
|
||||||
|
return (SmallDocumentBlock[]) blocks;
|
||||||
|
}
|
||||||
|
SmallDocumentBlock[] result = new SmallDocumentBlock[blocks.length];
|
||||||
|
System.arraycopy(blocks, 0, result, 0, blocks.length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor from small blocks
|
* Constructor from small blocks
|
||||||
*
|
*
|
||||||
@ -78,20 +91,9 @@ public class POIFSDocument
|
|||||||
* @param blocks the small blocks making up the POIFSDocument
|
* @param blocks the small blocks making up the POIFSDocument
|
||||||
* @param length the actual length of the POIFSDocument
|
* @param length the actual length of the POIFSDocument
|
||||||
*/
|
*/
|
||||||
|
public POIFSDocument(String name, SmallDocumentBlock[] blocks, int length) {
|
||||||
public POIFSDocument(final String name,
|
|
||||||
final SmallDocumentBlock [] blocks, final int length)
|
|
||||||
{
|
|
||||||
_size = length;
|
_size = length;
|
||||||
try
|
_big_store = new BigBlockStore(EMPTY_BIG_BLOCK_ARRAY);
|
||||||
{
|
|
||||||
_big_store = new BigBlockStore(new RawDataBlock[ 0 ]);
|
|
||||||
}
|
|
||||||
catch (IOException ignored)
|
|
||||||
{
|
|
||||||
|
|
||||||
// can't happen with that constructor
|
|
||||||
}
|
|
||||||
_property = new DocumentProperty(name, _size);
|
_property = new DocumentProperty(name, _size);
|
||||||
_small_store = new SmallBlockStore(blocks);
|
_small_store = new SmallBlockStore(blocks);
|
||||||
_property.setDocument(this);
|
_property.setDocument(this);
|
||||||
@ -103,26 +105,17 @@ public class POIFSDocument
|
|||||||
* @param name the name of the POIFSDocument
|
* @param name the name of the POIFSDocument
|
||||||
* @param blocks the small blocks making up the POIFSDocument
|
* @param blocks the small blocks making up the POIFSDocument
|
||||||
* @param length the actual length of the POIFSDocument
|
* @param length the actual length of the POIFSDocument
|
||||||
*
|
|
||||||
* @exception IOException
|
|
||||||
*/
|
*/
|
||||||
|
public POIFSDocument(String name, ListManagedBlock[] blocks, int length) throws IOException {
|
||||||
public POIFSDocument(final String name, final ListManagedBlock [] blocks,
|
|
||||||
final int length)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
_size = length;
|
_size = length;
|
||||||
_property = new DocumentProperty(name, _size);
|
_property = new DocumentProperty(name, _size);
|
||||||
_property.setDocument(this);
|
_property.setDocument(this);
|
||||||
if (Property.isSmall(_size))
|
if (Property.isSmall(_size)) {
|
||||||
{
|
_big_store = new BigBlockStore(EMPTY_BIG_BLOCK_ARRAY);
|
||||||
_big_store = new BigBlockStore(new RawDataBlock[ 0 ]);
|
_small_store = new SmallBlockStore(convertRawBlocksToSmallBlocks(blocks));
|
||||||
_small_store = new SmallBlockStore(blocks);
|
} else {
|
||||||
}
|
_big_store = new BigBlockStore(convertRawBlocksToBigBlocks(blocks));
|
||||||
else
|
_small_store = new SmallBlockStore(EMPTY_SMALL_BLOCK_ARRAY);
|
||||||
{
|
|
||||||
_big_store = new BigBlockStore(blocks);
|
|
||||||
_small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,47 +124,33 @@ public class POIFSDocument
|
|||||||
*
|
*
|
||||||
* @param name the name of the POIFSDocument
|
* @param name the name of the POIFSDocument
|
||||||
* @param stream the InputStream we read data from
|
* @param stream the InputStream we read data from
|
||||||
*
|
|
||||||
* @exception IOException thrown on read errors
|
|
||||||
*/
|
*/
|
||||||
|
public POIFSDocument(String name, InputStream stream) throws IOException {
|
||||||
public POIFSDocument(final String name, final InputStream stream)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
List blocks = new ArrayList();
|
List blocks = new ArrayList();
|
||||||
|
|
||||||
_size = 0;
|
_size = 0;
|
||||||
while (true)
|
while (true) {
|
||||||
{
|
|
||||||
DocumentBlock block = new DocumentBlock(stream);
|
DocumentBlock block = new DocumentBlock(stream);
|
||||||
int blockSize = block.size();
|
int blockSize = block.size();
|
||||||
|
|
||||||
if (blockSize > 0)
|
if (blockSize > 0) {
|
||||||
{
|
|
||||||
blocks.add(block);
|
blocks.add(block);
|
||||||
_size += blockSize;
|
_size += blockSize;
|
||||||
}
|
}
|
||||||
if (block.partiallyRead())
|
if (block.partiallyRead()) {
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DocumentBlock[] bigBlocks =
|
DocumentBlock[] bigBlocks = (DocumentBlock[]) blocks.toArray(new DocumentBlock[blocks.size()]);
|
||||||
( DocumentBlock [] ) blocks.toArray(new DocumentBlock[ 0 ]);
|
|
||||||
|
|
||||||
_big_store = new BigBlockStore(bigBlocks);
|
_big_store = new BigBlockStore(bigBlocks);
|
||||||
_property = new DocumentProperty(name, _size);
|
_property = new DocumentProperty(name, _size);
|
||||||
_property.setDocument(this);
|
_property.setDocument(this);
|
||||||
if (_property.shouldUseSmallBlocks())
|
if (_property.shouldUseSmallBlocks()) {
|
||||||
{
|
_small_store = new SmallBlockStore(SmallDocumentBlock.convert(bigBlocks, _size));
|
||||||
_small_store =
|
|
||||||
new SmallBlockStore(SmallDocumentBlock.convert(bigBlocks,
|
|
||||||
_size));
|
|
||||||
_big_store = new BigBlockStore(new DocumentBlock[0]);
|
_big_store = new BigBlockStore(new DocumentBlock[0]);
|
||||||
}
|
} else {
|
||||||
else
|
_small_store = new SmallBlockStore(EMPTY_SMALL_BLOCK_ARRAY);
|
||||||
{
|
|
||||||
_small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,49 +160,32 @@ public class POIFSDocument
|
|||||||
* @param name the name of the POIFSDocument
|
* @param name the name of the POIFSDocument
|
||||||
* @param size the length of the POIFSDocument
|
* @param size the length of the POIFSDocument
|
||||||
* @param path the path of the POIFSDocument
|
* @param path the path of the POIFSDocument
|
||||||
* @param writer the writer who will eventually write the document
|
* @param writer the writer who will eventually write the document contents
|
||||||
* contents
|
|
||||||
*
|
|
||||||
* @exception IOException thrown on read errors
|
|
||||||
*/
|
*/
|
||||||
|
public POIFSDocument(String name, int size, POIFSDocumentPath path, POIFSWriterListener writer) {
|
||||||
public POIFSDocument(final String name, final int size,
|
|
||||||
final POIFSDocumentPath path,
|
|
||||||
final POIFSWriterListener writer)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
_size = size;
|
_size = size;
|
||||||
_property = new DocumentProperty(name, _size);
|
_property = new DocumentProperty(name, _size);
|
||||||
_property.setDocument(this);
|
_property.setDocument(this);
|
||||||
if (_property.shouldUseSmallBlocks())
|
if (_property.shouldUseSmallBlocks()) {
|
||||||
{
|
|
||||||
_small_store = new SmallBlockStore(path, name, size, writer);
|
_small_store = new SmallBlockStore(path, name, size, writer);
|
||||||
_big_store = new BigBlockStore(new Object[ 0 ]);
|
_big_store = new BigBlockStore(EMPTY_BIG_BLOCK_ARRAY);
|
||||||
}
|
} else {
|
||||||
else
|
_small_store = new SmallBlockStore(EMPTY_SMALL_BLOCK_ARRAY);
|
||||||
{
|
|
||||||
_small_store = new SmallBlockStore(new BlockWritable[ 0 ]);
|
|
||||||
_big_store = new BigBlockStore(path, name, size, writer);
|
_big_store = new BigBlockStore(path, name, size, writer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the array of SmallDocumentBlocks used
|
|
||||||
*
|
|
||||||
* @return array of SmallDocumentBlocks; may be empty, cannot be null
|
* @return array of SmallDocumentBlocks; may be empty, cannot be null
|
||||||
*/
|
*/
|
||||||
|
public BlockWritable[] getSmallBlocks() {
|
||||||
public BlockWritable [] getSmallBlocks()
|
|
||||||
{
|
|
||||||
return _small_store.getBlocks();
|
return _small_store.getBlocks();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return size of the document
|
* @return size of the document
|
||||||
*/
|
*/
|
||||||
|
public int getSize() {
|
||||||
public int getSize()
|
|
||||||
{
|
|
||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,27 +195,19 @@ public class POIFSDocument
|
|||||||
* @param buffer the buffer to write to
|
* @param buffer the buffer to write to
|
||||||
* @param offset the offset into our storage to read from
|
* @param offset the offset into our storage to read from
|
||||||
*/
|
*/
|
||||||
|
void read(byte[] buffer, int offset) {
|
||||||
void read(final byte [] buffer, final int offset)
|
if (_property.shouldUseSmallBlocks()) {
|
||||||
{
|
|
||||||
if (_property.shouldUseSmallBlocks())
|
|
||||||
{
|
|
||||||
SmallDocumentBlock.read(_small_store.getBlocks(), buffer, offset);
|
SmallDocumentBlock.read(_small_store.getBlocks(), buffer, offset);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
DocumentBlock.read(_big_store.getBlocks(), buffer, offset);
|
DocumentBlock.read(_big_store.getBlocks(), buffer, offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the DocumentProperty
|
|
||||||
*
|
|
||||||
* @return the instance's DocumentProperty
|
* @return the instance's DocumentProperty
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DocumentProperty getDocumentProperty()
|
DocumentProperty getDocumentProperty() {
|
||||||
{
|
|
||||||
return _property;
|
return _property;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,16 +216,9 @@ public class POIFSDocument
|
|||||||
/**
|
/**
|
||||||
* Write the storage to an OutputStream
|
* Write the storage to an OutputStream
|
||||||
*
|
*
|
||||||
* @param stream the OutputStream to which the stored data should
|
* @param stream the OutputStream to which the stored data should be written
|
||||||
* be written
|
|
||||||
*
|
|
||||||
* @exception IOException on problems writing to the specified
|
|
||||||
* stream
|
|
||||||
*/
|
*/
|
||||||
|
public void writeBlocks(OutputStream stream) throws IOException {
|
||||||
public void writeBlocks(final OutputStream stream)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
_big_store.writeBlocks(stream);
|
_big_store.writeBlocks(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,21 +230,16 @@ public class POIFSDocument
|
|||||||
*
|
*
|
||||||
* @return count of BigBlock instances
|
* @return count of BigBlock instances
|
||||||
*/
|
*/
|
||||||
|
public int countBlocks() {
|
||||||
public int countBlocks()
|
|
||||||
{
|
|
||||||
return _big_store.countBlocks();
|
return _big_store.countBlocks();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the start block for this instance
|
* Set the start block for this instance
|
||||||
*
|
*
|
||||||
* @param index index into the array of blocks making up the
|
* @param index index into the array of blocks making up the filesystem
|
||||||
* filesystem
|
|
||||||
*/
|
*/
|
||||||
|
public void setStartBlock(int index) {
|
||||||
public void setStartBlock(final int index)
|
|
||||||
{
|
|
||||||
_property.setStartBlock(index);
|
_property.setStartBlock(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,40 +247,30 @@ public class POIFSDocument
|
|||||||
/* ********** START begin implementation of POIFSViewable ********** */
|
/* ********** START begin implementation of POIFSViewable ********** */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of objects, some of which may implement
|
* Get an array of objects, some of which may implement POIFSViewable
|
||||||
* POIFSViewable
|
|
||||||
*
|
*
|
||||||
* @return an array of Object; may not be null, but may be empty
|
* @return an array of Object; may not be null, but may be empty
|
||||||
*/
|
*/
|
||||||
|
public Object[] getViewableArray() {
|
||||||
public Object [] getViewableArray()
|
|
||||||
{
|
|
||||||
Object[] results = new Object[1];
|
Object[] results = new Object[1];
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||||
BlockWritable[] blocks = null;
|
BlockWritable[] blocks = null;
|
||||||
|
|
||||||
if (_big_store.isValid())
|
if (_big_store.isValid()) {
|
||||||
{
|
|
||||||
blocks = _big_store.getBlocks();
|
blocks = _big_store.getBlocks();
|
||||||
}
|
} else if (_small_store.isValid()) {
|
||||||
else if (_small_store.isValid())
|
|
||||||
{
|
|
||||||
blocks = _small_store.getBlocks();
|
blocks = _small_store.getBlocks();
|
||||||
}
|
}
|
||||||
if (blocks != null)
|
if (blocks != null) {
|
||||||
{
|
for (int k = 0; k < blocks.length; k++) {
|
||||||
for (int k = 0; k < blocks.length; k++)
|
|
||||||
{
|
|
||||||
blocks[k].writeBlocks(output);
|
blocks[k].writeBlocks(output);
|
||||||
}
|
}
|
||||||
byte[] data = output.toByteArray();
|
byte[] data = output.toByteArray();
|
||||||
|
|
||||||
if (data.length > _property.getSize())
|
if (data.length > _property.getSize()) {
|
||||||
{
|
|
||||||
byte[] tmp = new byte[_property.getSize()];
|
byte[] tmp = new byte[_property.getSize()];
|
||||||
|
|
||||||
System.arraycopy(data, 0, tmp, 0, tmp.length);
|
System.arraycopy(data, 0, tmp, 0, tmp.length);
|
||||||
@ -347,14 +279,10 @@ public class POIFSDocument
|
|||||||
output = new ByteArrayOutputStream();
|
output = new ByteArrayOutputStream();
|
||||||
HexDump.dump(data, 0, output, 0);
|
HexDump.dump(data, 0, output, 0);
|
||||||
result = output.toString();
|
result = output.toString();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
result = "<NO DATA>";
|
result = "<NO DATA>";
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
result = e.getMessage();
|
result = e.getMessage();
|
||||||
}
|
}
|
||||||
results[0] = result;
|
results[0] = result;
|
||||||
@ -362,15 +290,12 @@ public class POIFSDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an Iterator of objects, some of which may implement
|
* Get an Iterator of objects, some of which may implement POIFSViewable
|
||||||
* POIFSViewable
|
|
||||||
*
|
*
|
||||||
* @return an Iterator; may not be null, but may have an empty
|
* @return an Iterator; may not be null, but may have an empty back end
|
||||||
* back end store
|
* store
|
||||||
*/
|
*/
|
||||||
|
public Iterator getViewableIterator() {
|
||||||
public Iterator getViewableIterator()
|
|
||||||
{
|
|
||||||
return Collections.EMPTY_LIST.iterator();
|
return Collections.EMPTY_LIST.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,12 +303,10 @@ public class POIFSDocument
|
|||||||
* Give viewers a hint as to whether to call getViewableArray or
|
* Give viewers a hint as to whether to call getViewableArray or
|
||||||
* getViewableIterator
|
* getViewableIterator
|
||||||
*
|
*
|
||||||
* @return true if a viewer should call getViewableArray, false if
|
* @return <code>true</code> if a viewer should call getViewableArray,
|
||||||
* a viewer should call getViewableIterator
|
* <code>false</code> if a viewer should call getViewableIterator
|
||||||
*/
|
*/
|
||||||
|
public boolean preferArray() {
|
||||||
public boolean preferArray()
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,39 +316,29 @@ public class POIFSDocument
|
|||||||
*
|
*
|
||||||
* @return short description
|
* @return short description
|
||||||
*/
|
*/
|
||||||
|
public String getShortDescription() {
|
||||||
public String getShortDescription()
|
|
||||||
{
|
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
|
||||||
buffer.append("Document: \"").append(_property.getName())
|
buffer.append("Document: \"").append(_property.getName()).append("\"");
|
||||||
.append("\"");
|
|
||||||
buffer.append(" size = ").append(getSize());
|
buffer.append(" size = ").append(getSize());
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********** END begin implementation of POIFSViewable ********** */
|
/* ********** END begin implementation of POIFSViewable ********** */
|
||||||
private class SmallBlockStore
|
private static final class SmallBlockStore {
|
||||||
{
|
|
||||||
private SmallDocumentBlock[] smallBlocks;
|
private SmallDocumentBlock[] smallBlocks;
|
||||||
private POIFSDocumentPath path;
|
private final POIFSDocumentPath path;
|
||||||
private String name;
|
private final String name;
|
||||||
private int size;
|
private final int size;
|
||||||
private POIFSWriterListener writer;
|
private final POIFSWriterListener writer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param blocks blocks to construct the store from
|
* @param blocks blocks to construct the store from
|
||||||
*/
|
*/
|
||||||
|
SmallBlockStore(SmallDocumentBlock[] blocks) {
|
||||||
SmallBlockStore(final Object [] blocks)
|
smallBlocks = (SmallDocumentBlock[]) blocks.clone();
|
||||||
{
|
|
||||||
smallBlocks = new SmallDocumentBlock[ blocks.length ];
|
|
||||||
for (int j = 0; j < blocks.length; j++)
|
|
||||||
{
|
|
||||||
smallBlocks[ j ] = ( SmallDocumentBlock ) blocks[ j ];
|
|
||||||
}
|
|
||||||
this.path = null;
|
this.path = null;
|
||||||
this.name = null;
|
this.name = null;
|
||||||
this.size = -1;
|
this.size = -1;
|
||||||
@ -433,18 +346,14 @@ public class POIFSDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for a small block store that will be written
|
* Constructor for a small block store that will be written later
|
||||||
* later
|
|
||||||
*
|
*
|
||||||
* @param path path of the document
|
* @param path path of the document
|
||||||
* @param name name of the document
|
* @param name name of the document
|
||||||
* @param size length of the document
|
* @param size length of the document
|
||||||
* @param writer the object that will eventually write the document
|
* @param writer the object that will eventually write the document
|
||||||
*/
|
*/
|
||||||
|
SmallBlockStore(POIFSDocumentPath path, String name, int size, POIFSWriterListener writer) {
|
||||||
SmallBlockStore(final POIFSDocumentPath path, final String name,
|
|
||||||
final int size, final POIFSWriterListener writer)
|
|
||||||
{
|
|
||||||
smallBlocks = new SmallDocumentBlock[0];
|
smallBlocks = new SmallDocumentBlock[0];
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -453,68 +362,41 @@ public class POIFSDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if this store is a valid source of data
|
* @return <code>true</code> if this store is a valid source of data
|
||||||
*/
|
*/
|
||||||
|
boolean isValid() {
|
||||||
boolean isValid()
|
return smallBlocks.length > 0 || writer != null;
|
||||||
{
|
|
||||||
return ((smallBlocks.length > 0) || (writer != null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the SmallDocumentBlocks
|
* @return the SmallDocumentBlocks
|
||||||
*/
|
*/
|
||||||
|
SmallDocumentBlock[] getBlocks() {
|
||||||
|
if (isValid() && writer != null) {
|
||||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream(size);
|
||||||
|
DocumentOutputStream dstream = new DocumentOutputStream(stream, size);
|
||||||
|
|
||||||
BlockWritable [] getBlocks()
|
writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, path, name, size));
|
||||||
{
|
smallBlocks = SmallDocumentBlock.convert(stream.toByteArray(), size);
|
||||||
if (isValid() && (writer != null))
|
|
||||||
{
|
|
||||||
ByteArrayOutputStream stream =
|
|
||||||
new ByteArrayOutputStream(size);
|
|
||||||
DocumentOutputStream dstream =
|
|
||||||
new DocumentOutputStream(stream, size);
|
|
||||||
|
|
||||||
writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream,
|
|
||||||
path, name, size));
|
|
||||||
smallBlocks = SmallDocumentBlock.convert(stream.toByteArray(),
|
|
||||||
size);
|
|
||||||
}
|
}
|
||||||
return smallBlocks;
|
return smallBlocks;
|
||||||
}
|
}
|
||||||
} // end private class SmallBlockStore
|
} // end private class SmallBlockStore
|
||||||
|
|
||||||
private class BigBlockStore
|
private static final class BigBlockStore {
|
||||||
{
|
|
||||||
private DocumentBlock[] bigBlocks;
|
private DocumentBlock[] bigBlocks;
|
||||||
private POIFSDocumentPath path;
|
private final POIFSDocumentPath path;
|
||||||
private String name;
|
private final String name;
|
||||||
private int size;
|
private final int size;
|
||||||
private POIFSWriterListener writer;
|
private final POIFSWriterListener writer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param blocks the blocks making up the store
|
* @param blocks the blocks making up the store
|
||||||
*
|
|
||||||
* @exception IOException on I/O error
|
|
||||||
*/
|
*/
|
||||||
|
BigBlockStore(DocumentBlock[] blocks) {
|
||||||
BigBlockStore(final Object [] blocks)
|
bigBlocks = (DocumentBlock[]) blocks.clone();
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
bigBlocks = new DocumentBlock[ blocks.length ];
|
|
||||||
for (int j = 0; j < blocks.length; j++)
|
|
||||||
{
|
|
||||||
if (blocks[ j ] instanceof DocumentBlock)
|
|
||||||
{
|
|
||||||
bigBlocks[ j ] = ( DocumentBlock ) blocks[ j ];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
bigBlocks[ j ] =
|
|
||||||
new DocumentBlock(( RawDataBlock ) blocks[ j ]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.path = null;
|
this.path = null;
|
||||||
this.name = null;
|
this.name = null;
|
||||||
this.size = -1;
|
this.size = -1;
|
||||||
@ -522,19 +404,14 @@ public class POIFSDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for a big block store that will be written
|
* Constructor for a big block store that will be written later
|
||||||
* later
|
|
||||||
*
|
*
|
||||||
* @param path path of the document
|
* @param path path of the document
|
||||||
* @param name name of the document
|
* @param name name of the document
|
||||||
* @param size length of the document
|
* @param size length of the document
|
||||||
* @param writer the object that will eventually write the
|
* @param writer the object that will eventually write the document
|
||||||
* document
|
|
||||||
*/
|
*/
|
||||||
|
BigBlockStore(POIFSDocumentPath path, String name, int size, POIFSWriterListener writer) {
|
||||||
BigBlockStore(final POIFSDocumentPath path, final String name,
|
|
||||||
final int size, final POIFSWriterListener writer)
|
|
||||||
{
|
|
||||||
bigBlocks = new DocumentBlock[0];
|
bigBlocks = new DocumentBlock[0];
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -543,29 +420,21 @@ public class POIFSDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if this store is a valid source of data
|
* @return <code>true</code> if this store is a valid source of data
|
||||||
*/
|
*/
|
||||||
|
boolean isValid() {
|
||||||
boolean isValid()
|
return bigBlocks.length > 0 || writer != null;
|
||||||
{
|
|
||||||
return ((bigBlocks.length > 0) || (writer != null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the DocumentBlocks
|
* @return the DocumentBlocks
|
||||||
*/
|
*/
|
||||||
|
DocumentBlock[] getBlocks() {
|
||||||
|
if (isValid() && writer != null) {
|
||||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream(size);
|
||||||
|
DocumentOutputStream dstream = new DocumentOutputStream(stream, size);
|
||||||
|
|
||||||
DocumentBlock [] getBlocks()
|
writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, path, name, size));
|
||||||
{
|
|
||||||
if (isValid() && (writer != null))
|
|
||||||
{
|
|
||||||
ByteArrayOutputStream stream =
|
|
||||||
new ByteArrayOutputStream(size);
|
|
||||||
DocumentOutputStream dstream =
|
|
||||||
new DocumentOutputStream(stream, size);
|
|
||||||
|
|
||||||
writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream,
|
|
||||||
path, name, size));
|
|
||||||
bigBlocks = DocumentBlock.convert(stream.toByteArray(), size);
|
bigBlocks = DocumentBlock.convert(stream.toByteArray(), size);
|
||||||
}
|
}
|
||||||
return bigBlocks;
|
return bigBlocks;
|
||||||
@ -575,31 +444,17 @@ public class POIFSDocument
|
|||||||
* write the blocks to a stream
|
* write the blocks to a stream
|
||||||
*
|
*
|
||||||
* @param stream the stream to which the data is to be written
|
* @param stream the stream to which the data is to be written
|
||||||
*
|
|
||||||
* @exception IOException on error
|
|
||||||
*/
|
*/
|
||||||
|
void writeBlocks(OutputStream stream) throws IOException {
|
||||||
|
if (isValid()) {
|
||||||
|
if (writer != null) {
|
||||||
|
DocumentOutputStream dstream = new DocumentOutputStream(stream, size);
|
||||||
|
|
||||||
void writeBlocks(OutputStream stream)
|
writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, path, name, size));
|
||||||
throws IOException
|
dstream.writeFiller(countBlocks() * POIFSConstants.BIG_BLOCK_SIZE,
|
||||||
{
|
DocumentBlock.getFillByte());
|
||||||
if (isValid())
|
} else {
|
||||||
{
|
for (int k = 0; k < bigBlocks.length; k++) {
|
||||||
if (writer != null)
|
|
||||||
{
|
|
||||||
DocumentOutputStream dstream =
|
|
||||||
new DocumentOutputStream(stream, size);
|
|
||||||
|
|
||||||
writer.processPOIFSWriterEvent(
|
|
||||||
new POIFSWriterEvent(dstream, path, name, size));
|
|
||||||
dstream.writeFiller(countBlocks()
|
|
||||||
* POIFSConstants
|
|
||||||
.BIG_BLOCK_SIZE, DocumentBlock
|
|
||||||
.getFillByte());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (int k = 0; k < bigBlocks.length; k++)
|
|
||||||
{
|
|
||||||
bigBlocks[k].writeBlocks(stream);
|
bigBlocks[k].writeBlocks(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -609,25 +464,16 @@ public class POIFSDocument
|
|||||||
/**
|
/**
|
||||||
* @return number of big blocks making up this document
|
* @return number of big blocks making up this document
|
||||||
*/
|
*/
|
||||||
|
int countBlocks() {
|
||||||
|
|
||||||
int countBlocks()
|
if (isValid()) {
|
||||||
{
|
if (writer == null) {
|
||||||
int rval = 0;
|
return bigBlocks.length;
|
||||||
|
}
|
||||||
if (isValid())
|
return (size + POIFSConstants.BIG_BLOCK_SIZE - 1)
|
||||||
{
|
|
||||||
if (writer != null)
|
|
||||||
{
|
|
||||||
rval = (size + POIFSConstants.BIG_BLOCK_SIZE - 1)
|
|
||||||
/ POIFSConstants.BIG_BLOCK_SIZE;
|
/ POIFSConstants.BIG_BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
else
|
return 0;
|
||||||
{
|
|
||||||
rval = bigBlocks.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
} // end private class BigBlockStore
|
} // end private class BigBlockStore
|
||||||
} // end class POIFSDocument
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user