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:47 UTC

(commons-imaging) 03/05: Replace BinaryFunctions.startsWith(byte[], BinaryConstant) with BinaryConstant.isStartOf(byte[])

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 ccb3fee442b26a0e3890a08c94973fd7d9e1f012
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 22 09:28:51 2025 -0500

    Replace BinaryFunctions.startsWith(byte[], BinaryConstant) with
    BinaryConstant.isStartOf(byte[])
---
 src/changes/changes.xml                                      |  2 ++
 .../org/apache/commons/imaging/common/BinaryConstant.java    | 10 ++++++++++
 .../org/apache/commons/imaging/common/BinaryFunctions.java   | 12 ------------
 .../apache/commons/imaging/formats/jpeg/JpegImageParser.java |  5 ++---
 .../commons/imaging/formats/jpeg/exif/ExifRewriter.java      |  3 +--
 .../apache/commons/imaging/formats/jpeg/iptc/IptcParser.java |  2 +-
 .../commons/imaging/formats/jpeg/segments/App2Segment.java   |  3 +--
 .../commons/imaging/formats/jpeg/xmp/JpegRewriter.java       |  6 ++----
 .../commons/imaging/formats/jpeg/xmp/JpegXmpParser.java      |  3 +--
 9 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 476c2375..08ccd018 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -71,6 +71,7 @@ The <action> type attribute can be add,update,fix,remove.
       <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>
+      <action dev="ggregory" type="remove" due-to="Dependabot">Replace BinaryFunctions.startsWith(byte[], BinaryConstant) with BinaryConstant.isStartOf(byte[]).</action>
     </release>
     <release version="1.0.0-alpha5" date="2024-04-15" description="The 1.0.0-alpha5 release requires Java 8.">
       <!-- FIX -->
@@ -81,6 +82,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="update" due-to="Dependabot">Bump commons-io:commons-io from 2.16.0 to 2.16.1 #385.</action>
       <action dev="ggregory" type="update" due-to="Dependabot">Bump org.codehaus.mojo:animal-sniffer-maven-plugin from 1.23 to 1.24 #412.</action>
       <action dev="ggregory" type="update" due-to="Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.0.0 to 3.1.0 #411.</action>
+      <action dev="ggregory" type="update" due-to="Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.0.0 to 3.1.0 #411.</action>
     </release>
     <release version="1.0.0-alpha4" date="2024-03-30" description="The 1.0.0-alpha4 release requires Java 8.">
       <!-- FIX -->
diff --git a/src/main/java/org/apache/commons/imaging/common/BinaryConstant.java b/src/main/java/org/apache/commons/imaging/common/BinaryConstant.java
index 39dda0d3..fe507181 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryConstant.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryConstant.java
@@ -56,6 +56,16 @@ public class BinaryConstant {
         return value.length;
     }
 
