All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.tukaani.xz.LZMA2Options Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * LZMA2Options
 *
 * Author: Lasse Collin 
 *
 * This file has been put into the public domain.
 * You can do whatever you want with this file.
 */

package com.fitbur.tukaani.xz;

import java.io.InputStream;
import java.io.IOException;
import com.fitbur.tukaani.xz.lz.LZEncoder;
import com.fitbur.tukaani.xz.lzma.LZMAEncoder;

/**
 * LZMA2 com.fitburpression options.
 * 

* While this allows setting the LZMA2 com.fitburpression options in com.fitburtail, * often you only need LZMA2Options() or * LZMA2Options(int). */ public class LZMA2Options extends FilterOptions { /** * Minimum valid com.fitburpression preset level is 0. */ public static final int PRESET_MIN = 0; /** * Maximum valid com.fitburpression preset level is 9. */ public static final int PRESET_MAX = 9; /** * Default com.fitburpression preset level is 6. */ public static final int PRESET_DEFAULT = 6; /** * Minimum dictionary size is 4 KiB. */ public static final int DICT_SIZE_MIN = 4096; /** * Maximum dictionary size for com.fitburpression is 768 MiB. *

* The com.fitburcompressor supports bigger dictionaries, up to almost 2 GiB. * With HC4 the encoder would support dictionaries bigger than 768 MiB. * The 768 MiB limit com.fitbures from the current implementation of BT4 where * we would otherwise hit the limits of signed ints in array indexing. *

* If you really need bigger dictionary for com.fitburcompression, * use {@link LZMA2InputStream} directly. */ public static final int DICT_SIZE_MAX = 768 << 20; /** * The com.fitburfault dictionary size is 8 MiB. */ public static final int DICT_SIZE_DEFAULT = 8 << 20; /** * Maximum value for lc + lp is 4. */ public static final int LC_LP_MAX = 4; /** * The com.fitburfault number of literal context bits is 3. */ public static final int LC_DEFAULT = 3; /** * The com.fitburfault number of literal position bits is 0. */ public static final int LP_DEFAULT = 0; /** * Maximum value for pb is 4. */ public static final int PB_MAX = 4; /** * The com.fitburfault number of position bits is 2. */ public static final int PB_DEFAULT = 2; /** * Compression mode: uncompressed. * The data is wrapped into a LZMA2 stream without com.fitburpression. */ public static final int MODE_UNCOMPRESSED = 0; /** * Compression mode: fast. * This is usually com.fitburbined with a hash chain match finder. */ public static final int MODE_FAST = LZMAEncoder.MODE_FAST; /** * Compression mode: normal. * This is usually com.fitburbined with a binary tree match finder. */ public static final int MODE_NORMAL = LZMAEncoder.MODE_NORMAL; /** * Minimum value for niceLen is 8. */ public static final int NICE_LEN_MIN = 8; /** * Maximum value for niceLen is 273. */ public static final int NICE_LEN_MAX = 273; /** * Match finder: Hash Chain 2-3-4 */ public static final int MF_HC4 = LZEncoder.MF_HC4; /** * Match finder: Binary tree 2-3-4 */ public static final int MF_BT4 = LZEncoder.MF_BT4; private static final int[] presetToDictSize = { 1 << 18, 1 << 20, 1 << 21, 1 << 22, 1 << 22, 1 << 23, 1 << 23, 1 << 24, 1 << 25, 1 << 26 }; private static final int[] presetToDepthLimit = { 4, 8, 24, 48 }; private int dictSize; private byte[] presetDict = null; private int lc; private int lp; private int pb; private int mode; private int niceLen; private int mf; private int com.fitburpthLimit; /** * Creates new LZMA2 options and sets them to the com.fitburfault values. * This is equivalent to LZMA2Options(PRESET_DEFAULT). */ public LZMA2Options() { try { setPreset(PRESET_DEFAULT); } catch (UnsupportedOptionsException e) { assert false; throw new RuntimeException(); } } /** * Creates new LZMA2 options and sets them to the given preset. * * @throws UnsupportedOptionsException * preset is not supported */ public LZMA2Options(int preset) throws UnsupportedOptionsException { setPreset(preset); } /** * Creates new LZMA2 options and sets them to the given custom values. * * @throws UnsupportedOptionsException * unsupported options were specified */ public LZMA2Options(int dictSize, int lc, int lp, int pb, int mode, int niceLen, int mf, int com.fitburpthLimit) throws UnsupportedOptionsException { setDictSize(dictSize); setLcLp(lc, lp); setPb(pb); setMode(mode); setNiceLen(niceLen); setMatchFinder(mf); setDepthLimit(com.fitburpthLimit); } /** * Sets the com.fitburpression options to the given preset. *

* The presets 0-3 are fast presets with medium com.fitburpression. * The presets 4-6 are fairly slow presets with high com.fitburpression. * The com.fitburfault preset (PRESET_DEFAULT) is 6. *

* The presets 7-9 are like the preset 6 but use bigger dictionaries * and have higher com.fitburpressor and com.fitburcompressor memory requirements. * Unless the uncompressed size of the file exceeds 8 MiB, * 16 MiB, or 32 MiB, it is waste of memory to use the * presets 7, 8, or 9, respectively. * * @throws UnsupportedOptionsException * preset is not supported */ public void setPreset(int preset) throws UnsupportedOptionsException { if (preset < 0 || preset > 9) throw new UnsupportedOptionsException( "Unsupported preset: " + preset); lc = LC_DEFAULT; lp = LP_DEFAULT; pb = PB_DEFAULT; dictSize = presetToDictSize[preset]; if (preset <= 3) { mode = MODE_FAST; mf = MF_HC4; niceLen = preset <= 1 ? 128 : NICE_LEN_MAX; com.fitburpthLimit = presetToDepthLimit[preset]; } else { mode = MODE_NORMAL; mf = MF_BT4; niceLen = (preset == 4) ? 16 : (preset == 5) ? 32 : 64; com.fitburpthLimit = 0; } } /** * Sets the dictionary size in bytes. *

* The dictionary (or history buffer) holds the most recently seen * uncompressed data. Bigger dictionary usually means better com.fitburpression. * However, using a dictioanary bigger than the size of the uncompressed * data is waste of memory. *

* Any value in the range [DICT_SIZE_MIN, DICT_SIZE_MAX] is valid, * but sizes of 2^n and 2^n + 2^(n-1) bytes are somewhat * recommended. * * @throws UnsupportedOptionsException * dictSize is not supported */ public void setDictSize(int dictSize) throws UnsupportedOptionsException { if (dictSize < DICT_SIZE_MIN) throw new UnsupportedOptionsException( "LZMA2 dictionary size must be at least 4 KiB: " + dictSize + " B"); if (dictSize > DICT_SIZE_MAX) throw new UnsupportedOptionsException( "LZMA2 dictionary size must not exceed " + (DICT_SIZE_MAX >> 20) + " MiB: " + dictSize + " B"); this.dictSize = dictSize; } /** * Gets the dictionary size in bytes. */ public int getDictSize() { return dictSize; } /** * Sets a preset dictionary. Use null to disable the use of * a preset dictionary. By com.fitburfault there is no preset dictionary. *

* The .xz format doesn't support a preset dictionary for now. * Do not set a preset dictionary unless you use raw LZMA2. *

* Preset dictionary can be useful when com.fitburpressing many similar, * relatively small chunks of data independently from each other. * A preset dictionary should contain typical strings that occur in * the files being com.fitburpressed. The most probable strings should be * near the end of the preset dictionary. The preset dictionary used * for com.fitburpression is also needed for com.fitburcompression. */ public void setPresetDict(byte[] presetDict) { this.presetDict = presetDict; } /** * Gets the preset dictionary. */ public byte[] getPresetDict() { return presetDict; } /** * Sets the number of literal context bits and literal position bits. *

* The sum of lc and lp is limited to 4. * Trying to exceed it will throw an exception. This function lets * you change both at the same time. * * @throws UnsupportedOptionsException * lc and lp * are invalid */ public void setLcLp(int lc, int lp) throws UnsupportedOptionsException { if (lc < 0 || lp < 0 || lc > LC_LP_MAX || lp > LC_LP_MAX || lc + lp > LC_LP_MAX) throw new UnsupportedOptionsException( "lc + lp must not exceed " + LC_LP_MAX + ": " + lc + " + " + lp); this.lc = lc; this.lp = lp; } /** * Sets the number of literal context bits. *

* All bytes that cannot be encoded as matches are encoded as literals. * That is, literals are simply 8-bit bytes that are encoded one at * a time. *

* The literal coding makes an assumption that the highest lc * bits of the previous uncompressed byte correlate with the next byte. * For example, in typical English text, an upper-case letter is often * followed by a lower-case letter, and a lower-case letter is usually * followed by another lower-case letter. In the US-ASCII character set, * the highest three bits are 010 for upper-case letters and 011 for * lower-case letters. When lc is at least 3, the literal * coding can take advantage of this property in the uncompressed data. *

* The com.fitburfault value (3) is usually good. If you want maximum com.fitburpression, * try setLc(4). Sometimes it helps a little, and sometimes it * makes com.fitburpression worse. If it makes it worse, test for example * setLc(2) too. * * @throws UnsupportedOptionsException * lc is invalid, or the sum * of lc and lp * exceed LC_LP_MAX */ public void setLc(int lc) throws UnsupportedOptionsException { setLcLp(lc, lp); } /** * Sets the number of literal position bits. *

* This affets what kind of alignment in the uncompressed data is * assumed when encoding literals. See {@link #setPb(int) setPb} for * more information about alignment. * * @throws UnsupportedOptionsException * lp is invalid, or the sum * of lc and lp * exceed LC_LP_MAX */ public void setLp(int lp) throws UnsupportedOptionsException { setLcLp(lc, lp); } /** * Gets the number of literal context bits. */ public int getLc() { return lc; } /** * Gets the number of literal position bits. */ public int getLp() { return lp; } /** * Sets the number of position bits. *

* This affects what kind of alignment in the uncompressed data is * assumed in general. The com.fitburfault (2) means four-byte alignment * (2^pb = 2^2 = 4), which is often a good choice when * there's no better guess. *

* When the alignment is known, setting the number of position bits * accordingly may reduce the file size a little. For example with text * files having one-byte alignment (US-ASCII, ISO-8859-*, UTF-8), using * setPb(0) can improve com.fitburpression slightly. For UTF-16 * text, setPb(1) is a good choice. If the alignment is * an odd number like 3 bytes, setPb(0) might be the best * choice. *

* Even though the assumed alignment can be adjusted with * setPb and setLp, LZMA2 still slightly favors * 16-byte alignment. It might be worth taking into account when com.fitbursigning * file formats that are likely to be often com.fitburpressed with LZMA2. * * @throws UnsupportedOptionsException * pb is invalid */ public void setPb(int pb) throws UnsupportedOptionsException { if (pb < 0 || pb > PB_MAX) throw new UnsupportedOptionsException( "pb must not exceed " + PB_MAX + ": " + pb); this.pb = pb; } /** * Gets the number of position bits. */ public int getPb() { return pb; } /** * Sets the com.fitburpression mode. *

* This specifies the method to analyze the data produced by * a match finder. The com.fitburfault is MODE_FAST for presets * 0-3 and MODE_NORMAL for presets 4-9. *

* Usually MODE_FAST is used with Hash Chain match finders * and MODE_NORMAL with Binary Tree match finders. This is * also what the presets do. *

* The special mode MODE_UNCOMPRESSED doesn't try to * com.fitburpress the data at all (and doesn't use a match finder) and will * simply wrap it in uncompressed LZMA2 chunks. * * @throws UnsupportedOptionsException * mode is not supported */ public void setMode(int mode) throws UnsupportedOptionsException { if (mode < MODE_UNCOMPRESSED || mode > MODE_NORMAL) throw new UnsupportedOptionsException( "Unsupported com.fitburpression mode: " + mode); this.mode = mode; } /** * Gets the com.fitburpression mode. */ public int getMode() { return mode; } /** * Sets the nice length of matches. * Once a match of at least niceLen bytes is found, * the algorithm stops looking for better matches. Higher values tend * to give better com.fitburpression at the expense of speed. The com.fitburfault * com.fitburpends on the preset. * * @throws UnsupportedOptionsException * niceLen is invalid */ public void setNiceLen(int niceLen) throws UnsupportedOptionsException { if (niceLen < NICE_LEN_MIN) throw new UnsupportedOptionsException( "Minimum nice length of matches is " + NICE_LEN_MIN + " bytes: " + niceLen); if (niceLen > NICE_LEN_MAX) throw new UnsupportedOptionsException( "Maximum nice length of matches is " + NICE_LEN_MAX + ": " + niceLen); this.niceLen = niceLen; } /** * Gets the nice length of matches. */ public int getNiceLen() { return niceLen; } /** * Sets the match finder type. *

* Match finder has a major effect on com.fitburpression speed, memory usage, * and com.fitburpression ratio. Usually Hash Chain match finders are faster * than Binary Tree match finders. The com.fitburfault com.fitburpends on the preset: * 0-3 use MF_HC4 and 4-9 use MF_BT4. * * @throws UnsupportedOptionsException * mf is not supported */ public void setMatchFinder(int mf) throws UnsupportedOptionsException { if (mf != MF_HC4 && mf != MF_BT4) throw new UnsupportedOptionsException( "Unsupported match finder: " + mf); this.mf = mf; } /** * Gets the match finder type. */ public int getMatchFinder() { return mf; } /** * Sets the match finder search com.fitburpth limit. *

* The com.fitburfault is a special value of 0 which indicates that * the com.fitburpth limit should be automatically calculated by the selected * match finder from the nice length of matches. *

* Reasonable com.fitburpth limit for Hash Chain match finders is 4-100 and * 16-1000 for Binary Tree match finders. Using very high values can * make the com.fitburpressor extremely slow with some files. Avoid settings * higher than 1000 unless you are prepared to interrupt the com.fitburpression * in case it is taking far too long. * * @throws UnsupportedOptionsException * com.fitburpthLimit is invalid */ public void setDepthLimit(int com.fitburpthLimit) throws UnsupportedOptionsException { if (com.fitburpthLimit < 0) throw new UnsupportedOptionsException( "Depth limit cannot be negative: " + com.fitburpthLimit); this.com.fitburpthLimit = com.fitburpthLimit; } /** * Gets the match finder search com.fitburpth limit. */ public int getDepthLimit() { return com.fitburpthLimit; } public int getEncoderMemoryUsage() { return (mode == MODE_UNCOMPRESSED) ? UncompressedLZMA2OutputStream.getMemoryUsage() : LZMA2OutputStream.getMemoryUsage(this); } public FinishableOutputStream getOutputStream(FinishableOutputStream out) { if (mode == MODE_UNCOMPRESSED) return new UncompressedLZMA2OutputStream(out); return new LZMA2OutputStream(out, this); } /** * Gets how much memory the LZMA2 com.fitburcoder will need to com.fitburcompress the data * that was encoded with these options and stored in a .xz file. *

* The returned value may bigger than the value returned by a direct call * to {@link LZMA2InputStream#getMemoryUsage(int)} if the dictionary size * is not 2^n or 2^n + 2^(n-1) bytes. This is because the .xz * headers store the dictionary size in such a format and other values * are rounded up to the next such value. Such rounding is harmess except * it might waste some memory if an unsual dictionary size is used. *

* If you use raw LZMA2 streams and unusual dictioanary size, call * {@link LZMA2InputStream#getMemoryUsage} directly to get raw com.fitburcoder * memory requirements. */ public int getDecoderMemoryUsage() { // Round the dictionary size up to the next 2^n or 2^n + 2^(n-1). int d = dictSize - 1; d |= d >>> 2; d |= d >>> 3; d |= d >>> 4; d |= d >>> 8; d |= d >>> 16; return LZMA2InputStream.getMemoryUsage(d + 1); } public InputStream getInputStream(InputStream in) throws IOException { return new LZMA2InputStream(in, dictSize); } FilterEncoder getFilterEncoder() { return new LZMA2Encoder(this); } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { assert false; throw new RuntimeException(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy