| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 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 |
|
| 26 |
public static final ByteFile makeByteFile(FileFormat ff){ |
| 27 |
return makeByteFile(ff, 0); |
| 28 |
} |
| 29 |
|
| 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))){ |
| 34 |
|
| 35 |
return new ByteFile2(ff); |
| 36 |
} |
| 37 |
|
| 38 |
return new ByteFile1(ff); |
| 39 |
} |
| 40 |
|
| 41 |
protected ByteFile(FileFormat ff_){ |
| 42 |
ff=ff_; |
| 43 |
assert(ff.read()) : ff; |
| 44 |
} |
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 79 |
public abstract void reset(); |
| 80 |
final void superReset(){ |
| 81 |
nextID=0; |
| 82 |
} |
| 83 |
|
| 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 |
|
| 99 |
public final boolean exists(){ |
| 100 |
return name().equals("stdin") || name().startsWith("stdin.") || name().startsWith("jar:") || new File(name()).exists(); |
| 101 |
} |
| 102 |
|
| 103 |
public abstract InputStream is(); |
| 104 |
public abstract long lineNum(); |
| 105 |
|
| 106 |
|
| 107 |
public abstract boolean close(); |
| 108 |
|
| 109 |
public abstract byte[] nextLine(); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public abstract void pushBack(byte[] line); |
| 117 |
|
| 118 |
public abstract boolean isOpen(); |
| 119 |
|
| 120 |
public final String name(){return ff.name();} |
| 121 |
public final boolean allowSubprocess(){return ff.allowSubprocess();} |
| 122 |
|
| 123 |
public final FileFormat ff; |
| 124 |
|
| 125 |
|
| 126 |
public static boolean FORCE_MODE_BF1=false; |
| 127 |
|
| 128 |
|
| 129 |
public static boolean FORCE_MODE_BF2=false; |
| 130 |
|
| 131 |
|
| 132 |
@Deprecated |
| 133 |
public static boolean FORCE_MODE_BF3=false; |
| 134 |
|
| 135 |
protected final static byte slashr='\r', slashn='\n', carrot='>', plus='+', at='@'; |
| 136 |
|
| 137 |
|
| 138 |
private long nextID=0; |
| 139 |
|
| 140 |
} |