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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.OutputStream;
  20. import java.io.IOException;
  21. /**
  22. * Class representing a PDF stream.
  23. * <p>
  24. * A derivative of the PDF Object, a PDF Stream has not only a dictionary
  25. * but a stream of PDF commands. The stream of commands is where the real
  26. * work is done, the dictionary just provides information like the stream
  27. * length.
  28. */
  29. public class PDFStream extends AbstractPDFStream {
  30. /**
  31. * The stream of PDF commands
  32. */
  33. protected StreamCache data;
  34. /**
  35. * Create an empty stream object
  36. */
  37. public PDFStream() {
  38. super();
  39. try {
  40. data = StreamCacheFactory.getInstance().createStreamCache();
  41. } catch (IOException ex) {
  42. //TODO throw the exception and catch it elsewhere
  43. ex.printStackTrace();
  44. }
  45. }
  46. /**
  47. * Append data to the stream
  48. *
  49. * @param s the string of PDF to add
  50. */
  51. public void add(String s) {
  52. try {
  53. data.getOutputStream().write(PDFDocument.encode(s));
  54. } catch (IOException ex) {
  55. //TODO throw the exception and catch it elsewhere
  56. ex.printStackTrace();
  57. }
  58. }
  59. /**
  60. * Returns an OutputStream that can be used to write to the buffer which is used
  61. * to build up the PDF stream.
  62. * @return the OutputStream
  63. * @throws IOException In case of an I/O problem
  64. */
  65. public OutputStream getBufferOutputStream() throws IOException {
  66. return this.data.getOutputStream();
  67. }
  68. /**
  69. * Used to set the contents of the PDF stream.
  70. * @param data the contents as a byte array
  71. * @throws IOException in case of an I/O problem
  72. */
  73. public void setData(byte[] data) throws IOException {
  74. this.data.clear();
  75. this.data.write(data);
  76. }
  77. /**
  78. * Returns the size of the content.
  79. * @return size of the content
  80. */
  81. public int getDataLength() {
  82. try {
  83. return data.getSize();
  84. } catch (Exception e) {
  85. //TODO throw the exception and catch it elsewhere
  86. e.printStackTrace();
  87. return 0;
  88. }
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. protected int getSizeHint() throws IOException {
  94. return data.getSize();
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. protected void outputRawStreamData(OutputStream out) throws IOException {
  100. data.outputContents(out);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. protected int output(OutputStream stream) throws IOException {
  106. final int len = super.output(stream);
  107. //Now that the data has been written, it can be discarded.
  108. this.data = null;
  109. return len;
  110. }
  111. }