Author: tilman
Date: Tue Aug 18 08:30:49 2026
New Revision: 1937196
Log:
PDFBOX-5660: optimize, by Valery Bokov; closes #497
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
==============================================================================
---
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
Tue Aug 18 08:30:38 2026 (r1937195)
+++
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
Tue Aug 18 08:30:49 2026 (r1937196)
@@ -23,6 +23,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
+import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@@ -49,7 +50,6 @@ import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.filter.Filter;
import org.apache.pdfbox.filter.MissingImageReaderException;
-import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
@@ -82,25 +82,13 @@ public final class JPEGFactory
public static PDImageXObject createFromStream(PDDocument document,
InputStream stream)
throws IOException
{
- return createFromByteArray(document, IOUtils.toByteArray(stream));
- }
-
- /**
- * Creates a new JPEG Image XObject from a byte array containing JPEG data.
- *
- * @param document the document where the image will be created
- * @param byteArray bytes of JPEG image
- * @return a new Image XObject
- *
- * @throws IOException if the input stream cannot be read
- */
- public static PDImageXObject createFromByteArray(PDDocument document,
byte[] byteArray)
- throws IOException
- {
- // copy stream
- ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
+ if (!stream.markSupported())
+ {
+ stream = new BufferedInputStream(stream);
+ }
+ stream.mark(Integer.MAX_VALUE);
- Dimensions meta = retrieveDimensions(byteStream);
+ Dimensions meta = retrieveDimensions(stream);
PDColorSpace colorSpace;
switch (meta.numComponents)
@@ -120,7 +108,7 @@ public final class JPEGFactory
}
// create PDImageXObject from stream
- PDImageXObject pdImage = new PDImageXObject(document, byteStream,
+ PDImageXObject pdImage = new PDImageXObject(document, stream,
COSName.DCT_DECODE, meta.width, meta.height, 8, colorSpace);
if (colorSpace instanceof PDDeviceCMYK)
@@ -140,6 +128,21 @@ public final class JPEGFactory
return pdImage;
}
+ /**
+ * Creates a new JPEG Image XObject from a byte array containing JPEG data.
+ *
+ * @param document the document where the image will be created
+ * @param byteArray bytes of JPEG image
+ * @return a new Image XObject
+ *
+ * @throws IOException if the input stream cannot be read
+ */
+ public static PDImageXObject createFromByteArray(PDDocument document,
byte[] byteArray)
+ throws IOException
+ {
+ return createFromStream(document, new ByteArrayInputStream(byteArray));
+ }
+
private static class Dimensions
{
private int width;
@@ -147,7 +150,7 @@ public final class JPEGFactory
private int numComponents;
}
- private static Dimensions retrieveDimensions(ByteArrayInputStream stream)
throws IOException
+ private static Dimensions retrieveDimensions(InputStream stream) throws
IOException
{
ImageReader reader =
Filter.findRasterReader("JPEG", "a suitable JAI I/O image
filter is not installed");