diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2018-08-26 11:55:00 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2018-08-26 11:55:00 +0000 |
commit | 96d5fab4836225c0aebb0d71bff4a04ae98858e0 (patch) | |
tree | 9d00b62a855d26156f70ed40ee0805083b999e1b /src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java | |
parent | 636d9495a52686df0f570db57d56a72f12274e3c (diff) | |
download | poi-96d5fab4836225c0aebb0d71bff4a04ae98858e0.tar.gz poi-96d5fab4836225c0aebb0d71bff4a04ae98858e0.zip |
#62649 - Remove OPOIFS
Fix HPSF UTF-8 encoding error with Bug52311.doc while refactoring CopyCompare/EntryUtils
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1839201 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java')
-rw-r--r-- | src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java | 253 |
1 files changed, 82 insertions, 171 deletions
diff --git a/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java b/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java index e61039360d..abb90acc26 100644 --- a/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java +++ b/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java @@ -19,23 +19,19 @@ package org.apache.poi.poifs.eventfilesystem; -import java.io.FileInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.util.Iterator; import org.apache.poi.poifs.filesystem.DocumentInputStream; -import org.apache.poi.poifs.filesystem.OPOIFSDocument; +import org.apache.poi.poifs.filesystem.NPOIFSDocument; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSDocumentPath; import org.apache.poi.poifs.property.DirectoryProperty; +import org.apache.poi.poifs.property.DocumentProperty; +import org.apache.poi.poifs.property.NPropertyTable; import org.apache.poi.poifs.property.Property; -import org.apache.poi.poifs.property.PropertyTable; import org.apache.poi.poifs.property.RootProperty; -import org.apache.poi.poifs.storage.BlockAllocationTableReader; -import org.apache.poi.poifs.storage.BlockList; -import org.apache.poi.poifs.storage.HeaderBlock; -import org.apache.poi.poifs.storage.RawDataBlockList; -import org.apache.poi.poifs.storage.SmallBlockTableReader; import org.apache.poi.util.IOUtils; /** @@ -49,62 +45,54 @@ import org.apache.poi.util.IOUtils; public class POIFSReader { - private final POIFSReaderRegistry registry; - private boolean registryClosed; + private final POIFSReaderRegistry registry = new POIFSReaderRegistry(); + private boolean registryClosed = false; private boolean notifyEmptyDirectories; +// private NPOIFSFileSystem poifs; /** - * Create a POIFSReader + * Read from an InputStream and process the documents we get + * + * @param stream the InputStream from which to read the data + * + * @exception IOException on errors reading, or on invalid data */ - public POIFSReader() - { - registry = new POIFSReaderRegistry(); - registryClosed = false; + public void read(final InputStream stream) throws IOException { + try (NPOIFSFileSystem poifs = new NPOIFSFileSystem(stream)) { + read(poifs); + } } /** - * Read from an InputStream and process the documents we get + * Read from a File and process the documents we get * - * @param stream the InputStream from which to read the data + * @param poifsFile the file from which to read the data * * @exception IOException on errors reading, or on invalid data */ + public void read(final File poifsFile) throws IOException { + try (NPOIFSFileSystem poifs = new NPOIFSFileSystem(poifsFile, true)) { + read(poifs); + } + } - public void read(final InputStream stream) - throws IOException - { + /** + * Read from a NPOIFSFileSystem and process the documents we get + * + * @param poifs the NPOIFSFileSystem from which to read the data + * + * @exception IOException on errors reading, or on invalid data + */ + public void read(final NPOIFSFileSystem poifs) throws IOException { registryClosed = true; - // read the header block from the stream - HeaderBlock header_block = new HeaderBlock(stream); - - // read the rest of the stream into blocks - RawDataBlockList data_blocks = new RawDataBlockList(stream, header_block.getBigBlockSize()); - - // set up the block allocation table (necessary for the - // data_blocks to be manageable - new BlockAllocationTableReader(header_block.getBigBlockSize(), - header_block.getBATCount(), - header_block.getBATArray(), - header_block.getXBATCount(), - header_block.getXBATIndex(), - data_blocks); - // get property table from the document - PropertyTable properties = - new PropertyTable(header_block, data_blocks); + NPropertyTable properties = poifs.getPropertyTable(); // process documents RootProperty root = properties.getRoot(); - processProperties(SmallBlockTableReader - .getSmallDocumentBlocks( - header_block.getBigBlockSize(), - data_blocks, root, - header_block.getSBATStart() - ), - data_blocks, root.getChildren(), new POIFSDocumentPath() - ); + processProperties(poifs, root, new POIFSDocumentPath()); } /** @@ -117,14 +105,11 @@ public class POIFSReader * called */ - public void registerListener(final POIFSReaderListener listener) - { - if (listener == null) - { + public void registerListener(final POIFSReaderListener listener) { + if (listener == null) { throw new NullPointerException(); } - if (registryClosed) - { + if (registryClosed) { throw new IllegalStateException(); } registry.registerListener(listener); @@ -143,9 +128,7 @@ public class POIFSReader * called */ - public void registerListener(final POIFSReaderListener listener, - final String name) - { + public void registerListener(final POIFSReaderListener listener, final String name) { registerListener(listener, null, name); } @@ -166,19 +149,14 @@ public class POIFSReader public void registerListener(final POIFSReaderListener listener, final POIFSDocumentPath path, - final String name) - { - if ((listener == null) || (name == null) || (name.length() == 0)) - { + final String name) { + if ((listener == null) || (name == null) || (name.length() == 0)) { throw new NullPointerException(); } - if (registryClosed) - { + if (registryClosed) { throw new IllegalStateException(); } - registry.registerListener(listener, - (path == null) ? new POIFSDocumentPath() - : path, name); + registry.registerListener(listener, (path == null) ? new POIFSDocumentPath() : path, name); } /** @@ -186,7 +164,7 @@ public class POIFSReader * If this flag is activated, the {@link POIFSReaderListener listener} receives * {@link POIFSReaderEvent POIFSReaderEvents} with nulled {@code name} and {@code stream} * - * @param notifyEmptyDirectories + * @param notifyEmptyDirectories if {@code true}, empty directories will be notified */ public void setNotifyEmptyDirectories(boolean notifyEmptyDirectories) { this.notifyEmptyDirectories = notifyEmptyDirectories; @@ -198,139 +176,72 @@ public class POIFSReader * * @param args names of the files * - * @exception IOException + * @exception IOException if the files can't be read or have invalid content */ - public static void main(String args[]) - throws IOException - { - if (args.length == 0) - { + public static void main(String args[]) throws IOException { + if (args.length == 0) { System.err.println("at least one argument required: input filename(s)"); System.exit(1); } // register for all - for (String arg : args) - { - POIFSReader reader = new POIFSReader(); - POIFSReaderListener listener = new SampleListener(); - - reader.registerListener(listener); + for (String arg : args) { + POIFSReader reader = new POIFSReader(); + reader.registerListener(POIFSReader::readEntry); System.out.println("reading " + arg); - FileInputStream istream = new FileInputStream(arg); - reader.read(istream); - istream.close(); + reader.read(new File(arg)); } } - private void processProperties(final BlockList small_blocks, - final BlockList big_blocks, - final Iterator<Property> properties, - final POIFSDocumentPath path) - throws IOException { - if (!properties.hasNext() && notifyEmptyDirectories) { - Iterator<POIFSReaderListener> listeners = registry.getListeners(path, "."); - while (listeners.hasNext()) { - POIFSReaderListener pl = listeners.next(); - POIFSReaderEvent pe = new POIFSReaderEvent(null, path, null); - pl.processPOIFSReaderEvent(pe); + private static void readEntry(POIFSReaderEvent event) { + POIFSDocumentPath path = event.getPath(); + StringBuilder sb = new StringBuilder(); + + try (DocumentInputStream istream = event.getStream()) { + sb.setLength(0); + int pathLength = path.length(); + for (int k = 0; k < pathLength; k++) { + sb.append("/").append(path.getComponent(k)); } - return; + byte[] data = IOUtils.toByteArray(istream); + sb.append("/").append(event.getName()).append(": ").append(data.length).append(" bytes read"); + System.out.println(sb); + } catch (IOException ignored) { } + } - while (properties.hasNext()) - { - Property property = properties.next(); - String name = property.getName(); + private void processProperties(final NPOIFSFileSystem poifs, DirectoryProperty dir, final POIFSDocumentPath path) { + boolean hasChildren = false; + for (final Property property : dir) { + hasChildren = true; + String name = property.getName(); if (property.isDirectory()) { POIFSDocumentPath new_path = new POIFSDocumentPath(path,new String[]{name}); - DirectoryProperty dp = (DirectoryProperty) property; - processProperties(small_blocks, big_blocks, dp.getChildren(), new_path); + processProperties(poifs, (DirectoryProperty) property, new_path); } else { - int startBlock = property.getStartBlock(); - Iterator<POIFSReaderListener> listeners = registry.getListeners(path, name); - - if (listeners.hasNext()) - { - int size = property.getSize(); - OPOIFSDocument document = null; - - if (property.shouldUseSmallBlocks()) - { - document = - new OPOIFSDocument(name, small_blocks - .fetchBlocks(startBlock, -1), size); - } - else - { - document = - new OPOIFSDocument(name, big_blocks - .fetchBlocks(startBlock, -1), size); - } - while (listeners.hasNext()) - { - POIFSReaderListener listener = listeners.next(); - try (DocumentInputStream dis = new DocumentInputStream(document)) { - listener.processPOIFSReaderEvent(new POIFSReaderEvent(dis, path, name)); - } - } - } - else - { - - // consume the document's data and discard it - if (property.shouldUseSmallBlocks()) - { - small_blocks.fetchBlocks(startBlock, -1); + NPOIFSDocument document = null; + for (POIFSReaderListener rl : registry.getListeners(path, name)) { + if (document == null) { + document = new NPOIFSDocument((DocumentProperty)property, poifs); } - else - { - big_blocks.fetchBlocks(startBlock, -1); + try (DocumentInputStream dis = new DocumentInputStream(document)) { + POIFSReaderEvent pe = new POIFSReaderEvent(dis, path, name); + rl.processPOIFSReaderEvent(pe); } } } } - } - - private static class SampleListener - implements POIFSReaderListener - { - - /** - * Constructor SampleListener - */ - SampleListener() - { + if (hasChildren || !notifyEmptyDirectories) { + return; } - /** - * Method processPOIFSReaderEvent - * - * @param event - */ - - @Override - public void processPOIFSReaderEvent(final POIFSReaderEvent event) { - DocumentInputStream istream = event.getStream(); - POIFSDocumentPath path = event.getPath(); - String name = event.getName(); - - try { - byte[] data = IOUtils.toByteArray(istream); - int pathLength = path.length(); - - for (int k = 0; k < pathLength; k++) { - System.out.print("/" + path.getComponent(k)); - } - System.out.println("/" + name + ": " + data.length + " bytes read"); - } catch (IOException ignored) { - } finally { - IOUtils.closeQuietly(istream); - } + for (POIFSReaderListener rl : registry.getListeners(path, ".")) { + POIFSReaderEvent pe = new POIFSReaderEvent(null, path, null); + rl.processPOIFSReaderEvent(pe); } } } |