aboutsummaryrefslogtreecommitdiff
path: root/src/org/tukaani
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2016-09-10 17:13:13 +0300
committerLasse Collin <lasse.collin@tukaani.org>2016-09-10 17:13:13 +0300
commit5843091485368cb576b9498fc9f2ec61d104b336 (patch)
treef187b8f8f20a0eecb1077b64dc5238033a9624a5 /src/org/tukaani
parentfb6a596f636249b0595598e81716bd1f70b48782 (diff)
downloadxz-java-5843091485368cb576b9498fc9f2ec61d104b336.tar.gz
Split RangeEncoderToBuffer from RangeEncoder.
Diffstat (limited to 'src/org/tukaani')
-rw-r--r--src/org/tukaani/xz/LZMA2OutputStream.java6
-rw-r--r--src/org/tukaani/xz/rangecoder/RangeEncoder.java33
-rw-r--r--src/org/tukaani/xz/rangecoder/RangeEncoderToBuffer.java47
3 files changed, 64 insertions, 22 deletions
diff --git a/src/org/tukaani/xz/LZMA2OutputStream.java b/src/org/tukaani/xz/LZMA2OutputStream.java
index 5724d10..23341b0 100644
--- a/src/org/tukaani/xz/LZMA2OutputStream.java
+++ b/src/org/tukaani/xz/LZMA2OutputStream.java
@@ -13,7 +13,7 @@ package org.tukaani.xz;
import java.io.DataOutputStream;
import java.io.IOException;
import org.tukaani.xz.lz.LZEncoder;
-import org.tukaani.xz.rangecoder.RangeEncoder;
+import org.tukaani.xz.rangecoder.RangeEncoderToBuffer;
import org.tukaani.xz.lzma.LZMAEncoder;
class LZMA2OutputStream extends FinishableOutputStream {
@@ -23,7 +23,7 @@ class LZMA2OutputStream extends FinishableOutputStream {
private final DataOutputStream outData;
private final LZEncoder lz;
- private final RangeEncoder rc;
+ private final RangeEncoderToBuffer rc;
private final LZMAEncoder lzma;
private final int props; // Cannot change props on the fly for now.
@@ -57,7 +57,7 @@ class LZMA2OutputStream extends FinishableOutputStream {
this.out = out;
outData = new DataOutputStream(out);
- rc = new RangeEncoder(COMPRESSED_SIZE_MAX);
+ rc = new RangeEncoderToBuffer(COMPRESSED_SIZE_MAX);
int dictSize = options.getDictSize();
int extraSizeBefore = getExtraSizeBefore(dictSize);
diff --git a/src/org/tukaani/xz/rangecoder/RangeEncoder.java b/src/org/tukaani/xz/rangecoder/RangeEncoder.java
index a06fdcc..0472104 100644
--- a/src/org/tukaani/xz/rangecoder/RangeEncoder.java
+++ b/src/org/tukaani/xz/rangecoder/RangeEncoder.java
@@ -10,10 +10,9 @@
package org.tukaani.xz.rangecoder;
-import java.io.OutputStream;
import java.io.IOException;
-public final class RangeEncoder extends RangeCoder {
+public abstract class RangeEncoder extends RangeCoder {
private static final int MOVE_REDUCING_BITS = 4;
private static final int BIT_PRICE_SHIFT_BITS = 4;
@@ -26,13 +25,10 @@ public final class RangeEncoder extends RangeCoder {
// NOTE: int is OK for LZMA2 because a compressed chunk
// is not more than 64 KiB, but with LZMA1 there is no chunking
// so in theory cacheSize can grow very big. To be very safe,
- // use long instead of int if you adapt this code for LZMA1.
- private int cacheSize;
+ // use long instead of int since this code is used for LZMA1 too.
+ long cacheSize;
private byte cache;
- private final byte[] buf;
- private int bufPos;
-
static {
for (int i = (1 << MOVE_REDUCING_BITS) / 2; i < BIT_MODEL_TOTAL;
i += (1 << MOVE_REDUCING_BITS)) {
@@ -55,33 +51,32 @@ public final class RangeEncoder extends RangeCoder {
}
}
- public RangeEncoder(int bufSize) {
- buf = new byte[bufSize];
- reset();
- }
-
public void reset() {
low = 0;
range = 0xFFFFFFFF;
cache = 0x00;
cacheSize = 1;
- bufPos = 0;
}
public int getPendingSize() {
- return bufPos + cacheSize + 5 - 1;
+ // This function is only needed by users of RangeEncoderToBuffer,
+ // but providing a must-be-never-called version here makes
+ // LZMAEncoder simpler.
+ throw new Error();
}
public int finish() {
for (int i = 0; i < 5; ++i)
shiftLow();
- return bufPos;
+ // RangeEncoderToBuffer.finish() needs a return value to tell
+ // how big the finished buffer is. RangeEncoderToStream has no
+ // buffer and thus no return value is needed. Here we use a dummy
+ // value which can be overriden in RangeEncoderToBuffer.finish().
+ return -1;
}
- public void write(OutputStream out) throws IOException {
- out.write(buf, 0, bufPos);
- }
+ abstract void writeByte(byte b) /*throws IOException*/;
private void shiftLow() {
int lowHi = (int)(low >>> 32);
@@ -90,7 +85,7 @@ public final class RangeEncoder extends RangeCoder {
int temp = cache;
do {
- buf[bufPos++] = (byte)(temp + lowHi);
+ writeByte((byte)(temp + lowHi));
temp = 0xFF;
} while (--cacheSize != 0);
diff --git a/src/org/tukaani/xz/rangecoder/RangeEncoderToBuffer.java b/src/org/tukaani/xz/rangecoder/RangeEncoderToBuffer.java
new file mode 100644
index 0000000..01fe24e
--- /dev/null
+++ b/src/org/tukaani/xz/rangecoder/RangeEncoderToBuffer.java
@@ -0,0 +1,47 @@
+/*
+ * RangeEncoderToBuffer
+ *
+ * Authors: Lasse Collin <lasse.collin@tukaani.org>
+ * Igor Pavlov <http://7-zip.org/>
+ *
+ * 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 RangeEncoderToBuffer extends RangeEncoder {
+ private final byte[] buf;
+ private int bufPos;
+
+ public RangeEncoderToBuffer(int bufSize) {
+ buf = new byte[bufSize];
+ reset();
+ }
+
+ public void reset() {
+ super.reset();
+ bufPos = 0;
+ }
+
+ public int getPendingSize() {
+ // With LZMA2 it is known that cacheSize fits into an int.
+ return bufPos + (int)cacheSize + 5 - 1;
+ }
+
+ public int finish() {
+ super.finish();
+ return bufPos;
+ }
+
+ public void write(OutputStream out) throws IOException {
+ out.write(buf, 0, bufPos);
+ }
+
+ void writeByte(byte b) {
+ buf[bufPos++] = b;
+ }
+}