jpayne@68: package fileIO; jpayne@68: import java.io.File; jpayne@68: import java.io.InputStream; jpayne@68: import java.util.ArrayList; jpayne@68: jpayne@68: import shared.Shared; jpayne@68: import structures.ListNum; jpayne@68: jpayne@68: jpayne@68: public abstract class ByteFile { jpayne@68: jpayne@68: // public static final ByteFile makeByteFile(String fname){ jpayne@68: // return makeByteFile(fname, false, true); jpayne@68: // } jpayne@68: jpayne@68: public static final ByteFile makeByteFile1(String fname, boolean allowSubprocess){ jpayne@68: FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess, false); jpayne@68: return new ByteFile1(ff); jpayne@68: } jpayne@68: jpayne@68: public static final ByteFile makeByteFile(String fname, boolean allowSubprocess){ jpayne@68: FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess, false); jpayne@68: return makeByteFile(ff); jpayne@68: } jpayne@68: jpayne@68: public static final ByteFile makeByteFile(FileFormat ff){ jpayne@68: return makeByteFile(ff, 0); jpayne@68: } jpayne@68: jpayne@68: public static final ByteFile makeByteFile(FileFormat ff, int type){ jpayne@68: if(type==1){return new ByteFile1(ff);} jpayne@68: if(type==2){return new ByteFile2(ff);} jpayne@68: if(!Shared.LOW_MEMORY && (FORCE_MODE_BF2 || (!FORCE_MODE_BF1 && Shared.threads()>4/* && (ReadWrite.isCompressed(fname) || ReadWrite.isSam(fname))*/))){ jpayne@68: // if(allowSubprocess && ((ReadWrite.USE_UNPIGZ || ReadWrite.USE_GUNZIP) && (fname.endsWith(".gz") || fname.endsWith(".gzip")))){} jpayne@68: return new ByteFile2(ff); jpayne@68: } jpayne@68: // if(FORCE_MODE_BF3){return new QuickFile(ff);} jpayne@68: return new ByteFile1(ff); jpayne@68: } jpayne@68: jpayne@68: protected ByteFile(FileFormat ff_){ jpayne@68: ff=ff_; jpayne@68: assert(ff.read()) : ff; jpayne@68: } jpayne@68: jpayne@68: public final ArrayList 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 static final ArrayList toLines(FileFormat ff){ jpayne@68: ByteFile bf=makeByteFile(ff); jpayne@68: ArrayList lines=bf.toByteLines(); jpayne@68: bf.close(); jpayne@68: return lines; jpayne@68: } jpayne@68: jpayne@68: public static final ArrayList toLines(String fname){ jpayne@68: FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, true, false); jpayne@68: return toLines(ff); 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 abstract void reset(); jpayne@68: final void superReset(){ jpayne@68: nextID=0; 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 abstract InputStream is(); jpayne@68: public abstract long lineNum(); jpayne@68: jpayne@68: /** Returns true if there was an error */ jpayne@68: public abstract boolean close(); jpayne@68: jpayne@68: public abstract byte[] nextLine(); 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 abstract void pushBack(byte[] line); jpayne@68: jpayne@68: public abstract boolean isOpen(); 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: /** Force usage of ByteFile1 */ jpayne@68: public static boolean FORCE_MODE_BF1=false;//!(Data.GENEPOOL || Data.DENOVO || Data.CORI || Shared.WINDOWS); jpayne@68: jpayne@68: /** Force usage of ByteFile2 */ jpayne@68: public static boolean FORCE_MODE_BF2=false; jpayne@68: jpayne@68: /** Unused */ jpayne@68: @Deprecated 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: // byte[] pushBack=null; jpayne@68: private long nextID=0; jpayne@68: jpayne@68: }