| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
int nlpos=bstart; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
while(nlpos<bstop && buffer[nlpos]!=slashn){nlpos++;} |
| 141 |
|
| 142 |
if(nlpos>=bstop){ |
| 143 |
nlpos=fillBuffer(); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if(nlpos<0 || bstop<1){ |
| 149 |
close(); |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
lineNum++; |
| 154 |
|
| 155 |
|
| 156 |
final int limit=(nlpos>bstart && buffer[nlpos-1]==slashr) ? nlpos-1 : nlpos; |
| 157 |
if(bstart==limit){ |
| 158 |
bstart=nlpos+1; |
| 159 |
|
| 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 |
|
| 168 |
return line; |
| 169 |
} |
| 170 |
|
| 171 |
final byte[] dummy=new byte[100]; |
| 172 |
|
| 173 |
private int fillBuffer(){ |
| 174 |
if(bstart<bstop){ |
| 175 |
|
| 176 |
assert(bstart>0); |
| 177 |
|
| 178 |
int extra=bstop-bstart; |
| 179 |
for(int i=0; i<extra; i++, bstart++){ |
| 180 |
|
| 181 |
|
| 182 |
buffer[i]=buffer[bstart]; |
| 183 |
|
| 184 |
assert(buffer[i]!=slashn); |
| 185 |
} |
| 186 |
bstop=extra; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
}else{ |
| 195 |
bstop=0; |
| 196 |
} |
| 197 |
|
| 198 |
bstart=0; |
| 199 |
int len=bstop; |
| 200 |
int r=-1; |
| 201 |
while(len==bstop){ |
| 202 |
if(bstop==buffer.length){ |
| 203 |
|
| 204 |
buffer=KillSwitch.copyOf(buffer, buffer.length*2); |
| 205 |
} |
| 206 |
try { |
| 207 |
r=is.read(buffer, bstop, buffer.length-bstop); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 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 |
|
| 222 |
while(len<bstop && buffer[len]!=slashn){len++;} |
| 223 |
}else{ |
| 224 |
len=bstop; |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
assert(r==-1 || buffer[len]==slashn); |
| 235 |
|
| 236 |
|
| 237 |
|
| 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(); |
| 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; |
| 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='@'; |
| 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 |
} |