Debian Codegraph @

src/main/java/org/jfree/pdf/stream/Stream.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.stream;
33
34 import org.jfree.pdf.filter.Filter;
35 import org.jfree.pdf.util.Args;
36 import java.io.ByteArrayOutputStream;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import org.jfree.pdf.dictionary.Dictionary;
41 import org.jfree.pdf.internal.PDFObject;
42 import org.jfree.pdf.util.PDFUtils;
43
44 /**
45 * A {@code Stream} is a {@link PDFObject} that has a {@link Dictionary}
46 * and a byte stream.
47 */
48 public abstract class Stream extends PDFObject {
49
50 /** Filters (if any) to apply to the stream data. */
51 private final List<Filter> filters;
52
53 /**
54 * Creates a new stream.
55 *
56 * @param number the PDF object number.
57 */
58 Stream(int number) {
59 super(number);
60 this.filters = new ArrayList<>();
61 }
62
63 /**
64 * Adds a filter to the stream.
65 *
66 * @param f the filter ({@code null} not permitted).
67 *
68 * @see #removeFilters()
69 */
70 public void addFilter(Filter f) {
71 Args.nullNotPermitted(f, "f");
72 this.filters.add(f);
73 }
74
75 /**
76 * Removes any filters that were previously added.
77 *
78 * @see #addFilter(org.jfree.pdf.filter.Filter)
79 */
80 public void removeFilters() {
81 this.filters.clear();
82 }
83
84 /**
85 * Returns the PDF bytes for this stream object, with all current filters
86 * applied.
87 *
88 * @return The PDF bytes for this stream object.
89 *
90 * @throws IOException if there is a problem writing to the byte array.
91 */
92 @Override
93 public byte[] getObjectBytes() throws IOException {
94 ByteArrayOutputStream baos = new ByteArrayOutputStream();
95 byte[] streamData = getRawStreamData();
96 for (Filter f: this.filters) {
97 streamData = f.encode(streamData);
98 }
99 Dictionary dictionary = createDictionary(streamData.length);
100 baos.write(dictionary.toPDFBytes());
101 baos.write(PDFUtils.toBytes("stream\n"));
102 baos.write(streamData);
103 baos.write(PDFUtils.toBytes("endstream\n"));
104 return baos.toByteArray();
105 }
106
107 /**
108 * Creates the dictionary for this stream object. The dictionary will
109 * be populated with the stream length and the decode values for any
110 * filters that are currently applied.
111 *
112 * @param streamLength the stream length.
113 *
114 * @return The dictionary.
115 */
116 protected Dictionary createDictionary(int streamLength) {
117 Dictionary dictionary = new Dictionary();
118 dictionary.put("/Length", streamLength);
119 if (!this.filters.isEmpty()) {
120 String[] decodes = new String[this.filters.size()];
121 int count = this.filters.size();
122 for (int i = 0; i < count; i++) {
123 Filter f = this.filters.get(count - i - 1);
124 decodes[i] = f.getFilterType().getDecode();
125 }
126 dictionary.put("/Filter", decodes);
127 }
128 return dictionary;
129 }
130
131 /**
132 * Returns the raw data for the stream.
133 *
134 * @return The raw data for the stream.
135 */
136 public abstract byte[] getRawStreamData();
137
138 }