aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/BufferChain.java
blob: 8fbf13e32bacb9e3c813d4fd898e6c8f5194b585 (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
88
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import com.yahoo.text.AbstractUtf8Array;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.ArrayList;
import java.util.List;

/**
 * Data store for AbstractByteWriter. Tested in unit tests for ByteWriter.
 *
 * @author Steinar Knutsen
 */
public final class BufferChain {

    // refer to the revision history of ByteWriter for more information about
    // the reasons behind the sizing of BUFFERSIZE, WATERMARK and MAXBUFFERS
    static final int BUFFERSIZE = 4096;
    static final int WATERMARK = 1024;
    static final int MAXBUFFERS = 50;
    static {
        //noinspection ConstantConditions
        assert BUFFERSIZE > WATERMARK;
    }
    private final List<ByteBuffer> buffers = new ArrayList<>();
    private final WritableByteTransmitter endpoint;
    private ByteBuffer current = ByteBuffer.allocate(BUFFERSIZE);
    private long appended = 0L;

    public BufferChain(final WritableByteTransmitter endpoint) {
        this.endpoint = endpoint;
    }

    public void append(final byte b) throws IOException {
        makeRoom(1);
        current.put(b);
    }
    private final boolean shouldCopy(int length) {
         return (length < WATERMARK);
    }
    private final void makeRoom(int length) throws IOException {
        if (current.remaining() < length) {
            scratch();
        }
    }
    public void append(AbstractUtf8Array v) throws IOException {
        final int length = v.getByteLength();
        if (shouldCopy(length)) {
            makeRoom(length);
            v.writeTo(current);
        } else {
            append(v.wrap());
        }
    }
    public void append(final byte[] alreadyEncoded) throws java.io.IOException {
        if (alreadyEncoded.length > 0) {
            append(alreadyEncoded, 0, alreadyEncoded.length);
        }
    }

    public void append(final byte[] alreadyEncoded, final int offset, final int length) throws java.io.IOException {
        if (shouldCopy(length)) {
            makeRoom(length);
            current.put(alreadyEncoded, offset, length);
        } else {
            append(ByteBuffer.wrap(alreadyEncoded, offset, length));
        }
    }

    public void append(final ByteBuffer alreadyEncoded) throws java.io.IOException {
        if (alreadyEncoded.remaining() == 0) {
            return;
        }
        final int length = alreadyEncoded.limit() - alreadyEncoded.position();
        if (shouldCopy(length)) {
            makeRoom(length);
            current.put(alreadyEncoded);
        } else {
            scratch();
            add(alreadyEncoded);
        }
    }
    private final void add(final ByteBuffer buf) {
        buffers.add(buf);
        appended += buf.limit();
    }

    public void append(final CharBuffer toEncode, final CharsetEncoder encoder)
            throws java.io.IOException {
        CoderResult overflow;
        do {
            overflow = encoder.encode(toEncode, current, true);
            if (overflow.isOverflow()) {
                scratch();
            } else if (overflow.isError()) {
                try {
                    toEncode.get();
                } catch (final BufferUnderflowException e) {
                    // Give up if we can't discard some presumptively malformed
                    // or unmappable data
                    break;
                }
            }
        } while (!overflow.isUnderflow());
    }

    private void scratch() throws java.io.IOException {
        if (!possibleFlush() && current.position() != 0) {
            current.flip();
            add(current);
            current = ByteBuffer.allocate(BUFFERSIZE);
        }
    }

    private boolean possibleFlush() throws java.io.IOException {
        if (buffers.size() > MAXBUFFERS) {
            flush();
            return true;
        }
        return false;
    }

    public void flush() throws IOException {
        for (ByteBuffer b : buffers) {
            endpoint.send(b);
        }
        buffers.clear();
        if (current.position() > 0) {
            current.flip();
            appended += current.limit();
            endpoint.send(current);
            current = ByteBuffer.allocate(BUFFERSIZE);
        }
    }

    /**
     * @return number of bytes written to this buffer
     */
    public long appended() {
        return appended + current.position();
    }
}