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

import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * <p>This class extends the {@link AbstractContentOutputStream}, and forwards all write() and close() calls to a {@link
 * FastContentWriter}. This means that once {@link #close()} has been called, the asynchronous completion of all pending
 * operations can be awaited using the {@link Future} interface of this class. Any asynchronous failure will be
 * rethrown when calling either of the get() methods on this class.</p>
 * <p>Please notice that the Future implementation of this class will NEVER complete unless {@link #close()} has been
 * called.</p>
 *
 * @author Simon Thoresen Hult
 */
public class FastContentOutputStream extends AbstractContentOutputStream implements Future<Boolean> {

    private final FastContentWriter out;

    /**
     * <p>Constructs a new FastContentOutputStream that writes into the given {@link ContentChannel}.</p>
     *
     * @param out The ContentChannel to write the stream into.
     */
    public FastContentOutputStream(ContentChannel out) {
        this(new FastContentWriter(out));
    }

    /**
     * <p>Constructs a new FastContentOutputStream that writes into the given {@link FastContentWriter}.</p>
     *
     * @param out The ContentWriter to write the stream into.
     */
    public FastContentOutputStream(FastContentWriter out) {
        Objects.requireNonNull(out, "out");
        this.out = out;
    }

    @Override
    protected void doFlush(ByteBuffer buf) {
        out.write(buf);
    }

    @Override
    protected void doClose() {
        out.close();
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return out.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return out.isCancelled();
    }

    @Override
    public boolean isDone() {
        return out.isDone();
    }

    @Override
    public Boolean get() throws InterruptedException, ExecutionException {
        return out.get();
    }

    @Override
    public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return out.get(timeout, unit);
    }

    public void addListener(Runnable listener, Executor executor) {
        out.addListener(listener, executor);
    }
}