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.

DocumentOutputStream.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.IOException;
  17. import java.io.OutputStream;
  18. import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
  19. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  20. import org.apache.poi.poifs.common.POIFSConstants;
  21. import org.apache.poi.poifs.property.DocumentProperty;
  22. /**
  23. * This class provides methods to write a DocumentEntry managed by a
  24. * {@link POIFSFileSystem} instance.
  25. */
  26. public final class DocumentOutputStream extends OutputStream {
  27. /** the Document's size, i.e. the size of the big block data - mini block data is cached and not counted */
  28. private int _document_size = 0;
  29. /** have we been closed? */
  30. private boolean _closed = false;
  31. /** the actual Document */
  32. private final POIFSDocument _document;
  33. /** and its Property */
  34. private final DocumentProperty _property;
  35. /** our buffer, when null we're into normal blocks */
  36. private UnsynchronizedByteArrayOutputStream _buffer =
  37. new UnsynchronizedByteArrayOutputStream(POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE);
  38. /** our main block stream, when we're into normal blocks */
  39. private POIFSStream _stream;
  40. private OutputStream _stream_output;
  41. /** a write limit or -1 if unlimited */
  42. private final long _limit;
  43. /**
  44. * Create an OutputStream from the specified DocumentEntry.
  45. * The specified entry will be emptied.
  46. *
  47. * @param document the DocumentEntry to be written
  48. */
  49. public DocumentOutputStream(DocumentEntry document) throws IOException {
  50. this(document, -1);
  51. }
  52. /**
  53. * Create an OutputStream to create the specified new Entry
  54. *
  55. * @param parent Where to create the Entry
  56. * @param name Name of the new entry
  57. */
  58. public DocumentOutputStream(DirectoryEntry parent, String name) throws IOException {
  59. this(createDocument(parent, name), -1);
  60. }
  61. /**
  62. * Create a DocumentOutputStream
  63. *
  64. * @param document the DocumentEntry to which the data is actually written
  65. * @param limit the maximum number of bytes that can be written
  66. */
  67. DocumentOutputStream(DocumentEntry document, long limit) throws IOException {
  68. this(getDocument(document), limit);
  69. }
  70. DocumentOutputStream(POIFSDocument document, long limit) throws IOException {
  71. _document = document;
  72. _document.free();
  73. _property = document.getDocumentProperty();
  74. _limit = limit;
  75. }
  76. private static POIFSDocument getDocument(DocumentEntry document) throws IOException {
  77. if (!(document instanceof DocumentNode)) {
  78. throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
  79. }
  80. return new POIFSDocument((DocumentNode)document);
  81. }
  82. private static DocumentEntry createDocument(DirectoryEntry parent, String name) throws IOException {
  83. if (!(parent instanceof DirectoryNode)) {
  84. throw new IOException("Cannot open internal directory storage, " + parent + " not a Directory Node");
  85. }
  86. // Have an empty one created for now
  87. return parent.createDocument(name, new UnsynchronizedByteArrayInputStream(new byte[0]));
  88. }
  89. private void checkBufferSize() throws IOException {
  90. // Have we gone over the mini stream limit yet?
  91. if (_buffer.size() > POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
  92. // Will need to be in the main stream
  93. byte[] data = _buffer.toByteArray();
  94. _buffer = null;
  95. write(data, 0, data.length);
  96. }
  97. // otherwise mini stream will work, keep going
  98. }
  99. @Override
  100. public void write(int b) throws IOException {
  101. write(new byte[] { (byte)b }, 0, 1);
  102. }
  103. @Override
  104. public void write(byte[] b, int off, int len) throws IOException {
  105. if (_closed) {
  106. throw new IOException("cannot perform requested operation on a closed stream");
  107. }
  108. if (_limit > -1 && (size() + len) > _limit) {
  109. throw new IOException("tried to write too much data");
  110. }
  111. if (_buffer != null) {
  112. _buffer.write(b, off, len);
  113. checkBufferSize();
  114. } else {
  115. if (_stream == null) {
  116. _stream = new POIFSStream(_document.getFileSystem());
  117. _stream_output = _stream.getOutputStream();
  118. }
  119. _stream_output.write(b, off, len);
  120. _document_size += len;
  121. }
  122. }
  123. @Override
  124. public void close() throws IOException {
  125. // Do we have a pending buffer for the mini stream?
  126. if (_buffer != null) {
  127. // It's not much data, so ask POIFSDocument to do it for us
  128. _document.replaceContents(_buffer.toInputStream());
  129. }
  130. else {
  131. // We've been writing to the stream as we've gone along
  132. // Update the details on the property now
  133. _stream_output.close();
  134. _property.updateSize(_document_size);
  135. _property.setStartBlock(_stream.getStartBlock());
  136. }
  137. // No more!
  138. _closed = true;
  139. }
  140. /**
  141. * @return the amount of written bytes
  142. */
  143. public long size() {
  144. return _document_size + (_buffer == null ? 0L : _buffer.size());
  145. }
  146. }