This commit is contained in:
huanghongxun
2015-11-17 12:51:04 +08:00
parent 39b6d68006
commit 159a992081
243 changed files with 2990 additions and 2836 deletions

View File

@@ -7,17 +7,17 @@
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package org.tukaani.xz.rangecoder;
import java.util.Arrays;
public abstract class RangeCoder {
static final int SHIFT_BITS = 8;
static final int TOP_MASK = 0xFF000000;
static final int BIT_MODEL_TOTAL_BITS = 11;
static final int BIT_MODEL_TOTAL = 1 << BIT_MODEL_TOTAL_BITS;
static final short PROB_INIT = (short)(BIT_MODEL_TOTAL / 2);
static final short PROB_INIT = (short) (BIT_MODEL_TOTAL / 2);
static final int MOVE_BITS = 5;
public static final void initProbs(short[] probs) {

View File

@@ -7,13 +7,13 @@
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package org.tukaani.xz.rangecoder;
import java.io.DataInputStream;
import java.io.IOException;
public abstract class RangeDecoder extends RangeCoder {
int range = 0;
int code = 0;
@@ -29,13 +29,12 @@ public abstract class RangeDecoder extends RangeCoder {
// Compare code and bound as if they were unsigned 32-bit integers.
if ((code ^ 0x80000000) < (bound ^ 0x80000000)) {
range = bound;
probs[index] = (short)(
prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS));
probs[index] = (short) (prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS));
bit = 0;
} else {
range -= bound;
code -= bound;
probs[index] = (short)(prob - (prob >>> MOVE_BITS));
probs[index] = (short) (prob - (prob >>> MOVE_BITS));
bit = 1;
}
@@ -45,9 +44,9 @@ public abstract class RangeDecoder extends RangeCoder {
public int decodeBitTree(short[] probs) throws IOException {
int symbol = 1;
do {
do
symbol = (symbol << 1) | decodeBit(probs, symbol);
} while (symbol < probs.length);
while (symbol < probs.length);
return symbol - probs.length;
}

View File

@@ -7,7 +7,6 @@
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package org.tukaani.xz.rangecoder;
import java.io.DataInputStream;
@@ -15,6 +14,7 @@ import java.io.IOException;
import org.tukaani.xz.CorruptedInputException;
public final class RangeDecoderFromBuffer extends RangeDecoder {
private static final int INIT_SIZE = 5;
private final byte[] buf;
@@ -26,7 +26,7 @@ public final class RangeDecoderFromBuffer extends RangeDecoder {
}
public void prepareInputBuffer(DataInputStream in, int len)
throws IOException {
throws IOException {
if (len < INIT_SIZE)
throw new CorruptedInputException();
@@ -50,7 +50,7 @@ public final class RangeDecoderFromBuffer extends RangeDecoder {
}
public void normalize() throws IOException {
if ((range & TOP_MASK) == 0) {
if ((range & TOP_MASK) == 0)
try {
// If the input is corrupt, this might throw
// ArrayIndexOutOfBoundsException.
@@ -59,6 +59,5 @@ public final class RangeDecoderFromBuffer extends RangeDecoder {
} catch (ArrayIndexOutOfBoundsException e) {
throw new CorruptedInputException();
}
}
}
}

View File

@@ -7,7 +7,6 @@
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package org.tukaani.xz.rangecoder;
import java.io.InputStream;
@@ -16,6 +15,7 @@ import java.io.IOException;
import org.tukaani.xz.CorruptedInputException;
public final class RangeDecoderFromStream extends RangeDecoder {
private final DataInputStream inData;
public RangeDecoderFromStream(InputStream in) throws IOException {

View File

@@ -7,18 +7,18 @@
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package org.tukaani.xz.rangecoder;
import java.io.OutputStream;
import java.io.IOException;
public final class RangeEncoder extends RangeCoder {
private static final int MOVE_REDUCING_BITS = 4;
private static final int BIT_PRICE_SHIFT_BITS = 4;
private static final int[] prices
= new int[BIT_MODEL_TOTAL >>> MOVE_REDUCING_BITS];
= new int[BIT_MODEL_TOTAL >>> MOVE_REDUCING_BITS];
private long low;
private int range;
@@ -35,7 +35,7 @@ public final class RangeEncoder extends RangeCoder {
static {
for (int i = (1 << MOVE_REDUCING_BITS) / 2; i < BIT_MODEL_TOTAL;
i += (1 << MOVE_REDUCING_BITS)) {
i += (1 << MOVE_REDUCING_BITS)) {
int w = i;
int bitCount = 0;
@@ -50,8 +50,8 @@ public final class RangeEncoder extends RangeCoder {
}
prices[i >> MOVE_REDUCING_BITS]
= (BIT_MODEL_TOTAL_BITS << BIT_PRICE_SHIFT_BITS)
- 15 - bitCount;
= (BIT_MODEL_TOTAL_BITS << BIT_PRICE_SHIFT_BITS)
- 15 - bitCount;
}
}
@@ -84,17 +84,17 @@ public final class RangeEncoder extends RangeCoder {
}
private void shiftLow() {
int lowHi = (int)(low >>> 32);
int lowHi = (int) (low >>> 32);
if (lowHi != 0 || low < 0xFF000000L) {
int temp = cache;
do {
buf[bufPos++] = (byte)(temp + lowHi);
buf[bufPos++] = (byte) (temp + lowHi);
temp = 0xFF;
} while (--cacheSize != 0);
cache = (byte)(low >>> 24);
cache = (byte) (low >>> 24);
}
++cacheSize;
@@ -108,12 +108,11 @@ public final class RangeEncoder extends RangeCoder {
// NOTE: Any non-zero value for bit is taken as 1.
if (bit == 0) {
range = bound;
probs[index] = (short)(
prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS));
probs[index] = (short) (prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS));
} else {
low += bound & 0xFFFFFFFFL;
range -= bound;
probs[index] = (short)(prob - (prob >>> MOVE_BITS));
probs[index] = (short) (prob - (prob >>> MOVE_BITS));
}
if ((range & TOP_MASK) == 0) {