aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/NullContent.java
blob: 995eb5f61d1eadaa295df8b0ca802fc230ce5fbd (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
// 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 com.yahoo.jdisc.Request;

import java.nio.ByteBuffer;

/**
 * <p>This class provides a convenient implementation of {@link ContentChannel} that does not support being written to.
 * If {@link #write(ByteBuffer, CompletionHandler)} is called, it throws an UnsupportedOperationException. If {@link
 * #close(CompletionHandler)} is called, it calls the given {@link CompletionHandler}.</p>
 *
 * <p>A {@link RequestHandler}s that does not expect content can simply return the {@link #INSTANCE} of this class for
 * every invocation of its {@link RequestHandler#handleRequest(Request, ResponseHandler)}.</p>
 *
 * @author Simon Thoresen Hult
 */
public final class NullContent implements ContentChannel {

    public static final NullContent INSTANCE = new NullContent();

    private NullContent() {
        // hide
    }

    @Override
    public void write(ByteBuffer buf, CompletionHandler handler) {
        if (buf.hasRemaining()) {
            throw new UnsupportedOperationException();
        }
        if (handler != null) {
            handler.completed();
        }
    }

    @Override
    public void close(CompletionHandler handler) {
        if (handler != null) {
            handler.completed();
        }
    }

}