Class ArchiveUtils


  • public class ArchiveUtils
    extends java.lang.Object
    Generic Archive utilities
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isArrayZero​(byte[] a, int size)
      Returns true if the first N bytes of an array are all zero
      static boolean isEqual​(byte[] buffer1, byte[] buffer2)
      Compare byte buffers
      static boolean isEqual​(byte[] buffer1, byte[] buffer2, boolean ignoreTrailingNulls)
      Compare byte buffers, optionally ignoring trailing nulls
      static boolean isEqual​(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
      Compare byte buffers
      static boolean isEqual​(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2, boolean ignoreTrailingNulls)
      Compare byte buffers, optionally ignoring trailing nulls
      static boolean isEqualWithNull​(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
      Compare byte buffers, ignoring trailing nulls
      static boolean matchAsciiBuffer​(java.lang.String expected, byte[] buffer)
      Check if buffer contents matches Ascii String.
      static boolean matchAsciiBuffer​(java.lang.String expected, byte[] buffer, int offset, int length)
      Check if buffer contents matches Ascii String.
      static java.lang.String sanitize​(java.lang.String s)
      Returns a "sanitized" version of the string given as arguments, where sanitized means non-printable characters have been replaced with a question mark and the outcome is not longer than 255 chars.
      static byte[] toAsciiBytes​(java.lang.String inputString)
      Convert a string to Ascii bytes.
      static java.lang.String toAsciiString​(byte[] inputBytes)
      Convert an input byte array to a String using the ASCII character set.
      static java.lang.String toAsciiString​(byte[] inputBytes, int offset, int length)
      Convert an input byte array to a String using the ASCII character set.
      static java.lang.String toString​(ArchiveEntry entry)
      Generates a string containing the name, isDirectory setting and size of an entry.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toString

        public static java.lang.String toString​(ArchiveEntry entry)
        Generates a string containing the name, isDirectory setting and size of an entry.

        For example:

         -    2000 main.c
         d     100 testfiles
         
        Parameters:
        entry - the entry
        Returns:
        the representation of the entry
      • matchAsciiBuffer

        public static boolean matchAsciiBuffer​(java.lang.String expected,
                                               byte[] buffer,
                                               int offset,
                                               int length)
        Check if buffer contents matches Ascii String.
        Parameters:
        expected - expected string
        buffer - the buffer
        offset - offset to read from
        length - length of the buffer
        Returns:
        true if buffer is the same as the expected string
      • matchAsciiBuffer

        public static boolean matchAsciiBuffer​(java.lang.String expected,
                                               byte[] buffer)
        Check if buffer contents matches Ascii String.
        Parameters:
        expected - the expected strin
        buffer - the buffer
        Returns:
        true if buffer is the same as the expected string
      • toAsciiBytes

        public static byte[] toAsciiBytes​(java.lang.String inputString)
        Convert a string to Ascii bytes. Used for comparing "magic" strings which need to be independent of the default Locale.
        Parameters:
        inputString - string to convert
        Returns:
        the bytes
      • toAsciiString

        public static java.lang.String toAsciiString​(byte[] inputBytes)
        Convert an input byte array to a String using the ASCII character set.
        Parameters:
        inputBytes - bytes to convert
        Returns:
        the bytes, interpreted as an Ascii string
      • toAsciiString

        public static java.lang.String toAsciiString​(byte[] inputBytes,
                                                     int offset,
                                                     int length)
        Convert an input byte array to a String using the ASCII character set.
        Parameters:
        inputBytes - input byte array
        offset - offset within array
        length - length of array
        Returns:
        the bytes, interpreted as an Ascii string
      • isEqual

        public static boolean isEqual​(byte[] buffer1,
                                      int offset1,
                                      int length1,
                                      byte[] buffer2,
                                      int offset2,
                                      int length2,
                                      boolean ignoreTrailingNulls)
        Compare byte buffers, optionally ignoring trailing nulls
        Parameters:
        buffer1 - first buffer
        offset1 - first offset
        length1 - first length
        buffer2 - second buffer
        offset2 - second offset
        length2 - second length
        ignoreTrailingNulls - whether to ignore trailing nulls
        Returns:
        true if buffer1 and buffer2 have same contents, having regard to trailing nulls
      • isEqual

        public static boolean isEqual​(byte[] buffer1,
                                      int offset1,
                                      int length1,
                                      byte[] buffer2,
                                      int offset2,
                                      int length2)
        Compare byte buffers
        Parameters:
        buffer1 - the first buffer
        offset1 - the first offset
        length1 - the first length
        buffer2 - the second buffer
        offset2 - the second offset
        length2 - the second length
        Returns:
        true if buffer1 and buffer2 have same contents
      • isEqual

        public static boolean isEqual​(byte[] buffer1,
                                      byte[] buffer2)
        Compare byte buffers
        Parameters:
        buffer1 - the first buffer
        buffer2 - the second buffer
        Returns:
        true if buffer1 and buffer2 have same contents
      • isEqual

        public static boolean isEqual​(byte[] buffer1,
                                      byte[] buffer2,
                                      boolean ignoreTrailingNulls)
        Compare byte buffers, optionally ignoring trailing nulls
        Parameters:
        buffer1 - the first buffer
        buffer2 - the second buffer
        ignoreTrailingNulls - whether to ignore trailing nulls
        Returns:
        true if buffer1 and buffer2 have same contents
      • isEqualWithNull

        public static boolean isEqualWithNull​(byte[] buffer1,
                                              int offset1,
                                              int length1,
                                              byte[] buffer2,
                                              int offset2,
                                              int length2)
        Compare byte buffers, ignoring trailing nulls
        Parameters:
        buffer1 - the first buffer
        offset1 - the first offset
        length1 - the first length
        buffer2 - the second buffer
        offset2 - the second offset
        length2 - the second length
        Returns:
        true if buffer1 and buffer2 have same contents, having regard to trailing nulls
      • isArrayZero

        public static boolean isArrayZero​(byte[] a,
                                          int size)
        Returns true if the first N bytes of an array are all zero
        Parameters:
        a - The array to check
        size - The number of characters to check (not the size of the array)
        Returns:
        true if the first N bytes are zero
      • sanitize

        public static java.lang.String sanitize​(java.lang.String s)
        Returns a "sanitized" version of the string given as arguments, where sanitized means non-printable characters have been replaced with a question mark and the outcome is not longer than 255 chars.

        This method is used to clean up file names when they are used in exception messages as they may end up in log files or as console output and may have been read from a corrupted input.

        Parameters:
        s - the string to sanitize
        Returns:
        a sanitized version of the argument
        Since:
        Compress 1.12