Class ArrayUtil


  • public abstract class ArrayUtil
    extends java.lang.Object
    Utility class for constructing and reading and writing arrays from/to formatId streams and for performing other operations on arrays.
    Version:
    0.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte[] EMPTY_BYTE_ARRAY
      An instance of an empty byte array.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> asReadOnlyList​(T... array)
      Make the contents of an array available as a read-only list.
      static boolean[] copy​(boolean[] original)
      Copy a (possibly null) array of booleans
      static byte[] copy​(byte[] original)
      Copy a (possibly null) array of bytes
      static int[] copy​(int[] original)
      Copy a (possibly null) array of ints
      static long[] copy​(long[] original)
      Copy a (possibly null) array of longs
      static <T> T[] copy​(T[] original)
      Copy an array of objects; the original array could be null
      static int[][] copy2​(int[][] original)
      Copy a (possibly null) 2-dimensional array of ints
      static void readArrayItems​(java.io.ObjectInput in, java.lang.Object[] a)
      Read an array of objects out of a stream.
      static int readArrayLength​(java.io.ObjectInput in)
      Read the length of an array of objects in an object stream.
      static boolean[] readBooleanArray​(java.io.ObjectInput in)
      Read an array of integers from an ObjectInput.
      static int[] readIntArray​(java.io.ObjectInput in)
      Read an array of integers from an ObjectInput.
      static int[][] readInts​(java.io.ObjectInput in)  
      static long[] readLongArray​(java.io.ObjectInput in)
      Read an array of integers from an ObjectInput.
      static java.lang.Object[] readObjectArray​(java.io.ObjectInput in)
      Reads an array of objects from the stream.
      static java.lang.String[] readStringArray​(java.io.ObjectInput in)
      Read an array of strings from an ObjectInput.
      static java.lang.String toString​(int[] value)  
      static void writeArray​(java.io.ObjectOutput out, java.lang.Object[] a)
      Write an array of objects and length to an output stream.
      static void writeArrayItems​(java.io.ObjectOutput out, java.lang.Object[] a)
      Write an array of objects to an output stream.
      static void writeArrayLength​(java.io.ObjectOutput out, java.lang.Object[] a)
      Write the length of an array of objects to an output stream.
      static void writeBooleanArray​(java.io.ObjectOutput out, boolean[] a)
      Write an array of booleans to an ObjectOutput.
      static void writeIntArray​(java.io.ObjectOutput out, int[] a)
      Write an array of integers to an ObjectOutput.
      static void writeInts​(java.io.ObjectOutput out, int[][] val)  
      static void writeLongArray​(java.io.ObjectOutput out, long[] a)
      Write an array of longs to an ObjectOutput.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
        An instance of an empty byte array. Since empty arrays are immutable, this instance can safely be shared. Code that needs an empty byte array can use this static instance instead of allocating a new one.
    • Constructor Detail

      • ArrayUtil

        public ArrayUtil()
    • Method Detail

      • copy

        public static <T> T[] copy​(T[] original)
        Copy an array of objects; the original array could be null
      • copy

        public static boolean[] copy​(boolean[] original)
        Copy a (possibly null) array of booleans
      • copy

        public static byte[] copy​(byte[] original)
        Copy a (possibly null) array of bytes
      • copy

        public static int[] copy​(int[] original)
        Copy a (possibly null) array of ints
      • copy

        public static long[] copy​(long[] original)
        Copy a (possibly null) array of longs
      • copy2

        public static int[][] copy2​(int[][] original)
        Copy a (possibly null) 2-dimensional array of ints
      • asReadOnlyList

        @SafeVarargs
        public static <T> java.util.List<T> asReadOnlyList​(T... array)
        Make the contents of an array available as a read-only list. If the array is null, an empty list will be returned.
      • writeArrayLength

        public static void writeArrayLength​(java.io.ObjectOutput out,
                                            java.lang.Object[] a)
                                     throws java.io.IOException
        Write the length of an array of objects to an output stream. The length
        Parameters:
        out - ObjectOutput stream
        a - array of objects whose length should be written.
        Throws:
        java.io.IOException - The write caused an IOException.
      • writeArrayItems

        public static void writeArrayItems​(java.io.ObjectOutput out,
                                           java.lang.Object[] a)
                                    throws java.io.IOException
        Write an array of objects to an output stream.
        Parameters:
        out - Object output stream to write to.
        a - array of objects to write.
        Throws:
        java.io.IOException - The write caused an IOException.
      • writeArray

        public static void writeArray​(java.io.ObjectOutput out,
                                      java.lang.Object[] a)
                               throws java.io.IOException
        Write an array of objects and length to an output stream. Does equivalent of writeArrayLength() followed by writeArrayItems()
        Parameters:
        out - Object output stream to write to.
        a - array of objects to write.
        Throws:
        java.io.IOException - The write caused an IOException.
      • readArrayItems

        public static void readArrayItems​(java.io.ObjectInput in,
                                          java.lang.Object[] a)
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException
        Read an array of objects out of a stream.
        Parameters:
        in - Input stream
        a - array to read into
        Throws:
        java.io.IOException - The write caused an IOException.
        java.lang.ClassNotFoundException - The Class for an Object we are reading does not exist
      • readArrayLength

        public static int readArrayLength​(java.io.ObjectInput in)
                                   throws java.io.IOException
        Read the length of an array of objects in an object stream.
        Parameters:
        in - Input stream.
        Returns:
        length of the array of objects
        Throws:
        java.io.IOException - The write caused an IOException.
      • readObjectArray

        public static java.lang.Object[] readObjectArray​(java.io.ObjectInput in)
                                                  throws java.io.IOException,
                                                         java.lang.ClassNotFoundException
        Reads an array of objects from the stream.
        Parameters:
        in - Input stream
        Throws:
        java.io.IOException - The write caused an IOException.
        java.lang.ClassNotFoundException - The Class for an Object we are reading does not exist
      • writeIntArray

        public static void writeIntArray​(java.io.ObjectOutput out,
                                         int[] a)
                                  throws java.io.IOException
        Write an array of integers to an ObjectOutput. This writes the array in a format readIntArray understands.
        Parameters:
        out - the ObjectOutput.
        a - the array.
        Throws:
        java.io.IOException - The write caused an IOException.
      • readIntArray

        public static int[] readIntArray​(java.io.ObjectInput in)
                                  throws java.io.IOException
        Read an array of integers from an ObjectInput. This allocates the array.
        Parameters:
        in - the ObjectInput.
        Returns:
        the array of integers.
        Throws:
        java.io.IOException - The write caused an IOException.
      • writeInts

        public static void writeInts​(java.io.ObjectOutput out,
                                     int[][] val)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • readInts

        public static int[][] readInts​(java.io.ObjectInput in)
                                throws java.io.IOException,
                                       java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • toString

        public static java.lang.String toString​(int[] value)
      • writeLongArray

        public static void writeLongArray​(java.io.ObjectOutput out,
                                          long[] a)
                                   throws java.io.IOException
        Write an array of longs to an ObjectOutput. This writes the array in a format readLongArray understands.
        Parameters:
        out - the ObjectOutput.
        a - the array.
        Throws:
        java.io.IOException - The write caused an IOException.
      • readLongArray

        public static long[] readLongArray​(java.io.ObjectInput in)
                                    throws java.io.IOException
        Read an array of integers from an ObjectInput. This allocates the array.
        Parameters:
        in - the ObjectInput.
        Returns:
        the array of integers.
        Throws:
        java.io.IOException - The write caused an IOException.
      • readStringArray

        public static java.lang.String[] readStringArray​(java.io.ObjectInput in)
                                                  throws java.io.IOException,
                                                         java.lang.ClassNotFoundException
        Read an array of strings from an ObjectInput. This allocates the array.
        Parameters:
        in - the ObjectInput.
        Returns:
        the array of integers.
        Throws:
        java.io.IOException - The write caused an IOException.
        java.lang.ClassNotFoundException
      • writeBooleanArray

        public static void writeBooleanArray​(java.io.ObjectOutput out,
                                             boolean[] a)
                                      throws java.io.IOException
        Write an array of booleans to an ObjectOutput. This writes the array in a format readBooleanArray understands.
        Parameters:
        out - the ObjectOutput.
        a - the array.
        Throws:
        java.io.IOException - The write caused an IOException.
      • readBooleanArray

        public static boolean[] readBooleanArray​(java.io.ObjectInput in)
                                          throws java.io.IOException
        Read an array of integers from an ObjectInput. This allocates the array.
        Parameters:
        in - the ObjectInput.
        Returns:
        the array of integers.
        Throws:
        java.io.IOException - The write caused an IOException.