Debian Codegraph @

src/main/java/org/jfree/pdf/internal/PDFObject.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.internal;
33
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import org.jfree.pdf.util.PDFUtils;
37
38 /**
39 * A PDF Object (also referred to as an 'Indirect Object' in the PDF
40 * specification). Objects are identified within a document by their
41 * PDF object number (which must be unique within the document) and their
42 * PDF object generation number.
43 */
44 public abstract class PDFObject {
45
46 private int number;
47
48 private int generation;
49
50 /**
51 * Creates a new instance with the specified object number and the default
52 * generation number (0).
53 *
54 * @param number the object number.
55 */
56 protected PDFObject(int number) {
57 this(number, 0);
58 }
59
60 /**
61 * Creates a new instance.
62 *
63 * @param number the PDF object number (must be unique within the
64 * document).
65 * @param generation the PDF generation number.
66 */
67 protected PDFObject(int number, int generation) {
68 this.number = number;
69 this.generation = generation;
70 }
71
72 /**
73 * Returns the PDF object number (specified in the constructor).
74 *
75 * @return The PDF object number.
76 */
77 public int getNumber() {
78 return this.number;
79 }
80
81 /**
82 * Returns the PDF object generation number.
83 *
84 * @return The PDF object generation number.
85 */
86 public int getGeneration() {
87 return this.generation;
88 }
89
90 /**
91 * Returns the PDF reference string for this object (for example,
92 * {@code "2 0 R"}). This is the PDF object number followed by a space
93 * followed by the PDF object generation number, followed by a space and
94 * an {@code 'R'}.
95 *
96 * @return The PDF reference string.
97 */
98 public String getReference() {
99 return this.number + " " + this.generation + " R";
100 }
101
102 /**
103 * Returns the PDF bytes representing this object.
104 *
105 * @return The PDF bytes representing this object.
106 *
107 * @throws IOException if there is a problem writing to the byte array.
108 */
109 public byte[] toPDFBytes() throws IOException {
110 ByteArrayOutputStream baos = new ByteArrayOutputStream();
111 baos.write(PDFUtils.toBytes(objectIntroString()));
112 baos.write(getObjectBytes());
113 baos.write(PDFUtils.toBytes("endobj\n"));
114 return baos.toByteArray();
115 }
116
117 /**
118 * Returns the bytes that go between the 'obj' and 'endobj' in the
119 * PDF output for this object.
120 *
121 * @return A byte array.
122 * @throws IOException if there is a problem writing to the byte array.
123 */
124 public abstract byte[] getObjectBytes() throws IOException;
125
126 private String objectIntroString() {
127 StringBuilder b = new StringBuilder();
128 b.append(this.number).append(" ").append(this.generation).append(" ");
129 b.append("obj\n");
130 return b.toString();
131 }
132
133 }