This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git

commit 31eff5b50b5f623ebb3236b71b191fccfd839df6
Author: Gary Gregory <[email protected]>
AuthorDate: Tue May 16 11:17:27 2023 -0400

    Refactor common code pattern
---
 .../commons/imaging/common/BinaryFunctions.java    |  8 +++---
 .../imaging/formats/png/chunks/PngChunkIccp.java   | 11 +++-----
 .../imaging/formats/png/chunks/PngChunkItxt.java   | 29 +++++-----------------
 .../imaging/formats/png/chunks/PngChunkScal.java   |  9 ++-----
 .../imaging/formats/png/chunks/PngChunkText.java   |  8 ++----
 .../imaging/formats/png/chunks/PngChunkZtxt.java   | 20 ++++-----------
 .../formats/png/chunks/PngChunkTextTest.java       |  3 ++-
 7 files changed, 24 insertions(+), 64 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java 
b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
index 4d7d2b98..c19191f7 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
@@ -62,17 +62,17 @@ public final class BinaryFunctions {
         IOUtils.copy(is, os);
     }
 
-    public static int findNull(final byte[] src) {
-        return findNull(src, 0);
+    public static int findNull(final byte[] src, final String message) throws 
ImagingException {
+        return findNull(src, 0, message);
     }
 
-    public static int findNull(final byte[] src, final int start) {
+    public static int findNull(final byte[] src, final int start, final String 
message) throws ImagingException {
         for (int i = start; i < src.length; i++) {
             if (src[i] == 0) {
                 return i;
             }
         }
-        return -1;
+        throw new ImagingException(message);
     }
 
     public static byte[] getRAFBytes(final RandomAccessFile raf, final long 
pos,
diff --git 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java
index 46ea41ea..daeef98c 100644
--- 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java
+++ 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java
@@ -16,9 +16,6 @@
  */
 package org.apache.commons.imaging.formats.png.chunks;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.findNull;
-import static org.apache.commons.imaging.common.BinaryFunctions.getStreamBytes;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -29,6 +26,7 @@ import java.util.zip.InflaterInputStream;
 
 import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.imaging.common.Allocator;
+import org.apache.commons.imaging.common.BinaryFunctions;
 
 /**
  * The PNG iCCP chunk. If "present, the image samples conform to the color 
space represented by the embedded ICC
@@ -77,10 +75,7 @@ public class PngChunkIccp extends PngChunk {
             throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
 
-        final int index = findNull(bytes);
-        if (index < 0) {
-            throw new ImagingException("PngChunkIccp: No Profile Name");
-        }
+        final int index = BinaryFunctions.findNull(bytes, "PngChunkIccp: No 
Profile Name");
         final byte[] nameBytes = Arrays.copyOf(bytes, index);
         profileName = new String(nameBytes, StandardCharsets.ISO_8859_1);
 
@@ -98,7 +93,7 @@ public class PngChunkIccp extends PngChunk {
             LOGGER.finest("bytes.length: " + bytes.length);
         }
 
-        uncompressedProfile = getStreamBytes(new InflaterInputStream(new 
ByteArrayInputStream(compressedProfile)));
+        uncompressedProfile = BinaryFunctions.getStreamBytes(new 
InflaterInputStream(new ByteArrayInputStream(compressedProfile)));
 
         if (LOGGER.isLoggable(Level.FINEST)) {
             LOGGER.finest("UncompressedProfile: " + bytes.length);
diff --git 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java
index 55cd8a1d..9f2abfba 100644
--- 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java
+++ 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java
@@ -16,9 +16,6 @@
  */
 package org.apache.commons.imaging.formats.png.chunks;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.findNull;
-import static org.apache.commons.imaging.common.BinaryFunctions.getStreamBytes;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -26,6 +23,7 @@ import java.util.zip.InflaterInputStream;
 
 import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.imaging.common.Allocator;
+import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.imaging.formats.png.PngConstants;
 import org.apache.commons.imaging.formats.png.PngText;
 
@@ -50,20 +48,14 @@ public class PngChunkItxt extends PngTextChunk {
     public PngChunkItxt(final int length, final int chunkType, final int crc, 
final byte[] bytes)
             throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
-        int terminator = findNull(bytes);
-        if (terminator < 0) {
-            throw new ImagingException(
-                    "PNG iTXt chunk keyword is not terminated.");
-        }
+        int terminator = BinaryFunctions.findNull(bytes, "PNG iTXt chunk 
keyword is not terminated.");
 
         keyword = new String(bytes, 0, terminator, 
StandardCharsets.ISO_8859_1);
         int index = terminator + 1;
 
         final int compressionFlag = bytes[index++];
         if (compressionFlag != 0 && compressionFlag != 1) {
-            throw new ImagingException(
-                    "PNG iTXt chunk has invalid compression flag: "
-                            + compressionFlag);
+            throw new ImagingException("PNG iTXt chunk has invalid compression 
flag: " + compressionFlag);
         }
 
         final boolean compressed = compressionFlag == 1;
@@ -73,19 +65,11 @@ public class PngChunkItxt extends PngTextChunk {
             throw new ImagingException("PNG iTXt chunk has unexpected 
compression method: " + compressionMethod);
         }
 
-        terminator = findNull(bytes, index);
-        if (terminator < 0) {
-            throw new ImagingException("PNG iTXt chunk language tag is not 
terminated.");
-        }
-
+        terminator = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk 
language tag is not terminated.");
         languageTag = new String(bytes, index, terminator - index, 
StandardCharsets.ISO_8859_1);
         index = terminator + 1;
 
-        terminator = findNull(bytes, index);
-        if (terminator < 0) {
-            throw new ImagingException("PNG iTXt chunk translated keyword is 
not terminated.");
-        }
-
+        terminator = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk 
translated keyword is not terminated.");
         translatedKeyword = new String(bytes, index, terminator - index, 
StandardCharsets.UTF_8);
         index = terminator + 1;
 
@@ -95,8 +79,7 @@ public class PngChunkItxt extends PngTextChunk {
             final byte[] compressedText = 
Allocator.byteArray(compressedTextLength);
             System.arraycopy(bytes, index, compressedText, 0, 
compressedTextLength);
 
-            text = new String(getStreamBytes(
-                    new InflaterInputStream(new 
ByteArrayInputStream(compressedText))), StandardCharsets.UTF_8);
+            text = new String(BinaryFunctions.getStreamBytes(new 
InflaterInputStream(new ByteArrayInputStream(compressedText))), 
StandardCharsets.UTF_8);
 
         } else {
             text = new String(bytes, index, bytes.length - index, 
StandardCharsets.UTF_8);
diff --git 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java
index 9c026e14..d40e90f2 100644
--- 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java
+++ 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java
@@ -16,11 +16,10 @@
  */
 package org.apache.commons.imaging.formats.png.chunks;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.findNull;
-
 import java.nio.charset.StandardCharsets;
 
 import org.apache.commons.imaging.ImagingException;
+import org.apache.commons.imaging.common.BinaryFunctions;
 
 public class PngChunkScal extends PngChunk {
 
@@ -36,11 +35,7 @@ public class PngChunkScal extends PngChunk {
             throw new ImagingException("PNG sCAL invalid unit specifier: " + 
getUnitSpecifier());
         }
 
-        final int separator = findNull(bytes);
-        if (separator < 0) {
-            throw new ImagingException("PNG sCAL x and y axis value separator 
not found.");
-        }
-
+        final int separator = BinaryFunctions.findNull(bytes, "PNG sCAL x and 
y axis value separator not found.");
         final int xIndex = 1;
         final String xStr = new String(bytes, xIndex, separator - 1, 
StandardCharsets.ISO_8859_1);
         unitsPerPixelXAxis = toDouble(xStr);
diff --git 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkText.java 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkText.java
index c982d70e..2961ffcc 100644
--- 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkText.java
+++ 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkText.java
@@ -16,13 +16,12 @@
  */
 package org.apache.commons.imaging.formats.png.chunks;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.findNull;
-
 import java.nio.charset.StandardCharsets;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.commons.imaging.ImagingException;
+import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.imaging.formats.png.PngText;
 
 public class PngChunkText extends PngTextChunk {
@@ -34,10 +33,7 @@ public class PngChunkText extends PngTextChunk {
 
     public PngChunkText(final int length, final int chunkType, final int crc, 
final byte[] bytes) throws ImagingException {
         super(length, chunkType, crc, bytes);
-        final int index = findNull(bytes);
-        if (index < 0) {
-            throw new ImagingException("PNG tEXt chunk keyword is not 
terminated.");
-        }
+        final int index = BinaryFunctions.findNull(bytes, "PNG tEXt chunk 
keyword is not terminated.");
         keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
         final int textLength = bytes.length - (index + 1);
         text = new String(bytes, index + 1, textLength, 
StandardCharsets.ISO_8859_1);
diff --git 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java
index 53fe00c3..2dbf102a 100644
--- 
a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java
+++ 
b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java
@@ -16,9 +16,6 @@
  */
 package org.apache.commons.imaging.formats.png.chunks;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.findNull;
-import static org.apache.commons.imaging.common.BinaryFunctions.getStreamBytes;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -26,6 +23,7 @@ import java.util.zip.InflaterInputStream;
 
 import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.imaging.common.Allocator;
+import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.imaging.formats.png.PngConstants;
 import org.apache.commons.imaging.formats.png.PngText;
 
@@ -34,31 +32,23 @@ public class PngChunkZtxt extends PngTextChunk {
     private final String keyword;
     private final String text;
 
-    public PngChunkZtxt(final int length, final int chunkType, final int crc, 
final byte[] bytes)
-            throws ImagingException, IOException {
+    public PngChunkZtxt(final int length, final int chunkType, final int crc, 
final byte[] bytes) throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
 
-        int index = findNull(bytes);
-        if (index < 0) {
-            throw new ImagingException(
-                    "PNG zTXt chunk keyword is unterminated.");
-        }
-
+        int index = BinaryFunctions.findNull(bytes, "PNG zTXt chunk keyword is 
unterminated.");
         keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
         index++;
 
         final int compressionMethod = bytes[index++];
         if (compressionMethod != PngConstants.COMPRESSION_DEFLATE_INFLATE) {
-            throw new ImagingException(
-                    "PNG zTXt chunk has unexpected compression method: "
-                            + compressionMethod);
+            throw new ImagingException("PNG zTXt chunk has unexpected 
compression method: " + compressionMethod);
         }
 
         final int compressedTextLength = bytes.length - index;
         final byte[] compressedText = 
Allocator.byteArray(compressedTextLength);
         System.arraycopy(bytes, index, compressedText, 0, 
compressedTextLength);
 
-        text = new String(getStreamBytes(new InflaterInputStream(new 
ByteArrayInputStream(compressedText))), StandardCharsets.ISO_8859_1);
+        text = new String(BinaryFunctions.getStreamBytes(new 
InflaterInputStream(new ByteArrayInputStream(compressedText))), 
StandardCharsets.ISO_8859_1);
     }
 
     @Override
diff --git 
a/src/test/java/org/apache/commons/imaging/formats/png/chunks/PngChunkTextTest.java
 
b/src/test/java/org/apache/commons/imaging/formats/png/chunks/PngChunkTextTest.java
index 07e14f60..8c9bbd75 100644
--- 
a/src/test/java/org/apache/commons/imaging/formats/png/chunks/PngChunkTextTest.java
+++ 
b/src/test/java/org/apache/commons/imaging/formats/png/chunks/PngChunkTextTest.java
@@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.fail;
 
 import org.apache.commons.imaging.ImagingConstants;
+import org.apache.commons.imaging.common.BinaryFunctions;
 import org.junit.jupiter.api.Test;
 
 public class PngChunkTextTest{
@@ -32,7 +33,7 @@ public class PngChunkTextTest{
             fail("Expecting exception: Exception");
         } catch (final Throwable e) {
             assertEquals("PNG tEXt chunk keyword is not 
terminated.",e.getMessage());
-            assertEquals(PngChunkText.class.getName(), 
e.getStackTrace()[0].getClassName());
+            assertEquals(BinaryFunctions.class.getName(), 
e.getStackTrace()[0].getClassName());
         }
     }
 

Reply via email to