aboutsummaryrefslogtreecommitdiff
path: root/src/org/tukaani/xz/LZMA2Encoder.java
blob: 6f3cab4078d040fb941a3dc44467c16f2d51de80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * LZMA2Encoder
 *
 * Author: Lasse Collin <lasse.collin@tukaani.org>
 *
 * This file has been put into the public domain.
 * You can do whatever you want with this file.
 */

package org.tukaani.xz;

import org.tukaani.xz.lzma.LZMAEncoder;

class LZMA2Encoder extends LZMA2Coder implements FilterEncoder {
    private final LZMA2Options options;
    private final byte[] props = new byte[1];

    LZMA2Encoder(LZMA2Options options) {
        if (options.getPresetDict() != null)
            throw new IllegalArgumentException(
                    "XZ doesn't support a preset dictionary for now");

        if (options.getMode() == LZMA2Options.MODE_UNCOMPRESSED) {
            props[0] = (byte)0;
        } else {
            int d = Math.max(options.getDictSize(),
                             LZMA2Options.DICT_SIZE_MIN);
            props[0] = (byte)(LZMAEncoder.getDistSlot(d - 1) - 23);
        }

        // Make a private copy so that the caller is free to change its copy.
        this.options = (LZMA2Options)options.clone();
    }

    public long getFilterID() {
        return FILTER_ID;
    }

    public byte[] getFilterProps() {
        return props;
    }

    public boolean supportsFlushing() {
        return true;
    }

    public FinishableOutputStream getOutputStream(FinishableOutputStream out,
                                                  ArrayCache arrayCache) {
        return options.getOutputStream(out, arrayCache);
    }
}