ASF Bugzilla – Attachment 26710 Details for
Bug 50852
[PATCH] Improve generation of PDFs with accessibility information
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch 10/20
0010-Avoid-overhead-of-creating-writers.patch (text/plain), 19.27 KB, created by
Martin K
on 2011-03-02 07:03:35 UTC
(
hide
)
Description:
Patch 10/20
Filename:
MIME Type:
Creator:
Martin K
Created:
2011-03-02 07:03:35 UTC
Size:
19.27 KB
patch
obsolete
>Subject: [PATCH 10/20] Avoid overhead of creating writers > >--- > src/java/org/apache/fop/pdf/AbstractPDFStream.java | 10 ++---- > src/java/org/apache/fop/pdf/PDFArray.java | 15 ++++------ > src/java/org/apache/fop/pdf/PDFDestination.java | 9 ++---- > src/java/org/apache/fop/pdf/PDFDictionary.java | 30 ++++++++----------- > src/java/org/apache/fop/pdf/PDFDocument.java | 18 ------------ > src/java/org/apache/fop/pdf/PDFEmbeddedFiles.java | 5 +-- > src/java/org/apache/fop/pdf/PDFName.java | 15 ++++------ > src/java/org/apache/fop/pdf/PDFNull.java | 5 +-- > src/java/org/apache/fop/pdf/PDFNumsArray.java | 19 +++++------- > src/java/org/apache/fop/pdf/PDFObject.java | 22 +++++--------- > src/java/org/apache/fop/pdf/PDFRectangle.java | 5 +-- > src/java/org/apache/fop/pdf/PDFReference.java | 5 +-- > src/java/org/apache/fop/pdf/PDFWritable.java | 8 +---- > 13 files changed, 57 insertions(+), 109 deletions(-) > >diff --git a/src/java/org/apache/fop/pdf/AbstractPDFStream.java b/src/java/org/apache/fop/pdf/AbstractPDFStream.java >index 959058c..c40e57c 100644 >--- a/src/java/org/apache/fop/pdf/AbstractPDFStream.java >+++ b/src/java/org/apache/fop/pdf/AbstractPDFStream.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > import org.apache.commons.io.output.CountingOutputStream; > >@@ -184,8 +183,7 @@ public abstract class AbstractPDFStream extends PDFDictionary { > setupFilterList(); > > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); >- writer.write(getObjectID()); >+ cout.write(encode(getObjectID())); > //int length = 0; > > StreamCache encodedStream = null; >@@ -201,10 +199,9 @@ public abstract class AbstractPDFStream extends PDFDictionary { > } > > populateStreamDict(lengthEntry); >- writeDictionary(cout, writer); >+ writeDictionary(cout); > > //Send encoded stream to target OutputStream >- writer.flush(); > if (encodedStream == null) { > encodeAndWriteStream(cout, refLength); > } else { >@@ -212,8 +209,7 @@ public abstract class AbstractPDFStream extends PDFDictionary { > encodedStream.clear(); //Encoded stream can now be discarded > } > >- writer.write("\nendobj\n"); >- writer.flush(); >+ cout.write(encode("\nendobj\n")); > return cout.getCount(); > } > >diff --git a/src/java/org/apache/fop/pdf/PDFArray.java b/src/java/org/apache/fop/pdf/PDFArray.java >index a9137f9..971b8a5 100644 >--- a/src/java/org/apache/fop/pdf/PDFArray.java >+++ b/src/java/org/apache/fop/pdf/PDFArray.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > import java.util.Collection; > import java.util.List; > >@@ -182,26 +181,24 @@ public class PDFArray extends PDFObject { > /** {@inheritDoc} */ > protected int output(OutputStream stream) throws IOException { > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); > if (hasObjectNumber()) { >- writer.write(getObjectID()); >+ cout.write(encode(getObjectID())); > } > >- writer.write('['); >+ cout.write(encode("[")); > for (int i = 0; i < values.size(); i++) { > if (i > 0) { >- writer.write(' '); >+ cout.write(encode(" ")); > } > Object obj = this.values.get(i); >- formatObject(obj, cout, writer); >+ formatObject(obj, cout); > } >- writer.write(']'); >+ cout.write(encode("]")); > > if (hasObjectNumber()) { >- writer.write("\nendobj\n"); >+ cout.write(encode("\nendobj\n")); > } > >- writer.flush(); > return cout.getCount(); > } > >diff --git a/src/java/org/apache/fop/pdf/PDFDestination.java b/src/java/org/apache/fop/pdf/PDFDestination.java >index d79ae05..74222ad 100644 >--- a/src/java/org/apache/fop/pdf/PDFDestination.java >+++ b/src/java/org/apache/fop/pdf/PDFDestination.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > import org.apache.commons.io.output.CountingOutputStream; > >@@ -54,13 +53,11 @@ public class PDFDestination extends PDFObject { > /** {@inheritDoc} */ > protected int output(OutputStream stream) throws IOException { > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); > >- formatObject(getIDRef(), cout, writer); >- writer.write(' '); >- formatObject(goToReference, cout, writer); >+ formatObject(getIDRef(), cout); >+ cout.write(encode(" ")); >+ formatObject(goToReference, cout); > >- writer.flush(); > return cout.getCount(); > } > >diff --git a/src/java/org/apache/fop/pdf/PDFDictionary.java b/src/java/org/apache/fop/pdf/PDFDictionary.java >index b0b5daf..6b018d1 100644 >--- a/src/java/org/apache/fop/pdf/PDFDictionary.java >+++ b/src/java/org/apache/fop/pdf/PDFDictionary.java >@@ -21,7 +21,6 @@ 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; >@@ -101,49 +100,46 @@ public class PDFDictionary extends PDFObject { > /** {@inheritDoc} */ > protected int output(OutputStream stream) throws IOException { > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); > if (hasObjectNumber()) { >- writer.write(getObjectID()); >+ cout.write(encode(getObjectID())); > } > >- writeDictionary(cout, writer); >+ writeDictionary(cout); > > if (hasObjectNumber()) { >- writer.write("\nendobj\n"); >+ cout.write(encode("\nendobj\n")); > } > >- writer.flush(); > return cout.getCount(); > } > > /** > * Writes the contents of the dictionary to a 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(OutputStream out, Writer writer) throws IOException { >- writer.write("<<"); >+ protected void writeDictionary(OutputStream out) throws IOException { >+ out.write(encode("<<")); > boolean compact = (this.order.size() <= 2); > Iterator iter = this.order.iterator(); > while (iter.hasNext()) { > String key = (String)iter.next(); > if (compact) { >- writer.write(' '); >+ out.write(encode(" ")); > } else { >- writer.write("\n "); >+ out.write(encode("\n ")); > } >- writer.write(PDFName.escapeName(key)); >- writer.write(' '); >+ out.write(encode(PDFName.escapeName(key))); >+ out.write(encode(" ")); > Object obj = this.entries.get(key); >- formatObject(obj, out, writer); >+ formatObject(obj, out); > } > if (compact) { >- writer.write(' '); >+ out.write(encode(" ")); > } else { >- writer.write('\n'); >+ out.write(encode("\n")); > } >- writer.write(">>\n"); >+ out.write(encode(">>\n")); > } > > } >diff --git a/src/java/org/apache/fop/pdf/PDFDocument.java b/src/java/org/apache/fop/pdf/PDFDocument.java >index 8d47cc0..1890669 100644 >--- a/src/java/org/apache/fop/pdf/PDFDocument.java >+++ b/src/java/org/apache/fop/pdf/PDFDocument.java >@@ -24,7 +24,6 @@ import java.io.IOException; > import java.io.InputStream; > import java.io.OutputStream; > import java.io.UnsupportedEncodingException; >-import java.io.Writer; > import java.security.MessageDigest; > import java.security.NoSuchAlgorithmException; > import java.text.DateFormat; >@@ -275,23 +274,6 @@ public class PDFDocument { > } > > /** >- * Creates and returns a Writer object wrapping the given OutputStream. The Writer is >- * buffered to reduce the number of calls to the encoding converter so don't forget >- * to <code>flush()</code> the Writer after use or before writing directly to the >- * underlying OutputStream. >- * >- * @param out the OutputStream to write to >- * @return the requested Writer >- */ >- public static Writer getWriterFor(OutputStream out) { >- try { >- return new java.io.BufferedWriter(new java.io.OutputStreamWriter(out, ENCODING)); >- } catch (UnsupportedEncodingException uee) { >- throw new Error("JVM doesn't support " + ENCODING + " encoding!"); >- } >- } >- >- /** > * Sets the producer of the document. > * > * @param producer string indicating application producing the PDF >diff --git a/src/java/org/apache/fop/pdf/PDFEmbeddedFiles.java b/src/java/org/apache/fop/pdf/PDFEmbeddedFiles.java >index 9bb286d..af6963e 100644 >--- a/src/java/org/apache/fop/pdf/PDFEmbeddedFiles.java >+++ b/src/java/org/apache/fop/pdf/PDFEmbeddedFiles.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > import java.util.Iterator; > import java.util.Map; > import java.util.SortedMap; >@@ -39,9 +38,9 @@ public class PDFEmbeddedFiles extends PDFNameTreeNode { > } > > /** {@inheritDoc} */ >- protected void writeDictionary(OutputStream out, Writer writer) throws IOException { >+ protected void writeDictionary(OutputStream out) throws IOException { > sortNames(); //Sort the names before writing them out >- super.writeDictionary(out, writer); >+ super.writeDictionary(out); > } > > private void sortNames() { >diff --git a/src/java/org/apache/fop/pdf/PDFName.java b/src/java/org/apache/fop/pdf/PDFName.java >index 656ad94..f2a3358 100644 >--- a/src/java/org/apache/fop/pdf/PDFName.java >+++ b/src/java/org/apache/fop/pdf/PDFName.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > import org.apache.commons.io.output.CountingOutputStream; > >@@ -112,27 +111,25 @@ public class PDFName extends PDFObject { > /** {@inheritDoc} */ > protected int output(OutputStream stream) throws IOException { > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); > if (hasObjectNumber()) { >- writer.write(getObjectID()); >+ cout.write(encode(getObjectID())); > } > >- writer.write(toString()); >+ cout.write(encode(toString())); > > if (hasObjectNumber()) { >- writer.write("\nendobj\n"); >+ cout.write(encode("\nendobj\n")); > } > >- writer.flush(); > return cout.getCount(); > } > > /** {@inheritDoc} */ >- public void outputInline(OutputStream out, Writer writer) throws IOException { >+ public void outputInline(OutputStream out) throws IOException { > if (hasObjectNumber()) { >- writer.write(referencePDF()); >+ out.write(encode(referencePDF())); > } else { >- writer.write(toString()); >+ out.write(encode(toString())); > } > } > >diff --git a/src/java/org/apache/fop/pdf/PDFNull.java b/src/java/org/apache/fop/pdf/PDFNull.java >index 48d587a..26c6945 100644 >--- a/src/java/org/apache/fop/pdf/PDFNull.java >+++ b/src/java/org/apache/fop/pdf/PDFNull.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > /** > * Class representing a PDF name object. >@@ -43,8 +42,8 @@ public final class PDFNull implements PDFWritable { > } > > /** {@inheritDoc} */ >- public void outputInline(OutputStream out, Writer writer) throws IOException { >- writer.write(toString()); >+ public void outputInline(OutputStream out) throws IOException { >+ out.write(PDFDocument.encode(toString())); > } > > } >diff --git a/src/java/org/apache/fop/pdf/PDFNumsArray.java b/src/java/org/apache/fop/pdf/PDFNumsArray.java >index 9a6aaf0..60bc791 100644 >--- a/src/java/org/apache/fop/pdf/PDFNumsArray.java >+++ b/src/java/org/apache/fop/pdf/PDFNumsArray.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > import java.util.Iterator; > import java.util.Map; > import java.util.SortedMap; >@@ -91,31 +90,29 @@ public class PDFNumsArray extends PDFObject { > /** {@inheritDoc} */ > protected int output(OutputStream stream) throws IOException { > CountingOutputStream cout = new CountingOutputStream(stream); >- Writer writer = PDFDocument.getWriterFor(cout); > if (hasObjectNumber()) { >- writer.write(getObjectID()); >+ cout.write(encode(getObjectID())); > } > >- writer.write('['); >+ cout.write(encode("[")); > boolean first = true; > Iterator iter = this.map.entrySet().iterator(); > while (iter.hasNext()) { > Map.Entry entry = (Map.Entry)iter.next(); > if (!first) { >- writer.write(" "); >+ cout.write(encode(" ")); > } > first = false; >- formatObject(entry.getKey(), cout, writer); >- writer.write(" "); >- formatObject(entry.getValue(), cout, writer); >+ formatObject(entry.getKey(), cout); >+ cout.write(encode(" ")); >+ formatObject(entry.getValue(), cout); > } >- writer.write(']'); >+ cout.write(encode("]")); > > if (hasObjectNumber()) { >- writer.write("\nendobj\n"); >+ cout.write(encode("\nendobj\n")); > } > >- writer.flush(); > return cout.getCount(); > } > >diff --git a/src/java/org/apache/fop/pdf/PDFObject.java b/src/java/org/apache/fop/pdf/PDFObject.java >index 40e14c7..897c2f8 100644 >--- a/src/java/org/apache/fop/pdf/PDFObject.java >+++ b/src/java/org/apache/fop/pdf/PDFObject.java >@@ -22,7 +22,6 @@ package org.apache.fop.pdf; > // Java > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > import java.text.SimpleDateFormat; > import java.util.Calendar; > import java.util.Date; >@@ -216,11 +215,10 @@ public abstract class PDFObject implements PDFWritable { > } > > /** {@inheritDoc} */ >- public void outputInline(OutputStream out, Writer writer) throws IOException { >+ public void outputInline(OutputStream out) throws IOException { > if (hasObjectNumber()) { >- writer.write(referencePDF()); >+ out.write(encode(referencePDF())); > } else { >- writer.flush(); > output(out); > } > } >@@ -304,28 +302,24 @@ public abstract class PDFObject implements PDFWritable { > * Formats an object for serialization to PDF. > * @param obj the object > * @param out the OutputStream to write to >- * @param writer a Writer for text content (will always be a wrapper around the above >- * OutputStream. Make sure <code>flush</code> is called when mixing calls) > * @throws IOException If an I/O error occurs > */ >- protected void formatObject(Object obj, OutputStream out, Writer writer) throws IOException { >+ protected void formatObject(Object obj, OutputStream out) throws IOException { > if (obj == null) { >- writer.write("null"); >+ out.write(encode("null")); > } else if (obj instanceof PDFWritable) { >- ((PDFWritable)obj).outputInline(out, writer); >+ ((PDFWritable)obj).outputInline(out); > } else if (obj instanceof Number) { > if (obj instanceof Double || obj instanceof Float) { >- writer.write(PDFNumber.doubleOut(((Number)obj).doubleValue())); >+ out.write(encode(PDFNumber.doubleOut(((Number)obj).doubleValue()))); > } else { >- writer.write(obj.toString()); >+ out.write(encode(obj.toString())); > } > } else if (obj instanceof Boolean) { >- writer.write(obj.toString()); >+ out.write(encode(obj.toString())); > } else if (obj instanceof byte[]) { >- writer.flush(); > encodeBinaryToHexString((byte[])obj, out); > } else { >- writer.flush(); > out.write(encodeText(obj.toString())); > } > } >diff --git a/src/java/org/apache/fop/pdf/PDFRectangle.java b/src/java/org/apache/fop/pdf/PDFRectangle.java >index 95c6d00..bb0ffe4 100644 >--- a/src/java/org/apache/fop/pdf/PDFRectangle.java >+++ b/src/java/org/apache/fop/pdf/PDFRectangle.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > /** > * class representing a rectangle >@@ -87,7 +86,7 @@ public class PDFRectangle implements PDFWritable { > } > > /** {@inheritDoc} */ >- public void outputInline(OutputStream out, Writer writer) throws IOException { >- writer.write(format()); >+ public void outputInline(OutputStream out) throws IOException { >+ out.write(PDFDocument.encode(format())); > } > } >diff --git a/src/java/org/apache/fop/pdf/PDFReference.java b/src/java/org/apache/fop/pdf/PDFReference.java >index f5abe24..3996196 100644 >--- a/src/java/org/apache/fop/pdf/PDFReference.java >+++ b/src/java/org/apache/fop/pdf/PDFReference.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > import java.lang.ref.Reference; > import java.lang.ref.SoftReference; > >@@ -101,8 +100,8 @@ public class PDFReference implements PDFWritable { > } > > /** {@inheritDoc} */ >- public void outputInline(OutputStream out, Writer writer) throws IOException { >- writer.write(toString()); >+ public void outputInline(OutputStream out) throws IOException { >+ out.write(PDFDocument.encode(toString())); > } > > } >diff --git a/src/java/org/apache/fop/pdf/PDFWritable.java b/src/java/org/apache/fop/pdf/PDFWritable.java >index 8f8f335..653ae6a 100644 >--- a/src/java/org/apache/fop/pdf/PDFWritable.java >+++ b/src/java/org/apache/fop/pdf/PDFWritable.java >@@ -21,7 +21,6 @@ package org.apache.fop.pdf; > > import java.io.IOException; > import java.io.OutputStream; >-import java.io.Writer; > > /** > * This interface is implemented by classes that can be serialized to a PDF file either by >@@ -30,13 +29,10 @@ import java.io.Writer; > public interface PDFWritable { > > /** >- * Writes a "direct object" (inline object) representation to the stream. A Writer is given >- * for optimized encoding of text content. Since the Writer is buffered, make sure >- * <code>flush()</code> is called before any direct calls to <code>out</code> are made. >+ * Writes a "direct object" (inline object) representation to the stream. > * @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 > */ >- void outputInline(OutputStream out, Writer writer) throws IOException; >+ void outputInline(OutputStream out) throws IOException; > > } >-- >1.6.4.2 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 50852
:
26701
|
26702
|
26703
|
26704
|
26705
|
26706
|
26707
|
26708
|
26709
|
26710
|
26711
|
26712
|
26713
|
26714
|
26715
|
26716
|
26717
|
26718
|
26719
|
26720
|
27171
|
28824
|
28825
|
28826