Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream))
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)
Expand All @@ -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)
Expand All @@ -138,14 +127,29 @@ 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;
private int height;
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");
Expand Down