/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * File for moving and copying files. Uses the new Java IO classes so it should be
 * fast. 
 * @author Jon Maber
 * @author Colin Tatham
 * @author buckett
 */
public class FileMover
{	
    
    /**
     * Copies a file or folder to the folder specified.
     * @param source The file/folder to copy.
     * @param destination The destination.
     * @exception IOException Thrown if there is any problem copying the file/folder.
     */
    public static void copyFile( File source, File destination )
    throws IOException
    {
        try
        {
            cloneFile( source, destination );
        }
        catch (IOException e)
        {
            throw new IOException("Cannot copy file: "+ e.getMessage());
        }
    }
    
    /**
     * Moves a file or folder to the folder specified.
     * @param source The file/folder to move.
     * @param destination The destination.
     * @exception IOException Thrown if there is any problem moving the file/folder.
     */
    public static void moveFile( File source, File destination )
    throws IOException
    {
        try
        {
            cloneFile( source, destination );
        }
        catch (IOException e)
        {
            throw new IOException("Cannot move file: "+ e.getMessage());
        }
        source.delete();
    }
    
    /**
     * Low Level method that does the checks and copies the file.
     */
    private static void cloneFile(File source, File destination) throws IOException
    {
        if ( destination.exists() )
            throw new IOException( source.getAbsolutePath() + " because " + destination.getAbsolutePath() + " already exists." );
        if ( !source.exists() )
            throw new IOException( source.getAbsolutePath() + " because it doesn't exist." );
        if ( !source.isFile() )
            throw new IOException(source.getAbsolutePath() + " because it isn't a file." );
        
        FileChannel sourceChannel = null, destinationChannel = null;
        try
        {
            sourceChannel = new FileInputStream(source).getChannel();
            destinationChannel = new FileOutputStream(destination).getChannel();
            destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        }
        finally
        {
            if (sourceChannel != null)
                sourceChannel.close();
            if (destinationChannel != null)
                destinationChannel.close();
        }
    }
    
}
