aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/reader/NamedReader.java
blob: f54caa225b0315dcc5ddc0f5cfeb501ed2043894 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io.reader;

import com.yahoo.api.annotations.Beta;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.CharBuffer;
import java.util.List;

/**
 * A reader with a name. All reader methods are delegated to the wrapped reader.
 *
 * @author bratseth
 */
@Beta
public class NamedReader extends Reader {

    private final String name;
    private final Reader reader;

    public NamedReader(String name, Reader reader) {
        this.name = name;
        this.reader = reader;
    }

    public String getName() { return name; }

    public Reader getReader() { return reader; }

    /** Returns the name */
    @Override
    public String toString() {
        return name;
    }

    // The rest is reader method implementations which delegates to the wrapped reader
    public static Reader nullReader() { return new NamedReader("nullReader", Reader.nullReader()); }
    @Override
    public int read(CharBuffer charBuffer) throws IOException { return reader.read(charBuffer); }
    @Override
    public int read() throws IOException { return reader.read(); }
    @Override
    public int read(char[] chars) throws IOException { return reader.read(chars); }
    @Override
    public int read(char[] chars, int i, int i1) throws IOException { return reader.read(chars,i,i1); }
    @Override
    public long skip(long l) throws IOException { return reader.skip(l); }
    @Override
    public boolean ready() throws IOException { return reader.ready(); }
    @Override
    public boolean markSupported() { return reader.markSupported(); }
    @Override
    public void mark(int i) throws IOException { reader.mark(i); }
    @Override
    public void reset() throws IOException { reader.reset(); }
    @Override
    public void close() throws IOException { reader.close(); }
    @Override
    public long transferTo(Writer out) throws IOException { return reader.transferTo(out); }

    /** Convenience method for closing a list of readers. Does nothing if the given reader list is null. */
    public static void closeAll(List<NamedReader> readers) {
        if (readers==null) return;
        for (Reader reader : readers) {
            try {
                reader.close();
            }
            catch (IOException e) {
                // Nothing to do about it
            }
        }
    }

}