001: /*
002: * $Id: PdfIndirectObject.java 2739 2007-05-04 11:24:51Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055:
056: import com.lowagie.text.DocWriter;
057:
058: /**
059: * <CODE>PdfIndirectObject</CODE> is the Pdf indirect object.
060: * <P>
061: * An <I>indirect object</I> is an object that has been labeled so that it can be referenced by
062: * other objects. Any type of <CODE>PdfObject</CODE> may be labeled as an indirect object.<BR>
063: * An indirect object consists of an object identifier, a direct object, and the <B>endobj</B>
064: * keyword. The <I>object identifier</I> consists of an integer <I>object number</I>, an integer
065: * <I>generation number</I>, and the <B>obj</B> keyword.<BR>
066: * This object is described in the 'Portable Document Format Reference Manual version 1.7'
067: * section 3.2.9 (page 63-65).
068: *
069: * @see PdfObject
070: * @see PdfIndirectReference
071: */
072:
073: public class PdfIndirectObject {
074:
075: // membervariables
076:
077: /** The object number */
078: protected int number;
079:
080: /** the generation number */
081: protected int generation = 0;
082:
083: static final byte STARTOBJ[] = DocWriter.getISOBytes(" obj");
084: static final byte ENDOBJ[] = DocWriter.getISOBytes("\nendobj\n");
085: static final int SIZEOBJ = STARTOBJ.length + ENDOBJ.length;
086: PdfObject object;
087: PdfWriter writer;
088:
089: // constructors
090:
091: /**
092: * Constructs a <CODE>PdfIndirectObject</CODE>.
093: *
094: * @param number the object number
095: * @param object the direct object
096: */
097:
098: PdfIndirectObject(int number, PdfObject object, PdfWriter writer) {
099: this (number, 0, object, writer);
100: }
101:
102: PdfIndirectObject(PdfIndirectReference ref, PdfObject object,
103: PdfWriter writer) {
104: this (ref.getNumber(), ref.getGeneration(), object, writer);
105: }
106:
107: /**
108: * Constructs a <CODE>PdfIndirectObject</CODE>.
109: *
110: * @param number the object number
111: * @param generation the generation number
112: * @param object the direct object
113: */
114:
115: PdfIndirectObject(int number, int generation, PdfObject object,
116: PdfWriter writer) {
117: this .writer = writer;
118: this .number = number;
119: this .generation = generation;
120: this .object = object;
121: PdfEncryption crypto = null;
122: if (writer != null)
123: crypto = writer.getEncryption();
124: if (crypto != null) {
125: crypto.setHashKey(number, generation);
126: }
127: }
128:
129: // methods
130:
131: /**
132: * Return the length of this <CODE>PdfIndirectObject</CODE>.
133: *
134: * @return the length of the PDF-representation of this indirect object.
135: */
136:
137: // public int length() {
138: // if (isStream)
139: // return bytes.size() + SIZEOBJ + stream.getStreamLength(writer);
140: // else
141: // return bytes.size();
142: // }
143:
144: /**
145: * Returns a <CODE>PdfIndirectReference</CODE> to this <CODE>PdfIndirectObject</CODE>.
146: *
147: * @return a <CODE>PdfIndirectReference</CODE>
148: */
149:
150: public PdfIndirectReference getIndirectReference() {
151: return new PdfIndirectReference(object.type(), number,
152: generation);
153: }
154:
155: /**
156: * Writes eficiently to a stream
157: *
158: * @param os the stream to write to
159: * @throws IOException on write error
160: */
161: void writeTo(OutputStream os) throws IOException {
162: os.write(DocWriter.getISOBytes(String.valueOf(number)));
163: os.write(' ');
164: os.write(DocWriter.getISOBytes(String.valueOf(generation)));
165: os.write(STARTOBJ);
166: int type = object.type();
167: if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY
168: && type != PdfObject.NAME && type != PdfObject.STRING)
169: os.write(' ');
170: object.toPdf(writer, os);
171: os.write(ENDOBJ);
172: }
173: }
|