You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2014/05/02 06:14:25 UTC
svn commit: r1591812 - in
/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter:
FlateFilter.java LZWFilter.java
Author: tilman
Date: Fri May 2 04:14:24 2014
New Revision: 1591812
URL: http://svn.apache.org/r1591812
Log:
PDFBOX-2050: Optimize Flate filter, don't use ByteArrayStream when there is no predictor
Modified:
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java?rev=1591812&r1=1591811&r2=1591812&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java Fri May 2 04:14:24 2014
@@ -57,80 +57,52 @@ public class FlateFilter implements Filt
{
COSBase baseObj = options.getDictionaryObject(COSName.DECODE_PARMS, COSName.DP);
COSDictionary dict = null;
- if( baseObj instanceof COSDictionary )
+ if (baseObj instanceof COSDictionary)
{
- dict = (COSDictionary)baseObj;
+ dict = (COSDictionary) baseObj;
}
- else if( baseObj instanceof COSArray )
+ else if (baseObj instanceof COSArray)
{
- COSArray paramArray = (COSArray)baseObj;
- if( filterIndex < paramArray.size() )
+ COSArray paramArray = (COSArray) baseObj;
+ if (filterIndex < paramArray.size())
{
- dict = (COSDictionary)paramArray.getObject( filterIndex );
+ dict = (COSDictionary) paramArray.getObject(filterIndex);
}
}
- else if( baseObj != null )
+ else if (baseObj != null)
{
- throw new IOException( "Error: Expected COSArray or COSDictionary and not "
- + baseObj.getClass().getName() );
+ throw new IOException("Error: Expected COSArray or COSDictionary and not "
+ + baseObj.getClass().getName());
}
-
int predictor = -1;
- int colors = -1;
- int bitsPerPixel = -1;
- int columns = -1;
- ByteArrayInputStream bais = null;
- ByteArrayOutputStream baos = null;
- if (dict!=null)
+ if (dict != null)
{
predictor = dict.getInt(COSName.PREDICTOR);
- if(predictor > 1)
- {
- colors = dict.getInt(COSName.COLORS);
- bitsPerPixel = dict.getInt(COSName.BITS_PER_COMPONENT);
- columns = dict.getInt(COSName.COLUMNS);
- }
}
-
try
{
- baos = decompress(compressedData);
// Decode data using given predictor
- if (predictor==-1 || predictor == 1 )
+ if (predictor > 1)
{
- result.write(baos.toByteArray());
+ int colors = dict.getInt(COSName.COLORS, 1);
+ int bitsPerPixel = dict.getInt(COSName.BITS_PER_COMPONENT, 8);
+ int columns = dict.getInt(COSName.COLUMNS, 1);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ decompress(compressedData, baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ byte[] decodedData = Predictor.decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
+ result.write(decodedData);
+ result.flush();
+ baos.reset();
+ bais.reset();
}
else
{
- /*
- * Reverting back to default values
- */
- if( colors == -1 )
- {
- colors = 1;
- }
- if( bitsPerPixel == -1 )
- {
- bitsPerPixel = 8;
- }
- if( columns == -1 )
- {
- columns = 1;
- }
-
- // Copy data to ByteArrayInputStream for reading
- bais = new ByteArrayInputStream(baos.toByteArray());
-
- byte[] decodedData = Predictor.decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
- bais.close();
- bais = null;
-
- result.write(decodedData);
+ decompress(compressedData, result);
}
- result.flush();
- }
- catch (DataFormatException exception)
+ }
+ catch (DataFormatException exception)
{
// if the stream is corrupt a DataFormatException may occur
LOG.error("FlateFilter: stop reading corrupt stream due to a DataFormatException");
@@ -139,24 +111,12 @@ public class FlateFilter implements Filt
io.initCause(exception);
throw io;
}
- finally
- {
- if (bais != null)
- {
- bais.close();
- }
- if (baos != null)
- {
- baos.close();
- }
- }
}
// Use Inflater instead of InflateInputStream to avoid an EOFException due to a probably
// missing Z_STREAM_END, see PDFBOX-1232 for details
- private ByteArrayOutputStream decompress(InputStream in) throws IOException, DataFormatException
+ private void decompress(InputStream in, OutputStream out) throws IOException, DataFormatException
{
- ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[2048];
int read = in.read(buf);
if(read > 0)
@@ -181,7 +141,6 @@ public class FlateFilter implements Filt
}
}
out.close();
- return out;
}
/**
Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java?rev=1591812&r1=1591811&r2=1591812&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Fri May 2 04:14:24 2014
@@ -100,6 +100,9 @@ public class LZWFilter implements Filter
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
byte[] decodedData = Predictor.decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
result.write(decodedData);
+ result.flush();
+ baos.reset();
+ bais.reset();
}
else
{