com.ccg.io
Class Copy

java.lang.Object
  extended by com.ccg.io.Copy

public class Copy
extends Object

Some static methods to aid in the copying of files.

This class provides some helper methods related to copying files (or streams of information).

Since:
1.0
Version:
$Revision: 1.6 $
Author:
$Author: pkb $

Constructor Summary
Copy()
           
 
Method Summary
static boolean fileToDir(File from, File dir)
          Copies the contents of one File to a directory.
static boolean fileToFile(File from, File to)
          Copies the contents of one File to another File.
static void fileToStream(File from, OutputStream to)
          Copies the contents of a File to a OutputStream.
static boolean smartFileToFile(File from, File to)
          A "smart" copy of one File to another File.
static void streamToFile(InputStream from, File to)
          Copies the contents of a InputStream to a File.
static void streamToStream(InputStream from, OutputStream to)
          Copies contents of a InputStream to a OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Copy

public Copy()
Method Detail

streamToStream

public static void streamToStream(InputStream from,
                                  OutputStream to)
                           throws IOException
Copies contents of a InputStream to a OutputStream.

This method uses buffered stream wrappers around the streams passed to copy data from one stream to the other. After it finishes the copy (even it an exception occurs) an attempt will be made to close BOTH streams.

Parameters:
from - The InputStream to copy data from (must not be null).
to - The OutputStream to copy data to (must not be null).
Throws:
IOException - If there is a problem copying the data or closing one of the files (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
streamToFile(java.io.InputStream, java.io.File), fileToStream(java.io.File, java.io.OutputStream), fileToFile(java.io.File, java.io.File)

streamToFile

public static void streamToFile(InputStream from,
                                File to)
                         throws IOException
Copies the contents of a InputStream to a File. This method converts the File object passed into a OutputStream and then invokes streamToStream(java.io.InputStream, java.io.OutputStream) to copy the data from the stream to the file.

Parameters:
from - The InputStream to copy data from (must not be null).
to - The File to copy data to (must not be null).
Throws:
IOException - If there is a problem copying the data or closing one of the files (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
streamToStream(java.io.InputStream, java.io.OutputStream)

fileToStream

public static void fileToStream(File from,
                                OutputStream to)
                         throws IOException
Copies the contents of a File to a OutputStream. This method converts the File object passed into a InputStream and then invokes streamToStream(java.io.InputStream, java.io.OutputStream) to copy the data from the file to the stream.

Parameters:
from - The File to copy the data from (must not be null).
to - The OutputStream to copy data fo (must not be null).
Throws:
IOException - If there is a problem copying the data or closing one of the files (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
streamToStream(java.io.InputStream, java.io.OutputStream)

fileToFile

public static boolean fileToFile(File from,
                                 File to)
                          throws IOException
Copies the contents of one File to another File.

This method copies one file to another.

In addtion, after the file is copied, we then attempt to set the last modification time on the resulting file to match the source.

If the two files you specify are "equal", nothing is done (we just return false indicating that we couldn't copy one file on top of the other).

This method takes a all or nothing approach (if it is unable to copy the entire file, then nothing is copied and a error is thrown).

Parameters:
from - The File to copy the data from (must not be null).
to - The File to copy data to (must not be null).
Returns:
true if we actually did a physical copy of the file, false if a copy wasn't required.
Throws:
IOException - If there is a problem copying the data or closing one of the files (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
streamToStream(java.io.InputStream, java.io.OutputStream), smartFileToFile(java.io.File, java.io.File)

fileToDir

public static boolean fileToDir(File from,
                                File dir)
                         throws IOException
Copies the contents of one File to a directory.

This method copies one source file to a specified destination directory (preserving the file name). The destination directory (and any parent directories) will be created if they do not yet exist.

In addtion, after the file is copied, we then attempt to set the last modification time on the resulting file to match the source.

If the resulting destination file turns out to be the same as the source file, nothing is done (we just return false indicating that we couldn't copy one file on top of the other).

Parameters:
from - The File to copy the data from (must not be null).
dir - The directory where we should copy the file to. It will be created if it doesn't yet exist - including parent directories.
Returns:
true if we actually did a physical copy of the file, false if a copy wasn't required.
Throws:
IOException - If there is a problem copying the data or closing one of the files or unable to use directory specified (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
fileToFile(java.io.File, java.io.File), smartFileToFile(java.io.File, java.io.File)

smartFileToFile

public static boolean smartFileToFile(File from,
                                      File to)
                               throws IOException
A "smart" copy of one File to another File.

This method copies the contents of a source File to destination File, but only if the modification time of the source file is more recent than the destination file or the file sizes differ between the two files.

In addtion, after the file is copied, we then attempt to set the last modification time on the resulting file to match the source.

If the two files you specify are "equal", nothing is done (we just return false indicating that we couldn't copy one file on top of the other). However, in this case we consider two files equal if they have the same modificaton time and size.

Parameters:
from - The File to copy the data from (must not be null).
to - The File to copy data to (must not be null).
Returns:
true if we actually did a physical copy of the file, false if the copy wasn't required.
Throws:
IOException - If there is a problem copying the data or closing one of the files (both files will be closed prior to throwing the exception).
Since:
1.0
See Also:
fileToFile(java.io.File, java.io.File)


Copyright 1998-1998-2006 null. All Rights Reserved.