aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/GrowableBufferOutputStream.java
blob: b8dfedc8ede7711709768e9151f1f58932ae8d8d (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.nio.channels.WritableByteChannel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Stack;
import java.util.LinkedList;
import java.util.Iterator;
import java.nio.ByteBuffer;

/**
 * @author Bjørn Borud
 */
public class GrowableBufferOutputStream extends OutputStream {

    private ByteBuffer lastBuffer;
    private ByteBuffer directBuffer;
    private LinkedList<ByteBuffer> bufferList = new LinkedList<>();
    private Stack<ByteBuffer> recycledBuffers = new Stack<>();

    private int bufferSize;
    private int maxBuffers;

    public GrowableBufferOutputStream(int bufferSize, int maxBuffers) {
        this.bufferSize = bufferSize;
        this.maxBuffers = maxBuffers;
        lastBuffer = ByteBuffer.allocate(bufferSize);
        directBuffer = ByteBuffer.allocateDirect(bufferSize);
    }

    @Override
    public void write(byte[] cbuf, int off, int len) throws IOException {
        if (lastBuffer.remaining() >= len) {
            lastBuffer.put(cbuf, off, len);
            return;
        }

        int residue = len;

        while (residue > 0) {
            int newOffset = len - residue;
            int toWrite = Math.min(lastBuffer.remaining(), residue);

            lastBuffer.put(cbuf, newOffset, toWrite);
            residue -= toWrite;
            if (residue != 0) {
                extend();
            }
        }
    }

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

    @Override
    public String toString() {
        return "GrowableBufferOutputStream, writable size " + writableSize()
                + " bytes, " + numWritableBuffers() + " buffers, last buffer"
                + " position " + lastBuffer.position() + ", last buffer limit "
                + lastBuffer.limit();
    }

    public void write(int b) {
        if (lastBuffer.remaining() == 0) {
            extend();
        }
        lastBuffer.put((byte) b);
    }

    @Override
    public void flush() {
        // if the last buffer is untouched we do not need to do anything;  if
        // it has been touched we call extend(), which enqueues the buffer
        // and allocates or recycles a buffer for us
        if (lastBuffer.position() > 0) {
            extend();
        }
    }

    @Override
    public void close() {
        flush();
    }

    public int channelWrite(WritableByteChannel channel) throws IOException {
        ByteBuffer buffer;
        int totalWritten = 0;

        while (!bufferList.isEmpty()) {
            buffer = bufferList.getFirst();
            int written = 0;

            synchronized (directBuffer) {
                directBuffer.clear();
                directBuffer.put(buffer);
                directBuffer.flip();
                written = channel.write(directBuffer);
                int left = directBuffer.remaining();

                if (left > 0) {
                    int oldpos = buffer.position();

                    buffer.position(oldpos - left);
                }
                totalWritten += written;
            }

            // if we've completed writing this buffer we can dispose of it
            if (buffer.remaining() == 0) {
                bufferList.removeFirst();
                recycleBuffer(buffer);
            }

            // if we didn't write any bytes we terminate
            if (written == 0) {
                break;
            }
        }

        return totalWritten;
    }

    public int numWritableBuffers() {
        return bufferList.size();
    }

    public void clear() {
        flush();
        bufferList.clear();
    }

    public void clearCache() {
        recycledBuffers.clear();
    }

    public void clearAll() {
        clear();
        clearCache();
    }

    public int writableSize() {
        Iterator<ByteBuffer> it = bufferList.iterator();
        int size = 0;

        while (it.hasNext()) {
            size += (it.next()).remaining();
        }

        return size;
    }

    public ByteBuffer[] getWritableBuffers() {
        flush();
        ByteBuffer[] result = new ByteBuffer[numWritableBuffers()];
        return bufferList.toArray(result);
    }

    private void extend() {
        enqueueBuffer(lastBuffer);

        if (recycledBuffers.empty()) {
            lastBuffer = ByteBuffer.allocate(bufferSize);
        } else {
            lastBuffer = recycledBuffers.pop();
            lastBuffer.clear();
        }
    }

    private void enqueueBuffer(ByteBuffer buffer) {
        buffer.flip();
        bufferList.addLast(buffer);
    }

    private void recycleBuffer(ByteBuffer buffer) {
        if (recycledBuffers.size() >= maxBuffers) {
            return;
        }
        recycledBuffers.push(buffer);
    }

}