Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fileIO/CopyFile.java @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
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.Locale; | |
9 import java.util.zip.ZipOutputStream; | |
10 | |
11 import shared.Parse; | |
12 import shared.Parser; | |
13 import shared.PreParser; | |
14 import shared.ReadStats; | |
15 import shared.Timer; | |
16 | |
17 | |
18 /** | |
19 * Unlike ReadWrite's version, this one forces compression and decompression even with same extensions. | |
20 * Mainly for benchmarking. | |
21 * @author Brian Bushnell | |
22 * @date Jan 23, 2013 | |
23 * | |
24 */ | |
25 public class CopyFile { | |
26 | |
27 public static void main(String[] args){ | |
28 | |
29 {//Preparse block for help, config files, and outstream | |
30 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); | |
31 args=pp.args; | |
32 //outstream=pp.outstream; | |
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 //do nothing | |
47 }else if(Parser.parseZip(arg, a, b)){ | |
48 //do nothing | |
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(String.format(Locale.ROOT, "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 // else if(PROCESS_XZ && out.getClass()==org.tukaani.xz.XZOutputStream.class){ | |
103 // org.tukaani.xz.XZOutputStream zos=(org.tukaani.xz.XZOutputStream)out; | |
104 // zos.finish(); | |
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 } |