public class POIFSFileSystem extends BlockStore implements POIFSViewable, java.io.Closeable
This is the main class of the POIFS system; it manages the entire life cycle of the filesystem.
This is the new NIO version, which uses less memory
(这是 POIFS 系统的主要类;它管理文件系统的整个生命周期。这是新的 NIO 版本,使用更少的内存)BlockStore.ChainLoopDetector
Modifier and Type | Field and Description |
---|---|
protected DataSource |
_data |
Constructor and Description |
---|
POIFSFileSystem()
Constructor, intended for writing
|
POIFSFileSystem(java.io.File file)
Creates a POIFSFileSystem from a
File.
|
POIFSFileSystem(java.io.File file, boolean readOnly)
Creates a POIFSFileSystem from a
File.
|
POIFSFileSystem(java.nio.channels.FileChannel channel)
Creates a POIFSFileSystem from an open
FileChannel.
|
POIFSFileSystem(java.nio.channels.FileChannel channel, boolean readOnly)
Creates a POIFSFileSystem from an open
FileChannel.
|
POIFSFileSystem(java.io.InputStream stream)
Create a POIFSFileSystem from an
InputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the FileSystem, freeing any underlying files, streams and buffers.
(关闭文件系统,释放任何底层文件、流和缓冲区。)
|
static POIFSFileSystem |
create(java.io.File file)
|
protected java.nio.ByteBuffer |
createBlockIfNeeded(int offset)
Load the block at the given offset, extending the file if needed
(在给定的偏移处加载块,如果需要扩展文件)
|
DirectoryEntry |
createDirectory(java.lang.String name)
create a new DirectoryEntry in the root directory
(在根目录下新建一个 DirectoryEntry)
|
DocumentEntry |
createDocument(java.io.InputStream stream, java.lang.String name)
Create a new document to be added to the root directory
(创建要添加到根目录的新文档)
|
DocumentEntry |
createDocument(java.lang.String name, int size, POIFSWriterListener writer)
create a new DocumentEntry in the root entry; the data will be provided later
(在根条目中创建一个新的 DocumentEntry;数据将在稍后提供)
|
DocumentInputStream |
createDocumentInputStream(java.lang.String documentName)
open a document in the root entry's list of entries
(在根条目的条目列表中打开一个文档)
|
protected void |
createNewDataSource() |
DocumentEntry |
createOrUpdateDocument(java.io.InputStream stream, java.lang.String name)
Set the contents of a document in the root directory, creating if needed, otherwise updating
(设置根目录下文档的内容,需要时创建,否则更新)
|
protected BATBlock.BATBlockAndIndex |
getBATBlockAndIndex(int offset)
Returns the BATBlock that handles the specified offset, and the relative index within it
(返回处理指定偏移量的 BATBlock,以及其中的相对索引)
|
int |
getBigBlockSize() |
POIFSBigBlockSize |
getBigBlockSizeDetails() |
protected java.nio.ByteBuffer |
getBlockAt(int offset)
Load the block at the given offset.
(在给定的偏移量处加载块。)
|
protected int |
getBlockStoreBlockSize()
Returns the size of the blocks managed through the block store.
(返回通过块存储管理的块的大小。)
|
protected BlockStore.ChainLoopDetector |
getChainLoopDetector()
Creates a Detector for loops in the chain
(为链中的循环创建检测器)
|
protected int |
getFreeBlock()
Finds a free block, and returns its offset.
(找到一个空闲块,并返回它的偏移量。)
|
HeaderBlock |
getHeaderBlock() |
protected int |
getNextBlock(int offset)
Works out what block follows the specified one.
(计算出指定块后面的块。)
|
PropertyTable |
getPropertyTable() |
DirectoryNode |
getRoot()
Get the root entry
(获取根条目)
|
java.lang.String |
getShortDescription()
Provides a short description of the object, to be used when a POIFSViewable object has not provided its contents.
(提供对象的简短描述,在 POIFSViewable 对象未提供其内容时使用。)
|
java.lang.Object[] |
getViewableArray()
Get an array of objects, some of which may implement POIFSViewable
(获取对象数组,其中一些可能实现 POIFSViewable)
|
java.util.Iterator<java.lang.Object> |
getViewableIterator()
Get an Iterator of objects, some of which may implement POIFSViewable
(获取对象的Iterator,其中一些可能实现POIFSViewable)
|
boolean |
isInPlaceWriteable()
Does the filesystem support an in-place write via
writeFilesystem() ? If false, only writing out to a brand new file via
writeFilesystem(OutputStream) is supported.
(文件系统是否支持通过 writeFilesystem() 就地写入?如果为 false,则仅支持通过 writeFilesystem(OutputStream) 写入全新文件。)
|
static void |
main(java.lang.String[] args)
read in a file and write it back out again
(读入文件并再次写回)
|
boolean |
preferArray()
Give viewers a hint as to whether to call getViewableArray or getViewableIterator
(提示查看者是否调用 getViewableArray 或 getViewableIterator)
|
protected void |
releaseBuffer(java.nio.ByteBuffer buffer)
Releases a mmap-ed buffer, which you are sure won't be used again
(释放一个 mmap-ed 缓冲区,您确定不会再次使用该缓冲区)
|
protected void |
setNextBlock(int offset, int nextBlock)
Changes the record of what block follows the specified one.
(更改指定块后面的记录。)
|
protected long |
size() |
void |
writeFilesystem()
Write the filesystem out to the open file.
(将文件系统写入打开的文件。)
|
void |
writeFilesystem(java.io.OutputStream stream)
Write the filesystem out
(写出文件系统)
|
protected DataSource _data
public POIFSFileSystem()
public POIFSFileSystem(java.io.File file) throws java.io.IOException
Creates a POIFSFileSystem from a File. This uses less memory than creating from an InputStream. The File will be opened read-only
Note that with this constructor, you will need to call close()
when you're done to have the underlying file closed, as the file is kept open during normal operation to read the data out.
file
- the File from which to read the data
(file - 从中读取数据的文件)
java.io.IOException
- on errors reading, or on invalid data
(java.io.IOException - 读取错误或无效数据)
public POIFSFileSystem(java.io.File file, boolean readOnly) throws java.io.IOException
Creates a POIFSFileSystem from a File. This uses less memory than creating from an InputStream.
Note that with this constructor, you will need to call close()
when you're done to have the underlying file closed, as the file is kept open during normal operation to read the data out.
file
- the File from which to read or read/write the data
(file - 从中读取或读取/写入数据的文件)
readOnly
- whether the POIFileSystem will only be used in read-only mode
(readOnly - POIFileSystem 是否仅用于只读模式)
java.io.IOException
- on errors reading, or on invalid data
(java.io.IOException - 读取错误或无效数据)
public POIFSFileSystem(java.nio.channels.FileChannel channel) throws java.io.IOException
Creates a POIFSFileSystem from an open FileChannel. This uses less memory than creating from an InputStream. The stream will be used in read-only mode.
Note that with this constructor, you will need to call close()
when you're done to have the underlying Channel closed, as the channel is kept open during normal operation to read the data out.
channel
- the FileChannel from which to read the data
(channel - 从中读取数据的 FileChannel)
java.io.IOException
- on errors reading, or on invalid data
(java.io.IOException - 读取错误或无效数据)
public POIFSFileSystem(java.nio.channels.FileChannel channel, boolean readOnly) throws java.io.IOException
Creates a POIFSFileSystem from an open FileChannel. This uses less memory than creating from an InputStream.
Note that with this constructor, you will need to call close()
when you're done to have the underlying Channel closed, as the channel is kept open during normal operation to read the data out.
channel
- the FileChannel from which to read or read/write the data
(channel - 从中读取或读取/写入数据的 FileChannel)
readOnly
- whether the POIFileSystem will only be used in read-only mode
(readOnly - POIFileSystem 是否仅用于只读模式)
java.io.IOException
- on errors reading, or on invalid data
(java.io.IOException - 读取错误或无效数据)
public POIFSFileSystem(java.io.InputStream stream) throws java.io.IOException
Some streams are usable after reaching EOF (typically those that return true
for markSupported()). In the unlikely case that the caller has such a stream and needs to use it after this constructor completes, a work around is to wrap the stream in order to trap the close() call. A convenience method ( createNonClosingInputStream()) has been provided for this purpose:
InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is); HSSFWorkbook wb = new HSSFWorkbook(wrappedStream); is.reset(); doSomethingElse(is);Note also the special case of ByteArrayInputStream for which the close() method does nothing.
ByteArrayInputStream bais = ... HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() ! bais.reset(); // no problem doSomethingElse(bais);(从 InputStream 创建 POIFSFileSystem。通常读取流直到 EOF。流始终关闭。一些流在达到 EOF 后可用(通常是那些为 markSupported() 返回 true 的流)。在调用者有这样一个流并且需要在此构造函数完成后使用它的不太可能的情况下,解决方法是包装流以捕获 close() 调用。为此提供了一个方便的方法(createNonClosingInputStream()): InputStream WrappedStream = POIFSFileSystem.createNonClosingInputStream(is); HSSFWorkbook wb = new HSSFWorkbook(wrappedStream); is.reset(); doSomethingElse(is);还要注意 ByteArrayInputStream 的特殊情况,close() 方法对此什么都不做。 ByteArrayInputStream bais = ... HSSFWorkbook wb = new HSSFWorkbook(bais); // 调用 bais.close() ! bais.reset(); // 没问题 doSomethingElse(bais);)
stream
- the InputStream from which to read the data
(stream - 从中读取数据的 InputStream)
java.io.IOException
- on errors reading, or on invalid data
(java.io.IOException - 读取错误或无效数据)
protected void createNewDataSource()
protected java.nio.ByteBuffer getBlockAt(int offset) throws java.io.IOException
getBlockAt
in class
BlockStore
(类 BlockStore 中的 getBlockAt)
java.io.IOException
(java.io.IOException)
protected java.nio.ByteBuffer createBlockIfNeeded(int offset) throws java.io.IOException
createBlockIfNeeded
in class
BlockStore
(类 BlockStore 中的 createBlockIfNeeded)
java.io.IOException
(java.io.IOException)
protected BATBlock.BATBlockAndIndex getBATBlockAndIndex(int offset)
getBATBlockAndIndex
in class
BlockStore
(类 BlockStore 中的 getBATBlockAndIndex)
protected int getNextBlock(int offset)
getNextBlock
in class
BlockStore
(类 BlockStore 中的 getNextBlock)
protected void setNextBlock(int offset, int nextBlock)
setNextBlock
in class
BlockStore
(类 BlockStore 中的 setNextBlock)
protected int getFreeBlock() throws java.io.IOException
getFreeBlock
in class
BlockStore
(类 BlockStore 中的 getFreeBlock)
java.io.IOException
(java.io.IOException)
protected long size() throws java.io.IOException
java.io.IOException
(java.io.IOException)
protected BlockStore.ChainLoopDetector getChainLoopDetector() throws java.io.IOException
BlockStore
getChainLoopDetector
in class
BlockStore
(类 BlockStore 中的 getChainLoopDetector)
java.io.IOException
(java.io.IOException)
public DocumentEntry createDocument(java.io.InputStream stream, java.lang.String name) throws java.io.IOException
stream
- the InputStream from which the document's data will be obtained
(stream - 将从中获取文档数据的 InputStream)
name
- the name of the new POIFSDocument
(name - 新 POIFSDocument 的名称)
java.io.IOException
- on error creating the new POIFSDocument
(java.io.IOException - 创建新 POIFSDocument 时出错)
public DocumentEntry createDocument(java.lang.String name, int size, POIFSWriterListener writer) throws java.io.IOException
name
- the name of the new DocumentEntry
(name - 新 DocumentEntry 的名称)
size
- the size of the new DocumentEntry
(size - 新 DocumentEntry 的大小)
writer
- the writer of the new DocumentEntry
(writer - 新 DocumentEntry 的作者)
java.io.IOException
- if the writer exceeds the given size
(java.io.IOException - 如果 writer 超出给定大小)
public DirectoryEntry createDirectory(java.lang.String name) throws java.io.IOException
name
- the name of the new DirectoryEntry
(name - 新 DirectoryEntry 的名称)
java.io.IOException
- on name duplication
(java.io.IOException - 关于名称重复)
public DocumentEntry createOrUpdateDocument(java.io.InputStream stream, java.lang.String name) throws java.io.IOException
stream
- the InputStream from which the document's data will be obtained
(stream - 将从中获取文档数据的 InputStream)
name
- the name of the new or existing POIFSDocument
(name - 新的或现有 POIFSDocument 的名称)
java.io.IOException
- on error populating the POIFSDocument
(java.io.IOException - 填充 POIFSDocument 时出错)
public boolean isInPlaceWriteable()
writeFilesystem()
? If false, only writing out to a brand new file via
writeFilesystem(OutputStream)
is supported.
(文件系统是否支持通过 writeFilesystem() 就地写入?如果为 false,则仅支持通过 writeFilesystem(OutputStream) 写入全新文件。)
public void writeFilesystem() throws java.io.IOException
IllegalArgumentException
if opened from an
InputStream
.
(将文件系统写入打开的文件。如果从 InputStream 中打开,将引发 IllegalArgumentException。)
java.io.IOException
- thrown on errors writing to the stream
(java.io.IOException - 写入流时抛出错误)
public void writeFilesystem(java.io.OutputStream stream) throws java.io.IOException
stream
- the OutputStream to which the filesystem will be written
(stream - 文件系统将被写入的 OutputStream)
java.io.IOException
- thrown on errors writing to the stream
(java.io.IOException - 写入流时抛出错误)
public void close() throws java.io.IOException
close
in interface
java.io.Closeable
(在接口 java.io.Closeable 中关闭)
close
in interface
java.lang.AutoCloseable
(在接口 java.lang.AutoCloseable 中关闭)
java.io.IOException
(java.io.IOException)
public static void main(java.lang.String[] args) throws java.io.IOException
args
- names of the files; arg[ 0 ] is the input file, arg[ 1 ] is the output file
(args - 文件名; arg[0]是输入文件,arg[1]是输出文件)
java.io.IOException
(java.io.IOException)
public DirectoryNode getRoot()
public DocumentInputStream createDocumentInputStream(java.lang.String documentName) throws java.io.IOException
documentName
- the name of the document to be opened
(documentName - 要打开的文档的名称)
java.io.IOException
- if the document does not exist or the name is that of a DirectoryEntry
(java.io.IOException - 如果文档不存在或名称是 DirectoryEntry 的名称)
public java.lang.Object[] getViewableArray()
getViewableArray
in interface
POIFSViewable
(接口 POIFSViewable 中的 getViewableArray)
public java.util.Iterator<java.lang.Object> getViewableIterator()
getViewableIterator
in interface
POIFSViewable
(接口 POIFSViewable 中的 getViewableIterator)
public boolean preferArray()
preferArray
in interface
POIFSViewable
(接口 POIFSViewable 中的 preferArray)
public java.lang.String getShortDescription()
getShortDescription
in interface
POIFSViewable
(接口 POIFSViewable 中的 getShortDescription)
public int getBigBlockSize()
public POIFSBigBlockSize getBigBlockSizeDetails()
public static POIFSFileSystem create(java.io.File file) throws java.io.IOException
POIFSFileSystem
in a new
File
. Use
POIFSFileSystem(File)
to open an existing File, this should only be used to create a new empty filesystem.
(在新文件中创建新的 POIFSFileSystem。使用 POIFSFileSystem(File) 打开一个现有的文件,这应该只用于创建一个新的空文件系统。)
file
- The file to create and open
(file - 要创建和打开的文件)
POIFSFileSystem
(创建并打开的 POIFSFileSystem)
java.io.IOException
(java.io.IOException)
protected int getBlockStoreBlockSize()
BlockStore
getBlockStoreBlockSize
in class
BlockStore
(类 BlockStore 中的 getBlockStoreBlockSize)
@Internal public PropertyTable getPropertyTable()
@Internal public HeaderBlock getHeaderBlock()
protected void releaseBuffer(java.nio.ByteBuffer buffer)
BlockStore
releaseBuffer
in class
BlockStore
(类 BlockStore 中的 releaseBuffer)
buffer
- the buffer
(缓冲区 - 缓冲区)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.