You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/02/22 15:05:46 UTC

(commons-imaging) 02/05: Rename methods BinaryFunctions.findNull() to BinaryFunctions.indexOf0()

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 1ca759f7b036551398e735d2dffcad53f00cffca
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 22 09:21:03 2025 -0500

    Rename methods BinaryFunctions.findNull() to BinaryFunctions.indexOf0()
    
    - Use Arrays.copyOfRange()
    - Use ArrayUtils
---
 pom.xml                                            |  1 -
 src/changes/changes.xml                            |  3 +
 .../commons/imaging/common/BinaryFunctions.java    | 94 ++++++++++++----------
 .../imaging/formats/jpeg/iptc/IptcParser.java      |  2 +-
 .../imaging/formats/png/chunks/PngChunkIccp.java   |  2 +-
 .../imaging/formats/png/chunks/PngChunkItxt.java   |  6 +-
 .../imaging/formats/png/chunks/PngChunkScal.java   |  2 +-
 .../imaging/formats/png/chunks/PngChunkText.java   |  2 +-
 .../imaging/formats/png/chunks/PngChunkZtxt.java   |  2 +-
 .../commons/imaging/formats/tiff/TiffField.java    |  2 +-
 10 files changed, 64 insertions(+), 52 deletions(-)

diff --git a/pom.xml b/pom.xml
index 5a417f27..ed7b91fb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -238,7 +238,6 @@
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.17.0</version>
-      <scope>test</scope>
     </dependency>
   </dependencies>
   <reporting>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c0d07bef..476c2375 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -68,6 +68,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="update" due-to="Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #453.</action>
       <!-- REMOVE -->
       <action dev="ggregory" type="remove" due-to="Dependabot">Remove direct dependency on org.hamcrest:hamcrest.</action>
+      <action dev="ggregory" type="remove" due-to="Dependabot">Rename methods BinaryFunctions.findNull() to indexOf0() like String.</action>
+      <action dev="ggregory" type="remove" due-to="Dependabot">Rename method BinaryFunctions.slice() to copyOfRange() like Arrays.</action>
+      <action dev="ggregory" type="remove" due-to="Dependabot">Rename method BinaryFunctions.head() to copyOfStart() like Arrays.</action>
     </release>
     <release version="1.0.0-alpha5" date="2024-04-15" description="The 1.0.0-alpha5 release requires Java 8.">
       <!-- FIX -->
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 ae5ae04d..2579428e 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
@@ -21,17 +21,20 @@ import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.RandomAccessFile;
 import java.nio.ByteOrder;
+import java.util.Arrays;
 import java.util.logging.Logger;
 
 import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.RandomAccessFiles;
+import org.apache.commons.lang3.ArrayUtils;
 
 /**
  * Convenience methods for various binary and I/O operations.
  */
 public final class BinaryFunctions {
 
+    private static final byte NUL = (byte) 0;
     private static final Logger LOGGER = Logger.getLogger(BinaryFunctions.class.getName());
 
     public static int charsToQuad(final char c1, final char c2, final char c3, final char c4) {
@@ -45,28 +48,38 @@ public final class BinaryFunctions {
         if (b.length < bStart + length) {
             return false;
         }
-
         for (int i = 0; i < length; i++) {
             if (a[aStart + i] != b[bStart + i]) {
                 return false;
             }
         }
-
         return true;
     }
 
-
-    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;
-            }
-        }
-        throw new ImagingException(message);
+    /**
+     * Copies the specified range of the specified array into a new array.
+     *
+     * @param original the array from which a range is to be copied.
+     * @param from     the initial index of the range to be copied, inclusive.
+     * @param count    the amount of bytes to copy.
+     * @return a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length.
+     */
+    public static byte[] copyOfRange(final byte[] original, final int from, final int count) {
+        return Arrays.copyOfRange(original, from, from + Allocator.checkByteArray(count));
     }
 
