java.lang.Object
org.bzdev.util.CopyUtilities
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Copy a file to a file.static void
copyFile
(File iFile, OutputStream os) Copy a file to an output stream.static void
copyResource
(Class clasz, String resource, Appendable a, Charset charset) Copy a resource in the same package as the given class and containing character data to an Appendable.static void
copyResource
(String resource, Appendable a, Charset charset) Copy a resource containing character data to an Appendable The resource is accessed via the system class loader.static void
copyResourceToFile
(Class clasz, String resource, File outputFile) Copy a resource in the same package as the given class to a file.static void
copyResourceToFile
(String resource, File outputFile) Copy a resource to a file.static void
copyResourceToStream
(Class clasz, String resource, OutputStream target) Copy a resource in the same package as the given class to an output stream.static void
copyResourceToStream
(String resource, OutputStream target) Copy a resource to an output stream.static void
copyResourceToZipStream
(String resource, String zipEntryName, ZipOutputStream zos, boolean stored) Copy a resource to a zip output stream.static void
copyStream
(InputStream is, OutputStream os) Deprecated.static void
copyStream
(InputStream is, Appendable a, Charset charset) Copy an input stream to an Appendable.static void
Copy a network resource to a filestatic void
copyURL
(URL url, OutputStream os) Copy a network resource to an output streamstatic void
copyURL
(URL url, Appendable a, Charset charset) Copy a resource referenced by a URL and containing character data to an Appendable.static boolean[]
toBooleanArray
(ArrayList<Boolean> list) Copy an array list to an array of boolean.static boolean[]
toBooleanArray
(ArrayList<Boolean> list, int start, int end) Copy an array list to an array of boolean.static byte[]
toByteArray
(ArrayList<Byte> list) Copy an array list to an array of byte.static byte[]
toByteArray
(ArrayList<Byte> list, int start, int end) Copy an array list to an array of byte.static char[]
toCharArray
(ArrayList<Character> list) Copy an array list to an array of char.static char[]
toCharArray
(ArrayList<Character> list, int start, int end) Copy an array list to an array of char specifying a range.static double[]
toDoubleArray
(ArrayList<Double> list) Copy an array list to an array of double.static double[]
toDoubleArray
(ArrayList<Double> list, int start, int end) Copy an array list to an array of double with a specified range.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.static float[]
toFloatArray
(ArrayList<Float> list, int start, int end) Copy an array list to an array of float with a specified range.static int[]
toIntArray
(ArrayList<Integer> list) Copy an array list to an array of int.static int[]
toIntArray
(ArrayList<Integer> list, int start, int end) Copy an array list to an array of int.static long[]
toLongArray
(ArrayList<Long> list) Copy an array list to an array of long.static long[]
toLongArray
(ArrayList<Long> list, int start, int end) Copy an array list to an array of long.static short[]
toShortArray
(ArrayList<Short> list) Copy an array list to an array of short.static short[]
toShortArray
(ArrayList<Short> list, int start, int end) Copy an array list to an array of short.
-
Constructor Details
-
CopyUtilities
public CopyUtilities()
-
-
Method Details
-
copyResourceToFile
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 resourceoutputFile
- the output file- Throws:
IOException
- an IO error occurred
-
copyResourceToStream
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 resourcetarget
- 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 resourcea
- is the object used to store the copycharset
- the character encoding used by the resource- Throws:
IOException
- an IO error occurredNullPointerException
- 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 bethis.getClass()
or justgetClass()
. 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 resourceresource
- the resourceoutputFile
- 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 bethis.getClass()
or justgetClass()
. 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 resourceresource
- the resourcetarget
- 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 bethis.getClass()
or justgetClass()
. 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 resourceresource
- the resourcea
- is the object used to store the copycharset
- the character encoding used by the resource- Throws:
IOException
- an IO error occurredNullPointerException
- an argument was null
-
copyFile
Copy a file to a file.- Parameters:
iFile
- the input fileoFile
- the output file- Throws:
IOException
- an IO error occurred
-
copyFile
Copy a file to an output stream.- Parameters:
iFile
- the input fileos
- the output stream- Throws:
IOException
- an IO error occurred
-
copyURL
Copy a network resource to an output stream- Parameters:
url
- the resource's URLos
- the output stream- Throws:
IOException
- an IO error occurred
-
copyURL
Copy a network resource to a file- Parameters:
url
- the resource's URLoFile
- 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 URLa
- is the object used to store the copycharset
- the character encoding used by the resource- Throws:
IOException
- an IO error occurredNullPointerException
- an argument was null
-
copyStream
Deprecated.InputStream.transferTo(OutputStream)
was introduced in Java 9, and with access to the internal state of the input stream, transferTo should be more efficientCopy an input stream to an output stream. Generally if an error occurs the streams should be closed. Please useInputStream.transferTo(OutputStream)
instead.- Parameters:
is
- the input streamos
- the output stream- Throws:
IOException
- an IO error occurred
-
copyStream
Copy an input stream to an Appendable. The input stream is copied from its current position to its end.- Parameters:
is
- the input streama
- is the object used to store the copycharset
- 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 resourcezipEntryName
- the name of the zip-file entry under which to store the resourcezos
- the zip output streamstored
- true if stored as is; false if compressed- Throws:
IOException
- an IO error occurred
-
toDoubleArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toFloatArray
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
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 copystart
- the starting offsetend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toIntArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toLongArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toShortArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toByteArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toCharArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
toBooleanArray
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
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 copystart
- the starting index to copyend
- the index just past the last element to copy- Returns:
- an array containing the elements of the list
-
InputStream.transferTo(OutputStream)
was introduced in Java 9, and with access to the internal state of the input stream, transferTo should be more efficient