Skip to content

Commit 1f2c526

Browse files
author
ilgoo.kim
committed
PDFBOX-6032: refactor duplicate logic and switch to CustomFactory
PDFBOX-6032: change test comment
1 parent 1530529 commit 1f2c526

File tree

3 files changed

+22
-92
lines changed

3 files changed

+22
-92
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/DefaultFactory.java renamed to pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CustomFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
import org.apache.pdfbox.pdmodel.PDDocument;
66

77
@FunctionalInterface
8-
public interface DefaultFactory {
8+
public interface CustomFactory {
99
PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws IOException;
1010
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -340,47 +340,7 @@ public static PDImageXObject createFromFileByContent(File file, PDDocument doc)
340340
*/
341341
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name) throws IOException
342342
{
343-
FileType fileType = FileTypeDetector.detectFileType(byteArray);
344-
if (fileType == null)
345-
{
346-
throw new IllegalArgumentException("Image type not supported: " + name);
347-
}
348-
349-
if (fileType == FileType.JPEG)
350-
{
351-
return JPEGFactory.createFromByteArray(document, byteArray);
352-
}
353-
if (fileType == FileType.PNG)
354-
{
355-
// Try to directly convert the image without recoding it.
356-
PDImageXObject image = PNGConverter.convertPNGImage(document, byteArray);
357-
if (image != null)
358-
{
359-
return image;
360-
}
361-
}
362-
if (fileType == FileType.TIFF)
363-
{
364-
try
365-
{
366-
return CCITTFactory.createFromByteArray(document, byteArray);
367-
}
368-
catch (IOException ex)
369-
{
370-
LOG.debug("Reading as TIFF failed, setting fileType to PNG", ex);
371-
// Plan B: try reading with ImageIO
372-
// common exception:
373-
// First image in tiff is not CCITT T4 or T6 compressed
374-
fileType = FileType.PNG;
375-
}
376-
}
377-
if (fileType == FileType.BMP || fileType == FileType.GIF || fileType == FileType.PNG)
378-
{
379-
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
380-
BufferedImage bim = ImageIO.read(bais);
381-
return LosslessFactory.createFromImage(document, bim);
382-
}
383-
throw new IllegalArgumentException("Image type " + fileType + " not supported: " + name);
343+
return createFromByteArray(document, byteArray, name, null);
384344
}
385345

386346
/**
@@ -391,20 +351,16 @@ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byt
391351
* @param document the document that shall use this PDImageXObject.
392352
* @param byteArray bytes from an image file.
393353
* @param name name of image file for exception messages, can be null.
394-
* @param defaultFactory optional factory used to handle BMP, GIF, or fallback cases
354+
* @param customFactory optional factory used to handle BMP, GIF, or fallback cases
395355
* (e.g., for PNG or TIFF). If {@code null}, this method delegates to
396356
* {@link #createFromByteArray(PDDocument, byte[], String)}.
397357
* @return a PDImageXObject.
398358
* @throws IOException if there is an error when reading the file or creating the
399359
* PDImageXObject.
400360
* @throws IllegalArgumentException if the image type is not supported.
401361
*/
402-
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name, DefaultFactory defaultFactory) throws IOException
362+
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name, CustomFactory customFactory) throws IOException
403363
{
404-
if (defaultFactory == null) {
405-
return createFromByteArray(document, byteArray, name);
406-
}
407-
408364
FileType fileType = FileTypeDetector.detectFileType(byteArray);
409365
if (fileType == null)
410366
{
@@ -441,7 +397,16 @@ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byt
441397
}
442398
if (fileType == FileType.BMP || fileType == FileType.GIF || fileType == FileType.PNG)
443399
{
444-
return defaultFactory.createFromByteArray(document, byteArray);
400+
if (customFactory != null)
401+
{
402+
return customFactory.createFromByteArray(document, byteArray);
403+
}
404+
else
405+
{
406+
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
407+
BufferedImage bim = ImageIO.read(bais);
408+
return LosslessFactory.createFromImage(document, bim);
409+
}
445410
}
446411
throw new IllegalArgumentException("Image type " + fileType + " not supported: " + name);
447412
}

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObjectTest.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,16 @@ void testCreateFromByteArray() throws IOException, URISyntaxException
130130
}
131131

