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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import java.io.OutputStream;
  19. import java.io.IOException;
  20. /**
  21. * Class representing a PDF stream.
  22. * <p>
  23. * A derivative of the PDF Object, a PDF Stream has not only a dictionary
  24. * but a stream of PDF commands. The stream of commands is where the real
  25. * work is done, the dictionary just provides information like the stream
  26. * length.
  27. */
  28. public class PDFStream extends AbstractPDFStream {
  29. /**
  30. * The stream of PDF commands
  31. */
  32. protected StreamCache data;
  33. /**
  34. * Create an empty stream object
  35. */
  36. public PDFStream() {
  37. super();
  38. try {
  39. data = StreamCacheFactory.getInstance().createStreamCache();
  40. } catch (IOException ex) {
  41. /**@todo Log with Logger */
  42. ex.printStackTrace();
  43. }
  44. }
  45. /**
  46. * Append data to the stream
  47. *
  48. * @param s the string of PDF to add
  49. */
  50. public void add(String s) {
  51. try {
  52. data.getOutputStream().write(s.getBytes());
  53. } catch (IOException ex) {
  54. /**@todo Log with Logger */
  55. ex.printStackTrace();
  56. }
  57. }
  58. /**
  59. * Used to set the contents of the PDF stream.
  60. * @param data the contents as a byte array
  61. * @throws IOException in case of an I/O problem
  62. */
  63. public void setData(byte[] data) throws IOException {
  64. this.data.clear();
  65. this.data.write(data);
  66. }
  67. /**
  68. * Returns the size of the content.
  69. * @return size of the content
  70. */
  71. public int getDataLength() {
  72. try {
  73. return data.getSize();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. return 0;
  77. }
  78. }
  79. /**
  80. * @see org.apache.fop.pdf.AbstractPDFStream#getSizeHint()
  81. */
  82. protected int getSizeHint() throws IOException {
  83. return data.getSize();
  84. }
  85. /**
  86. * @see org.apache.fop.pdf.AbstractPDFStream#outputRawStreamData(OutputStream)
  87. */
  88. protected void outputRawStreamData(OutputStream out) throws IOException {
  89. data.outputContents(out);
  90. }
  91. /**
  92. * @see org.apache.fop.pdf.PDFObject#output(OutputStream)
  93. */
  94. protected int output(OutputStream stream) throws IOException {
  95. final int len = super.output(stream);
  96. //Now that the data has been written, it can be discarded.
  97. this.data = null;
  98. return len;
  99. }
  100. }