| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.io.PrintStream; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.ByteFile1; |
| 9 |
import fileIO.ByteFile2; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class Unzip { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static void main(String[] args){ |
| 36 |
|
| 37 |
Timer t=new Timer(); |
| 38 |
|
| 39 |
|
| 40 |
Unzip x=new Unzip(args); |
| 41 |
|
| 42 |
|
| 43 |
x.process(t); |
| 44 |
|
| 45 |
|
| 46 |
Shared.closeStream(x.outstream); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public Unzip(String[] args){ |
| 54 |
|
| 55 |
{ |
| 56 |
PreParser pp=new PreParser(args, null, false); |
| 57 |
args=pp.args; |
| 58 |
outstream=pp.outstream; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 63 |
ReadWrite.setZipThreads(Shared.threads()); |
| 64 |
|
| 65 |
{ |
| 66 |
final Parser parser=parse(args); |
| 67 |
overwrite=parser.overwrite; |
| 68 |
append=parser.append; |
| 69 |
|
| 70 |
in1=parser.in1; |
| 71 |
|
| 72 |
out1=parser.out1; |
| 73 |
} |
| 74 |
|
| 75 |
fixExtensions(); |
| 76 |
checkFileExistence(); |
| 77 |
checkStatics(); |
| 78 |
|
| 79 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 80 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false); |
| 81 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private Parser parse(String[] args){ |
| 90 |
|
| 91 |
Parser parser=new Parser(); |
| 92 |
parser.overwrite=true; |
| 93 |
for(int i=0; i<args.length; i++){ |
| 94 |
String arg=args[i]; |
| 95 |
String[] split=arg.split("="); |
| 96 |
String a=split[0].toLowerCase(); |
| 97 |
String b=split.length>1 ? split[1] : null; |
| 98 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 99 |
|
| 100 |
if(a.equals("invalid")){ |
| 101 |
outInvalid=b; |
| 102 |
}else if(a.equals("lines")){ |
| 103 |
maxLines=Long.parseLong(b); |
| 104 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 105 |
}else if(a.equals("verbose")){ |
| 106 |
verbose=Parse.parseBoolean(b); |
| 107 |
ByteFile1.verbose=verbose; |
| 108 |
ByteFile2.verbose=verbose; |
| 109 |
ReadWrite.verbose=verbose; |
| 110 |
}else if(parser.parse(arg, a, b)){ |
| 111 |
|
| 112 |
}else{ |
| 113 |
outstream.println("Unknown parameter "+args[i]); |
| 114 |
assert(false) : "Unknown parameter "+args[i]; |
| 115 |
|
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return parser; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
private void fixExtensions(){ |
| 124 |
in1=Tools.fixExtension(in1); |
| 125 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
private void checkFileExistence(){ |
| 130 |
|
| 131 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 132 |
outstream.println((out1==null)+", "+out1); |
| 133 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 138 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
if(!Tools.testForDuplicateFiles(true, in1, out1)){ |
| 143 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
private static void checkStatics(){ |
| 149 |
|
| 150 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 151 |
ByteFile.FORCE_MODE_BF2=true; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void process(Timer t){ |
| 165 |
|
| 166 |
InputStream is=ReadWrite.getInputStream(ffin1.name(), false, true); |
| 167 |
ByteStreamWriter bsw=makeBSW(ffout1); |
| 168 |
|
| 169 |
|
| 170 |
if(bsw!=null){ |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
processInner(is, bsw); |
| 175 |
|
| 176 |
try { |
| 177 |
is.close(); |
| 178 |
} catch (IOException e) { |
| 179 |
errorState=true; |
| 180 |
e.printStackTrace(); |
| 181 |
} |
| 182 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 183 |
|
| 184 |
t.stop(); |
| 185 |
|
| 186 |
if(showSpeed){ |
| 187 |
outstream.println(Tools.timeLinesBytesProcessed(t, 1, bytesProcessed, 8)); |
| 188 |
|
| 189 |
outstream.println(); |
| 190 |
outstream.println("Bytes Processed: \t"+bytesProcessed); |
| 191 |
} |
| 192 |
|
| 193 |
if(errorState){ |
| 194 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private void processInner(InputStream is, ByteStreamWriter bsw){ |
| 203 |
final byte[] buffer=new byte[65536<<2]; |
| 204 |
|
| 205 |
int len=0; |
| 206 |
try { |
| 207 |
len = is.read(buffer); |
| 208 |
} catch (IOException e) { |
| 209 |
errorState=true; |
| 210 |
e.printStackTrace(); |
| 211 |
} |
| 212 |
while(len>0){ |
| 213 |
bytesProcessed+=len; |
| 214 |
bsw.print(buffer, len); |
| 215 |
len=0; |
| 216 |
try { |
| 217 |
len=is.read(buffer); |
| 218 |
} catch (IOException e) { |
| 219 |
errorState=true; |
| 220 |
e.printStackTrace(); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 226 |
if(ff==null){return null;} |
| 227 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 228 |
bsw.start(); |
| 229 |
return bsw; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private String in1=null; |
| 237 |
private String out1=null; |
| 238 |
private String outInvalid=null; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private long bytesProcessed=0; |
| 243 |
|
| 244 |
private long maxLines=Long.MAX_VALUE; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private final FileFormat ffin1; |
| 251 |
private final FileFormat ffout1; |
| 252 |
private final FileFormat ffoutInvalid; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private PrintStream outstream=System.err; |
| 259 |
public static boolean verbose=false; |
| 260 |
public boolean errorState=false; |
| 261 |
public boolean showSpeed=false; |
| 262 |
private boolean overwrite=true; |
| 263 |
private boolean append=false; |
| 264 |
|
| 265 |
} |