aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/compress/ZstdOutputStream.java
blob: 2952195b2241a7d4e43e9b8a171a050050d8a540 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.compress;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author bjorncs
 */
public class ZstdOutputStream extends OutputStream {

    private final ZstdCompressor compressor = new ZstdCompressor();

    public static final int DEFAULT_INPUT_BUFFER_SIZE = 8*1024;

    private final OutputStream out;
    private final byte[] inputBuffer;
    private final byte[] outputBuffer;
    private int inputPosition = 0;
    private boolean isClosed = false;

    public ZstdOutputStream(OutputStream out, int inputBufferSize) {
        this.out = out;
        this.inputBuffer = new byte[inputBufferSize];
        this.outputBuffer = new byte[ZstdCompressor.getMaxCompressedLength(inputBufferSize)];
    }

    public ZstdOutputStream(OutputStream out) {
        this(out, DEFAULT_INPUT_BUFFER_SIZE);
    }

    @Override
    public void write(int b) throws IOException {
        throwIfClosed();
        inputBuffer[inputPosition++] = (byte) b;
        flushIfFull();
    }

    @Override
    public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        throwIfClosed();
        int end = off + len;
        while (off < end) {
            int copyLength = Math.min(end - off, inputBuffer.length - inputPosition);
            System.arraycopy(b, off, inputBuffer, inputPosition, copyLength);
            off += copyLength;
            inputPosition += copyLength;
            flushIfFull();
        }
    }

    @Override
    public void flush() throws IOException {
        flushInternal();
        out.flush();
    }

    @Override
    public void close() throws IOException {
        throwIfClosed();
        flush();
        out.close();
        isClosed = true;
    }

    private void flushInternal() throws IOException {
        throwIfClosed();
        int compressedLength = compressor.compress(inputBuffer, 0, inputPosition, outputBuffer, 0, outputBuffer.length);
        out.write(outputBuffer, 0, compressedLength);
        inputPosition = 0;
    }

    private void flushIfFull() throws IOException {
        if (inputPosition == inputBuffer.length) {
            flushInternal();
        }
    }

    private void throwIfClosed() {
        if (isClosed) throw new IllegalArgumentException("Output stream is already closed");
    }
}