1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
package fileIO;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import shared.Shared;
import structures.ListNum;
public abstract class ByteFile {
// public static final ByteFile makeByteFile(String fname){
// return makeByteFile(fname, false, true);
// }
public static final ByteFile makeByteFile1(String fname, boolean allowSubprocess){
FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess, false);
return new ByteFile1(ff);
}
public static final ByteFile makeByteFile(String fname, boolean allowSubprocess){
FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess, false);
return makeByteFile(ff);
}
public static final ByteFile makeByteFile(FileFormat ff){
return makeByteFile(ff, 0);
}
public static final ByteFile makeByteFile(FileFormat ff, int type){
if(type==1){return new ByteFile1(ff);}
if(type==2){return new ByteFile2(ff);}
if(!Shared.LOW_MEMORY && (FORCE_MODE_BF2 || (!FORCE_MODE_BF1 && Shared.threads()>4/* && (ReadWrite.isCompressed(fname) || ReadWrite.isSam(fname))*/))){
// if(allowSubprocess && ((ReadWrite.USE_UNPIGZ || ReadWrite.USE_GUNZIP) && (fname.endsWith(".gz") || fname.endsWith(".gzip")))){}
return new ByteFile2(ff);
}
// if(FORCE_MODE_BF3){return new QuickFile(ff);}
return new ByteFile1(ff);
}
protected ByteFile(FileFormat ff_){
ff=ff_;
assert(ff.read()) : ff;
}
public final ArrayList<byte[]> toByteLines(){
byte[] s=null;
ArrayList<byte[]> list=new ArrayList<byte[]>(4096);
for(s=nextLine(); s!=null; s=nextLine()){
list.add(s);
}
return list;
}
public static final ArrayList<byte[]> toLines(FileFormat ff){
ByteFile bf=makeByteFile(ff);
ArrayList<byte[]> lines=bf.toByteLines();
bf.close();
return lines;
}
public static final ArrayList<byte[]> toLines(String fname){
FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, true, false);
return toLines(ff);
}
public final long countLines(){
byte[] s=null;
long count=0;
for(s=nextLine(); s!=null; s=nextLine()){count++;}
reset();
return count;
}
public abstract void reset();
final void superReset(){
nextID=0;
}
public synchronized final ListNum<byte[]> nextList(){
byte[] line=nextLine();
if(line==null){return null;}
ArrayList<byte[]> list=new ArrayList<byte[]>(200);
list.add(line);
for(int i=1; i<200; i++){
line=nextLine();
if(line==null){break;}
list.add(line);
}
ListNum<byte[]> ln=new ListNum<byte[]>(list, nextID);
nextID++;
return ln;
}
public final boolean exists(){
return name().equals("stdin") || name().startsWith("stdin.") || name().startsWith("jar:") || new File(name()).exists(); //TODO Ugly and unsafe hack for files in jars
}
public abstract InputStream is();
public abstract long lineNum();
/** Returns true if there was an error */
public abstract boolean close();
public abstract byte[] nextLine();
// public final void pushBack(byte[] line){
// assert(pushBack==null);
// pushBack=line;
// }
public abstract void pushBack(byte[] line);
public abstract boolean isOpen();
public final String name(){return ff.name();}
public final boolean allowSubprocess(){return ff.allowSubprocess();}
public final FileFormat ff;
/** Force usage of ByteFile1 */
public static boolean FORCE_MODE_BF1=false;//!(Data.GENEPOOL || Data.DENOVO || Data.CORI || Shared.WINDOWS);
/** Force usage of ByteFile2 */
public static boolean FORCE_MODE_BF2=false;
/** Unused */
@Deprecated
public static boolean FORCE_MODE_BF3=false;
protected final static byte slashr='\r', slashn='\n', carrot='>', plus='+', at='@';//, tab='\t';
// byte[] pushBack=null;
private long nextID=0;
}
|