Class CopyUtilities

java.lang.Object
org.bzdev.util.CopyUtilities

public class CopyUtilities extends Object
Utility class for various copy operations: file to file, resource to file, object referenced by a URL to a file, resource to output stream, file to output stream, URL to output stream, input stream to output stream, resource to a ZIP stream.

This class will also copy array lists whose type parameters are Integer, Long, Short, Character, Byte, Float, and Double to newly allocated arrays with the corresponding primitive types.

All the methods are static.

  • Constructor Details

    • CopyUtilities

      public CopyUtilities()
  • Method Details

    • copyResourceToFile

      public static void copyResourceToFile(String resource, File outputFile) throws IOException
      Copy a resource to a file. The resource is accessed via the system class loader. This can be problematic in Java modules unless the module is an open module.
      Parameters:
      resource - the resource
      outputFile - the output file
      Throws:
      IOException - an IO error occurred
    • copyResourceToStream

      public static void copyResourceToStream(String resource, OutputStream target) throws IOException
      Copy a resource to an output stream. The resource is accessed via the system class loader. This can be problematic with Java modules unless the module containing the resource is an open module.
      Parameters:
      resource - the resource
      target - the output stream
      Throws:
      IOException - an IO error occurred
    • copyResource

      public static void copyResource(String resource, Appendable a, Charset charset) throws IOException, NullPointerException
      Copy a resource containing character data to an Appendable The resource is accessed via the system class loader. This can be problematic with Java modules unless the module containing the resource is an open module.
      Parameters:
      resource - the resource
      a - is the object used to store the copy
      charset - the character encoding used by the resource
      Throws:
      IOException - an IO error occurred
      NullPointerException - an argument was null
    • copyResourceToFile

      public static void copyResourceToFile(Class clasz, String resource, File outputFile) throws IOException
      Copy a resource in the same package as the given class to a file. The resource is accessed via the system class loader. Typically, the first argument will be this.getClass() or just getClass(). The first argument is needed in most cases due to the restrictions modules place on the visibility of resources.
      Parameters:
      clasz - a class in the same package as the resource
      resource - the resource
      outputFile - the output file
      Throws:
      IOException - an IO error occurred
    • copyResourceToStream

      public static void copyResourceToStream(Class clasz, String resource, OutputStream target) throws IOException
      Copy a resource in the same package as the given class to an output stream. The resource is accessed via the system class loader. Typically, the first argument will be this.getClass() or just getClass(). The first argument is needed in most cases due to the restrictions modules place on the visibility of resources.
      Parameters:
      clasz - a class in the same package as the resource
      resource - the resource
      target - the output stream
      Throws:
      IOException - an IO error occurred
    • copyResource

      public static void copyResource(Class clasz, String resource, Appendable a, Charset charset) throws IOException, NullPointerException
      Copy a resource in the same package as the given class and containing character data to an Appendable. The resource is accessed via the system class loader. Typically, the first argument will be this.getClass() or just getClass(). The first argument is needed in most cases due to the restrictions modules place on the visibility of resources.
      Parameters:
      clasz - a class in the same package as the resource
      resource - the resource
      a - is the object used to store the copy
      charset - the character encoding used by the resource
      Throws:
      IOException - an IO error occurred
      NullPointerException - an argument was null
    • copyFile

      public static void copyFile(File iFile, File oFile) throws IOException
      Copy a file to a file.
      Parameters:
      iFile - the input file
      oFile - the output file
      Throws:
      IOException - an IO error occurred
    • copyFile

      public static void copyFile(File iFile, OutputStream os) throws IOException
      Copy a file to an output stream.
      Parameters:
      iFile - the input file
      os - the output stream
      Throws:
      IOException - an IO error occurred
    • copyURL

      public static void copyURL(URL url, OutputStream os) throws IOException
      Copy a network resource to an output stream
      Parameters:
      url - the resource's URL
      os - the output stream
      Throws:
      IOException - an IO error occurred
    • copyURL

      public static void copyURL(URL url, File oFile) throws IOException
      Copy a network resource to a file
      Parameters:
      url - the resource's URL
      oFile - the output file
      Throws:
      IOException - an IO error occurred
    • copyURL

      public static void copyURL(URL url, Appendable a, Charset charset) throws IOException, NullPointerException
      Copy a resource referenced by a URL and containing character data to an Appendable.
      Parameters:
      url - the resource's URL
      a - is the object used to store the copy
      charset - the character encoding used by the resource
      Throws:
      IOException - an IO error occurred
      NullPointerException - an argument was null
    • copyStream

      @Deprecated public static void copyStream(InputStream is, OutputStream os) throws IOException
      Deprecated.
      InputStream.transferTo(OutputStream) was introduced in Java 9, and with access to the internal state of the input stream, transferTo should be more efficient
      Copy an input stream to an output stream. Generally if an error occurs the streams should be closed. Please use InputStream.transferTo(OutputStream) instead.
      Parameters:
      is - the input stream
      os - the output stream
      Throws:
      IOException - an IO error occurred
    • copyStream

      public static void copyStream(InputStream is, Appendable a, Charset charset) throws IOException
      Copy an input stream to an Appendable. The input stream is copied from its current position to its end.
      Parameters:
      is - the input stream
      a - is the object used to store the copy
      charset - the character encoding used by the input stream.
      Throws:
      IOException - an IO error occurred.
    • copyResourceToZipStream

      public static void copyResourceToZipStream(String resource, String zipEntryName, ZipOutputStream zos, boolean stored) throws IOException
      Copy a resource to a zip output stream.
      Parameters:
      resource - the resource
      zipEntryName - the name of the zip-file entry under which to store the resource
      zos - the zip output stream
      stored - true if stored as is; false if compressed
      Throws:
      IOException - an IO error occurred
    • toDoubleArray

      public static double[] toDoubleArray(ArrayList<Double> list)
      Copy an array list to an array of double. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toDoubleArray

      public static double[] toDoubleArray(ArrayList<Double> list, int start, int end)
      Copy an array list to an array of double with a specified range. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toFloatArray

      public static float[] toFloatArray(ArrayList<Float> list)
      Copy an array list to an array of float.b One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toFloatArray

      public static float[] toFloatArray(ArrayList<Float> list, int start, int end)
      Copy an array list to an array of float with a specified range. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting offset
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toIntArray

      public static int[] toIntArray(ArrayList<Integer> list)
      Copy an array list to an array of int. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toIntArray

      public static int[] toIntArray(ArrayList<Integer> list, int start, int end)
      Copy an array list to an array of int. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toLongArray

      public static long[] toLongArray(ArrayList<Long> list)
      Copy an array list to an array of long. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toLongArray

      public static long[] toLongArray(ArrayList<Long> list, int start, int end)
      Copy an array list to an array of long. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toShortArray

      public static short[] toShortArray(ArrayList<Short> list)
      Copy an array list to an array of short. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toShortArray

      public static short[] toShortArray(ArrayList<Short> list, int start, int end)
      Copy an array list to an array of short. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toByteArray

      public static byte[] toByteArray(ArrayList<Byte> list)
      Copy an array list to an array of byte. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toByteArray

      public static byte[] toByteArray(ArrayList<Byte> list, int start, int end)
      Copy an array list to an array of byte. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toCharArray

      public static char[] toCharArray(ArrayList<Character> list)
      Copy an array list to an array of char. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toCharArray

      public static char[] toCharArray(ArrayList<Character> list, int start, int end)
      Copy an array list to an array of char specifying a range. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list
    • toBooleanArray

      public static boolean[] toBooleanArray(ArrayList<Boolean> list)
      Copy an array list to an array of boolean. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      Returns:
      an array containing the elements of the list
    • toBooleanArray

      public static boolean[] toBooleanArray(ArrayList<Boolean> list, int start, int end)
      Copy an array list to an array of boolean. One of ArrayList's toArray methods allows one to copy an ArrayList to an array, but the type of that array cannot be a primitive type. The ArrayList must not be modified while this method is executing.
      Parameters:
      list - the array list to copy
      start - the starting index to copy
      end - the index just past the last element to copy
      Returns:
      an array containing the elements of the list