| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.rangecoder; |
| 7 |
|
| 8 |
import java.io.DataInputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import org.tukaani.xz.ArrayCache; |
| 11 |
import org.tukaani.xz.CorruptedInputException; |
| 12 |
|
| 13 |
public final class RangeDecoderFromBuffer extends RangeDecoder { |
| 14 |
private static final int INIT_SIZE = 5; |
| 15 |
|
| 16 |
private final byte[] buf; |
| 17 |
private int pos; |
| 18 |
|
| 19 |
public RangeDecoderFromBuffer(int inputSizeMax, ArrayCache arrayCache) { |
| 20 |
|
| 21 |
|
| 22 |
buf = arrayCache.getByteArray(inputSizeMax - INIT_SIZE, false); |
| 23 |
pos = buf.length; |
| 24 |
} |
| 25 |
|
| 26 |
public void putArraysToCache(ArrayCache arrayCache) { |
| 27 |
arrayCache.putArray(buf); |
| 28 |
} |
| 29 |
|
| 30 |
public void prepareInputBuffer(DataInputStream in, int len) |
| 31 |
throws IOException { |
| 32 |
if (len < INIT_SIZE) |
| 33 |
throw new CorruptedInputException(); |
| 34 |
|
| 35 |
if (in.readUnsignedByte() != 0x00) |
| 36 |
throw new CorruptedInputException(); |
| 37 |
|
| 38 |
code = in.readInt(); |
| 39 |
range = 0xFFFFFFFF; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
len -= INIT_SIZE; |
| 46 |
pos = buf.length - len; |
| 47 |
in.readFully(buf, pos, len); |
| 48 |
} |
| 49 |
|
| 50 |
public boolean isFinished() { |
| 51 |
return pos == buf.length && code == 0; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void normalize() throws IOException { |
| 56 |
if ((range & TOP_MASK) == 0) { |
| 57 |
try { |
| 58 |
|
| 59 |
|
| 60 |
code = (code << SHIFT_BITS) | (buf[pos++] & 0xFF); |
| 61 |
range <<= SHIFT_BITS; |
| 62 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 63 |
throw new CorruptedInputException(); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |