Debian Codegraph @

current/jgi/Unzip.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintStream;
6
7 import fileIO.ByteFile;
8 import fileIO.ByteFile1;
9 import fileIO.ByteFile2;
10 import fileIO.ByteStreamWriter;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import shared.Parse;
14 import shared.Parser;
15 import shared.PreParser;
16 import shared.Shared;
17 import shared.Timer;
18 import shared.Tools;
19
20 /**
21 * @author Brian Bushnell
22 * @date May 9, 2016
23 *
24 */
25 public class Unzip {
26
27 /*--------------------------------------------------------------*/
28 /*---------------- Initialization ----------------*/
29 /*--------------------------------------------------------------*/
30
31 /**
32 * Code entrance from the command line.
33 * @param args Command line arguments
34 */
35 public static void main(String[] args){
36 //Start a timer immediately upon code entrance.
37 Timer t=new Timer();
38
39 //Create an instance of this class
40 Unzip x=new Unzip(args);
41
42 //Run the object
43 x.process(t);
44
45 //Close the print stream if it was redirected
46 Shared.closeStream(x.outstream);
47 }
48
49 /**
50 * Constructor.
51 * @param args Command line arguments
52 */
53 public Unzip(String[] args){
54
55 {//Preparse block for help, config files, and outstream
56 PreParser pp=new PreParser(args, /*getClass()*/null, false);
57 args=pp.args;
58 outstream=pp.outstream;
59 }
60
61 //Set shared static variables prior to parsing
62 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
63 ReadWrite.setZipThreads(Shared.threads());
64
65 {//Parse the arguments
66 final Parser parser=parse(args);
67 overwrite=parser.overwrite;
68 append=parser.append;
69
70 in1=parser.in1;
71
72 out1=parser.out1;
73 }
74
75 fixExtensions(); //Add or remove .gz or .bz2 as needed
76 checkFileExistence(); //Ensure files can be read and written
77 checkStatics(); //Adjust file-related static fields as needed for this program
78
79 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false);
80 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false);
81 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
82 }
83
84 /*--------------------------------------------------------------*/
85 /*---------------- Initialization Helpers ----------------*/
86 /*--------------------------------------------------------------*/
87
88 /** Parse arguments from the command line */
89 private Parser parse(String[] args){
90
91 Parser parser=new Parser();
92 parser.overwrite=true;
93 for(int i=0; i<args.length; i++){
94 String arg=args[i];
95 String[] split=arg.split("=");
96 String a=split[0].toLowerCase();
97 String b=split.length>1 ? split[1] : null;
98 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
99
100 if(a.equals("invalid")){
101 outInvalid=b;
102 }else if(a.equals("lines")){
103 maxLines=Long.parseLong(b);
104 if(maxLines<0){maxLines=Long.MAX_VALUE;}
105 }else if(a.equals("verbose")){
106 verbose=Parse.parseBoolean(b);
107 ByteFile1.verbose=verbose;
108 ByteFile2.verbose=verbose;
109 ReadWrite.verbose=verbose;
110 }else if(parser.parse(arg, a, b)){
111 //do nothing
112 }else{
113 outstream.println("Unknown parameter "+args[i]);
114 assert(false) : "Unknown parameter "+args[i];
115 // throw new RuntimeException("Unknown parameter "+args[i]);
116 }
117 }
118
119 return parser;
120 }
121
122 /** Add or remove .gz or .bz2 as needed */
123 private void fixExtensions(){
124 in1=Tools.fixExtension(in1);
125 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
126 }
127
128 /** Ensure files can be read and written */
129 private void checkFileExistence(){
130 //Ensure output files can be written
131 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
132 outstream.println((out1==null)+", "+out1);
133 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
134 }
135
136 //Ensure input files can be read
137 if(!Tools.testInputFiles(false, true, in1)){
138 throw new RuntimeException("\nCan't read some input files.\n");
139 }
140
141 //Ensure that no file was specified multiple times
142 if(!Tools.testForDuplicateFiles(true, in1, out1)){
143 throw new RuntimeException("\nSome file names were specified multiple times.\n");
144 }
145 }
146
147 /** Adjust file-related static fields as needed for this program */
148 private static void checkStatics(){
149 //Adjust the number of threads for input file reading
150 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
151 ByteFile.FORCE_MODE_BF2=true;
152 }
153
154 // if(!ByteFile.FORCE_MODE_BF2){
155 // ByteFile.FORCE_MODE_BF2=false;
156 // ByteFile.FORCE_MODE_BF1=true;
157 // }
158 }
159
160 /*--------------------------------------------------------------*/
161 /*---------------- Outer Methods ----------------*/
162 /*--------------------------------------------------------------*/
163
164 void process(Timer t){
165
166 InputStream is=ReadWrite.getInputStream(ffin1.name(), false, true);
167 ByteStreamWriter bsw=makeBSW(ffout1);
168
169 // assert(false) : "Header goes here.";
170 if(bsw!=null){
171 // assert(false) : "Header goes here.";
172 }
173
174 processInner(is, bsw);
175
176 try {
177 is.close();
178 } catch (IOException e) {
179 errorState=true;
180 e.printStackTrace();
181 }
182 if(bsw!=null){errorState|=bsw.poisonAndWait();}
183
184 t.stop();
185
186 if(showSpeed){
187 outstream.println(Tools.timeLinesBytesProcessed(t, 1, bytesProcessed, 8));
188
189 outstream.println();
190 outstream.println("Bytes Processed: \t"+bytesProcessed);
191 }
192
193 if(errorState){
194 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
195 }
196 }
197
198 /*--------------------------------------------------------------*/
199 /*---------------- Inner Methods ----------------*/
200 /*--------------------------------------------------------------*/
201
202 private void processInner(InputStream is, ByteStreamWriter bsw){
203 final byte[] buffer=new byte[65536<<2];
204
205 int len=0;
206 try {
207 len = is.read(buffer);
208 } catch (IOException e) {
209 errorState=true;
210 e.printStackTrace();
211 }
212 while(len>0){
213 bytesProcessed+=len;
214 bsw.print(buffer, len);
215 len=0;
216 try {
217 len=is.read(buffer);
218 } catch (IOException e) {
219 errorState=true;
220 e.printStackTrace();
221 }
222 }
223 }
224
225 private static ByteStreamWriter makeBSW(FileFormat ff){
226 if(ff==null){return null;}
227 ByteStreamWriter bsw=new ByteStreamWriter(ff);
228 bsw.start();
229 return bsw;
230 }
231
232 /*--------------------------------------------------------------*/
233 /*---------------- Fields ----------------*/
234 /*--------------------------------------------------------------*/
235
236 private String in1=null;
237 private String out1=null;
238 private String outInvalid=null;
239
240 /*--------------------------------------------------------------*/
241
242 private long bytesProcessed=0;
243
244 private long maxLines=Long.MAX_VALUE;
245
246 /*--------------------------------------------------------------*/
247 /*---------------- Final Fields ----------------*/
248 /*--------------------------------------------------------------*/
249
250 private final FileFormat ffin1;
251 private final FileFormat ffout1;
252 private final FileFormat ffoutInvalid;
253
254 /*--------------------------------------------------------------*/
255 /*---------------- Common Fields ----------------*/
256 /*--------------------------------------------------------------*/
257
258 private PrintStream outstream=System.err;
259 public static boolean verbose=false;
260 public boolean errorState=false;
261 public boolean showSpeed=false;
262 private boolean overwrite=true;
263 private boolean append=false;
264
265 }