From 7d8c75e4dacc92810e16bd3eb9d3cd6d8012f10b Mon Sep 17 00:00:00 2001 From: Anton Oellerer <13524304+AntonOellerer@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:53:14 +0100 Subject: [PATCH] Update image save workflow * throw new checked exception if format not found * add `saveAsPng` method --- build.gradle | 2 +- .../jocument/image/DefaultImageReference.java | 19 ++++++++++++++----- .../jocument/image/ImageReference.java | 6 ++++-- .../image/NoWriterFoundException.java | 7 +++++++ 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/docutools/jocument/image/NoWriterFoundException.java diff --git a/build.gradle b/build.gradle index dd6bb279..98fb9cbb 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group 'com.docutools' -version = '4.2.8' +version = '5.0.0' java { toolchain { diff --git a/src/main/java/com/docutools/jocument/image/DefaultImageReference.java b/src/main/java/com/docutools/jocument/image/DefaultImageReference.java index 18021e75..5e342963 100644 --- a/src/main/java/com/docutools/jocument/image/DefaultImageReference.java +++ b/src/main/java/com/docutools/jocument/image/DefaultImageReference.java @@ -40,19 +40,28 @@ public BufferedImage getImage() { } @Override - public Path saveAsJpeg() throws IOException { + public Path saveAsJpeg() throws IOException, NoWriterFoundException { + return saveAs(".jpg", "JPEG"); + } + + @Override + public Path saveAsPng() throws IOException, NoWriterFoundException { + return saveAs(".png", "PNG"); + } + + private Path saveAs(String suffix, String formatName) throws IOException, NoWriterFoundException { if (image == null) { throw new ImageReferenceClosedException("Image was already closed."); } - var filePath = Files.createTempFile("jocument-", ".jpg"); + var filePath = Files.createTempFile("jocument-", suffix); log.trace("Saving image {} to '{}'...", id, filePath); - boolean foundWriter = ImageIO.write(image, "JPEG", filePath.toFile()); + boolean foundWriter = ImageIO.write(image, formatName, filePath.toFile()); if (!foundWriter) { - throw new IOException("Found no JPG writer"); + throw new NoWriterFoundException(formatName); } - log.trace("Successfully saved image {} to '{}'!", id, filePath); + log.trace("Successfully saved image {} to '{}'", id, filePath); return filePath; } diff --git a/src/main/java/com/docutools/jocument/image/ImageReference.java b/src/main/java/com/docutools/jocument/image/ImageReference.java index 0444a731..f2ca9e89 100644 --- a/src/main/java/com/docutools/jocument/image/ImageReference.java +++ b/src/main/java/com/docutools/jocument/image/ImageReference.java @@ -24,7 +24,7 @@ public abstract class ImageReference implements AutoCloseable { * @param width width in pixel * @param height height in pixel */ - public ImageReference(int width, int height) { + protected ImageReference(int width, int height) { this.id = UUID.randomUUID(); this.width = width; this.height = height; @@ -62,7 +62,9 @@ public int getHeight() { * * @return the {@link Path} to the JPEG */ - public abstract Path saveAsJpeg() throws IOException; + public abstract Path saveAsJpeg() throws IOException, NoWriterFoundException; + + public abstract Path saveAsPng() throws IOException, NoWriterFoundException; @Override public abstract void close(); diff --git a/src/main/java/com/docutools/jocument/image/NoWriterFoundException.java b/src/main/java/com/docutools/jocument/image/NoWriterFoundException.java new file mode 100644 index 00000000..97924708 --- /dev/null +++ b/src/main/java/com/docutools/jocument/image/NoWriterFoundException.java @@ -0,0 +1,7 @@ +package com.docutools.jocument.image; + +public class NoWriterFoundException extends Exception { + public NoWriterFoundException(String type) { + super("Found no writer for type %s".formatted(type)); + } +}