1 20 package gnu.jpdf; 21 22 import java.io.*; 23 import java.util.*; 24 import java.util.zip.*; 25 26 40 public class PDFStream extends PDFObject implements Serializable 41 { 42 43 44 52 53 56 transient ByteArrayOutputStream buf; 57 58 61 boolean deflate; 62 63 67 public PDFStream() { 68 this(null); 69 } 70 71 79 public PDFStream(String type) { 80 super(type); 81 buf = new ByteArrayOutputStream(); 82 83 deflate = false; 85 } 86 87 90 public void setDeflate(boolean mode) { 91 deflate = mode; 92 } 93 94 98 public boolean getDeflate() { 99 return deflate; 100 } 101 102 106 public OutputStream getOutputStream() { 107 return (OutputStream)buf; 108 } 109 110 114 public PrintWriter getWriter() { 115 return new PrintWriter(buf,true); 116 } 117 118 122 public ByteArrayOutputStream getStream() { 123 return buf; 124 } 125 126 130 public void write(OutputStream os) throws IOException { 131 writeStart(os); 132 writeStream(os); 133 } 136 137 148 public void writeStream(OutputStream os) throws IOException { 149 if(deflate) { 150 ByteArrayOutputStream b = new ByteArrayOutputStream(); 151 DeflaterOutputStream dos = new DeflaterOutputStream(b); 152 buf.writeTo(dos); 154 dos.finish(); 155 dos.close(); 156 157 os.write("/Filter /FlateDecode\n".getBytes()); 159 os.write("/Length ".getBytes()); 160 os.write(Integer.toString(b.size()+1).getBytes()); 161 os.write("\n>>\nstream\n".getBytes()); 162 b.writeTo(os); 163 os.write("\n".getBytes()); 164 } else { 165 os.write("/Length ".getBytes()); 167 os.write(Integer.toString(buf.size()).getBytes()); 168 os.write("\n>>\nstream\n".getBytes()); 169 buf.writeTo(os); 170 } 171 172 os.write("endstream\nendobj\n".getBytes()); 173 174 } 177 178 179 180 182 183 191 private void writeObject(java.io.ObjectOutputStream out) throws IOException 192 { 193 out.writeInt(buf.size()); 194 out.write(buf.toByteArray()); 195 } 196 197 206 private void readObject(java.io.ObjectInputStream in) throws IOException 207 { 208 int l = in.readInt(); 209 byte b[] = new byte[l]; 210 in.read(b,0,l); 211 buf=new ByteArrayOutputStream(l); 212 buf.write(b); 213 } 214 215 } 216 | Popular Tags |