KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PRStream


1 /*
2  * $Id: PRStream.java 2446 2006-11-08 17:33:44Z psoares33 $
3  * $Name$
4  *
5  * Copyright 2001, 2002 by Paulo Soares.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.zip.DeflaterOutputStream JavaDoc;
57
58 import com.lowagie.text.Document;
59 import com.lowagie.text.ExceptionConverter;
60
61 public class PRStream extends PdfStream {
62     
63     protected PdfReader reader;
64     protected int offset;
65     protected int length;
66     
67     //added by ujihara for decryption
68
protected int objNum = 0;
69     protected int objGen = 0;
70     
71     public PRStream(PRStream stream, PdfDictionary newDic) {
72         reader = stream.reader;
73         offset = stream.offset;
74         length = stream.length;
75         compressed = stream.compressed;
76         streamBytes = stream.streamBytes;
77         bytes = stream.bytes;
78         objNum = stream.objNum;
79         objGen = stream.objGen;
80         if (newDic != null)
81             putAll(newDic);
82         else
83             hashMap.putAll(stream.hashMap);
84     }
85
86     public PRStream(PRStream stream, PdfDictionary newDic, PdfReader reader) {
87         this(stream, newDic);
88         this.reader = reader;
89     }
90
91     public PRStream(PdfReader reader, int offset) {
92         this.reader = reader;
93         this.offset = offset;
94     }
95     
96     public PRStream(PdfReader reader, byte conts[]) {
97         this.reader = reader;
98         this.offset = -1;
99         if (Document.compress) {
100             try {
101                 ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
102                 DeflaterOutputStream JavaDoc zip = new DeflaterOutputStream JavaDoc(stream);
103                 zip.write(conts);
104                 zip.close();
105                 bytes = stream.toByteArray();
106             }
107             catch (IOException JavaDoc ioe) {
108                 throw new ExceptionConverter(ioe);
109             }
110             put(PdfName.FILTER, PdfName.FLATEDECODE);
111         }
112         else
113             bytes = conts;
114         setLength(bytes.length);
115     }
116     
117     /**Sets the data associated with the stream
118      * @param data raw data, decrypted and uncompressed.
119      */

120     public void setData(byte[] data) {
121         remove(PdfName.FILTER);
122         this.offset = -1;
123         if (Document.compress) {
124             try {
125                 ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
126                 DeflaterOutputStream JavaDoc zip = new DeflaterOutputStream JavaDoc(stream);
127                 zip.write(data);
128                 zip.close();
129                 bytes = stream.toByteArray();
130             }
131             catch (IOException JavaDoc ioe) {
132                 throw new ExceptionConverter(ioe);
133             }
134             put(PdfName.FILTER, PdfName.FLATEDECODE);
135         }
136         else
137             bytes = data;
138         setLength(bytes.length);
139     }
140
141     public void setLength(int length) {
142         this.length = length;
143         put(PdfName.LENGTH, new PdfNumber(length));
144     }
145     
146     public int getOffset() {
147         return offset;
148     }
149     
150     public int getLength() {
151         return length;
152     }
153     
154     public PdfReader getReader() {
155         return reader;
156     }
157     
158     public byte[] getBytes() {
159         return bytes;
160     }
161     
162     public void setObjNum(int objNum, int objGen) {
163         this.objNum = objNum;
164         this.objGen = objGen;
165     }
166     
167     int getObjNum() {
168         return objNum;
169     }
170     
171     int getObjGen() {
172         return objGen;
173     }
174     
175     public void toPdf(PdfWriter writer, OutputStream JavaDoc os) throws IOException JavaDoc {
176         byte[] b = PdfReader.getStreamBytesRaw(this);
177         PdfEncryption crypto = null;
178         if (writer != null)
179             crypto = writer.getEncryption();
180         PdfObject objLen = get(PdfName.LENGTH);
181         int nn = b.length;
182         if (crypto != null)
183             nn = crypto.calculateStreamSize(nn);
184         put(PdfName.LENGTH, new PdfNumber(nn));
185         superToPdf(writer, os);
186         put(PdfName.LENGTH, objLen);
187         os.write(STARTSTREAM);
188         if (length > 0) {
189             if (crypto != null)
190                 b = crypto.encryptByteArray(b);
191             os.write(b);
192         }
193         os.write(ENDSTREAM);
194     }
195 }
Popular Tags