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.

PDFStream.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.io.OutputStreamWriter;
  23. import java.io.Writer;
  24. import java.security.MessageDigest;
  25. import java.security.NoSuchAlgorithmException;
  26. /**
  27. * Class representing a PDF stream.
  28. * <p>
  29. * A derivative of the PDF Object, a PDF Stream has not only a dictionary
  30. * but a stream of PDF commands. The stream of commands is where the real
  31. * work is done, the dictionary just provides information like the stream
  32. * length.
  33. */
  34. public class PDFStream extends AbstractPDFStream {
  35. /**
  36. * The stream of PDF commands
  37. */
  38. protected StreamCache data;
  39. private transient Writer streamWriter;
  40. private transient char[] charBuffer;
  41. /**
  42. * Create an empty stream object
  43. */
  44. public PDFStream() {
  45. setUp();
  46. }
  47. public PDFStream(PDFDictionary dictionary) {
  48. super(dictionary);
  49. setUp();
  50. }
  51. public PDFStream(PDFDictionary dictionary, boolean encodeOnTheFly) {
  52. super(dictionary, encodeOnTheFly);
  53. setUp();
  54. }
  55. public PDFStream(boolean encodeOnTheFly) {
  56. super(encodeOnTheFly);
  57. setUp();
  58. }
  59. private void setUp() {
  60. try {
  61. data = StreamCacheFactory.getInstance().createStreamCache();
  62. this.streamWriter = new OutputStreamWriter(
  63. getBufferOutputStream(), PDFDocument.ENCODING);
  64. //Buffer to minimize calls to the converter
  65. this.streamWriter = new java.io.BufferedWriter(this.streamWriter);
  66. } catch (IOException e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. /**
  71. * Append data to the stream
  72. *
  73. * @param s the string of PDF to add
  74. */
  75. public void add(String s) {
  76. try {
  77. this.streamWriter.write(s);
  78. } catch (IOException ex) {
  79. //TODO throw the exception and catch it elsewhere
  80. ex.printStackTrace();
  81. }
  82. }
  83. /**
  84. * Append data to the stream
  85. *
  86. * @param sb the string buffer of PDF to add
  87. */
  88. public void add(StringBuffer sb) {
  89. try {
  90. int nHave = sb.length();
  91. if (charBuffer == null) {
  92. charBuffer = new char [ nHave * 2 ];
  93. } else {
  94. int nAvail = charBuffer.length;
  95. if (nAvail < nHave) {
  96. int nAlloc = nAvail;
  97. while (nAlloc < nHave) {
  98. nAlloc *= 2;
  99. }
  100. charBuffer = new char [ nAlloc ];
  101. }
  102. }
  103. sb.getChars(0, nHave, charBuffer, 0);
  104. this.streamWriter.write(charBuffer, 0, nHave);
  105. } catch (IOException ex) {
  106. //TODO throw the exception and catch it elsewhere
  107. ex.printStackTrace();
  108. }
  109. }
  110. private void flush() throws IOException {
  111. this.streamWriter.flush();
  112. }
  113. /**
  114. * Returns a Writer that writes to the OutputStream of the buffer.
  115. * @return the Writer
  116. */
  117. public Writer getBufferWriter() {
  118. return this.streamWriter;
  119. }
  120. /**
  121. * Returns an OutputStream that can be used to write to the buffer which is used
  122. * to build up the PDF stream.
  123. * @return the OutputStream
  124. * @throws IOException In case of an I/O problem
  125. */
  126. public OutputStream getBufferOutputStream() throws IOException {
  127. if (this.streamWriter != null) {
  128. flush(); //Just to be sure
  129. }
  130. return this.data.getOutputStream();
  131. }
  132. /**
  133. * Used to set the contents of the PDF stream.
  134. * @param data the contents as a byte array
  135. * @throws IOException in case of an I/O problem
  136. */
  137. public void setData(byte[] data) throws IOException {
  138. this.data.clear();
  139. this.data.write(data);
  140. }
  141. /**
  142. * Returns the size of the content.
  143. * @return size of the content
  144. */
  145. public int getDataLength() {
  146. try {
  147. flush();
  148. return data.getSize();
  149. } catch (Exception e) {
  150. //TODO throw the exception and catch it elsewhere
  151. e.printStackTrace();
  152. return 0;
  153. }
  154. }
  155. /** {@inheritDoc} */
  156. protected int getSizeHint() throws IOException {
  157. flush();
  158. return data.getSize();
  159. }
  160. /** {@inheritDoc} */
  161. protected void outputRawStreamData(OutputStream out) throws IOException {
  162. flush();
  163. data.outputContents(out);
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public int output(OutputStream stream) throws IOException {
  169. final int len = super.output(stream);
  170. //Now that the data has been written, it can be discarded.
  171. // this.data = null;
  172. return len;
  173. }
  174. public String streamHashCode() throws IOException {
  175. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  176. outputRawStreamData(bos);
  177. try {
  178. MessageDigest md = MessageDigest.getInstance("MD5");
  179. byte[] thedigest = md.digest(bos.toByteArray());
  180. StringBuilder hex = new StringBuilder();
  181. for (byte b : thedigest) {
  182. hex.append(String.format("%02x", b));
  183. }
  184. return hex.toString();
  185. } catch (NoSuchAlgorithmException e) {
  186. throw new IOException(e);
  187. }
  188. }
  189. }