aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
blob: 4af40a2244731808f02749905d35e69df92fa42a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Objects;

/**
 * <p>This class provides an adapter from a {@link ReadableContentChannel} to an InputStream. This class supports all
 * regular InputStream operations, and can be combined with any other InputStream API.</p>
 *
 * <p>Because this class encapsulates the reference-counted {@link ContentChannel} operations, one must be sure to
 * always call {@link #close()} before discarding it. Failure to do so will prevent the Container from ever shutting
 * down.</p>
 *
 * @author Simon Thoresen Hult
 */
public class UnsafeContentInputStream extends InputStream {

    private final ReadableContentChannel content;
    private ByteBuffer currBuf = ByteBuffer.allocate(0);
    private byte [] marked;
    private int readSinceMarked;

    /**
     * <p>Constructs a new ContentInputStream that reads from the given {@link ReadableContentChannel}.</p>
     *
     * @param content The content to read the stream from.
     */
    public UnsafeContentInputStream(ReadableContentChannel content) {
        this.content = content;
    }

    @Override
    public int read() {
        fetchNonEmptyBuffer();
        if (currBuf == null) return -1;

        byte b = currBuf.get();
        if (marked != null) {
            if (readSinceMarked < marked.length) {
                marked[readSinceMarked++] = b;
            } else {
                marked = null;
            }
        }
        return ((int)b) & 0xFF;
    }

    private boolean fetchNonEmptyBuffer() {
        while (currBuf != null && currBuf.remaining() == 0) {
            currBuf = content.read();
        }
        return (currBuf != null && currBuf.hasRemaining());
    }

    @Override
    public int read(byte buf[], int off, int len) {
        Objects.requireNonNull(buf, "buf");
        if (off < 0 || len < 0 || len > buf.length - off) {
            throw new IndexOutOfBoundsException();
        }
        if (len == 0) return 0;

        if ( ! fetchNonEmptyBuffer() ) return -1;
        int read = 0;
        while ((available() > 0) && fetchNonEmptyBuffer() && ((len - read) > 0)) {
            int toRead = Math.min(currBuf.remaining(), (len - read));
            currBuf.get(buf, off + read, toRead);
            read += toRead;
        }
        if (marked != null) {
            if (readSinceMarked + read <= marked.length) {
                System.arraycopy(buf, off, marked, readSinceMarked, read);
                readSinceMarked += read;
            } else {
                marked = null;
            }

        }
        return read;
    }

    @Override
    public int available() {
        if (currBuf != null && currBuf.remaining() > 0) {
            return currBuf.remaining();
        }
        return content.available();
    }

    @Override
    public void close() {
        // noinspection StatementWithEmptyBody
        while (content.read() != null) {

        }
    }

    @Override
    public synchronized void mark(int readlimit) {
        marked = new byte[readlimit];
        readSinceMarked = 0;
    }

    @Override
    public synchronized void reset() throws IOException {
        if (marked == null) {
            throw new IOException("mark has not been called, or too much has been read since marked.");
        }
        ByteBuffer newBuf = ByteBuffer.allocate(readSinceMarked + currBuf.remaining());
        newBuf.put(marked, 0, readSinceMarked);
        newBuf.put(currBuf);
        newBuf.flip();
        currBuf = newBuf;
        marked = null;
    }

    @Override
    public boolean markSupported() {
        return true;
    }
}