/*
 FileCopy.java - utility class to copy files and directories
 
 Copyright  (C) 2005 Hugues Johnson
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 the GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import com.huguesjohnson.PathResolver;

/**
 * FileCopy - utility class to copy files and directories
 * @author Hugues Johnson
 */
public abstract class FileCopy{
	/**
	 * Copy a file.
	 * @param sourceFile The source file to copy.
	 * @param destinationFile The destination for the copy.
	 * @return True if the operation is successful.
	 */
	public static final boolean copyFile(String sourceFile,String destinationFile){
		boolean success=true;
		FileChannel inChannel=null;
		FileChannel outChannel=null;
		try{
			inChannel=new FileInputStream(sourceFile).getChannel();
			outChannel=new FileOutputStream(destinationFile).getChannel();
			long fileSize=inChannel.size();
			MappedByteBuffer mappedByteBuffer=inChannel.map(MapMode.READ_ONLY,0,fileSize);
			outChannel.write(mappedByteBuffer);
			outChannel.close();
			inChannel.close();
		} catch(IOException iox){
			iox.printStackTrace();
			success=false;
		}
		return(success);
	}

	/**
	 * Copy a directory.
	 * @param sourceDirectory The source directory to copy.
	 * @param destinationDirectory The destination for the copy.
	 * @param recursive Set to true to copy sub-directories.
	 * @return True if the operation is successful.
	 */
	public static final boolean copyDirectory(String sourceDirectory,String destinationDirectory,boolean recursive){
		boolean success=true;
		File source=new File(sourceDirectory);
		if(source.isDirectory()){
			File destination=new File(destinationDirectory);
			if(!destination.exists()){
				success=destination.mkdirs();
			}
			if(success){
				File[] children=source.listFiles();
				if(children!=null){
					int fileCount=children.length;
					int index=0;
					while((success)&&(index<fileCount)){
						String absoluteSource=children[index].getAbsolutePath();
						String relativePath=PathResolver.getRelativePath(sourceDirectory,absoluteSource);
						String absoluteDestination=PathResolver.getAbsolutePath(destinationDirectory,relativePath);
						if(children[index].isDirectory()){
							if(recursive){
								if(!absoluteSource.endsWith(File.separator)){
									absoluteSource=absoluteSource.concat(File.separator);
								}
								if(!absoluteDestination.endsWith(File.separator)){
									absoluteDestination=absoluteDestination.concat(File.separator);
								}
								success=copyDirectory(absoluteSource,absoluteDestination,true);
							}
						} else{
							success=copyFile(absoluteSource,absoluteDestination);
						}
						index++;
					}
				}
			}
		} else{
			success=false;
		}
		return(success);
	}
}