OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
182 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.doml;
15
 
16
import java.io.IOException;
17
import java.io.Reader;
18
 
19
public class PushBackReader extends Reader {
20
    /**
21
     * The underlying character-input stream.
22
     */
23
    protected Reader in;
24
    /** Pushback buffer */
25
    private char[] buf;
26
 
27
    /** Current position in buffer */
28
    private int pos;
29
 
30
    /**
31
     * Creates a new pushback reader with a pushback buffer of the given size.
32
     *
33
     * @param in The reader from which characters will be read
34
     * @param size The size of the pushback buffer
35
     * @exception IllegalArgumentException if {@code size <= 0}
36
     */
37
    public PushBackReader(Reader in, int size) {
38
        this.in = in;
39
        if (size <= 0) {
40
            throw new IllegalArgumentException("size <= 0");
41
        }
42
        this.buf = new char[size];
43
        this.pos = size;
44
    }
45
 
46
    /**
47
     * Creates a new pushback reader with a one-character pushback buffer.
48
     *
49
     * @param in The reader from which characters will be read
50
     */
51
    public PushBackReader(Reader in) {
52
        this(in, 1);
53
    }
54
 
55
    /**
56
     * Reads a single character.
57
     *
58
     * @return The character read, or -1 if the end of the stream has been reached
59
     *
60
     * @exception IOException If an I/O error occurs
61
     */
62
    @Override
63
    public int read() throws IOException {
64
        if (pos < this.buf.length)
65
            return this.buf[pos++];
66
        else
67
            return this.in.read();
68
    }
69
 
70
    /**
71
     * Pushes back a single character by copying it to the front of the pushback buffer. After this
72
     * method returns, the next character to be read will have the value <code>(char)c</code>.
73
     *
74
     * @param c The int value representing a character to be pushed back
75
     *
76
     * @exception IOException If the pushback buffer is full, or if some other I/O error occurs
77
     */
78
    public void unread(int c) throws IOException {
79
        this.buf[--pos] = (char) c;
80
    }
81
 
82
    /**
83
     * Marks the present position in the stream. The <code>mark</code> for class
84
     * <code>PushbackReader</code> always throws an exception.
85
     *
86
     * @exception IOException Always, since mark is not supported
87
     */
88
    @Override
89
    public void mark(int readAheadLimit) throws IOException {
90
        throw new IOException("mark/reset not supported");
91
    }
92
 
93
    /**
94
     * Resets the stream. The <code>reset</code> method of <code>PushbackReader</code> always throws
95
     * an exception.
96
     *
97
     * @exception IOException Always, since reset is not supported
98
     */
99
    @Override
100
    public void reset() throws IOException {
101
        throw new IOException("mark/reset not supported");
102
    }
103
 
104
    /**
105
     * Tells whether this stream supports the mark() operation, which it does not.
106
     */
107
    @Override
108
    public boolean markSupported() {
109
        return false;
110
    }
111
 
112
    /**
113
     * Closes the stream and releases any system resources associated with it. Once the stream has
114
     * been closed, further read(), unread(), ready(), or skip() invocations will throw an
115
     * IOException. Closing a previously closed stream has no effect.
116
     *
117
     * @exception IOException If an I/O error occurs
118
     */
119
    @Override
120
    public void close() throws IOException {
121
        this.in.close();
122
        this.buf = null;
123
    }
124
 
125
    @Override
126
    public int read(char[] cbuf, int off, int len) throws IOException {
127
        throw new IOException("read not supported");
128
    }
129
 
130
}