/*
 * Created on May 12, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package com.cosylab.util;

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;

/**
 * @author dvitas
 */
public class FileHelper {

	/**
	 * Method getTempFileName.
	 * This is a copy from C++ acsutilTempFile
	 *
	 * @param fileNameEnvVar
	 * @param fileName
	 * @return String
	 */
	static public String getTempFileName(String fileNameProperty, String fileName) {

		final String ACS_TEMP_DIR = "ACS.tmp";
		final String TEMP_DIR = "tmp";

		String propertyVal;
		String filePath = null;

		// check for <fileNameEnvVal> env. var
		if (fileNameProperty != null) {
			propertyVal = System.getProperty(fileNameProperty);
			if (propertyVal != null)
				return propertyVal;
		}

		// fileName is needed, check if defined
		if (fileName == null)
			return new String();

		// check if another temp. dir. is specified
		propertyVal = System.getProperty(ACS_TEMP_DIR);
		if (propertyVal != null)
			filePath = propertyVal;
		// use default
		else {
			filePath = "";
			filePath += File.separatorChar;
			filePath += TEMP_DIR;
		}

		filePath += File.separatorChar;
		filePath += fileName;

		return filePath;
	}

	/**
	 * Tries to set attributes given by attribs on given file
	 * by executing "chmod -R attribs filePath" as external
	 * process.
	 * If it fails or there is no such command on current OS 
	 * it does nothing naither printouts the error.
	 * 
	 * @param attribs
	 * @param filePath
	 */
	static public void setFileAttributes(String attribs, String filePath) {
		File file = new File(filePath);
		if( !file.exists() ) 
			return;
		String command = "chmod -R " +  attribs + " " + filePath;
		try {
			Runtime.getRuntime().exec(command).waitFor();
		} catch (Exception e) {
			// nop
		}
	}
	
	public static void copy(File source, File dest) throws IOException {
	     FileChannel in = null, out = null;
	     try {          
	          in = new FileInputStream(source).getChannel();
	          out = new FileOutputStream(dest).getChannel();
	 
	          long size = in.size();
	          MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
	 
	          out.write(buf);
	 
	     } finally {
	          if (in != null) {
	        	  in.close();
	          }
	          if (out != null) {
	        	  out.close();
	          }
	     }
	}

}

@@ -1969,16 +1969,236 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Copies file %3Ccode%3Esource%3C/code%3E to location %3Ccode%3Edest%3C/code%3E.%0A%09 * Necessary directories are created automatically.%0A%09 * The modification time is preserved if %3Ccode%3EpreserveTime%3C/code%3E is %3Ccode%3Etrue%3C/code%3E.%0A%09 */%0A
 %09public 
@@ -2236,16 +2236,38 @@
 ile dest
+, boolean preserveTime
 ) throws
@@ -2282,21 +2282,17 @@
 tion %7B%0A%09
-     
+%09
 FileChan
@@ -2323,40 +2323,56 @@
 l;%0A%09
-     try %7B          %0A%09          
+%09try %7B%0A%09%09%09dest.getParentFile().mkdirs();%0A%09%09%09%0A%09%09%09
 in =
@@ -2415,26 +2415,18 @@
 nel();%0A%09
-          
+%09%09
 out = ne
@@ -2464,30 +2464,20 @@
 nnel();%0A
-%09 %0A%09          
+%0A%09%09%09
 long siz
@@ -2492,26 +2492,18 @@
 ize();%0A%09
-          
+%09%09
 MappedBy
@@ -2569,22 +2569,12 @@
 e);%0A
-%09 %0A%09          
+%0A%09%09%09
 out.
@@ -2589,17 +2589,88 @@
 f);%0A
-%09 %0A%09     
+%0A%09%09%09if (preserveTime) %7B%0A%09%09%09%09dest.setLastModified(source.lastModified());%0A%09%09%09%7D%0A%09%09
 %7D fi
@@ -2678,26 +2678,18 @@
 ally %7B%0A%09
-          
+%09%09
 if (in !
@@ -2699,27 +2699,19 @@
 ull) %7B%0A%09
-        %09  
+%09%09%09
 in.close
@@ -2719,31 +2719,15 @@
 );%0A%09
-          %7D%0A%09          
+%09%09%7D%0A%09%09%09
 if (
@@ -2746,19 +2746,11 @@
  %7B%0A%09
-        %09  
+%09%09%09
 out.
@@ -2763,26 +2763,14 @@
 );%0A%09
-          %7D%0A%09     
+%09%09%7D%0A%09%09
 %7D%0A%09%7D

copy supports preserveTime flag and creates missing target dirs