You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2018/02/09 17:04:13 UTC
svn commit: r1823681 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
Author: msahyoun
Date: Fri Feb 9 17:04:13 2018
New Revision: 1823681
URL: http://svn.apache.org/viewvc?rev=1823681&view=rev
Log:
PDFBOX-4071: reuse IOUtils instead of internal code; use try with; break large methods
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java?rev=1823681&r1=1823680&r2=1823681&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java Fri Feb 9 17:04:13 2018
@@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigura
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@@ -40,11 +41,6 @@ import org.xml.sax.SAXException;
public final class PDXFAResource implements COSObjectable
{
- /**
- * The default buffer size
- */
- private static final int BUFFER_SIZE = 1024;
-
private final COSBase xfa;
/**
@@ -86,53 +82,46 @@ public final class PDXFAResource impleme
*/
public byte[] getBytes() throws IOException
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- InputStream is = null;
- byte[] xfaBytes;
-
- try
+ // handle the case if the XFA is split into individual parts
+ if (this.getCOSObject() instanceof COSArray)
{
- // handle the case if the XFA is split into individual parts
- if (this.getCOSObject() instanceof COSArray)
- {
- xfaBytes = new byte[BUFFER_SIZE];
- COSArray cosArray = (COSArray) this.getCOSObject();
- for (int i = 1; i < cosArray.size(); i += 2)
- {
- COSBase cosObj = cosArray.getObject(i);
- if (cosObj instanceof COSStream)
- {
- is = ((COSStream) cosObj).createInputStream();
- int nRead;
- while ((nRead = is.read(xfaBytes, 0, xfaBytes.length)) != -1)
- {
- baos.write(xfaBytes, 0, nRead);
- }
- baos.flush();
- }
- }
- // handle the case if the XFA is represented as a single stream
- }
- else if (xfa.getCOSObject() instanceof COSStream)
+ return getBytesFromPacket((COSArray) this.getCOSObject());
+ }
+ else if (xfa.getCOSObject() instanceof COSStream)
+ {
+ return getBytesFromStream((COSStream) this.getCOSObject());
+ }
+ return new byte[0];
+ }
+
+ /*
+ * Read all bytes from a packet
+ */
+ private static byte[] getBytesFromPacket(final COSArray cosArray) throws IOException
+ {
+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream())
+ {
+ for (int i = 1; i < cosArray.size(); i += 2)
{
- xfaBytes = new byte[BUFFER_SIZE];
- is = ((COSStream) xfa.getCOSObject()).createInputStream();
- int nRead;
- while ((nRead = is.read(xfaBytes, 0, xfaBytes.length)) != -1)
+ COSBase cosObj = cosArray.getObject(i);
+ if (cosObj instanceof COSStream)
{
- baos.write(xfaBytes, 0, nRead);
+ baos.write(getBytesFromStream((COSStream) cosObj.getCOSObject()));
}
- baos.flush();
}
- }
- finally
+ return baos.toByteArray();
+ }
+ }
+
+ /*
+ * Read all bytes from a COSStream
+ */
+ private static byte[] getBytesFromStream(final COSStream stream) throws IOException
+ {
+ try (final InputStream is = stream.createInputStream())
{
- if (is != null)
- {
- is.close();
- }
+ return IOUtils.toByteArray(is);
}
- return baos.toByteArray();
}
/**