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/04/27 17:39:01 UTC

(commons-compress) branch master updated (3d74a3af4 -> ea32bf9c9)

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 3d74a3af4 Fix typo in comment
     new f14962c26 Refactor duplicate code
     new 932a012e3 Whitespace
     new ea32bf9c9 Replace vague assertion message with JUnit message

The 3 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:
 .../archivers/ar/ArArchiveInputStream.java         | 27 +++++-----
 .../archivers/ar/ArArchiveOutputStream.java        | 59 ++++++++--------------
 .../archivers/ar/ArArchiveOutputStreamTest.java    |  2 +-
 3 files changed, 34 insertions(+), 54 deletions(-)


(commons-compress) 03/03: Replace vague assertion message with JUnit message

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 ea32bf9c9b527e1895662ea8bfb25e99333f3383
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 27 13:38:57 2024 -0400

    Replace vague assertion message with JUnit message
---
 .../apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java
index 20f05503f..4759df672 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStreamTest.java
@@ -37,7 +37,7 @@ public class ArArchiveOutputStreamTest extends AbstractTest {
     public void testLongFileNamesCauseExceptionByDefault() throws IOException {
         try (ArArchiveOutputStream os = new ArArchiveOutputStream(new ByteArrayOutputStream())) {
             final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 0);
-            final IOException ex = assertThrows(IOException.class, () -> os.putArchiveEntry(ae), "Expected an exception");
+            final IOException ex = assertThrows(IOException.class, () -> os.putArchiveEntry(ae));
             assertTrue(ex.getMessage().startsWith("File name too long"));
         }
     }


(commons-compress) 01/03: Refactor duplicate code

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 f14962c26032616190743a854ec7730748dc3734
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 27 10:19:39 2024 -0400

    Refactor duplicate code
    
    - Normalize parameter names
    - Better local variable name
    - Whitespace
---
 .../archivers/ar/ArArchiveOutputStream.java        | 59 ++++++++--------------
 1 file changed, 21 insertions(+), 38 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
index fe0735e5f..fd3052f0c 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
@@ -68,6 +68,13 @@ public class ArArchiveOutputStream extends ArchiveOutputStream<ArArchiveEntry> {
         }
     }
 
+    private String checkLength(final String value, final int max, final String name) throws IOException {
+        if (value.length() > max) {
+            throw new IOException(name + " too long");
+        }
+        return value;
+    }
+
     /**
      * Calls finish if necessary, and then closes the OutputStream
      */
@@ -112,14 +119,14 @@ public class ArArchiveOutputStream extends ArchiveOutputStream<ArArchiveEntry> {
         return new ArArchiveEntry(inputPath, entryName, options);
     }
 
