jpayne@68: package fileIO; jpayne@68: import java.io.File; jpayne@68: import java.io.IOException; jpayne@68: import java.io.InputStream; jpayne@68: import java.util.ArrayList; jpayne@68: jpayne@68: import shared.KillSwitch; jpayne@68: import shared.Shared; jpayne@68: import shared.Timer; jpayne@68: import shared.Tools; jpayne@68: import structures.ListNum; jpayne@68: jpayne@68: jpayne@68: /** jpayne@68: * Written for testing a NERSC slowdown in multithreaded file reading. jpayne@68: * The problem was sometimes sidestepped by eliminating "if(pushBack!=null){" and reimplementing pushback. jpayne@68: * However, that does not address the cause, so is not an overall solution; the cause remains a mystery. jpayne@68: * This class may safely be deleted. jpayne@68: * jpayne@68: * @author Brian Bushnell jpayne@68: * jpayne@68: */ jpayne@68: public class QuickFile { jpayne@68: jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: QuickFile tf=new QuickFile(args.length>0 ? args[0] : "stdin", true); jpayne@68: long first=0, last=100; jpayne@68: boolean speedtest=false; jpayne@68: if(args.length>1){ jpayne@68: if(args[1].equalsIgnoreCase("speedtest")){ jpayne@68: speedtest=true; jpayne@68: first=0; jpayne@68: last=Long.MAX_VALUE; jpayne@68: }else{ jpayne@68: first=Integer.parseInt(args[1]); jpayne@68: last=first+100; jpayne@68: } jpayne@68: } jpayne@68: if(args.length>2){ jpayne@68: last=Integer.parseInt(args[2]); jpayne@68: } jpayne@68: speedtest(tf, first, last, !speedtest); jpayne@68: jpayne@68: tf.close(); jpayne@68: tf.reset(); jpayne@68: tf.close(); jpayne@68: } jpayne@68: jpayne@68: private static void speedtest(QuickFile tf, long first, long last, boolean reprint){ jpayne@68: Timer t=new Timer(); jpayne@68: long lines=0; jpayne@68: long bytes=0; jpayne@68: for(long i=0; i0) : bstart+", "+nlpos+", "+limit; jpayne@68: bstart=nlpos+1; jpayne@68: // System.out.println("F: bstart="+bstart+", bstop="+bstop+", nlpos="+nlpos+", returning='"+printNL(line)+"'"); jpayne@68: return line; jpayne@68: } jpayne@68: jpayne@68: final byte[] dummy=new byte[100]; jpayne@68: jpayne@68: private int fillBuffer(){ jpayne@68: if(bstart0); jpayne@68: // assert(bstop==buffer.length); jpayne@68: int extra=bstop-bstart; jpayne@68: for(int i=0; i=slasher || buffer[i]==tab); jpayne@68: assert(buffer[i]!=slashn); jpayne@68: } jpayne@68: bstop=extra; jpayne@68: // System.err.println(); jpayne@68: jpayne@68: // {//for debugging only jpayne@68: // buffer=new byte[bufferlen]; jpayne@68: // bstop=0; jpayne@68: // bstart=0; jpayne@68: // } jpayne@68: }else{ jpayne@68: bstop=0; jpayne@68: } jpayne@68: jpayne@68: bstart=0; jpayne@68: int len=bstop; jpayne@68: int r=-1; jpayne@68: while(len==bstop){//hit end of input without encountering a newline jpayne@68: if(bstop==buffer.length){ jpayne@68: // assert(false) : len+", "+bstop; jpayne@68: buffer=KillSwitch.copyOf(buffer, buffer.length*2); jpayne@68: } jpayne@68: try { jpayne@68: r=is.read(buffer, bstop, buffer.length-bstop); jpayne@68: // byte[] x=new byte[buffer.length-bstop]; jpayne@68: // r=is.read(x); jpayne@68: // if(r>0){ jpayne@68: // for(int i=0, j=bstop; i toByteLines(){ jpayne@68: jpayne@68: byte[] s=null; jpayne@68: ArrayList list=new ArrayList(4096); jpayne@68: jpayne@68: for(s=nextLine(); s!=null; s=nextLine()){ jpayne@68: list.add(s); jpayne@68: } jpayne@68: jpayne@68: return list; jpayne@68: } jpayne@68: jpayne@68: public final long countLines(){ jpayne@68: byte[] s=null; jpayne@68: long count=0; jpayne@68: for(s=nextLine(); s!=null; s=nextLine()){count++;} jpayne@68: reset(); jpayne@68: jpayne@68: return count; jpayne@68: } jpayne@68: jpayne@68: public synchronized final ListNum nextList(){ jpayne@68: byte[] line=nextLine(); jpayne@68: if(line==null){return null;} jpayne@68: ArrayList list=new ArrayList(200); jpayne@68: list.add(line); jpayne@68: for(int i=1; i<200; i++){ jpayne@68: line=nextLine(); jpayne@68: if(line==null){break;} jpayne@68: list.add(line); jpayne@68: } jpayne@68: ListNum ln=new ListNum(list, nextID); jpayne@68: nextID++; jpayne@68: return ln; jpayne@68: } jpayne@68: jpayne@68: public final boolean exists(){ jpayne@68: return name().equals("stdin") || name().startsWith("stdin.") || name().startsWith("jar:") || new File(name()).exists(); //TODO Ugly and unsafe hack for files in jars jpayne@68: } jpayne@68: jpayne@68: public final void pushBack(byte[] line){ jpayne@68: assert(pushBack==null); jpayne@68: pushBack=line; jpayne@68: } jpayne@68: jpayne@68: public final String name(){return ff.name();} jpayne@68: public final boolean allowSubprocess(){return ff.allowSubprocess();} jpayne@68: jpayne@68: public final FileFormat ff; jpayne@68: jpayne@68: public static boolean FORCE_MODE_BF1=false;//!(Shared.GENEPOOL || Shared.DENOVO || Shared.CORI || Shared.WINDOWS); jpayne@68: public static boolean FORCE_MODE_BF2=false; jpayne@68: public static boolean FORCE_MODE_BF3=false; jpayne@68: jpayne@68: protected final static byte slashr='\r', slashn='\n', carrot='>', plus='+', at='@';//, tab='\t'; jpayne@68: jpayne@68: long a=1; jpayne@68: long b=2; jpayne@68: long c=3; jpayne@68: long d=4; jpayne@68: byte[] p0=null; jpayne@68: byte[] p1=null; jpayne@68: byte[] p2=null; jpayne@68: byte[] p3=null; jpayne@68: private byte[] pushBack=null; jpayne@68: private long nextID=0; jpayne@68: jpayne@68: private boolean open=false; jpayne@68: private byte[] buffer=new byte[bufferlen]; jpayne@68: private static final byte[] blankLine=new byte[0]; jpayne@68: private int bstart=0, bstop=0; jpayne@68: public InputStream is; jpayne@68: public long lineNum=-1; jpayne@68: jpayne@68: public static boolean verbose=false; jpayne@68: public static boolean BUFFERED=false; jpayne@68: public static int bufferlen=16384; jpayne@68: jpayne@68: private boolean errorState=false; jpayne@68: jpayne@68: }