Debian Codegraph @

current/template/A_SampleByteFile.java

raw symbols SCIP
1 package template;
2
3 import java.io.PrintStream;
4
5 import fileIO.ByteFile;
6 import fileIO.ByteFile1;
7 import fileIO.ByteFile2;
8 import fileIO.ByteStreamWriter;
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Shared;
15 import shared.Timer;
16 import shared.Tools;
17 import structures.ByteBuilder;
18
19 /**
20 * Reads a text file.
21 * Prints it to another text file.
22 * Filters out invalid lines and prints them to an optional third file.
23 * @author Brian Bushnell
24 * @date May 9, 2016
25 *
26 */
27 public class A_SampleByteFile {
28
29 /*--------------------------------------------------------------*/
30 /*---------------- Initialization ----------------*/
31 /*--------------------------------------------------------------*/
32
33 /**
34 * Code entrance from the command line.
35 * @param args Command line arguments
36 */
37 public static void main(String[] args){
38 //Start a timer immediately upon code entrance.
39 Timer t=new Timer();
40
41 //Create an instance of this class
42 A_SampleByteFile x=new A_SampleByteFile(args);
43
44 //Run the object
45 x.process(t);
46
47 //Close the print stream if it was redirected
48 Shared.closeStream(x.outstream);
49 }
50
51 /**
52 * Constructor.
53 * @param args Command line arguments
54 */
55 public A_SampleByteFile(String[] args){
56
57 {//Preparse block for help, config files, and outstream
58 PreParser pp=new PreParser(args, /*getClass()*/null, false);
59 args=pp.args;
60 outstream=pp.outstream;
61 }
62
63 //Set shared static variables prior to parsing
64 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
65 ReadWrite.setZipThreads(Shared.threads());
66
67 {//Parse the arguments
68 final Parser parser=parse(args);
69 overwrite=parser.overwrite;
70 append=parser.append;
71
72 in1=parser.in1;
73
74 out1=parser.out1;
75 }
76
77 validateParams();
78 fixExtensions(); //Add or remove .gz or .bz2 as needed
79 checkFileExistence(); //Ensure files can be read and written
80 checkStatics(); //Adjust file-related static fields as needed for this program
81
82 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false);
83 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false);
84 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
85 }
86
87 /*--------------------------------------------------------------*/
88 /*---------------- Initialization Helpers ----------------*/
89 /*--------------------------------------------------------------*/
90
91 /** Parse arguments from the command line */
92 private Parser parse(String[] args){
93
94 //Create a parser object
95 Parser parser=new Parser();
96
97 //Set any necessary Parser defaults here
98 //parser.foo=bar;
99 // parser.out1="stdout";
100
101 //Parse each argument
102 for(int i=0; i<args.length; i++){
103 String arg=args[i];
104
105 //Break arguments into their constituent parts, in the form of "a=b"
106 String[] split=arg.split("=");
107 String a=split[0].toLowerCase();
108 String b=split.length>1 ? split[1] : null;
109 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
110
111 if(a.equals("invalid")){
112 outInvalid=b;
113 }else if(a.equals("lines")){
114 maxLines=Long.parseLong(b);
115 if(maxLines<0){maxLines=Long.MAX_VALUE;}
116 }else if(a.equals("verbose")){
117 verbose=Parse.parseBoolean(b);
118 ByteFile1.verbose=verbose;
119 ByteFile2.verbose=verbose;
120 ReadWrite.verbose=verbose;
121 }else if(a.equals("parse_flag_goes_here")){
122 long fake_variable=Parse.parseKMG(b);
123 //Set a variable here
124 }else if(parser.parse(arg, a, b)){
125 //do nothing
126 }else{
127 outstream.println("Unknown parameter "+args[i]);
128 assert(false) : "Unknown parameter "+args[i];
129 throw new RuntimeException("Unknown parameter "+args[i]);
130 }
131 }
132
133 return parser;
134 }
135
136 /** Add or remove .gz or .bz2 as needed */
137 private void fixExtensions(){
138 in1=Tools.fixExtension(in1);
139 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
140 }
141
142 /** Ensure files can be read and written */
143 private void checkFileExistence(){
144 //Ensure output files can be written
145 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
146 outstream.println((out1==null)+", "+out1);
147 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
148 }
149
150 //Ensure input files can be read
151 if(!Tools.testInputFiles(false, true, in1)){
152 throw new RuntimeException("\nCan't read some input files.\n");
153 }
154
155 //Ensure that no file was specified multiple times
156 if(!Tools.testForDuplicateFiles(true, in1, out1)){
157 throw new RuntimeException("\nSome file names were specified multiple times.\n");
158 }
159 }
160
161 /** Adjust file-related static fields as needed for this program */
162 private static void checkStatics(){
163 //Adjust the number of threads for input file reading
164 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
165 ByteFile.FORCE_MODE_BF2=true;
166 }
167
168 // if(!ByteFile.FORCE_MODE_BF2){
169 // ByteFile.FORCE_MODE_BF2=false;
170 // ByteFile.FORCE_MODE_BF1=true;
171 // }
172 }
173
174 /** Ensure parameter ranges are within bounds and required parameters are set */
175 private boolean validateParams(){
176 // assert(minfoo>0 && minfoo<=maxfoo) : minfoo+", "+maxfoo;
177 assert(false) : "TODO";
178 return true;
179 }
180
181 /*--------------------------------------------------------------*/
182 /*---------------- Outer Methods ----------------*/
183 /*--------------------------------------------------------------*/
184
185 /** Create streams and process all data */
186 void process(Timer t){
187
188 ByteFile bf=ByteFile.makeByteFile(ffin1);
189 ByteStreamWriter bsw=makeBSW(ffout1);
190 ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid);
191
192 // assert(false) : "Header goes here.";
193 if(bsw!=null){
194 // assert(false) : "Header goes here.";
195 }
196
197 processInner(bf, bsw, bswInvalid);
198
199 errorState|=bf.close();
200 if(bsw!=null){errorState|=bsw.poisonAndWait();}
201 if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();}
202
203 t.stop();
204
205 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
206
207 outstream.println();
208 outstream.println("Valid Lines: \t"+linesOut);
209 outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
210
211 //Throw an exception of there was an error in a thread
212 if(errorState){
213 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
214 }
215 }
216
217 /*--------------------------------------------------------------*/
218 /*---------------- Inner Methods ----------------*/
219 /*--------------------------------------------------------------*/
220
221 private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){
222 byte[] line=bf.nextLine();
223 ByteBuilder bb=new ByteBuilder();
224
225 while(line!=null){
226 if(line.length>0){
227 if(maxLines>0 && linesProcessed>=maxLines){break;}
228 linesProcessed++;
229 bytesProcessed+=(line.length+1);
230
231 final boolean valid=(line[0]!='#');
232
233 if(valid){
234 linesOut++;
235 bytesOut+=(line.length+1);
236 for(int i=0; i<line.length && line[i]!='\t'; i++){
237 bb.append(line[i]);
238 }
239 bb.nl();
240 bsw.print(bb.toBytes());
241 bb.clear();
242 }else{
243 if(bswInvalid!=null){
244 bswInvalid.println(line);
245 }
246 }
247 }
248 line=bf.nextLine();
249 }
250 }
251
252 private static ByteStreamWriter makeBSW(FileFormat ff){
253 if(ff==null){return null;}
254 ByteStreamWriter bsw=new ByteStreamWriter(ff);
255 bsw.start();
256 return bsw;
257 }
258
259 /*--------------------------------------------------------------*/
260 /*---------------- Fields ----------------*/
261 /*--------------------------------------------------------------*/
262
263 /** Primary input file path */
264 private String in1=null;
265
266 /** Primary output file path */
267 private String out1=null;
268
269 /** Junk output file path */
270 private String outInvalid=null;
271
272 /*--------------------------------------------------------------*/
273
274 private long linesProcessed=0;
275 private long linesOut=0;
276 private long bytesProcessed=0;
277 private long bytesOut=0;
278
279 private long maxLines=Long.MAX_VALUE;
280
281 /*--------------------------------------------------------------*/
282 /*---------------- Final Fields ----------------*/
283 /*--------------------------------------------------------------*/
284
285 /** Input File */
286 private final FileFormat ffin1;
287 /** Output File */
288 private final FileFormat ffout1;
289 /** Optional Output File for Junk */
290 private final FileFormat ffoutInvalid;
291
292 /*--------------------------------------------------------------*/
293 /*---------------- Common Fields ----------------*/
294 /*--------------------------------------------------------------*/
295
296 /** Print status messages to this output stream */
297 private PrintStream outstream=System.err;
298 /** Print verbose messages */
299 public static boolean verbose=false;
300 /** True if an error was encountered */
301 public boolean errorState=false;
302 /** Overwrite existing output files */
303 private boolean overwrite=true;
304 /** Append to existing output files */
305 private boolean append=false;
306
307 }