+    /**
+     * Tests whether the give buffer starts with this binary constant.
+     *
+     * @param buffer The buffer to test.
+     * @return Whether the give buffer starts with this binary constant.
+     */
+    public boolean isStartOf(final byte[] buffer) {
+        return BinaryFunctions.startsWith(buffer, value);
+    }
+
     public void writeTo(final OutputStream os) throws IOException {
         for (final byte element : value) {
             os.write(element);
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 2579428e..a7412719 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
@@ -322,18 +322,6 @@ public final class BinaryFunctions {
         }
     }
 
-    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;
-    }
-
     public static boolean startsWith(final byte[] buffer, final byte[] search) {
         if (search == null) {
             return false;
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
index 05ae95f1..cafbd5f1 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
@@ -17,7 +17,6 @@
 package org.apache.commons.imaging.formats.jpeg;
 
 import static org.apache.commons.imaging.common.BinaryFunctions.remainingBytes;
-import static org.apache.commons.imaging.common.BinaryFunctions.startsWith;
 
 import java.awt.Dimension;
 import java.awt.image.BufferedImage;
@@ -71,7 +70,7 @@ public class JpegImageParser extends AbstractImageParser<JpegImagingParameters>
     private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.JPEG.getExtensions();
 
     public static boolean isExifApp1Segment(final GenericSegment segment) {
-        return startsWith(segment.getSegmentData(), JpegConstants.EXIF_IDENTIFIER_CODE);
+        return JpegConstants.EXIF_IDENTIFIER_CODE.isStartOf(segment.getSegmentData());
     }
 
     /**
@@ -760,7 +759,7 @@ public class JpegImageParser extends AbstractImageParser<JpegImagingParameters>
                     return false;
                 }
 
-                if (marker == JpegConstants.JPEG_APP1_MARKER && startsWith(segmentData, JpegConstants.EXIF_IDENTIFIER_CODE)) {
+                if (marker == JpegConstants.JPEG_APP1_MARKER && JpegConstants.EXIF_IDENTIFIER_CODE.isStartOf(segmentData)) {
                     result[0] = true;
                     return false;
                 }
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java
index 2db6f533..17fc1f24 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java
@@ -17,7 +17,6 @@
 package org.apache.commons.imaging.formats.jpeg.exif;
 
 import static org.apache.commons.imaging.common.BinaryFunctions.remainingBytes;
-import static org.apache.commons.imaging.common.BinaryFunctions.startsWith;
 
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
@@ -149,7 +148,7 @@ public class ExifRewriter extends BinaryFileParser {
                     final byte[] segmentData) throws
             // ImageWriteException,
             ImagingException, IOException {
-                if (marker != JpegConstants.JPEG_APP1_MARKER || !startsWith(segmentData, JpegConstants.EXIF_IDENTIFIER_CODE)) {
+                if (marker != JpegConstants.JPEG_APP1_MARKER || !JpegConstants.EXIF_IDENTIFIER_CODE.isStartOf(segmentData)) {
                     pieces.add(new JFIFPieceSegment(marker, markerBytes, markerLengthBytes, segmentData));
                 } else {
                     final JFIFPiece piece = new JFIFPieceSegmentExif(marker, markerBytes, markerLengthBytes, segmentData);
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 7326d64f..cac55442 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
@@ -96,7 +96,7 @@ public class IptcParser extends BinaryFileParser {
     }
 
     public boolean isPhotoshopJpegSegment(final byte[] segmentData) {
-        if (!BinaryFunctions.startsWith(segmentData, JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING)) {
+        if (!JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING.isStartOf(segmentData)) {
             return false;
         }
 
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/App2Segment.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/App2Segment.java
index 8dbd506c..a92a49fd 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/App2Segment.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/App2Segment.java
@@ -19,7 +19,6 @@ package org.apache.commons.imaging.formats.jpeg.segments;
 import static org.apache.commons.imaging.common.BinaryFunctions.readAndVerifyBytes;
 import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
 import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
-import static org.apache.commons.imaging.common.BinaryFunctions.startsWith;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -40,7 +39,7 @@ public class App2Segment extends AppnSegment implements Comparable<App2Segment>
     public App2Segment(final int marker, int markerLength, final InputStream is2) throws ImagingException, IOException {
         super(marker, markerLength, is2);
 
-        if (startsWith(getSegmentData(), JpegConstants.ICC_PROFILE_LABEL)) {
+        if (JpegConstants.ICC_PROFILE_LABEL.isStartOf(getSegmentData())) {
             final InputStream is = new ByteArrayInputStream(getSegmentData());
 
             readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL, "Not a Valid App2 Segment: missing ICC Profile label");
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java
index 5618f07d..becbed6b 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.imaging.formats.jpeg.xmp;
 
-import static org.apache.commons.imaging.common.BinaryFunctions.startsWith;
-
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -107,7 +105,7 @@ public class JpegRewriter extends BinaryFileParser {
             if (marker != JpegConstants.JPEG_APP1_MARKER) {
                 return false;
             }
-            if (!startsWith(segmentData, JpegConstants.EXIF_IDENTIFIER_CODE)) {
+            if (!JpegConstants.EXIF_IDENTIFIER_CODE.isStartOf(segmentData)) {
                 return false;
             }
             return true;
@@ -127,7 +125,7 @@ public class JpegRewriter extends BinaryFileParser {
             if (marker != JpegConstants.JPEG_APP1_MARKER) {
                 return false;
             }
-            if (!startsWith(segmentData, JpegConstants.XMP_IDENTIFIER)) {
+            if (!JpegConstants.XMP_IDENTIFIER.isStartOf(segmentData)) {
                 return false;
             }
             return true;
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpParser.java
index 98f55751..771c1243 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpParser.java
@@ -21,7 +21,6 @@ import java.nio.charset.StandardCharsets;
 
 import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.imaging.common.BinaryFileParser;
-import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.imaging.formats.jpeg.JpegConstants;
 
 public class JpegXmpParser extends BinaryFileParser {
@@ -34,7 +33,7 @@ public class JpegXmpParser extends BinaryFileParser {
     }
 
     public boolean isXmpJpegSegment(final byte[] segmentData) {
-        return BinaryFunctions.startsWith(segmentData, JpegConstants.XMP_IDENTIFIER);
+        return JpegConstants.XMP_IDENTIFIER.isStartOf(segmentData);
     }
 
     public String parseXmpJpegSegment(final byte[] segmentData) throws ImagingException {