From 299fe1aae9787ac088080c15ca4e5a28713ea64a Mon Sep 17 00:00:00 2001 From: "valery.bokov" Date: Mon, 17 Aug 2026 21:13:54 +0200 Subject: [PATCH 1/2] optimize JPEGFactory methods --- .../pdmodel/graphics/image/JPEGFactory.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java index 7c02cbea8e3..855e2474af2 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java @@ -25,6 +25,7 @@ import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.BufferedInputStream; import java.io.InputStream; import java.io.IOException; import java.util.Iterator; @@ -80,25 +81,13 @@ private JPEGFactory() public static PDImageXObject createFromStream(PDDocument document, InputStream stream) throws IOException { - return createFromByteArray(document, stream.readAllBytes()); - } - - /** - * 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 instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream)) + { + stream = new BufferedInputStream(stream); + } + stream.mark(Integer.MAX_VALUE); - Dimensions meta = retrieveDimensions(byteStream); + Dimensions meta = retrieveDimensions(stream); PDColorSpace colorSpace; switch (meta.numComponents) @@ -118,7 +107,7 @@ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byt } // 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) @@ -138,6 +127,21 @@ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byt 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; @@ -145,7 +149,7 @@ private static class Dimensions 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"); From b4283f7ee67987bd3ea1fe2d3658a96b984db8b5 Mon Sep 17 00:00:00 2001 From: valery <67366451+valerybokov@users.noreply.github.com> Date: Tue, 18 Aug 2026 09:02:54 +0200 Subject: [PATCH 2/2] Update pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java change condition in JPEGFactory.createFromStream method Co-authored-by: Tilman Hausherr --- .../org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java index 855e2474af2..6cc769ca5e6 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java @@ -81,7 +81,7 @@ private JPEGFactory() public static PDImageXObject createFromStream(PDDocument document, InputStream stream) throws IOException { - if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream)) + if (!stream.markSupported()) { stream = new BufferedInputStream(stream); }