1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import org.apache.commons.io.output.CountingOutputStream; 26 import org.apache.fop.util.CloseBlockerOutputStream; 27 28 31 public abstract class AbstractPDFStream extends PDFObject { 32 33 34 private PDFFilterList filters; 35 36 39 public AbstractPDFStream() { 40 super(); 41 } 42 43 47 protected void setupFilterList() { 48 if (!getFilterList().isInitialized()) { 49 getFilterList().addDefaultFilters( 50 getDocumentSafely().getFilterMap(), 51 PDFFilterList.DEFAULT_FILTER); 52 } 53 prepareImplicitFilters(); 54 getDocument().applyEncryption(this); 55 } 56 57 61 public PDFFilterList getFilterList() { 62 if (this.filters == null) { 63 if (getDocument() == null) { 64 this.filters = new PDFFilterList(); 65 } else { 66 this.filters = new PDFFilterList(getDocument().isEncryptionActive()); 67 } 69 } 70 return this.filters; 71 } 72 73 80 protected abstract int getSizeHint() throws IOException ; 81 82 87 protected abstract void outputRawStreamData(OutputStream out) 88 throws IOException ; 89 90 97 protected int outputStreamData(StreamCache encodedStream, OutputStream out) throws IOException { 98 int length = 0; 99 byte[] p = encode("stream\n"); 100 out.write(p); 101 length += p.length; 102 103 encodedStream.outputContents(out); 104 length += encodedStream.getSize(); 105 106 p = encode("\nendstream"); 107 out.write(p); 108 length += p.length; 109 return length; 110 } 111 112 117 protected StreamCache encodeStream() throws IOException { 118 final StreamCache encodedStream = StreamCacheFactory.getInstance(). 120 createStreamCache(getSizeHint()); 121 OutputStream filteredOutput = 122 getFilterList().applyFilters(encodedStream.getOutputStream()); 123 outputRawStreamData(filteredOutput); 124 filteredOutput.flush(); 125 filteredOutput.close(); 126 return encodedStream; 127 } 128 129 138 protected int encodeAndWriteStream(OutputStream out, PDFNumber refLength) 139 throws IOException { 140 int bytesWritten = 0; 141 byte[] buf = encode("stream\n"); 143 out.write(buf); 144 bytesWritten += buf.length; 145 146 CloseBlockerOutputStream cbout = new CloseBlockerOutputStream(out); 148 CountingOutputStream cout = new CountingOutputStream(cbout); 149 OutputStream filteredOutput = 150 getFilterList().applyFilters(cout); 151 outputRawStreamData(filteredOutput); 152 filteredOutput.close(); 153 refLength.setNumber(new Integer (cout.getCount())); 154 bytesWritten += cout.getCount(); 155 156 buf = encode("\nendstream"); 158 out.write(buf); 159 bytesWritten += buf.length; 160 161 return bytesWritten; 162 } 163 164 169 protected int output(OutputStream stream) throws IOException { 170 int length = 0; 171 setupFilterList(); 172 173 StreamCache encodedStream = null; 174 PDFNumber refLength = null; 175 final String lengthEntry; 176 if (getDocument().isEncodingOnTheFly()) { 177 refLength = new PDFNumber(); 178 getDocumentSafely().registerObject(refLength); 179 lengthEntry = refLength.referencePDF(); 180 } else { 181 encodedStream = encodeStream(); 182 lengthEntry = Integer.toString(encodedStream.getSize() + 1); 183 } 184 185 String filterEntry = getFilterList().buildFilterDictEntries(); 186 byte[] p = encode(buildStreamDict(lengthEntry)); 187 188 stream.write(p); 189 length += p.length; 190 191 if (encodedStream == null) { 193 int bytesWritten = encodeAndWriteStream(stream, refLength); 194 length += bytesWritten; 195 } else { 196 length += outputStreamData(encodedStream, stream); 197 encodedStream.clear(); } 199 200 p = encode("\nendobj\n"); 201 stream.write(p); 202 length += p.length; 203 return length; 204 } 205 206 212 protected String buildStreamDict(String lengthEntry) { 213 final String filterEntry = getFilterList().buildFilterDictEntries(); 214 return (getObjectID() 215 + "<< /Length " + lengthEntry + "\n" 216 + filterEntry 217 + "\n>>\n"); 218 } 219 220 225 protected void prepareImplicitFilters() { 226 } 228 229 } 230 | Popular Tags |