-
Notifications
You must be signed in to change notification settings - Fork 62
perf: stream image outputs directly into zip entries #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,11 @@ public byte[] convert(byte[] pdfBytes, String format, int dpi) throws IOExceptio | |
| for (int i = 0; i < doc.getNumberOfPages(); i++) { | ||
| BufferedImage image = renderer.renderImageWithDPI(i, dpi, ImageType.RGB); | ||
|
|
||
| ByteArrayOutputStream imgOut = new ByteArrayOutputStream(); | ||
| ImageIO.write(image, imageioFormat, imgOut); | ||
|
|
||
| ZipEntry entry = new ZipEntry(String.format("page_%03d.%s", i + 1, fmt)); | ||
| zos.putNextEntry(entry); | ||
| zos.write(imgOut.toByteArray()); | ||
| if (!ImageIO.write(image, imageioFormat, zos)) { | ||
| throw new IOException("Unsupported image output format: " + imageioFormat); | ||
| } | ||
| zos.closeEntry(); | ||
|
Comment on lines
34
to
39
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Covered in |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.crystalpdf.backend.service; | ||
|
|
||
| import com.crystalpdf.backend.helper.PdfTestHelper; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class ExtractImagesServiceTest { | ||
|
|
||
| private final ExtractImagesService extractImagesService = new ExtractImagesService(); | ||
|
|
||
| @Test | ||
| void extractImages_returnsZipWithDecodableImages() throws Exception { | ||
| byte[] pdf = PdfTestHelper.createPdfWithImage(); | ||
| byte[] zipBytes = extractImagesService.extractImages(pdf, "png"); | ||
|
|
||
| try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(zipBytes))) { | ||
| ZipEntry entry = zip.getNextEntry(); | ||
| assertThat(entry).isNotNull(); | ||
| assertThat(entry.getName()).isEqualTo("page1_img1.png"); | ||
| assertThat(ImageIO.read(new ByteArrayInputStream(readEntry(zip)))).isNotNull(); | ||
| zip.closeEntry(); | ||
|
|
||
| assertThat(zip.getNextEntry()).isNull(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void extractImages_throwsForUnsupportedFormat() throws Exception { | ||
| byte[] pdf = PdfTestHelper.createPdfWithImage(); | ||
|
|
||
| assertThatThrownBy(() -> extractImagesService.extractImages(pdf, "tiff")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("Unsupported image output format: tiff"); | ||
| } | ||
|
|
||
| private static byte[] readEntry(ZipInputStream zip) throws Exception { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| zip.transferTo(out); | ||
| return out.toByteArray(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.crystalpdf.backend.service; | ||
|
|
||
| import com.crystalpdf.backend.helper.PdfTestHelper; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class PdfToImageServiceTest { | ||
|
|
||
| private final PdfToImageService pdfToImageService = new PdfToImageService(); | ||
|
|
||
| @Test | ||
| void convert_returnsZipWithDecodablePageImages() throws Exception { | ||
| byte[] pdf = PdfTestHelper.createPdf(2); | ||
| byte[] zipBytes = pdfToImageService.convert(pdf, "png", 72); | ||
|
|
||
| try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(zipBytes))) { | ||
| ZipEntry first = zip.getNextEntry(); | ||
| assertThat(first).isNotNull(); | ||
| assertThat(first.getName()).isEqualTo("page_001.png"); | ||
| assertThat(ImageIO.read(new ByteArrayInputStream(readEntry(zip)))).isNotNull(); | ||
| zip.closeEntry(); | ||
|
|
||
| ZipEntry second = zip.getNextEntry(); | ||
| assertThat(second).isNotNull(); | ||
| assertThat(second.getName()).isEqualTo("page_002.png"); | ||
| assertThat(ImageIO.read(new ByteArrayInputStream(readEntry(zip)))).isNotNull(); | ||
| zip.closeEntry(); | ||
|
|
||
| assertThat(zip.getNextEntry()).isNull(); | ||
| } | ||
| } | ||
|
|
||
| private static byte[] readEntry(ZipInputStream zip) throws Exception { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| zip.transferTo(out); | ||
| return out.toByteArray(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Covered in
ExtractImagesServiceTest:extractImages_returnsZipWithDecodableImagesopens the ZIP, checks thepage1_img1.pngentry name, and verifies the entry decodes throughImageIO.read(...);extractImages_throwsForUnsupportedFormatlocks the invalid-format behavior toIllegalArgumentException. Focused Gradle run passed 3/3.