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.

NPOIFSDocument.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.nio.ByteBuffer;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import org.apache.poi.poifs.common.POIFSConstants;
  24. import org.apache.poi.poifs.dev.POIFSViewable;
  25. import org.apache.poi.poifs.property.DocumentProperty;
  26. import org.apache.poi.util.HexDump;
  27. import org.apache.poi.util.IOUtils;
  28. /**
  29. * This class manages a document in the NIO POIFS filesystem.
  30. * This is the {@link NPOIFSFileSystem} version.
  31. */
  32. public final class NPOIFSDocument implements POIFSViewable {
  33. private DocumentProperty _property;
  34. private NPOIFSFileSystem _filesystem;
  35. private NPOIFSStream _stream;
  36. private int _block_size;
  37. /**
  38. * Constructor for an existing Document
  39. */
  40. public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem)
  41. throws IOException
  42. {
  43. this._property = property;
  44. this._filesystem = filesystem;
  45. if(property.getSize() <= POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
  46. _stream = new NPOIFSStream(_filesystem.getMiniStore(), property.getStartBlock());
  47. _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
  48. } else {
  49. _stream = new NPOIFSStream(_filesystem, property.getStartBlock());
  50. _block_size = _filesystem.getBlockStoreBlockSize();
  51. }
  52. }
  53. /**
  54. * Constructor for a new Document
  55. *
  56. * @param name the name of the POIFSDocument
  57. * @param stream the InputStream we read data from
  58. */
  59. public NPOIFSDocument(String name, NPOIFSFileSystem filesystem, InputStream stream)
  60. throws IOException
  61. {
  62. this._filesystem = filesystem;
  63. // Buffer the contents into memory. This is a bit icky...
  64. // TODO Replace with a buffer up to the mini stream size, then streaming write
  65. byte[] contents;
  66. if(stream instanceof ByteArrayInputStream) {
  67. ByteArrayInputStream bais = (ByteArrayInputStream)stream;
  68. contents = new byte[bais.available()];
  69. bais.read(contents);
  70. } else {
  71. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  72. IOUtils.copy(stream, baos);
  73. contents = baos.toByteArray();
  74. }
  75. // Do we need to store as a mini stream or a full one?
  76. if(contents.length <= POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
  77. _stream = new NPOIFSStream(filesystem.getMiniStore());
  78. _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
  79. } else {
  80. _stream = new NPOIFSStream(filesystem);
  81. _block_size = _filesystem.getBlockStoreBlockSize();
  82. }
  83. // Store it
  84. _stream.updateContents(contents);
  85. // And build the property for it
  86. this._property = new DocumentProperty(name, contents.length);
  87. _property.setStartBlock(_stream.getStartBlock());
  88. }
  89. int getDocumentBlockSize() {
  90. return _block_size;
  91. }
  92. Iterator<ByteBuffer> getBlockIterator() {
  93. return _stream.getBlockIterator();
  94. }
  95. /**
  96. * @return size of the document
  97. */
  98. public int getSize() {
  99. return _property.getSize();
  100. }
  101. /**
  102. * @return the instance's DocumentProperty
  103. */
  104. DocumentProperty getDocumentProperty() {
  105. return _property;
  106. }
  107. /**
  108. * Get an array of objects, some of which may implement POIFSViewable
  109. *
  110. * @return an array of Object; may not be null, but may be empty
  111. */
  112. public Object[] getViewableArray() {
  113. Object[] results = new Object[1];
  114. String result;
  115. try {
  116. if(getSize() > 0) {
  117. // Get all the data into a single array
  118. byte[] data = new byte[getSize()];
  119. int offset = 0;
  120. for(ByteBuffer buffer : _stream) {
  121. int length = Math.min(_block_size, data.length-offset);
  122. buffer.get(data, offset, length);
  123. offset += length;
  124. }
  125. ByteArrayOutputStream output = new ByteArrayOutputStream();
  126. HexDump.dump(data, 0, output, 0);
  127. result = output.toString();
  128. } else {
  129. result = "<NO DATA>";
  130. }
  131. } catch (IOException e) {
  132. result = e.getMessage();
  133. }
  134. results[0] = result;
  135. return results;
  136. }
  137. /**
  138. * Get an Iterator of objects, some of which may implement POIFSViewable
  139. *
  140. * @return an Iterator; may not be null, but may have an empty back end
  141. * store
  142. */
  143. public Iterator getViewableIterator() {
  144. return Collections.EMPTY_LIST.iterator();
  145. }
  146. /**
  147. * Give viewers a hint as to whether to call getViewableArray or
  148. * getViewableIterator
  149. *
  150. * @return <code>true</code> if a viewer should call getViewableArray,
  151. * <code>false</code> if a viewer should call getViewableIterator
  152. */
  153. public boolean preferArray() {
  154. return true;
  155. }
  156. /**
  157. * Provides a short description of the object, to be used when a
  158. * POIFSViewable object has not provided its contents.
  159. *
  160. * @return short description
  161. */
  162. public String getShortDescription() {
  163. StringBuffer buffer = new StringBuffer();
  164. buffer.append("Document: \"").append(_property.getName()).append("\"");
  165. buffer.append(" size = ").append(getSize());
  166. return buffer.toString();
  167. }
  168. }