summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ByteBufferInputStream.java
blob: f88519c261557f2a994e93a57526f05948ec2861 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.communication;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author Einar M R Rosenvinge
 */
class ByteBufferInputStream extends InputStream {
    private final Deque<ByteBuffer> currentBuffers = new ArrayDeque<>();

    ByteBufferInputStream(ByteBuffer[] buffers) {
        for (int i = buffers.length - 1; i > -1; i--) {
            currentBuffers.push(buffers[i]);
        }
    }

    @Override
    public int read() throws IOException {
        pop();
        if (currentBuffers.isEmpty()) {
            return -1;
        }
        return currentBuffers.peek().get();
    }

    private void pop() {
        if (currentBuffers.isEmpty()) {
            return;
        }

        while (!currentBuffers.isEmpty() && !currentBuffers.peek().hasRemaining()) {
            //it's exhausted, get rid of it
            currentBuffers.pop();
        }
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }
        pop();
        if (currentBuffers.isEmpty()) {
            return -1;
        }
        int toRead = Math.min(len, currentBuffers.peek().remaining());
        currentBuffers.peek().get(b, off, toRead);
        return toRead;
    }

    @Override
    public long skip(long n) throws IOException {
        throw new IOException("skip() not supported.");
    }

    @Override
    public int available() throws IOException {
        if (currentBuffers.isEmpty()) {
            return 0;
        }

        int size = 0;
        for (ByteBuffer b : currentBuffers) {
            size += b.remaining();
        }
        return size;
    }
}