1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.zip.DeflaterOutputStream ; 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 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 stream = new ByteArrayOutputStream (); 102 DeflaterOutputStream zip = new DeflaterOutputStream (stream); 103 zip.write(conts); 104 zip.close(); 105 bytes = stream.toByteArray(); 106 } 107 catch (IOException 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 120 public void setData(byte[] data) { 121 remove(PdfName.FILTER); 122 this.offset = -1; 123 if (Document.compress) { 124 try { 125 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 126 DeflaterOutputStream zip = new DeflaterOutputStream (stream); 127 zip.write(data); 128 zip.close(); 129 bytes = stream.toByteArray(); 130 } 131 catch (IOException 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 os) throws IOException { 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 |