aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
blob: 41374edafd1e872cb92fdaccf604f3b325568f01 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

import com.yahoo.compress.Compressor;

import java.nio.charset.Charset;

final class BufferedOutput {

    private byte[] buf;
    private int capacity;
    private int pos;

    BufferedOutput(int cap) {
        capacity = Math.max(cap, 64);
        buf = new byte[capacity];
    }

    BufferedOutput() {
        this(4096);
    }

    void reset() {
        pos = 0;
    }

    private void reserve(int bytes) {
        if (pos + bytes > capacity) {
            while (pos + bytes > capacity) {
                capacity = capacity * 2;
            }
            byte[] tmp = new byte[capacity];
            System.arraycopy(buf, 0, tmp, 0, pos);
            buf = tmp;
        }
    }

    int position() { return pos; }

    void put(byte b) {
        reserve(1);
        buf[pos++] = b;
    }

    void absolutePut(int position, byte b) {
        buf[position] = b;
    }

    void put(byte[] bytes) {
        reserve(bytes.length);
        for (byte b : bytes) {
            buf[pos++] = b;
        }
    }

    byte[] toArray() {
        byte[] ret = new byte[pos];
        System.arraycopy(buf, 0, ret, 0, pos);
        return ret;
    }
    public String toString(Charset charset) {
        return new String(buf, 0, pos, charset);
    }
    Compressor.Compression compress(Compressor compressor) {
        return compressor.compress(buf, pos);
    }
}