| 1 |
package template; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteFile1; |
| 7 |
import fileIO.ByteFile2; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import structures.ByteBuilder; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class A_SampleBasic2 { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
|
| 38 |
Timer t=new Timer(); |
| 39 |
|
| 40 |
|
| 41 |
A_SampleBasic2 x=new A_SampleBasic2(args); |
| 42 |
|
| 43 |
|
| 44 |
x.process(t); |
| 45 |
|
| 46 |
|
| 47 |
Shared.closeStream(x.outstream); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public A_SampleBasic2(String[] args){ |
| 55 |
|
| 56 |
{ |
| 57 |
PreParser pp=new PreParser(args, null, false); |
| 58 |
args=pp.args; |
| 59 |
outstream=pp.outstream; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
{ |
| 65 |
final Parser parser=parse(args); |
| 66 |
parser.out1="stdout.txt"; |
| 67 |
overwrite=parser.overwrite; |
| 68 |
append=parser.append; |
| 69 |
|
| 70 |
out=parser.out1; |
| 71 |
} |
| 72 |
|
| 73 |
checkFileExistence(); |
| 74 |
checkStatics(); |
| 75 |
|
| 76 |
ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private Parser parse(String[] args){ |
| 85 |
|
| 86 |
Parser parser=new Parser(); |
| 87 |
for(int i=0; i<args.length; i++){ |
| 88 |
String arg=args[i]; |
| 89 |
String[] split=arg.split("="); |
| 90 |
String a=split[0].toLowerCase(); |
| 91 |
String b=split.length>1 ? split[1] : null; |
| 92 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 93 |
|
| 94 |
if(a.equals("verbose")){ |
| 95 |
verbose=Parse.parseBoolean(b); |
| 96 |
ByteFile1.verbose=verbose; |
| 97 |
ByteFile2.verbose=verbose; |
| 98 |
ReadWrite.verbose=verbose; |
| 99 |
}else if(parser.parse(arg, a, b)){ |
| 100 |
|
| 101 |
}else{ |
| 102 |
outstream.println("Unknown parameter "+args[i]); |
| 103 |
assert(false) : "Unknown parameter "+args[i]; |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return parser; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
private void checkFileExistence(){ |
| 113 |
|
| 114 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 115 |
outstream.println((out==null)+", "+out); |
| 116 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
private static void checkStatics(){ |
| 122 |
|
| 123 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 124 |
ByteFile.FORCE_MODE_BF2=true; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
void process(Timer t){ |
| 133 |
|
| 134 |
ByteStreamWriter bsw=makeBSW(ffout); |
| 135 |
|
| 136 |
|
| 137 |
if(bsw!=null){ |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
processInner(bsw); |
| 142 |
|
| 143 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 144 |
|
| 145 |
t.stop(); |
| 146 |
|
| 147 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesOut, bytesOut, 8)); |
| 148 |
outstream.println(); |
| 149 |
|
| 150 |
if(errorState){ |
| 151 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private void processInner(ByteStreamWriter bsw){ |
| 160 |
ByteBuilder bb=new ByteBuilder(); |
| 161 |
|
| 162 |
for(long cycle=0; cycle<maxCycles; cycle++){ |
| 163 |
doSomething(bsw, bb, cycle); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 168 |
if(ff==null){return null;} |
| 169 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 170 |
bsw.start(); |
| 171 |
return bsw; |
| 172 |
} |
| 173 |
|
| 174 |
private boolean doSomething(ByteStreamWriter bsw, ByteBuilder bb, long cycle){ |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private String out=null; |
| 200 |
private long maxCycles=100; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private long linesOut=0; |
| 205 |
private long bytesOut=0; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private final FileFormat ffout; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private PrintStream outstream=System.err; |
| 218 |
public static boolean verbose=false; |
| 219 |
public boolean errorState=false; |
| 220 |
private boolean overwrite=true; |
| 221 |
private boolean append=false; |
| 222 |
|
| 223 |
} |