Debian Codegraph @

current/fileIO/ByteFile1.java

raw symbols SCIP
1 package fileIO;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.util.Arrays;
5
6 import shared.KillSwitch;
7 import shared.Shared;
8 import shared.Timer;
9 import shared.Tools;
10
11
12 /**
13 * @author Brian Bushnell
14 *
15 */
16 public final class ByteFile1 extends ByteFile {
17
18
19 public static void main(String[] args){
20 ByteFile1 tf=new ByteFile1(args.length>0 ? args[0] : "stdin", true);
21 long first=0, last=100;
22 boolean speedtest=false;
23 if(args.length>1){
24 if(args[1].equalsIgnoreCase("speedtest")){
25 speedtest=true;
26 first=0;
27 last=Long.MAX_VALUE;
28 }else{
29 first=Integer.parseInt(args[1]);
30 last=first+100;
31 }
32 }
33 if(args.length>2){
34 if(args[2].equalsIgnoreCase("simd")){
35 Shared.SIMD=true;
36 }else {
37 last=Integer.parseInt(args[2]);
38 }
39 }
40 speedtest(tf, first, last, !speedtest);
41
42 tf.close();
43 tf.reset();
44 tf.close();
45
46 // tf.reset();
47 // byte[] b=tf.nextLine();
48 // System.err.println(new String(b));
49 // tf.pushBack(b);
50 // b=tf.nextLine();
51 // System.err.println(new String(b));
52 // tf.bstart+=2;
53 // tf.pushBack(b);
54 // b=tf.nextLine();
55 // System.err.println(new String(b));
56 // b=tf.nextLine();
57 // System.err.println(new String(b));
58 // b=tf.nextLine();
59 // System.err.println(new String(b));
60 // b=tf.nextLine();
61 // System.err.println(new String(b));
62 }
63
64 private static void speedtest(ByteFile1 tf, long first, long last, boolean reprint){
65 Timer t=new Timer();
66 long lines=0;
67 long bytes=0;
68 for(long i=0; i<first; i++){tf.nextLine();}
69 if(reprint){
70 for(long i=first; i<last; i++){
71 byte[] s=tf.nextLine();
72 if(s==null){break;}
73
74 lines++;
75 bytes+=s.length;
76 System.out.println(new String(s));
77 }
78
79 System.err.println("\n");
80 System.err.println("Lines: "+lines);
81 System.err.println("Bytes: "+bytes);
82 }else{
83 for(long i=first; i<last; i++){
84 byte[] s=tf.nextLine();
85 if(s==null){break;}
86 lines++;
87 bytes+=s.length;
88 }
89 }
90 t.stop();
91
92 if(!reprint){
93 System.err.println(Tools.timeLinesBytesProcessed(t, lines, bytes, 8));
94 }
95 }
96
97 public ByteFile1(String fname, boolean allowSubprocess_){
98 this(FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess_, false));
99 }
100
101 public ByteFile1(FileFormat ff){
102 super(ff);
103 if(verbose){System.err.println("ByteFile1("+ff+")");}
104 is=open();
105 }
106
107 @Override
108 public final void reset(){
109 close();
110 is=open();
111 superReset();
112 }
113
114 @Override
115 public synchronized final boolean close(){
116 if(verbose){System.err.println("Closing "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
117 if(!open){return errorState;}
118 open=false;
119 assert(is!=null);
120 // assert(false) : name()+","+allowSubprocess();
121 errorState|=ReadWrite.finishReading(is, name(), (allowSubprocess() || FileFormat.isBamFile(name())));
122
123 is=null;
124 lineNum=-1;
125 // pushBack=null;
126 if(verbose){System.err.println("Closed "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
127 return errorState;
128 }
129
130 @Override
131 public final byte[] nextLine(){
132 // if(pushBack!=null){//This was making the massive post-Meltdown-patch performance problem more common
133 // byte[] temp=pushBack;
134 // pushBack=null;
135 // return temp;
136 // }
137
138 if(verbose){System.err.println("Reading line "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
139
140 if(!open || is==null){
141 if(Shared.WINDOWS){System.err.println("Attempting to read from a closed file: "+name());}
142 return null;
143 }
144
145 // System.out.println("\nCalled nextLine() for line "+lineNum);
146 // System.out.println("A: bstart="+bstart+", bstop="+bstop);
147
148 //if(bstart<bstop && lasteol==slasher && buffer[bstart]==slashn){bstart++;}
149 // assert(bstart>=bstop || (buffer[bstart]!=slashn)/*buffer[bstart]>slasher || buffer[bstart]==slashn*/);
150 int nlpos=bstart;
151
152 // System.out.println("B: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
153 // while(nlpos<bstop && (buffer[nlpos]>slasher || buffer[nlpos]==tab)){nlpos++;}
154 while(nlpos<bstop && buffer[nlpos]!=slashn){nlpos++;}//Another vectorization place
155 // System.out.println("C: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
156 if(nlpos>=bstop){
157 nlpos=fillBuffer();
158 // System.out.println("Filled buffer.");
159 }
160 // System.out.println("D: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
161
162 if(nlpos<0 || bstop<1){
163 close();
164 return null;
165 }
166
167 lineNum++;
168 //Limit is the position after the last position to copy.
169 //Limit equals nlpos unless there was a \r before the \n.
170 final int limit=(nlpos>bstart && buffer[nlpos-1]==slashr) ? nlpos-1 : nlpos;
171 if(bstart==limit){//Empty line.
172 bstart=nlpos+1;
173 // System.out.println("E: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos+", returning='"+printNL(blankLine)+"'");
174 return blankLine;
175 }
176
177 byte[] line=KillSwitch.copyOfRange(buffer, bstart, limit);
178 // byte[] line=Arrays.copyOfRange(buffer, bstart, limit);
179 // byte[] line=new String(buffer, bstart, limit-bstart).getBytes();
180 // byte[] line=new byte[limit-bstart];
181 // byte[] line=dummy;
182
183 assert(line.length>0) : bstart+", "+nlpos+", "+limit;
184 bstart=nlpos+1;
185 // System.out.println("F: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos+", returning='"+printNL(line)+"'");
186 return line;
187 }
188
189 final byte[] dummy=new byte[100];
190
191 private static final String printNL(byte[] b){
192 StringBuilder sb=new StringBuilder();
193 for(int i=0; i<b.length; i++){
194 char c=(char)b[i];
195 if(c=='\n'){
196 sb.append("\\n");
197 }else if(c==slashr){
198 sb.append("\\r");
199 }else{
200 sb.append(c);
201 }
202 }
203 return sb.toString();
204 }
205
206 private final void printBuffer(){
207 for(int i=0; i<bstop; i++){
208 char c=(char)buffer[i];
209 if(c=='\n'){
210 System.err.println("\\n");
211 }else if(c==slashr){
212 System.err.print("\\r");
213 }else{
214 System.err.print(c);
215 }
216 }
217 }
218
219 private int fillBuffer(){
220 if(bstart<bstop){ //Shift end bytes to beginning
221 // System.err.println("Shift: "+bstart+", "+bstop);
222 assert(bstart>0);
223 // assert(bstop==buffer.length);
224 int extra=bstop-bstart;
225 for(int i=0; i<extra; i++, bstart++){
226 // System.err.print((char)buffer[bstart]);
227 //System.err.print('.');
228 buffer[i]=buffer[bstart];
229 // assert(buffer[i]>=slasher || buffer[i]==tab);
230 assert(buffer[i]!=slashn);
231 }
232 bstop=extra;
233 // System.err.println();
234
235 // {//for debugging only
236 // buffer=new byte[bufferlen];
237 // bstop=0;
238 // bstart=0;
239 // }
240 }else{
241 bstop=0;
242 }
243
244 bstart=0;
245 int len=bstop;
246 int r=-1;
247 while(len==bstop){//hit end of input without encountering a newline
248 if(bstop==buffer.length){
249 // assert(false) : len+", "+bstop;
250 buffer=KillSwitch.copyOf(buffer, buffer.length*2);
251 }
252 try {
253 r=is.read(buffer, bstop, buffer.length-bstop);
254 // byte[] x=new byte[buffer.length-bstop];
255 // r=is.read(x);
256 // if(r>0){
257 // for(int i=0, j=bstop; i<r; i++, j++){
258 // buffer[j]=x[i];
259 // }
260 // }
261 } catch (IOException e) {//java.io.IOException: Stream Closed
262 //TODO: This should be avoided rather than caught. It happens when a stream is shut down with e.g. "reads=100".
263 if(!Shared.anomaly){
264 e.printStackTrace();
265 System.err.println("open="+open);
266 }
267 } catch (NullPointerException e) {//Can be thrown by java.util.zip.Inflater.ensureOpen(Inflater.java:389)
268 //TODO: This should be avoided rather than caught. It happens when a stream is shut down with e.g. "reads=100".
269 if(!Shared.anomaly){
270 e.printStackTrace();
271 System.err.println("open="+open);
272 }
273 }
274 if(r>0){
275 bstop=bstop+r;
276 // //while(len<bstop && (buffer[len]>slasher || buffer[len]==tab)){len++;}//Obsolete; handled old-style Mac convention
277
278 while(len<bstop && buffer[len]!=slashn){len++;}
279 // len=Vector.find(buffer, slashn, len, bstop); //x0.85 speed in simd mode for short sequences; x1.00 for 150bp fastq
280 }else{
281 len=bstop;
282 break;
283 }
284 }
285
286 // System.err.println("After Fill: ");
287 // printBuffer();
288 // System.err.println();
289
290 // System.out.println("Filled buffer; r="+r+", returning "+len);
291 assert(r==-1 || buffer[len]==slashn);
292
293 // System.err.println("lasteol="+(lasteol=='\n' ? "\\n" : lasteol==slashr ? "\\r" : ""+(int)lasteol));
294 // System.err.println("First="+(int)buffer[0]+"\nLastEOL="+(int)lasteol);
295
296 return len;
297 }
298
299 @Override
300 public void pushBack(byte[] line) {
301 if(bstart>line.length){
302 bstart--;
303 buffer[bstart]='\n';
304 for(int i=0, j=bstart-line.length; i<line.length; i++, j++){
305 buffer[j]=line[i];
306 }
307 bstart=bstart-line.length;
308 return;
309 }
310
311 int bLen=bstop-bstart;
312 int newLen=bLen+line.length+1;
313 int rShift=line.length+1-bstart;
314 assert(rShift>0) : bstop+", "+bstart+", "+line.length;
315 while(newLen>buffer.length){
316 //This could get big if pushback is used often,
317 //unless special steps are taken to prevent it, like leaving extra space for pushbacks.
318 buffer=Arrays.copyOf(buffer, buffer.length*2);
319 }
320
321 Tools.shiftRight(buffer, rShift);
322
323 for(int i=0; i<line.length; i++){
324 buffer[i]=line[i];
325 }
326 buffer[line.length]='\n';
327 bstart=0;
328 bstop=newLen;
329 }
330
331 private final synchronized InputStream open(){
332 if(open){
333 throw new RuntimeException("Attempt to open already-opened TextFile "+name());
334 }
335 open=true;
336 is=ReadWrite.getInputStream(name(), BUFFERED, allowSubprocess());
337 bstart=-1;
338 bstop=-1;
339 return is;
340 }
341
342 @Override
343 public boolean isOpen(){return open;}
344
345 @Override
346 public final InputStream is(){return is;}
347
348 @Override
349 public final long lineNum(){return lineNum;}
350
351 private boolean open=false;
352 private byte[] buffer=new byte[bufferlen];
353 private static final byte[] blankLine=new byte[0];
354 private int bstart=0, bstop=0;
355 public InputStream is;
356 public long lineNum=-1;
357
358 public static boolean verbose=false;
359 public static boolean BUFFERED=false;
360 public static int bufferlen=16384;
361
362 private boolean errorState=false;
363
364
365 }