| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.lz; |
| 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 LZDecoder { |
| 14 |
private final byte[] buf; |
| 15 |
private final int bufSize; |
| 16 |
private int start = 0; |
| 17 |
private int pos = 0; |
| 18 |
private int full = 0; |
| 19 |
private int limit = 0; |
| 20 |
private int pendingLen = 0; |
| 21 |
private int pendingDist = 0; |
| 22 |
|
| 23 |
public LZDecoder(int dictSize, byte[] presetDict, ArrayCache arrayCache) { |
| 24 |
bufSize = dictSize; |
| 25 |
buf = arrayCache.getByteArray(bufSize, false); |
| 26 |
|
| 27 |
if (presetDict != null) { |
| 28 |
pos = Math.min(presetDict.length, dictSize); |
| 29 |
full = pos; |
| 30 |
start = pos; |
| 31 |
System.arraycopy(presetDict, presetDict.length - pos, buf, 0, pos); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public void putArraysToCache(ArrayCache arrayCache) { |
| 36 |
arrayCache.putArray(buf); |
| 37 |
} |
| 38 |
|
| 39 |
public void reset() { |
| 40 |
start = 0; |
| 41 |
pos = 0; |
| 42 |
full = 0; |
| 43 |
limit = 0; |
| 44 |
buf[bufSize - 1] = 0x00; |
| 45 |
} |
| 46 |
|
| 47 |
public void setLimit(int outMax) { |
| 48 |
if (bufSize - pos <= outMax) |
| 49 |
limit = bufSize; |
| 50 |
else |
| 51 |
limit = pos + outMax; |
| 52 |
} |
| 53 |
|
| 54 |
public boolean hasSpace() { |
| 55 |
return pos < limit; |
| 56 |
} |
| 57 |
|
| 58 |
public boolean hasPending() { |
| 59 |
return pendingLen > 0; |
| 60 |
} |
| 61 |
|
| 62 |
public int getPos() { |
| 63 |
return pos; |
| 64 |
} |
| 65 |
|
| 66 |
public int getByte(int dist) { |
| 67 |
int offset = pos - dist - 1; |
| 68 |
if (dist >= pos) |
| 69 |
offset += bufSize; |
| 70 |
|
| 71 |
return buf[offset] & 0xFF; |
| 72 |
} |
| 73 |
|
| 74 |
public void putByte(byte b) { |
| 75 |
buf[pos++] = b; |
| 76 |
|
| 77 |
if (full < pos) |
| 78 |
full = pos; |
| 79 |
} |
| 80 |
|
| 81 |
public void repeat(int dist, int len) throws IOException { |
| 82 |
if (dist < 0 || dist >= full) |
| 83 |
throw new CorruptedInputException(); |
| 84 |
|
| 85 |
int left = Math.min(limit - pos, len); |
| 86 |
pendingLen = len - left; |
| 87 |
pendingDist = dist; |
| 88 |
|
| 89 |
int back = pos - dist - 1; |
| 90 |
if (back < 0) { |
| 91 |
|
| 92 |
|
| 93 |
assert full == bufSize; |
| 94 |
back += bufSize; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
int copySize = Math.min(bufSize - back, left); |
| 100 |
assert copySize <= dist + 1; |
| 101 |
|
| 102 |
System.arraycopy(buf, back, buf, pos, copySize); |
| 103 |
pos += copySize; |
| 104 |
back = 0; |
| 105 |
left -= copySize; |
| 106 |
|
| 107 |
if (left == 0) |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
assert back < pos; |
| 112 |
assert left > 0; |
| 113 |
|
| 114 |
do { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int copySize = Math.min(left, pos - back); |
| 123 |
System.arraycopy(buf, back, buf, pos, copySize); |
| 124 |
pos += copySize; |
| 125 |
left -= copySize; |
| 126 |
} while (left > 0); |
| 127 |
|
| 128 |
if (full < pos) |
| 129 |
full = pos; |
| 130 |
} |
| 131 |
|
| 132 |
public void repeatPending() throws IOException { |
| 133 |
if (pendingLen > 0) |
| 134 |
repeat(pendingDist, pendingLen); |
| 135 |
} |
| 136 |
|
| 137 |
public void copyUncompressed(DataInputStream inData, int len) |
| 138 |
throws IOException { |
| 139 |
int copySize = Math.min(bufSize - pos, len); |
| 140 |
inData.readFully(buf, pos, copySize); |
| 141 |
pos += copySize; |
| 142 |
|
| 143 |
if (full < pos) |
| 144 |
full = pos; |
| 145 |
} |
| 146 |
|
| 147 |
public int flush(byte[] out, int outOff) { |
| 148 |
int copySize = pos - start; |
| 149 |
if (pos == bufSize) |
| 150 |
pos = 0; |
| 151 |
|
| 152 |
System.arraycopy(buf, start, out, outOff, copySize); |
| 153 |
start = pos; |
| 154 |
|
| 155 |
return copySize; |
| 156 |
} |
| 157 |
} |