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 2024/11/18 14:50:53 UTC

(commons-compress) branch master updated (55cb3b235 -> 16fc9e1cb)

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

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


    from 55cb3b235 (doc) Add gzip's ExtraField public apis for accessing SubField(s). (#608)
     new 9c41f4cd1 Sort members
     new 16fc9e1cb Add missing Javadoc

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../compress/compressors/gzip/ExtraField.java      | 125 ++++++++++-----------
 .../gzip/GzipCompressorOutputStreamTest.java       |   6 +-
 2 files changed, 64 insertions(+), 67 deletions(-)


(commons-compress) 02/02: Add missing Javadoc

Posted by gg...@apache.org.
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-compress.git

commit 16fc9e1cb3e105778ad27fd9093c032036468e98
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Nov 18 09:50:47 2024 -0500

    Add missing Javadoc
    
    - A getter method "Gets..."
    - getEncodedSize() doesn't "Calculate" anything, it's a getter
    - Use the same terminology as List#iterator(),
    Collections.unmodifiableList(), List#get(), and so on.
    - Use longer lines (160)
---
 .../compress/compressors/gzip/ExtraField.java      | 71 +++++++++++-----------
 .../gzip/GzipCompressorOutputStreamTest.java       |  6 +-
 2 files changed, 37 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
index 5e8e80b20..ae5d618ee 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
@@ -29,17 +29,15 @@ import java.util.Objects;
 import org.apache.commons.compress.compressors.gzip.ExtraField.SubField;
 
 /**
- * If the {@code FLG.FEXTRA} bit is set, an "extra field" is present in the
- * header, with total length XLEN bytes.
+ * If the {@code FLG.FEXTRA} bit is set, an "extra field" is present in the header, with total length XLEN bytes.
  *
  * <pre>
  * +---+---+=================================+
- * | XLEN  |...XLEN bytes of "extra field"...| (more-->)
+ * | XLEN  |...XLEN bytes of "extra field"...| (more...)
  * +---+---+=================================+
  * </pre>
  *
- * This class represents the extra field payload (excluding the XLEN 2 bytes).
- * The ExtraField payload consists of a series of subfields, each of the form:
+ * This class represents the extra field payload (excluding the XLEN 2 bytes). The ExtraField payload consists of a series of subfields, each of the form:
  *
  * <pre>
  * +---+---+---+---+==================================+
@@ -47,9 +45,8 @@ import org.apache.commons.compress.compressors.gzip.ExtraField.SubField;
  * +---+---+---+---+==================================+
  * </pre>
  *
- * This class does not expose the internal subfields list to prevent adding
- * subfields without total extra length validation. The class is iterable, but
- * this iterator is immutable.
+ * This class does not expose the internal subfields list to prevent adding subfields without total extra length validation. The class is iterable, but this
+ * iterator is immutable.
  *
  * @see <a href="https://datatracker.ietf.org/doc/html/rfc1952">RFC 1952 GZIP File Format Specification</a>
  * @since 1.28.0
@@ -57,8 +54,8 @@ import org.apache.commons.compress.compressors.gzip.ExtraField.SubField;
 public class ExtraField implements Iterable<SubField> {
 
     /**
-     * If the {@code FLG.FEXTRA} bit is set, an "extra field" is present in the header, with total length XLEN bytes.
-     * It consists of a series of subfields, each of the form:
+     * If the {@code FLG.FEXTRA} bit is set, an "extra field" is present in the header, with total length XLEN bytes. It consists of a series of subfields, each
+     * of the form:
      *
      * <pre>
      * +---+---+---+---+==================================+
@@ -68,6 +65,7 @@ public class ExtraField implements Iterable<SubField> {
      * <p>
      * The reserved IDs are:
      * </p>
+     *
      * <pre>
      * SI1         SI2         Data
      * ----------  ----------  ----
@@ -155,11 +153,11 @@ public class ExtraField implements Iterable<SubField> {
     /**
      * Append a subfield by a 2-chars ISO-8859-1 string. The char at index 0 and 1 are respectiovely si1 and si2 (subfield id 1 and 2).
      *
-     * @param id The subfield ID.
+     * @param id      The subfield ID.
      * @param payload The subfield payload.
      * @return this instance.
-     * @throws NullPointerException if {@code id} is {@code null}.
-     * @throws NullPointerException if {@code payload} is {@code null}.
+     * @throws NullPointerException     if {@code id} is {@code null}.
+     * @throws NullPointerException     if {@code payload} is {@code null}.
      * @throws IllegalArgumentException if the subfield is not 2 characters or the payload is null
      * @throws IOException              if appending this subfield would exceed the max size 65535 of the extra header.
      */
