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