diff options
Diffstat (limited to 'src/java/org/apache/fop/pdf/PDFDictionary.java')
-rw-r--r-- | src/java/org/apache/fop/pdf/PDFDictionary.java | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/java/org/apache/fop/pdf/PDFDictionary.java b/src/java/org/apache/fop/pdf/PDFDictionary.java index c183871b5..49d48312c 100644 --- a/src/java/org/apache/fop/pdf/PDFDictionary.java +++ b/src/java/org/apache/fop/pdf/PDFDictionary.java @@ -19,10 +19,15 @@ package org.apache.fop.pdf; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; import java.util.Iterator; import java.util.List; import java.util.Map; +import org.apache.commons.io.output.CountingOutputStream; + /** * Class representing a PDF dictionary object */ @@ -40,14 +45,21 @@ public class PDFDictionary extends PDFObject { protected List order = new java.util.ArrayList(); /** - * Create the dictionary object + * Create a new dictionary object. */ public PDFDictionary() { - /* generic creation of PDF object */ super(); } /** + * Create a new dictionary object. + * @param parent the object's parent if any + */ + public PDFDictionary(PDFObject parent) { + super(parent); + } + + /** * Puts a new name/value pair. * @param name the name * @param value the value @@ -80,47 +92,53 @@ public class PDFDictionary extends PDFObject { return this.entries.get(name); } - /** - * {@inheritDoc} - */ - public String toPDFString() { - StringBuffer p = new StringBuffer(64); + /** {@inheritDoc} */ + protected int output(OutputStream stream) throws IOException { + CountingOutputStream cout = new CountingOutputStream(stream); + Writer writer = PDFDocument.getWriterFor(cout); if (hasObjectNumber()) { - p.append(getObjectID()); + writer.write(getObjectID()); } - writeDictionary(p); + + writeDictionary(cout, writer); + if (hasObjectNumber()) { - p.append("endobj\n"); + writer.write("\nendobj\n"); } - return p.toString(); + + writer.flush(); + return cout.getCount(); } - + /** * Writes the contents of the dictionary to a StringBuffer. - * @param sb the target StringBuffer + * @param out the OutputStream (for binary content) + * @param writer the Writer (for text content, wraps the above OutputStream) + * @throws IOException if an I/O error occurs */ - protected void writeDictionary(StringBuffer sb) { - sb.append("<<"); + protected void writeDictionary(OutputStream out, Writer writer) throws IOException { + writer.write("<<"); boolean compact = (this.order.size() <= 2); Iterator iter = this.order.iterator(); while (iter.hasNext()) { String key = (String)iter.next(); if (compact) { - sb.append(' '); + writer.write(' '); } else { - sb.append("\n "); + writer.write("\n "); } - sb.append('/').append(key); - sb.append(' '); + writer.write('/'); + writer.write(key); + writer.write(' '); Object obj = this.entries.get(key); - formatObject(obj, sb); + formatObject(obj, out, writer); } if (compact) { - sb.append(' '); + writer.write(' '); } else { - sb.append('\n'); + writer.write('\n'); } - sb.append(">>\n"); + writer.write(">>\n"); } } |