Debian Codegraph @

current/fileIO/QuickFile.java

raw symbols SCIP
1 package fileIO;
2 import java.io.File;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6
7 import shared.KillSwitch;
8 import shared.Shared;
9 import shared.Timer;
10 import shared.Tools;
11 import structures.ListNum;
12
13
14 /**
15 * Written for testing a NERSC slowdown in multithreaded file reading.
16 * The problem was sometimes sidestepped by eliminating "if(pushBack!=null){" and reimplementing pushback.
17 * However, that does not address the cause, so is not an overall solution; the cause remains a mystery.
18 * This class may safely be deleted.
19 *
20 * @author Brian Bushnell
21 *
22 */
23 public class QuickFile {
24
25
26 public static void main(String[] args){
27 QuickFile tf=new QuickFile(args.length>0 ? args[0] : "stdin", true);
28 long first=0, last=100;
29 boolean speedtest=false;
30 if(args.length>1){
31 if(args[1].equalsIgnoreCase("speedtest")){
32 speedtest=true;
33 first=0;
34 last=Long.MAX_VALUE;
35 }else{
36 first=Integer.parseInt(args[1]);
37 last=first+100;
38 }
39 }
40 if(args.length>2){
41 last=Integer.parseInt(args[2]);
42 }
43 speedtest(tf, first, last, !speedtest);
44
45 tf.close();
46 tf.reset();
47 tf.close();
48 }
49
50 private static void speedtest(QuickFile tf, long first, long last, boolean reprint){
51 Timer t=new Timer();
52 long lines=0;
53 long bytes=0;
54 for(long i=0; i<first; i++){tf.nextLine();}
55 if(reprint){
56 for(long i=first; i<last; i++){
57 byte[] s=tf.nextLine();
58 if(s==null){break;}
59
60 lines++;
61 bytes+=s.length;
62 System.out.println(new String(s));
63 }
64
65 System.err.println("\n");
66 System.err.println("Lines: "+lines);
67 System.err.println("Bytes: "+bytes);
68 }else{
69 for(long i=first; i<last; i++){
70 byte[] s=tf.nextLine();
71 if(s==null){break;}
72 lines++;
73 bytes+=s.length;
74 }
75 }
76 t.stop();
77
78 if(!reprint){
79 System.err.println(Tools.timeLinesBytesProcessed(t, lines, bytes, 8));
80 }
81 }
82
83 public QuickFile(String fname, boolean allowSubprocess_){
84 this(FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess_, false));
85 }
86
87 public QuickFile(FileFormat ff_){
88 ff=ff_;
89 assert(ff.read()) : ff;
90 if(verbose){System.err.println("ByteFile1("+ff+")");}
91 is=open();
92 }
93
94 public final void reset(){
95 close();
96 is=open();
97
98 pushBack=null;
99 nextID=0;
100 }
101
102 public synchronized final boolean close(){
103 if(verbose){System.err.println("Closing "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
104 if(!open){return errorState;}
105 open=false;
106 assert(is!=null);
107 // assert(false) : name()+","+allowSubprocess();
108 errorState|=ReadWrite.finishReading(is, name(), (allowSubprocess() || FileFormat.isBamFile(name())));
109
110 is=null;
111 lineNum=-1;
112 pushBack=null;
113 if(verbose){System.err.println("Closed "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
114 return errorState;
115 }
116
117 public byte[] nextLine(){
118 // if(pushBack!=null){
119 // byte[] temp=pushBack;
120 // pushBack=null;
121 // return temp;
122 // }
123
124 if(verbose){System.err.println("Reading line "+this.getClass().getName()+" for "+name()+"; open="+open+"; errorState="+errorState);}
125
126 if(!open || is==null){
127 if(Shared.WINDOWS){System.err.println("Attempting to read from a closed file: "+name());}
128 return null;
129 }
130
131 // System.out.println("\nCalled nextLine() for line "+lineNum);
132 // System.out.println("A: bstart="+bstart+", bstop="+bstop);
133
134 //if(bstart<bstop && lasteol==slasher && buffer[bstart]==slashn){bstart++;}
135 // assert(bstart>=bstop || (buffer[bstart]!=slashn)/*buffer[bstart]>slasher || buffer[bstart]==slashn*/);
136 int nlpos=bstart;
137
138 // System.out.println("B: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
139 // while(nlpos<bstop && (buffer[nlpos]>slasher || buffer[nlpos]==tab)){nlpos++;}
140 while(nlpos<bstop && buffer[nlpos]!=slashn){nlpos++;}
141 // System.out.println("C: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
142 if(nlpos>=bstop){
143 nlpos=fillBuffer();
144 // System.out.println("Filled buffer.");
145 }
146 // System.out.println("D: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos);
147
148 if(nlpos<0 || bstop<1){
149 close();
150 return null;
151 }
152
153 lineNum++;
154 //Limit is the position after the last position to copy.
155 //Limit equals nlpos unless there was a \r before the \n.
156 final int limit=(nlpos>bstart && buffer[nlpos-1]==slashr) ? nlpos-1 : nlpos;
157 if(bstart==limit){//Empty line.
158 bstart=nlpos+1;
159 // System.out.println("E: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos+", returning='"+printNL(blankLine)+"'");
160 return blankLine;
161 }
162
163 byte[] line=KillSwitch.copyOfRange(buffer, bstart, limit);
164
165 assert(line.length>0) : bstart+", "+nlpos+", "+limit;
166 bstart=nlpos+1;
167 // System.out.println("F: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos+", returning='"+printNL(line)+"'");
168 return line;
169 }
170
171 final byte[] dummy=new byte[100];
172
173 private int fillBuffer(){
174 if(bstart<bstop){ //Shift end bytes to beginning
175 // System.err.println("Shift: "+bstart+", "+bstop);
176 assert(bstart>0);
177 // assert(bstop==buffer.length);
178 int extra=bstop-bstart;
179 for(int i=0; i<extra; i++, bstart++){
180 // System.err.print((char)buffer[bstart]);
181 //System.err.print('.');
182 buffer[i]=buffer[bstart];
183 // assert(buffer[i]>=slasher || buffer[i]==tab);
184 assert(buffer[i]!=slashn);
185 }
186 bstop=extra;
187 // System.err.println();
188
189 // {//for debugging only
190 // buffer=new byte[bufferlen];
191 // bstop=0;
192 // bstart=0;
193 // }
194 }else{
195 bstop=0;
196 }
197
198 bstart=0;
199 int len=bstop;
200 int r=-1;
201 while(len==bstop){//hit end of input without encountering a newline
202 if(bstop==buffer.length){
203 // assert(false) : len+", "+bstop;
204 buffer=KillSwitch.copyOf(buffer, buffer.length*2);
205 }
206 try {
207 r=is.read(buffer, bstop, buffer.length-bstop);
208 // byte[] x=new byte[buffer.length-bstop];
209 // r=is.read(x);
210 // if(r>0){
211 // for(int i=0, j=bstop; i<r; i++, j++){
212 // buffer[j]=x[i];
213 // }
214 // }
215 } catch (IOException e) {
216 e.printStackTrace();
217 System.err.println("open="+open);
218 }
219 if(r>0){
220 bstop=bstop+r;
221 // while(len<bstop && (buffer[len]>slasher || buffer[len]==tab)){len++;}
222 while(len<bstop && buffer[len]!=slashn){len++;}
223 }else{
224 len=bstop;
225 break;
226 }
227 }
228
229 // System.err.println("After Fill: ");
230 // printBuffer();
231 // System.err.println();
232
233 // System.out.println("Filled buffer; r="+r+", returning "+len);
234 assert(r==-1 || buffer[len]==slashn);
235
236 // System.err.println("lasteol="+(lasteol=='\n' ? "\\n" : lasteol==slashr ? "\\r" : ""+(int)lasteol));
237 // System.err.println("First="+(int)buffer[0]+"\nLastEOL="+(int)lasteol);
238
239 return len;
240 }
241
242 private final synchronized InputStream open(){
243 if(open){
244 throw new RuntimeException("Attempt to open already-opened TextFile "+name());
245 }
246 open=true;
247 is=ReadWrite.getInputStream(name(), BUFFERED, allowSubprocess());
248 bstart=-1;
249 bstop=-1;
250 return is;
251 }
252
253 public final ArrayList<byte[]> toByteLines(){
254
255 byte[] s=null;
256 ArrayList<byte[]> list=new ArrayList<byte[]>(4096);
257
258 for(s=nextLine(); s!=null; s=nextLine()){
259 list.add(s);
260 }
261
262 return list;
263 }
264
265 public final long countLines(){
266 byte[] s=null;
267 long count=0;
268 for(s=nextLine(); s!=null; s=nextLine()){count++;}
269 reset();
270
271 return count;
272 }
273
274 public synchronized final ListNum<byte[]> nextList(){
275 byte[] line=nextLine();
276 if(line==null){return null;}
277 ArrayList<byte[]> list=new ArrayList<byte[]>(200);
278 list.add(line);
279 for(int i=1; i<200; i++){
280 line=nextLine();
281 if(line==null){break;}
282 list.add(line);
283 }
284 ListNum<byte[]> ln=new ListNum<byte[]>(list, nextID);
285 nextID++;
286 return ln;
287 }
288
289 public final boolean exists(){
290 return name().equals("stdin") || name().startsWith("stdin.") || name().startsWith("jar:") || new File(name()).exists(); //TODO Ugly and unsafe hack for files in jars
291 }
292
293 public final void pushBack(byte[] line){
294 assert(pushBack==null);
295 pushBack=line;
296 }
297
298 public final String name(){return ff.name();}
299 public final boolean allowSubprocess(){return ff.allowSubprocess();}
300
301 public final FileFormat ff;
302
303 public static boolean FORCE_MODE_BF1=false;//!(Shared.GENEPOOL || Shared.DENOVO || Shared.CORI || Shared.WINDOWS);
304 public static boolean FORCE_MODE_BF2=false;
305 public static boolean FORCE_MODE_BF3=false;
306
307 protected final static byte slashr='\r', slashn='\n', carrot='>', plus='+', at='@';//, tab='\t';
308
309 long a=1;
310 long b=2;
311 long c=3;
312 long d=4;
313 byte[] p0=null;
314 byte[] p1=null;
315 byte[] p2=null;
316 byte[] p3=null;
317 private byte[] pushBack=null;
318 private long nextID=0;
319
320 private boolean open=false;
321 private byte[] buffer=new byte[bufferlen];
322 private static final byte[] blankLine=new byte[0];
323 private int bstart=0, bstop=0;
324 public InputStream is;
325 public long lineNum=-1;
326
327 public static boolean verbose=false;
328 public static boolean BUFFERED=false;
329 public static int bufferlen=16384;
330
331 private boolean errorState=false;
332
333 }