| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.lz; |
| 7 |
|
| 8 |
import java.io.OutputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import org.tukaani.xz.ArrayCache; |
| 11 |
|
| 12 |
public abstract class LZEncoder { |
| 13 |
public static final int MF_HC4 = 0x04; |
| 14 |
public static final int MF_BT4 = 0x14; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private final int keepSizeBefore; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private final int keepSizeAfter; |
| 29 |
|
| 30 |
final int matchLenMax; |
| 31 |
final int niceLen; |
| 32 |
|
| 33 |
final byte[] buf; |
| 34 |
final int bufSize; |
| 35 |
|
| 36 |
int readPos = -1; |
| 37 |
private int readLimit = -1; |
| 38 |
private boolean finishing = false; |
| 39 |
private int writePos = 0; |
| 40 |
private int pendingSize = 0; |
| 41 |
|
| 42 |
static void normalize(int[] positions, int positionsCount, |
| 43 |
int normalizationOffset) { |
| 44 |
for (int i = 0; i < positionsCount; ++i) { |
| 45 |
if (positions[i] <= normalizationOffset) |
| 46 |
positions[i] = 0; |
| 47 |
else |
| 48 |
positions[i] -= normalizationOffset; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private static int getBufSize( |
| 56 |
int dictSize, int extraSizeBefore, int extraSizeAfter, |
| 57 |
int matchLenMax) { |
| 58 |
int keepSizeBefore = extraSizeBefore + dictSize; |
| 59 |
int keepSizeAfter = extraSizeAfter + matchLenMax; |
| 60 |
int reserveSize = Math.min(dictSize / 2 + (256 << 10), 512 << 20); |
| 61 |
return keepSizeBefore + keepSizeAfter + reserveSize; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static int getMemoryUsage( |
| 69 |
int dictSize, int extraSizeBefore, int extraSizeAfter, |
| 70 |
int matchLenMax, int mf) { |
| 71 |
|
| 72 |
int m = getBufSize(dictSize, extraSizeBefore, extraSizeAfter, |
| 73 |
matchLenMax) / 1024 + 10; |
| 74 |
|
| 75 |
switch (mf) { |
| 76 |
case MF_HC4: |
| 77 |
m += HC4.getMemoryUsage(dictSize); |
| 78 |
break; |
| 79 |
|
| 80 |
case MF_BT4: |
| 81 |
m += BT4.getMemoryUsage(dictSize); |
| 82 |
break; |
| 83 |
|
| 84 |
default: |
| 85 |
throw new IllegalArgumentException(); |
| 86 |
} |
| 87 |
|
| 88 |
return m; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static LZEncoder getInstance( |
| 116 |
int dictSize, int extraSizeBefore, int extraSizeAfter, |
| 117 |
int niceLen, int matchLenMax, int mf, int depthLimit, |
| 118 |
ArrayCache arrayCache) { |
| 119 |
switch (mf) { |
| 120 |
case MF_HC4: |
| 121 |
return new HC4(dictSize, extraSizeBefore, extraSizeAfter, |
| 122 |
niceLen, matchLenMax, depthLimit, arrayCache); |
| 123 |
|
| 124 |
case MF_BT4: |
| 125 |
return new BT4(dictSize, extraSizeBefore, extraSizeAfter, |
| 126 |
niceLen, matchLenMax, depthLimit, arrayCache); |
| 127 |
} |
| 128 |
|
| 129 |
throw new IllegalArgumentException(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
LZEncoder(int dictSize, int extraSizeBefore, int extraSizeAfter, |
| 136 |
int niceLen, int matchLenMax, ArrayCache arrayCache) { |
| 137 |
bufSize = getBufSize(dictSize, extraSizeBefore, extraSizeAfter, |
| 138 |
matchLenMax); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
buf = arrayCache.getByteArray(bufSize + MatchLength.EXTRA_SIZE, false); |
| 143 |
|
| 144 |
keepSizeBefore = extraSizeBefore + dictSize; |
| 145 |
keepSizeAfter = extraSizeAfter + matchLenMax; |
| 146 |
|
| 147 |
this.matchLenMax = matchLenMax; |
| 148 |
this.niceLen = niceLen; |
| 149 |
} |
| 150 |
|
| 151 |
public void putArraysToCache(ArrayCache arrayCache) { |
| 152 |
arrayCache.putArray(buf); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void setPresetDict(int dictSize, byte[] presetDict) { |
| 161 |
assert !isStarted(); |
| 162 |
assert writePos == 0; |
| 163 |
|
| 164 |
if (presetDict != null) { |
| 165 |
|
| 166 |
|
| 167 |
int copySize = Math.min(presetDict.length, dictSize); |
| 168 |
int offset = presetDict.length - copySize; |
| 169 |
System.arraycopy(presetDict, offset, buf, 0, copySize); |
| 170 |
writePos += copySize; |
| 171 |
skip(copySize); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private void moveWindow() { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
int moveOffset = (readPos + 1 - keepSizeBefore) & ~15; |
| 184 |
int moveSize = writePos - moveOffset; |
| 185 |
System.arraycopy(buf, moveOffset, buf, 0, moveSize); |
| 186 |
|
| 187 |
readPos -= moveOffset; |
| 188 |
readLimit -= moveOffset; |
| 189 |
writePos -= moveOffset; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public int fillWindow(byte[] in, int off, int len) { |
| 196 |
assert !finishing; |
| 197 |
|
| 198 |
|
| 199 |
if (readPos >= bufSize - keepSizeAfter) |
| 200 |
moveWindow(); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
if (len > bufSize - writePos) |
| 205 |
len = bufSize - writePos; |
| 206 |
|
| 207 |
System.arraycopy(in, off, buf, writePos, len); |
| 208 |
writePos += len; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (writePos >= keepSizeAfter) |
| 213 |
readLimit = writePos - keepSizeAfter; |
| 214 |
|
| 215 |
processPendingBytes(); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
return len; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private void processPendingBytes() { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if (pendingSize > 0 && readPos < readLimit) { |
| 236 |
readPos -= pendingSize; |
| 237 |
int oldPendingSize = pendingSize; |
| 238 |
pendingSize = 0; |
| 239 |
skip(oldPendingSize); |
| 240 |
assert pendingSize < oldPendingSize; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public boolean isStarted() { |
| 249 |
return readPos != -1; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setFlushing() { |
| 257 |
readLimit = writePos - 1; |
| 258 |
processPendingBytes(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public void setFinishing() { |
| 266 |
readLimit = writePos - 1; |
| 267 |
finishing = true; |
| 268 |
processPendingBytes(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public boolean hasEnoughData(int alreadyReadLen) { |
| 276 |
return readPos - alreadyReadLen < readLimit; |
| 277 |
} |
| 278 |
|
| 279 |
public void copyUncompressed(OutputStream out, int backward, int len) |
| 280 |
throws IOException { |
| 281 |
out.write(buf, readPos + 1 - backward, len); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public int getAvail() { |
| 292 |
assert isStarted(); |
| 293 |
return writePos - readPos; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public int getPos() { |
| 301 |
return readPos; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public int getByte(int backward) { |
| 314 |
return buf[readPos - backward] & 0xFF; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public int getByte(int forward, int backward) { |
| 323 |
return buf[readPos + forward - backward] & 0xFF; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public int getMatchLen(int dist, int lenLimit) { |
| 335 |
return MatchLength.getLen(buf, readPos, dist + 1, 0, lenLimit); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public int getMatchLen(int forward, int dist, int lenLimit) { |
| 348 |
return MatchLength.getLen(buf, readPos + forward, dist + 1, |
| 349 |
0, lenLimit); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public boolean verifyMatches(Matches matches) { |
| 363 |
int lenLimit = Math.min(getAvail(), matchLenMax); |
| 364 |
|
| 365 |
for (int i = 0; i < matches.count; ++i) |
| 366 |
if (getMatchLen(matches.dist[i], lenLimit) != matches.len[i]) |
| 367 |
return false; |
| 368 |
|
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
int movePos(int requiredForFlushing, int requiredForFinishing) { |
| 390 |
assert requiredForFlushing >= requiredForFinishing; |
| 391 |
|
| 392 |
++readPos; |
| 393 |
int avail = writePos - readPos; |
| 394 |
|
| 395 |
if (avail < requiredForFlushing) { |
| 396 |
if (avail < requiredForFinishing || !finishing) { |
| 397 |
++pendingSize; |
| 398 |
avail = 0; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
return avail; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public abstract Matches getMatches(); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public abstract void skip(int len); |
| 414 |
} |