Support for replacing the contents of a Document in a NPOIFSFileSytem, in place
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590177 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0250a0372f
commit
868a108fe3
@ -43,6 +43,14 @@ public final class NPOIFSDocument implements POIFSViewable {
|
|||||||
private NPOIFSStream _stream;
|
private NPOIFSStream _stream;
|
||||||
private int _block_size;
|
private int _block_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for an existing Document
|
||||||
|
*/
|
||||||
|
public NPOIFSDocument(DocumentNode document) throws IOException {
|
||||||
|
this((DocumentProperty)document.getProperty(),
|
||||||
|
((DirectoryNode)document.getParent()).getNFileSystem());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for an existing Document
|
* Constructor for an existing Document
|
||||||
*/
|
*/
|
||||||
@ -72,32 +80,10 @@ public final class NPOIFSDocument implements POIFSViewable {
|
|||||||
{
|
{
|
||||||
this._filesystem = filesystem;
|
this._filesystem = filesystem;
|
||||||
|
|
||||||
final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
|
|
||||||
BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
|
|
||||||
bis.mark(bigBlockSize);
|
|
||||||
|
|
||||||
// Do we need to store as a mini stream or a full one?
|
|
||||||
if(bis.skip(bigBlockSize) < bigBlockSize) {
|
|
||||||
_stream = new NPOIFSStream(filesystem.getMiniStore());
|
|
||||||
_block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
|
|
||||||
} else {
|
|
||||||
_stream = new NPOIFSStream(filesystem);
|
|
||||||
_block_size = _filesystem.getBlockStoreBlockSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// start from the beginning
|
|
||||||
bis.reset();
|
|
||||||
|
|
||||||
// Store it
|
// Store it
|
||||||
OutputStream os = _stream.getOutputStream();
|
int length = store(stream);
|
||||||
byte buf[] = new byte[1024];
|
|
||||||
int length = 0;
|
|
||||||
|
|
||||||
for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
|
// Build the property for it
|
||||||
os.write(buf, 0, readBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// And build the property for it
|
|
||||||
this._property = new DocumentProperty(name, length);
|
this._property = new DocumentProperty(name, length);
|
||||||
_property.setStartBlock(_stream.getStartBlock());
|
_property.setStartBlock(_stream.getStartBlock());
|
||||||
}
|
}
|
||||||
@ -128,6 +114,38 @@ public final class NPOIFSDocument implements POIFSViewable {
|
|||||||
_property.setStartBlock(_stream.getStartBlock());
|
_property.setStartBlock(_stream.getStartBlock());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the given data for this Document
|
||||||
|
*/
|
||||||
|
private int store(InputStream stream) throws IOException {
|
||||||
|
final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
|
||||||
|
bis.mark(bigBlockSize);
|
||||||
|
|
||||||
|
// Do we need to store as a mini stream or a full one?
|
||||||
|
if(bis.skip(bigBlockSize) < bigBlockSize) {
|
||||||
|
_stream = new NPOIFSStream(_filesystem.getMiniStore());
|
||||||
|
_block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
|
||||||
|
} else {
|
||||||
|
_stream = new NPOIFSStream(_filesystem);
|
||||||
|
_block_size = _filesystem.getBlockStoreBlockSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// start from the beginning
|
||||||
|
bis.reset();
|
||||||
|
|
||||||
|
// Store it
|
||||||
|
OutputStream os = _stream.getOutputStream();
|
||||||
|
byte buf[] = new byte[1024];
|
||||||
|
int length = 0;
|
||||||
|
|
||||||
|
for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
|
||||||
|
os.write(buf, 0, readBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees the underlying stream and property
|
* Frees the underlying stream and property
|
||||||
*/
|
*/
|
||||||
@ -156,6 +174,11 @@ public final class NPOIFSDocument implements POIFSViewable {
|
|||||||
return _property.getSize();
|
return _property.getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void replaceContents(InputStream stream) throws IOException {
|
||||||
|
free();
|
||||||
|
store(stream);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the instance's DocumentProperty
|
* @return the instance's DocumentProperty
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user