| 1 |
package fileIO; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileNotFoundException; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.InputStream; |
| 7 |
import java.io.OutputStream; |
| 8 |
import java.util.zip.ZipOutputStream; |
| 9 |
|
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Timer; |
| 14 |
import shared.Tools; |
| 15 |
import tracker.ReadStats; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class CopyFile { |
| 26 |
|
| 27 |
public static void main(String[] args){ |
| 28 |
|
| 29 |
{ |
| 30 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 31 |
args=pp.args; |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
String in=null, out=null; |
| 36 |
boolean overwrite=true; |
| 37 |
boolean append=false; |
| 38 |
|
| 39 |
for(int i=0; i<args.length; i++){ |
| 40 |
final String arg=args[i]; |
| 41 |
final String[] split=arg.split("="); |
| 42 |
String a=split[0].toLowerCase(); |
| 43 |
String b=split.length>1 ? split[1] : null; |
| 44 |
|
| 45 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 46 |
|
| 47 |
}else if(Parser.parseZip(arg, a, b)){ |
| 48 |
|
| 49 |
}else if(a.equals("in")){ |
| 50 |
in=b; |
| 51 |
}else if(a.equals("out")){ |
| 52 |
out=b; |
| 53 |
}else if(a.equals("append") || a.equals("app")){ |
| 54 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 55 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 56 |
overwrite=Parse.parseBoolean(b); |
| 57 |
}else if(in==null && i==0 && !args[i].contains("=")){ |
| 58 |
in=args[i]; |
| 59 |
}else if(out==null && i==1 && !args[i].contains("=")){ |
| 60 |
out=args[i]; |
| 61 |
} |
| 62 |
} |
| 63 |
assert(in!=null && out!=null); |
| 64 |
long bytes=new File(in).length(); |
| 65 |
Timer t=new Timer(); |
| 66 |
copyFile(in, out, false, overwrite); |
| 67 |
t.stop(); |
| 68 |
double mbps1=bytes*1000d/t.elapsed; |
| 69 |
System.err.println("Time: \t"+t); |
| 70 |
System.err.println(Tools.format("Speed: \t%.2f MB/s", mbps1)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
public static synchronized void copyFile(String source, String dest, boolean createPathIfNeeded, boolean overwrite){ |
| 75 |
|
| 76 |
assert(overwrite || !new File(dest).exists()) : "Destination file already exists: "+dest; |
| 77 |
if(createPathIfNeeded){ |
| 78 |
File parent=new File(dest).getParentFile(); |
| 79 |
if(parent!=null && !parent.exists()){ |
| 80 |
parent.mkdirs(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
try{ |
| 85 |
InputStream in=ReadWrite.getInputStream(source, false, true); |
| 86 |
OutputStream out=ReadWrite.getOutputStream(dest, false, false, true); |
| 87 |
|
| 88 |
final byte[] buffer=new byte[16384]; |
| 89 |
int len; |
| 90 |
|
| 91 |
while((len = in.read(buffer)) > 0){ |
| 92 |
out.write(buffer, 0, len); |
| 93 |
} |
| 94 |
|
| 95 |
in.close(); |
| 96 |
out.flush(); |
| 97 |
if(out.getClass()==ZipOutputStream.class){ |
| 98 |
ZipOutputStream zos=(ZipOutputStream)out; |
| 99 |
zos.closeEntry(); |
| 100 |
zos.finish(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
out.close(); |
| 107 |
|
| 108 |
}catch(FileNotFoundException e){ |
| 109 |
throw new RuntimeException(e); |
| 110 |
}catch(IOException e){ |
| 111 |
throw new RuntimeException(e); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} |