| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz; |
| 7 |
|
| 8 |
import java.io.InputStream; |
| 9 |
import java.io.DataInputStream; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.EOFException; |
| 12 |
import org.tukaani.xz.lz.LZDecoder; |
| 13 |
import org.tukaani.xz.rangecoder.RangeDecoderFromBuffer; |
| 14 |
import org.tukaani.xz.lzma.LZMADecoder; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class LZMA2InputStream extends InputStream { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public static final int DICT_SIZE_MIN = 4096; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static final int DICT_SIZE_MAX = Integer.MAX_VALUE & ~15; |
| 39 |
|
| 40 |
private static final int COMPRESSED_SIZE_MAX = 1 << 16; |
| 41 |
|
| 42 |
private final ArrayCache arrayCache; |
| 43 |
private DataInputStream in; |
| 44 |
|
| 45 |
private LZDecoder lz; |
| 46 |
private RangeDecoderFromBuffer rc; |
| 47 |
private LZMADecoder lzma; |
| 48 |
|
| 49 |
private int uncompressedSize = 0; |
| 50 |
private boolean isLZMAChunk = false; |
| 51 |
|
| 52 |
private boolean needDictReset = true; |
| 53 |
private boolean needProps = true; |
| 54 |
private boolean endReached = false; |
| 55 |
|
| 56 |
private IOException exception = null; |
| 57 |
|
| 58 |
private final byte[] tempBuf = new byte[1]; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static int getMemoryUsage(int dictSize) { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
return 40 + COMPRESSED_SIZE_MAX / 1024 + getDictSize(dictSize) / 1024; |
| 75 |
} |
| 76 |
|
| 77 |
private static int getDictSize(int dictSize) { |
| 78 |
if (dictSize < DICT_SIZE_MIN || dictSize > DICT_SIZE_MAX) |
| 79 |
throw new IllegalArgumentException( |
| 80 |
"Unsupported dictionary size " + dictSize); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
return (dictSize + 15) & ~15; |
| 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 |
public LZMA2InputStream(InputStream in, int dictSize) { |
| 113 |
this(in, dictSize, null); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public LZMA2InputStream(InputStream in, int dictSize, byte[] presetDict) { |
| 135 |
this(in, dictSize, presetDict, ArrayCache.getDefaultCache()); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
LZMA2InputStream(InputStream in, int dictSize, byte[] presetDict, |
| 160 |
ArrayCache arrayCache) { |
| 161 |
|
| 162 |
|
| 163 |
if (in == null) |
| 164 |
throw new NullPointerException(); |
| 165 |
|
| 166 |
this.arrayCache = arrayCache; |
| 167 |
this.in = new DataInputStream(in); |
| 168 |
this.rc = new RangeDecoderFromBuffer(COMPRESSED_SIZE_MAX, arrayCache); |
| 169 |
this.lz = new LZDecoder(getDictSize(dictSize), presetDict, arrayCache); |
| 170 |
|
| 171 |
if (presetDict != null && presetDict.length > 0) |
| 172 |
needDictReset = false; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@Override |
| 195 |
public int read() throws IOException { |
| 196 |
return read(tempBuf, 0, 1) == -1 ? -1 : (tempBuf[0] & 0xFF); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@Override |
| 224 |
public int read(byte[] buf, int off, int len) throws IOException { |
| 225 |
if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) |
| 226 |
throw new IndexOutOfBoundsException(); |
| 227 |
|
| 228 |
if (len == 0) |
| 229 |
return 0; |
| 230 |
|
| 231 |
if (in == null) |
| 232 |
throw new XZIOException("Stream closed"); |
| 233 |
|
| 234 |
if (exception != null) |
| 235 |
throw exception; |
| 236 |
|
| 237 |
if (endReached) |
| 238 |
return -1; |
| 239 |
|
| 240 |
try { |
| 241 |
int size = 0; |
| 242 |
|
| 243 |
while (len > 0) { |
| 244 |
if (uncompressedSize == 0) { |
| 245 |
decodeChunkHeader(); |
| 246 |
if (endReached) |
| 247 |
return size == 0 ? -1 : size; |
| 248 |
} |
| 249 |
|
| 250 |
int copySizeMax = Math.min(uncompressedSize, len); |
| 251 |
|
| 252 |
if (!isLZMAChunk) { |
| 253 |
lz.copyUncompressed(in, copySizeMax); |
| 254 |
} else { |
| 255 |
lz.setLimit(copySizeMax); |
| 256 |
lzma.decode(); |
| 257 |
} |
| 258 |
|
| 259 |
int copiedSize = lz.flush(buf, off); |
| 260 |
off += copiedSize; |
| 261 |
len -= copiedSize; |
| 262 |
size += copiedSize; |
| 263 |
uncompressedSize -= copiedSize; |
| 264 |
|
| 265 |
if (uncompressedSize == 0) |
| 266 |
if (!rc.isFinished() || lz.hasPending()) |
| 267 |
throw new CorruptedInputException(); |
| 268 |
} |
| 269 |
|
| 270 |
return size; |
| 271 |
|
| 272 |
} catch (IOException e) { |
| 273 |
exception = e; |
| 274 |
throw e; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
private void decodeChunkHeader() throws IOException { |
| 279 |
int control = in.readUnsignedByte(); |
| 280 |
|
| 281 |
if (control == 0x00) { |
| 282 |
endReached = true; |
| 283 |
putArraysToCache(); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
if (control >= 0xE0 || control == 0x01) { |
| 288 |
needProps = true; |
| 289 |
needDictReset = false; |
| 290 |
lz.reset(); |
| 291 |
} else if (needDictReset) { |
| 292 |
throw new CorruptedInputException(); |
| 293 |
} |
| 294 |
|
| 295 |
if (control >= 0x80) { |
| 296 |
isLZMAChunk = true; |
| 297 |
|
| 298 |
uncompressedSize = (control & 0x1F) << 16; |
| 299 |
uncompressedSize += in.readUnsignedShort() + 1; |
| 300 |
|
| 301 |
int compressedSize = in.readUnsignedShort() + 1; |
| 302 |
|
| 303 |
if (control >= 0xC0) { |
| 304 |
needProps = false; |
| 305 |
decodeProps(); |
| 306 |
|
| 307 |
} else if (needProps) { |
| 308 |
throw new CorruptedInputException(); |
| 309 |
|
| 310 |
} else if (control >= 0xA0) { |
| 311 |
lzma.reset(); |
| 312 |
} |
| 313 |
|
| 314 |
rc.prepareInputBuffer(in, compressedSize); |
| 315 |
|
| 316 |
} else if (control > 0x02) { |
| 317 |
throw new CorruptedInputException(); |
| 318 |
|
| 319 |
} else { |
| 320 |
isLZMAChunk = false; |
| 321 |
uncompressedSize = in.readUnsignedShort() + 1; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
private void decodeProps() throws IOException { |
| 326 |
int props = in.readUnsignedByte(); |
| 327 |
|
| 328 |
if (props > (4 * 5 + 4) * 9 + 8) |
| 329 |
throw new CorruptedInputException(); |
| 330 |
|
| 331 |
int pb = props / (9 * 5); |
| 332 |
props -= pb * 9 * 5; |
| 333 |
int lp = props / 9; |
| 334 |
int lc = props - lp * 9; |
| 335 |
|
| 336 |
if (lc + lp > 4) |
| 337 |
throw new CorruptedInputException(); |
| 338 |
|
| 339 |
lzma = new LZMADecoder(lz, rc, lc, lp, pb); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
@Override |
| 361 |
public int available() throws IOException { |
| 362 |
if (in == null) |
| 363 |
throw new XZIOException("Stream closed"); |
| 364 |
|
| 365 |
if (exception != null) |
| 366 |
throw exception; |
| 367 |
|
| 368 |
return isLZMAChunk ? uncompressedSize |
| 369 |
: Math.min(uncompressedSize, in.available()); |
| 370 |
} |
| 371 |
|
| 372 |
private void putArraysToCache() { |
| 373 |
if (lz != null) { |
| 374 |
lz.putArraysToCache(arrayCache); |
| 375 |
lz = null; |
| 376 |
|
| 377 |
rc.putArraysToCache(arrayCache); |
| 378 |
rc = null; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
@Override |
| 389 |
public void close() throws IOException { |
| 390 |
if (in != null) { |
| 391 |
putArraysToCache(); |
| 392 |
|
| 393 |
try { |
| 394 |
in.close(); |
| 395 |
} finally { |
| 396 |
in = null; |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |