Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentOutputStream.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.*;
  17. import java.util.*;
  18. /**
  19. * This class provides a wrapper over an OutputStream so that Document
  20. * writers can't accidently go over their size limits
  21. *
  22. * @author Marc Johnson (mjohnson at apache dot org)
  23. */
  24. public final class DocumentOutputStream extends OutputStream {
  25. private final OutputStream _stream;
  26. private final int _limit;
  27. private int _written;
  28. /**
  29. * Create a DocumentOutputStream
  30. *
  31. * @param stream the OutputStream to which the data is actually
  32. * read
  33. * @param limit the maximum number of bytes that can be written
  34. */
  35. DocumentOutputStream(OutputStream stream, int limit) {
  36. _stream = stream;
  37. _limit = limit;
  38. _written = 0;
  39. }
  40. /**
  41. * Writes the specified byte to this output stream. The general
  42. * contract for write is that one byte is written to the output
  43. * stream. The byte to be written is the eight low-order bits of
  44. * the argument b. The 24 high-order bits of b are ignored.
  45. *
  46. * @param b the byte.
  47. * @exception IOException if an I/O error occurs. In particular,
  48. * an IOException may be thrown if the
  49. * output stream has been closed, or if the
  50. * writer tries to write too much data.
  51. */
  52. public void write(int b)
  53. throws IOException
  54. {
  55. limitCheck(1);
  56. _stream.write(b);
  57. }
  58. /**
  59. * Writes b.length bytes from the specified byte array
  60. * to this output stream.
  61. *
  62. * @param b the data.
  63. * @exception IOException if an I/O error occurs.
  64. */
  65. public void write(byte b[])
  66. throws IOException
  67. {
  68. write(b, 0, b.length);
  69. }
  70. /**
  71. * Writes len bytes from the specified byte array starting at
  72. * offset off to this output stream. The general contract for
  73. * write(b, off, len) is that some of the bytes in the array b are
  74. * written to the output stream in order; element b[off] is the
  75. * first byte written and b[off+len-1] is the last byte written by
  76. * this operation.<p>
  77. * If b is null, a NullPointerException is thrown.<p>
  78. * If off is negative, or len is negative, or off+len is greater
  79. * than the length of the array b, then an
  80. * IndexOutOfBoundsException is thrown.
  81. *
  82. * @param b the data.
  83. * @param off the start offset in the data.
  84. * @param len the number of bytes to write.
  85. * @exception IOException if an I/O error occurs. In particular,
  86. * an IOException</code> is thrown if the
  87. * output stream is closed or if the writer
  88. * tries to write too many bytes.
  89. */
  90. public void write(byte b[], int off, int len)
  91. throws IOException
  92. {
  93. limitCheck(len);
  94. _stream.write(b, off, len);
  95. }
  96. /**
  97. * Flushes this output stream and forces any buffered output bytes
  98. * to be written out.
  99. *
  100. * @exception IOException if an I/O error occurs.
  101. */
  102. public void flush()
  103. throws IOException
  104. {
  105. _stream.flush();
  106. }
  107. /**
  108. * Closes this output stream and releases any system resources
  109. * associated with this stream. The general contract of close is
  110. * that it closes the output stream. A closed stream cannot
  111. * perform output operations and cannot be reopened.
  112. *
  113. * @exception IOException if an I/O error occurs.
  114. */
  115. public void close() {
  116. // ignore this call
  117. }
  118. /**
  119. * write the rest of the document's data (fill in at the end)
  120. *
  121. * @param totalLimit the actual number of bytes the corresponding
  122. * document must fill
  123. * @param fill the byte to fill remaining space with
  124. *
  125. * @exception IOException on I/O error
  126. */
  127. void writeFiller(int totalLimit, byte fill)
  128. throws IOException
  129. {
  130. if (totalLimit > _written)
  131. {
  132. byte[] filler = new byte[ totalLimit - _written ];
  133. Arrays.fill(filler, fill);
  134. _stream.write(filler);
  135. }
  136. }
  137. private void limitCheck(int toBeWritten)
  138. throws IOException
  139. {
  140. if ((_written + toBeWritten) > _limit)
  141. {
  142. throw new IOException("tried to write too much data");
  143. }
  144. _written += toBeWritten;
  145. }
  146. }