You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POIFSDocument.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import static java.util.Collections.emptyIterator;
  17. import java.io.BufferedInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.nio.ByteBuffer;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.Iterator;
  25. import org.apache.poi.poifs.common.POIFSConstants;
  26. import org.apache.poi.poifs.dev.POIFSViewable;
  27. import org.apache.poi.poifs.property.DocumentProperty;
  28. import org.apache.poi.util.HexDump;
  29. import org.apache.poi.util.IOUtils;
  30. /**
  31. * This class manages a document in the NIO POIFS filesystem.
  32. * This is the {@link POIFSFileSystem} version.
  33. */
  34. public final class POIFSDocument implements POIFSViewable, Iterable<ByteBuffer> {
  35. private DocumentProperty _property;
  36. private POIFSFileSystem _filesystem;
  37. private POIFSStream _stream;
  38. private int _block_size;
  39. /**
  40. * Constructor for an existing Document
  41. */
  42. public POIFSDocument(DocumentNode document) {
  43. this((DocumentProperty) document.getProperty(),
  44. ((DirectoryNode) document.getParent()).getFileSystem());
  45. }
  46. /**
  47. * Constructor for an existing Document
  48. */
  49. public POIFSDocument(DocumentProperty property, POIFSFileSystem filesystem) {
  50. this._property = property;
  51. this._filesystem = filesystem;
  52. if (property.getSize() < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
  53. _stream = new POIFSStream(_filesystem.getMiniStore(), property.getStartBlock());
  54. _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
  55. } else {
  56. _stream = new POIFSStream(_filesystem, property.getStartBlock());
  57. _block_size = _filesystem.getBlockStoreBlockSize();
  58. }
  59. }
  60. /**
  61. * Constructor for a new Document
  62. *
  63. * @param name the name of the POIFSDocument
  64. * @param stream the InputStream we read data from
  65. */
  66. public POIFSDocument(String name, POIFSFileSystem filesystem, InputStream stream)
  67. throws IOException {
  68. this._filesystem = filesystem;
  69. // Store it
  70. int length = store(stream);
  71. // Build the property for it
  72. this._property = new DocumentProperty(name, length);
  73. _property.setStartBlock(_stream.getStartBlock());
  74. _property.setDocument(this);
  75. }
  76. public POIFSDocument(String name, final int size, POIFSFileSystem filesystem, POIFSWriterListener writer)
  77. throws IOException {
  78. this._filesystem = filesystem;
  79. if (size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
  80. _stream = new POIFSStream(filesystem.getMiniStore());
  81. _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
  82. } else {
  83. _stream = new POIFSStream(filesystem);
  84. _block_size = _filesystem.getBlockStoreBlockSize();
  85. }
  86. this._property = new DocumentProperty(name, size);
  87. _property.setStartBlock(_stream.getStartBlock());
  88. _property.setDocument(this);
  89. try (DocumentOutputStream os = new DocumentOutputStream(this, size)) {
  90. POIFSDocumentPath path = new POIFSDocumentPath(name.split("\\\\"));
  91. String docName = path.getComponent(path.length() - 1);
  92. POIFSWriterEvent event = new POIFSWriterEvent(os, path, docName, size);
  93. writer.processPOIFSWriterEvent(event);
  94. }
  95. }
  96. /**
  97. * Stores the given data for this Document
  98. */
  99. private int store(InputStream stream) throws IOException {
  100. final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
  101. BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize + 1);
  102. bis.mark(bigBlockSize);
  103. // Do we need to store as a mini stream or a full one?
  104. long streamBlockSize = IOUtils.skipFully(bis, bigBlockSize);
  105. if (streamBlockSize < bigBlockSize) {
  106. _stream = new POIFSStream(_filesystem.getMiniStore());
  107. _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
  108. } else {
  109. _stream = new POIFSStream(_filesystem);
  110. _block_size = _filesystem.getBlockStoreBlockSize();
  111. }
  112. // start from the beginning
  113. bis.reset();
  114. // Store it
  115. final long length;
  116. try (OutputStream os = _stream.getOutputStream()) {
  117. length = IOUtils.copy(bis, os);
  118. // Pad to the end of the block with -1s
  119. int usedInBlock = (int) (length % _block_size);
  120. if (usedInBlock != 0 && usedInBlock != _block_size) {
  121. int toBlockEnd = _block_size - usedInBlock;
  122. byte[] padding = IOUtils.safelyAllocate(toBlockEnd, POIFSFileSystem.getMaxRecordLength());
  123. Arrays.fill(padding, (byte) 0xFF);
  124. os.write(padding);
  125. }
  126. }
  127. return Math.toIntExact(length);
  128. }
  129. /**
  130. * Frees the underlying stream and property
  131. */
  132. void free() throws IOException {
  133. _stream.free();
  134. _property.setStartBlock(POIFSConstants.END_OF_CHAIN);
  135. }
  136. POIFSFileSystem getFileSystem() {
  137. return _filesystem;
  138. }
  139. int getDocumentBlockSize() {
  140. return _block_size;
  141. }
  142. @Override
  143. public Iterator<ByteBuffer> iterator() {
  144. return getBlockIterator();
  145. }
  146. Iterator<ByteBuffer> getBlockIterator() {
  147. return (getSize() > 0 ? _stream : Collections.<ByteBuffer>emptyList()).iterator();
  148. }
  149. /**
  150. * @return size of the document
  151. */
  152. public int getSize() {
  153. return _property.getSize();
  154. }
  155. public void replaceContents(InputStream stream) throws IOException {
  156. free();
  157. int size = store(stream);
  158. _property.setStartBlock(_stream.getStartBlock());
  159. _property.updateSize(size);
  160. }
  161. /**
  162. * @return the instance's DocumentProperty
  163. */
  164. DocumentProperty getDocumentProperty() {
  165. return _property;
  166. }
  167. /**
  168. * Get an array of objects, some of which may implement POIFSViewable
  169. *
  170. * @return an array of Object; may not be null, but may be empty
  171. */
  172. public Object[] getViewableArray() {
  173. String result = "<NO DATA>";
  174. if (getSize() > 0) {
  175. // Get all the data into a single array
  176. byte[] data = IOUtils.safelyAllocate(getSize(), POIFSFileSystem.getMaxRecordLength());
  177. int offset = 0;
  178. for (ByteBuffer buffer : _stream) {
  179. int length = Math.min(_block_size, data.length - offset);
  180. buffer.get(data, offset, length);
  181. offset += length;
  182. }
  183. result = HexDump.dump(data, 0, 0);
  184. }
  185. return new String[]{result};
  186. }
  187. /**
  188. * Get an Iterator of objects, some of which may implement POIFSViewable
  189. *
  190. * @return an Iterator; may not be null, but may have an empty back end
  191. * store
  192. */
  193. public Iterator<Object> getViewableIterator() {
  194. return emptyIterator();
  195. }
  196. /**
  197. * Give viewers a hint as to whether to call getViewableArray or
  198. * getViewableIterator
  199. *
  200. * @return <code>true</code> if a viewer should call getViewableArray,
  201. * <code>false</code> if a viewer should call getViewableIterator
  202. */
  203. public boolean preferArray() {
  204. return true;
  205. }
  206. /**
  207. * Provides a short description of the object, to be used when a
  208. * POIFSViewable object has not provided its contents.
  209. *
  210. * @return short description
  211. */
  212. public String getShortDescription() {
  213. return "Document: \"" + _property.getName() + "\" size = " + getSize();
  214. }
  215. }