@@ -193,18 +191,18 @@ public class ExtraField implements Iterable<SubField> {
     }
 
     /**
-     * Finds the first subfield that matched the id if found, null otherwise
+     * Finds the first subfield that matched the id if found, null otherwise.
      *
-     * @return the 1st SubField that matched or null.
+     * @param id The ID to find.
+     * @return The first SubField that matched or null.
      */
-    public SubField findFirstSubField(String subfieldId) {
-        return subFields.stream().filter(f -> f.getId().equals(subfieldId)).findFirst().orElse(null);
+    public SubField findFirstSubField(final String id) {
+        return subFields.stream().filter(f -> f.getId().equals(id)).findFirst().orElse(null);
     }
 
     /**
-     * Calculate the size in bytes of the encoded extra field. This does not include
-     * its own 16 bits size when embeded in the gzip header. For N sub fields, the
-     * total is all subfields payloads bytes + 4N.
+     * Gets the size in bytes of the encoded extra field. This does not include its own 16 bits size when embeded in the gzip header. For N sub fields,
+     * the total is all subfields payloads bytes + 4N.
      *
      * @return the bytes count of this extra payload when encoded.
      */
@@ -212,28 +210,19 @@ public class ExtraField implements Iterable<SubField> {
         return totalSize;
     }
 
-    /**
-     * Return the count of subfields currently in in this extra field.
-     *
-     * @return the count of subfields contained in this instance.
-     */
-    public int getSize() {
-        return subFields.size();
-    }
-
     /**
      * Gets the subfield at the given index.
      *
-     * @param index index of the element to return
-     * @return the subfield at the specified position in this list
-     * @throws IndexOutOfBoundsException if the index is out of range ({@code index &lt; 0 || index &gt;= size()})
+     * @param index index of the element to return.
+     * @return the subfield at the specified position in this list.
+     * @throws IndexOutOfBoundsException if the index is out of range ({@code index &lt; 0 || index &gt;= size()}).
      */
-    public SubField getSubFieldAt(final int index) {
+    public SubField getSubField(final int index) {
         return subFields.get(index);
     }
 
     /**
-     * Test is this extra field has no subfields.
+     * Tests is this extra field has no subfields.
      *
      * @return true if there are no subfields, false otherwise.
      */
@@ -242,16 +231,24 @@ public class ExtraField implements Iterable<SubField> {
     }
 
     /**
-     * Returns an immutable iterator over the SubField elements in this extra field
-     * in the order they were added.
+     * Returns an unmodifiable iterator over the elements in the SubField list in proper sequence.
      *
-     * @return an immutable naturally ordered iterator over the SubField elements.
+     * @return an unmodifiable naturally ordered iterator over the SubField elements.
      */
     @Override
     public Iterator<SubField> iterator() {
         return Collections.unmodifiableList(subFields).iterator();
     }
 
+    /**
+     * Gets the count of subfields currently in in this extra field.
+     *
+     * @return the count of subfields contained in this instance.
+     */
+    public int size() {
+        return subFields.size();
+    }
+
     byte[] toByteArray() {
         if (subFields.isEmpty()) {
             return ZERO_BYTES;
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
index 370148b98..e6069a79d 100644
--- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
@@ -147,13 +147,13 @@ public class GzipCompressorOutputStreamTest {
         try (GzipCompressorInputStream gis = new GzipCompressorInputStream(Files.newInputStream(targetFile))) {
             final ExtraField extra2 = gis.getMetaData().getExtraField();
             assertEquals(subFieldCount == 0, extra2.isEmpty());
-            assertEquals(subFieldCount, extra2.getSize());
+            assertEquals(subFieldCount, extra2.size());
             assertEquals(4 * subFieldCount + subFieldCount * payloadSize, extra2.getEncodedSize());
             ArrayList<SubField> listCopy = new ArrayList<>();
             extra2.forEach(listCopy::add);
             assertEquals(subFieldCount, listCopy.size());
             for (int i = 0; i < subFieldCount; i++) {
-                final SubField sf = extra2.getSubFieldAt(i);
+                final SubField sf = extra2.getSubField(i);
                 assertSame(sf, listCopy.get(i));
                 assertSame(sf, extra2.findFirstSubField("z" + i));
                 assertEquals("z" + i, sf.getId()); // id was saved/loaded correctly
@@ -172,7 +172,7 @@ public class GzipCompressorOutputStreamTest {
         extra.forEach(e -> {
             fail("Not emprt");
         });
-        assertThrows(IndexOutOfBoundsException.class, () -> extra.getSubFieldAt(0));
+        assertThrows(IndexOutOfBoundsException.class, () -> extra.getSubField(0));
     }
 
     private void testFileName(final String expected, final String sourceFile) throws IOException {


(commons-compress) 01/02: Sort members

Posted by gg...@apache.org.
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-compress.git

commit 9c41f4cd10c57f8f2e1df8f6b844938edf4d19ff
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Nov 18 09:01:41 2024 -0500

    Sort members
---
 .../compress/compressors/gzip/ExtraField.java      | 92 +++++++++++-----------
 1 file changed, 46 insertions(+), 46 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
index 2de74e9fb..5e8e80b20 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
@@ -184,6 +184,43 @@ public class ExtraField implements Iterable<SubField> {
         return this;
     }
 
+    /**
+     * Removes all subfields from this instance.
+     */
+    public void clear() {
+        subFields.clear();
+        totalSize = 0;
+    }
+
+    /**
+     * Finds the first subfield that matched the id if found, null otherwise
+     *
+     * @return the 1st SubField that matched or null.
+     */
+    public SubField findFirstSubField(String subfieldId) {
+        return subFields.stream().filter(f -> f.getId().equals(subfieldId)).findFirst().orElse(null);
+    }
+
+    /**
+     * Calculate the size in bytes of the encoded extra field. This does not include
+     * its own 16 bits size when embeded in the gzip header. For N sub fields, the
+     * total is all subfields payloads bytes + 4N.
+     *
+     * @return the bytes count of this extra payload when encoded.
+     */
+    public int getEncodedSize() {
+        return totalSize;
+    }
+
+    /**
+     * Return the count of subfields currently in in this extra field.
+     *
+     * @return the count of subfields contained in this instance.
+     */
+    public int getSize() {
+        return subFields.size();
+    }
+
     /**
      * Gets the subfield at the given index.
      *
@@ -195,6 +232,15 @@ public class ExtraField implements Iterable<SubField> {
         return subFields.get(index);
     }
 
+    /**
+     * Test is this extra field has no subfields.
+     *
+     * @return true if there are no subfields, false otherwise.
+     */
+    public boolean isEmpty() {
+        return subFields.isEmpty();
+    }
+
     /**
      * Returns an immutable iterator over the SubField elements in this extra field
      * in the order they were added.
@@ -223,50 +269,4 @@ public class ExtraField implements Iterable<SubField> {
         return ba;
     }
 
-    /**
-     * Test is this extra field has no subfields.
-     *
-     * @return true if there are no subfields, false otherwise.
-     */
-    public boolean isEmpty() {
-        return subFields.isEmpty();
-    }
-
-    /**
-     * Removes all subfields from this instance.
-     */
-    public void clear() {
-        subFields.clear();
-        totalSize = 0;
-    }
-
-    /**
-     * Calculate the size in bytes of the encoded extra field. This does not include
-     * its own 16 bits size when embeded in the gzip header. For N sub fields, the
-     * total is all subfields payloads bytes + 4N.
-     *
-     * @return the bytes count of this extra payload when encoded.
-     */
-    public int getEncodedSize() {
-        return totalSize;
-    }
-
-    /**
-     * Return the count of subfields currently in in this extra field.
-     *
-     * @return the count of subfields contained in this instance.
-     */
-    public int getSize() {
-        return subFields.size();
-    }
-
-    /**
-     * Finds the first subfield that matched the id if found, null otherwise
-     *
-     * @return the 1st SubField that matched or null.
-     */
-    public SubField findFirstSubField(String subfieldId) {
-        return subFields.stream().filter(f -> f.getId().equals(subfieldId)).findFirst().orElse(null);
-    }
-
 }