132132
/**
133-
* Test of createFromByteArray method with DefaultFactory parameter, of class PDImageXObject.
133+
* Test of createFromByteArray method with CustomFactory parameter, of class PDImageXObject.
134134
* @throws java.io.IOException
135135
* @throws java.net.URISyntaxException
136136
*/
137137
@Test
138-
void testCreateFromByteArrayWithDefaultFactory() throws IOException, URISyntaxException
138+
void testCreateFromByteArrayWithCustomFactory() throws IOException, URISyntaxException
139139
{
140-
testCompareCreatedFromByteArrayWithCreatedByDefaultFactory("gif.gif");
141-
testCompareCreatedFromByteArrayWithCreatedByDefaultFactory("gif-1bit-transparent.gif");
142-
testCompareCreatedFromByteArrayWithCreatedByDefaultFactory("lzw.tif");
143-
}
144-
145-
/**
146-
* Test of createFromByteArray method with null DefaultFactory parameter, of class PDImageXObject.
147-
* @throws java.io.IOException
148-
* @throws java.net.URISyntaxException
149-
*/
150-
@Test
151-
void testCreateFromByteArrayWithNullDefaultFactory() throws IOException, URISyntaxException
152-
{
153-
testCompareCreatedFromByteArrayWithCreatedByNullDefaultFactory("gif.gif");
154-
testCompareCreatedFromByteArrayWithCreatedByNullDefaultFactory("gif-1bit-transparent.gif");
155-
testCompareCreatedFromByteArrayWithCreatedByNullDefaultFactory("lzw.tif");
140+
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("gif.gif");
141+
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("gif-1bit-transparent.gif");
142+
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("lzw.tif");
156143
}
157144

158145
private void testCompareCreatedFileByExtensionWithCreatedByLosslessFactory(String filename)
@@ -348,7 +335,7 @@ private void testCompareCreatedFromByteArrayWithCreatedByJPEGFactory(String file
348335
}
349336
}
350337

351-
private void testCompareCreatedFromByteArrayWithCreatedByDefaultFactory(String filename)
338+
private void testCompareCreatedFromByteArrayWithCreatedByCustomFactory(String filename)
352339
throws IOException, URISyntaxException
353340
{
354341
try (PDDocument doc = new PDDocument())
@@ -357,9 +344,9 @@ private void testCompareCreatedFromByteArrayWithCreatedByDefaultFactory(String f
357344
InputStream in = new FileInputStream(file);
358345
byte[] byteArray = in.readAllBytes();
359346

360-
DefaultFactory defaultFactory = this::alphaFlattenedJPEGFactory;
347+
CustomFactory customFactory = this::alphaFlattenedJPEGFactory;
361348

362-
PDImageXObject image = PDImageXObject.createFromByteArray(doc, byteArray, filename, defaultFactory);
349+
PDImageXObject image = PDImageXObject.createFromByteArray(doc, byteArray, filename, customFactory);
363350

364351
PDImageXObject expectedImage = alphaFlattenedJPEGFactory(doc, byteArray);
365352

@@ -368,28 +355,6 @@ private void testCompareCreatedFromByteArrayWithCreatedByDefaultFactory(String f
368355
}
369356
}
370357

371-
private void testCompareCreatedFromByteArrayWithCreatedByNullDefaultFactory(String filename)
372-
throws IOException, URISyntaxException
373-
{
374-
try (PDDocument doc = new PDDocument())
375-
{
376-
File file = new File(PDImageXObjectTest.class.getResource(filename).toURI());
377-
InputStream in = new FileInputStream(file);
378-
byte[] byteArray = in.readAllBytes();
379-
380-
DefaultFactory defaultFactory = null;
381-
382-
PDImageXObject image = PDImageXObject.createFromByteArray(doc, byteArray, filename, defaultFactory);
383-
384-
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
385-
BufferedImage bim = ImageIO.read(bais);
386-
PDImageXObject expectedImage = LosslessFactory.createFromImage(doc, bim);
387-
388-
assertEquals(expectedImage.getSuffix(), image.getSuffix());
389-
checkIdentARGB(image.getImage(), expectedImage.getImage());
390-
}
391-
}
392-
393358
private void checkIdentARGB(BufferedImage expectedImage, BufferedImage actualImage)
394359
{
395360
String errMsg = "";

0 commit comments

Comments
 (0)