-    private long fill(final long pOffset, final long pNewOffset, final char pFill) throws IOException {
-        final long diff = pNewOffset - pOffset;
+    private long fill(final long offset, final long newOffset, final char fill) throws IOException {
+        final long diff = newOffset - offset;
         if (diff > 0) {
             for (int i = 0; i < diff; i++) {
-                write(pFill);
+                write(fill);
             }
         }
-        return pNewOffset;
+        return newOffset;
     }
 
     @Override
@@ -127,9 +134,7 @@ public class ArArchiveOutputStream extends ArchiveOutputStream<ArArchiveEntry> {
         if (haveUnclosedEntry) {
             throw new IOException("This archive contains unclosed entries.");
         }
-        if (finished) {
-            throw new IOException("This archive has already been finished");
-        }
+        checkFinished();
         finished = true;
     }
 
@@ -142,7 +147,6 @@ public class ArArchiveOutputStream extends ArchiveOutputStream<ArArchiveEntry> {
             if (prevEntry.getLength() != entryOffset) {
                 throw new IOException("Length does not match entry (" + prevEntry.getLength() + " != " + entryOffset);
             }
-
             if (haveUnclosedEntry) {
                 closeArchiveEntry();
             }
@@ -178,63 +182,42 @@ public class ArArchiveOutputStream extends ArchiveOutputStream<ArArchiveEntry> {
     }
 
     private void writeArchiveHeader() throws IOException {
-        final byte[] header = ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
-        out.write(header);
+        out.write(ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER));
     }
 
     private void writeEntryHeader(final ArArchiveEntry entry) throws IOException {
         long offset = 0;
-        boolean mustAppendName = false;
+        boolean appendName = false;
         final String n = entry.getName();
         final int nLength = n.length();
         if (LONGFILE_ERROR == longFileMode && nLength > 16) {
             throw new IOException("File name too long, > 16 chars: " + n);
         }
         if (LONGFILE_BSD == longFileMode && (nLength > 16 || n.contains(" "))) {
-            mustAppendName = true;
+            appendName = true;
             offset += write(ArArchiveInputStream.BSD_LONGNAME_PREFIX + nLength);
         } else {
             offset += write(n);
         }
         // Last modified
         offset = fill(offset, 16, SPACE);
-        final String m = String.valueOf(entry.getLastModified());
-        if (m.length() > 12) {
-            throw new IOException("Last modified too long");
-        }
-        offset += write(m);
+        offset += write(checkLength(String.valueOf(entry.getLastModified()), 12, "Last modified"));
         // User ID
         offset = fill(offset, 28, SPACE);
-        final String u = String.valueOf(entry.getUserId());
-        if (u.length() > 6) {
-            throw new IOException("User id too long");
-        }
-        offset += write(u);
+        offset += write(checkLength(String.valueOf(entry.getUserId()), 6, "User ID"));
         // Group ID
         offset = fill(offset, 34, SPACE);
-        final String g = String.valueOf(entry.getGroupId());
-        if (g.length() > 6) {
-            throw new IOException("Group id too long");
-        }
-        offset += write(g);
+        offset += write(checkLength(String.valueOf(entry.getGroupId()), 6, "Group ID"));
         // Mode
         offset = fill(offset, 40, SPACE);
-        final String fm = String.valueOf(Integer.toString(entry.getMode(), 8));
-        if (fm.length() > 8) {
-            throw new IOException("Filemode too long");
-        }
-        offset += write(fm);
+        offset += write(checkLength(String.valueOf(Integer.toString(entry.getMode(), 8)), 8, "File mode"));
         // Length
         offset = fill(offset, 48, SPACE);
-        final String s = String.valueOf(entry.getLength() + (mustAppendName ? nLength : 0));
-        if (s.length() > 10) {
-            throw new IOException("Size too long");
-        }
-        offset += write(s);
+        offset += write(checkLength(String.valueOf(entry.getLength() + (appendName ? nLength : 0)), 10, "Size"));
         // Trailer
         offset = fill(offset, 58, SPACE);
         offset += write(ArArchiveEntry.TRAILER);
-        if (mustAppendName) {
+        if (appendName) {
             offset += write(n);
         }
 


(commons-compress) 02/03: Whitespace

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 932a012e3714080cf6ce0ca7a708087156e08e46
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 27 10:19:57 2024 -0400

    Whitespace
---
 .../archivers/ar/ArArchiveInputStream.java         | 27 ++++++++++------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
index 2ccae9bf9..d4607848a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
@@ -113,9 +113,17 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
      */
     public static boolean matches(final byte[] signature, final int length) {
         // 3c21 7261 6863 0a3e
-
-        return length >= 8 && signature[0] == 0x21 && signature[1] == 0x3c && signature[2] == 0x61 && signature[3] == 0x72 && signature[4] == 0x63
-                && signature[5] == 0x68 && signature[6] == 0x3e && signature[7] == 0x0a;
+        // @formatter:off
+        return length >= 8 &&
+                signature[0] == 0x21 &&
+                signature[1] == 0x3c &&
+                signature[2] == 0x61 &&
+                signature[3] == 0x72 &&
+                signature[4] == 0x63 &&
+                signature[5] == 0x68 &&
+                signature[6] == 0x3e &&
+                signature[7] == 0x0a;
+        // @formatter:on
     }
 
     private long offset;
@@ -223,7 +231,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
                 if (namebuffer[i - 1] == '/') {
                     i--; // drop trailing /
                 }
-
                 // Check there is a something to return, otherwise break out of the loop
                 if (i - offset > 0) {
                     return ArchiveUtils.toAsciiString(namebuffer, offset, i - offset);
@@ -249,7 +256,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
             trackReadBytes(skipped);
             currentEntry = null;
         }
-
         if (offset == 0) {
             final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
             final byte[] realized = IOUtils.readRange(in, expected.length);
@@ -262,7 +268,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
                 throw new IOException("Invalid header " + ArchiveUtils.toAsciiString(realized));
             }
         }
-
         if (offset % 2 != 0) {
             if (in.read() < 0) {
                 // hit eof
@@ -270,7 +275,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
             }
             trackReadBytes(1);
         }
-
         {
             final int read = IOUtils.readFully(in, metaData);
             trackReadBytes(read);
@@ -281,7 +285,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
                 throw new IOException("Truncated ar archive");
             }
         }
-
         {
             final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.TRAILER);
             final byte[] realized = IOUtils.readRange(in, expected.length);
@@ -296,16 +299,13 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
         }
 
         entryOffset = offset;
-
-//        GNU ar uses a '/' to mark the end of the file name; this allows for the use of spaces without the use of an extended file name.
-
+        // GNU ar uses a '/' to mark the end of the file name; this allows for the use of spaces without the use of an extended file name.
         // entry name is stored as ASCII string
         String temp = ArchiveUtils.toAsciiString(metaData, NAME_OFFSET, NAME_LEN).trim();
         if (isGNUStringTable(temp)) { // GNU extended file names entry
             currentEntry = readGNUStringTable(metaData, LENGTH_OFFSET, LENGTH_LEN);
             return getNextArEntry();
         }
-
         long len;
         try {
             len = asLong(metaData, LENGTH_OFFSET, LENGTH_LEN);
@@ -326,11 +326,9 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
             len -= nameLen;
             entryOffset += nameLen;
         }
-
         if (len < 0) {
             throw new IOException("broken archive, entry with negative size");
         }
-
         try {
             currentEntry = new ArArchiveEntry(temp, len, asInt(metaData, USER_ID_OFFSET, USER_ID_LEN, true),
                     asInt(metaData, GROUP_ID_OFFSET, GROUP_ID_LEN, true), asInt(metaData, FILE_MODE_OFFSET, FILE_MODE_LEN, 8),
@@ -395,7 +393,6 @@ public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
         } catch (final NumberFormatException ex) {
             throw new IOException("Broken archive, unable to parse GNU string table length field as a number", ex);
         }
-
         namebuffer = IOUtils.readRange(in, bufflen);
         final int read = namebuffer.length;
         trackReadBytes(read);