-    public static int findNull(final byte[] src, final String message) throws ImagingException {
-        return findNull(src, 0, message);
+    /**
+     * Copies the start of the specified array into a new array.
+     *
+     * @param original the array from which a range is to be copied.
+     * @param count    the amount of bytes to copy.
+     * @return a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length.
+     */
+    public static byte[] copyOfStart(final byte[] original, int count) {
+        if (count > original.length) {
+            count = original.length;
+        }
+        return copyOfRange(original, 0, count);
     }
 
     public static byte[] getBytes(final RandomAccessFile raf, final long pos, final int length, final String exception) throws IOException {
@@ -78,11 +91,33 @@ public final class BinaryFunctions {
 
     }
 
-    public static byte[] head(final byte[] bytes, int count) {
-        if (count > bytes.length) {
-            count = bytes.length;
+    /**
+     * Finds the index of the first 0 in the array starting at the given index.
+     *
+     * @param src  the array to search through for the object, may be {@code null}
+     * @param start  the index to start searching at
+     * @param message The ImagingException message if 0 is not found.
+     * @return the index of the value within the array,
+     * @throws ImagingException Thrown if 0 is not found.
+     */
+    public static int indexOf0(final byte[] src, final int start, final String message) throws ImagingException {
+        final int i = ArrayUtils.indexOf(src, NUL, start);
+        if (i < 0) {
+            throw new ImagingException(message);
         }
-        return slice(bytes, 0, count);
+        return i;
+    }
+
+    /**
+     * Finds the index of the first 0 in the array starting at the given index.
+     *
+     * @param src  the array to search through for the object, may be {@code null}
+     * @param message The ImagingException message if 0 is not found.
+     * @return the index of the value within the array,
+     * @throws ImagingException Thrown if 0 is not found.
+     */
+    public static int indexOf0(final byte[] src, final String message) throws ImagingException {
+        return indexOf0(src, 0, message);
     }
 
     public static void logByteBits(final String msg, final byte i) {
@@ -96,7 +131,6 @@ public final class BinaryFunctions {
 
     public static void printCharQuad(final PrintWriter pw, final String msg, final int i) {
         pw.println(msg + ": '" + (char) (0xff & i >> 24) + (char) (0xff & i >> 16) + (char) (0xff & i >> 8) + (char) (0xff & i >> 0) + "'");
-
     }
 
     /**
@@ -120,14 +154,12 @@ public final class BinaryFunctions {
         if ((byte0 | byte1) < 0) {
             throw new IOException(exception);
         }
-
         final int result;
         if (byteOrder == ByteOrder.BIG_ENDIAN) {
             result = byte0 << 8 | byte1;
         } else {
             result = byte1 << 8 | byte0;
         }
-
         return result;
     }
 
@@ -138,14 +170,12 @@ public final class BinaryFunctions {
         if ((byte0 | byte1 | byte2) < 0) {
             throw new IOException(exception);
         }
-
         final int result;
         if (byteOrder == ByteOrder.BIG_ENDIAN) {
             result = byte0 << 16 | byte1 << 8 | byte2 << 0;
         } else {
             result = byte2 << 16 | byte1 << 8 | byte0 << 0;
         }
-
         return result;
     }
 
@@ -157,14 +187,12 @@ public final class BinaryFunctions {
         if ((byte0 | byte1 | byte2 | byte3) < 0) {
             throw new IOException(exception);
         }
-
         final int result;
         if (byteOrder == ByteOrder.BIG_ENDIAN) {
             result = byte0 << 24 | byte1 << 16 | byte2 << 8 | byte3 << 0;
         } else {
             result = byte3 << 24 | byte2 << 16 | byte1 << 8 | byte0 << 0;
         }
-
         return result;
     }
 
@@ -179,7 +207,6 @@ public final class BinaryFunctions {
      * @throws IOException in the event of a non-recoverable error, such as an attempt to read past the end of file.
      */
     public static long read8Bytes(final String name, final InputStream is, final String exception, final ByteOrder byteOrder) throws IOException {
-
         final long byte0 = is.read();
         final long byte1 = is.read();
         final long byte2 = is.read();
@@ -188,18 +215,15 @@ public final class BinaryFunctions {
         final long byte5 = is.read();
         final long byte6 = is.read();
         final long byte7 = is.read();
-
         if ((byte0 | byte1 | byte2 | byte3 | byte4 | byte5 | byte6 | byte7) < 0) {
             throw new IOException(exception);
         }
-
         final long result;
         if (byteOrder == ByteOrder.BIG_ENDIAN) {
             result = byte0 << 56 | byte1 << 48 | byte2 << 40 | byte3 << 32 | byte4 << 24 | byte5 << 16 | byte6 << 8 | byte7 << 0;
         } else {
             result = byte7 << 56 | byte6 << 48 | byte5 << 40 | byte4 << 32 | byte3 << 24 | byte2 << 16 | byte1 << 8 | byte0 << 0;
         }
-
         return result;
     }
 
@@ -207,11 +231,9 @@ public final class BinaryFunctions {
         for (int i = 0; i < expected.size(); i++) {
             final int data = is.read();
             final byte b = (byte) (0xff & data);
-
             if (data < 0) {
                 throw new ImagingException("Unexpected EOF.");
             }
-
             if (b != expected.get(i)) {
                 throw new ImagingException(exception);
             }
@@ -222,11 +244,9 @@ public final class BinaryFunctions {
         for (final byte element : expected) {
             final int data = is.read();
             final byte b = (byte) (0xff & data);
-
             if (data < 0) {
                 throw new ImagingException("Unexpected EOF.");
             }
-
             if (b != element) {
                 throw new ImagingException(exception);
             }
@@ -258,7 +278,7 @@ public final class BinaryFunctions {
     }
 
     public static byte[] remainingBytes(final String name, final byte[] bytes, final int count) {
-        return slice(bytes, count, bytes.length - count);
+        return copyOfRange(bytes, count, bytes.length - count);
     }
 
     /**
@@ -302,23 +322,15 @@ public final class BinaryFunctions {
         }
     }
 
-    public static byte[] slice(final byte[] bytes, final int start, final int count) {
-        final byte[] result = Allocator.byteArray(count);
-        System.arraycopy(bytes, start, result, 0, count);
-        return result;
-    }
-
     public static boolean startsWith(final byte[] buffer, final BinaryConstant search) {
         if (buffer == null || buffer.length < search.size()) {
             return false;
         }
-
         for (int i = 0; i < search.size(); i++) {
             if (buffer[i] != search.get(i)) {
                 return false;
             }
         }
-
         return true;
     }
 
@@ -332,13 +344,11 @@ public final class BinaryFunctions {
         if (search.length > buffer.length) {
             return false;
         }
-
         for (int i = 0; i < search.length; i++) {
             if (search[i] != buffer[i]) {
                 return false;
             }
         }
-
         return true;
     }
 
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
index 997b8bc0..7326d64f 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
@@ -253,7 +253,7 @@ public class IptcParser extends BinaryFileParser {
                 return elements;
             }
 
-            final byte[] recordData = BinaryFunctions.slice(bytes, index, recordSize);
+            final byte[] recordData = BinaryFunctions.copyOfRange(bytes, index, recordSize);
             index += recordSize;
 
             // Debug.debug("recordSize", recordSize + " (0x"
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 6a3d2228..d41767a8 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
@@ -75,7 +75,7 @@ public class PngChunkIccp extends PngChunk {
     public PngChunkIccp(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
 
-        final int index = BinaryFunctions.findNull(bytes, "PngChunkIccp: No Profile Name");
+        final int index = BinaryFunctions.indexOf0(bytes, "PngChunkIccp: No Profile Name");
         final byte[] nameBytes = Arrays.copyOf(bytes, index);
         profileName = new String(nameBytes, StandardCharsets.ISO_8859_1);
 
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 df479694..1e5c17e1 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
@@ -56,7 +56,7 @@ public class PngChunkItxt extends AbstractPngTextChunk {
      */
     public PngChunkItxt(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
-        int terminator = BinaryFunctions.findNull(bytes, "PNG iTXt chunk keyword is not terminated.");
+        int terminator = BinaryFunctions.indexOf0(bytes, "PNG iTXt chunk keyword is not terminated.");
 
         keyword = new String(bytes, 0, terminator, StandardCharsets.ISO_8859_1);
         int index = terminator + 1;
@@ -73,11 +73,11 @@ public class PngChunkItxt extends AbstractPngTextChunk {
             throw new ImagingException("PNG iTXt chunk has unexpected compression method: " + compressionMethod);
         }
 
-        terminator = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk language tag is not terminated.");
+        terminator = BinaryFunctions.indexOf0(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 = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk translated keyword is not terminated.");
+        terminator = BinaryFunctions.indexOf0(bytes, index, "PNG iTXt chunk translated keyword is not terminated.");
         translatedKeyword = new String(bytes, index, terminator - index, StandardCharsets.UTF_8);
         index = terminator + 1;
 
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 d40e90f2..4ae1810a 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
@@ -35,7 +35,7 @@ public class PngChunkScal extends PngChunk {
             throw new ImagingException("PNG sCAL invalid unit specifier: " + getUnitSpecifier());
         }
 
-        final int separator = BinaryFunctions.findNull(bytes, "PNG sCAL x and y axis value separator not found.");
+        final int separator = BinaryFunctions.indexOf0(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 dbcea227..33cb18bc 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
@@ -43,7 +43,7 @@ public class PngChunkText extends AbstractPngTextChunk {
      */
     public PngChunkText(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException {
         super(length, chunkType, crc, bytes);
-        final int index = BinaryFunctions.findNull(bytes, "PNG tEXt chunk keyword is not terminated.");
+        final int index = BinaryFunctions.indexOf0(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 63fffa07..d560e041 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
@@ -36,7 +36,7 @@ public class PngChunkZtxt extends AbstractPngTextChunk {
     public PngChunkZtxt(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
         super(length, chunkType, crc, bytes);
 
-        int index = BinaryFunctions.findNull(bytes, "PNG zTXt chunk keyword is unterminated.");
+        int index = BinaryFunctions.indexOf0(bytes, "PNG zTXt chunk keyword is unterminated.");
         keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
         index++;
 
diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java
index 150cb945..36d4a8a0 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java
@@ -110,7 +110,7 @@ public class TiffField {
      * @return the value of the field, in the byte order of the field.
      */
     public byte[] getByteArrayValue() {
-        return BinaryFunctions.head(value, getBytesLength());
+        return BinaryFunctions.copyOfStart(value, getBytesLength());
     }
 
     /**