Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.utils.doml;
import java.io.IOException;
import java.io.Reader;
public class PushBackReader extends Reader {
/**
* The underlying character-input stream.
*/
protected Reader in;
/** Pushback buffer */
private char[] buf;
/** Current position in buffer */
private int pos;
/**
* Creates a new pushback reader with a pushback buffer of the given size.
*
* @param in The reader from which characters will be read
* @param size The size of the pushback buffer
* @exception IllegalArgumentException if {@code size <= 0}
*/
public PushBackReader(Reader in, int size) {
this.in = in;
if (size <= 0) {
throw new IllegalArgumentException("size <= 0");
}
this.buf = new char[size];
this.pos = size;
}
/**
* Creates a new pushback reader with a one-character pushback buffer.
*
* @param in The reader from which characters will be read
*/
public PushBackReader(Reader in) {
this(in, 1);
}
/**
* Reads a single character.
*
* @return The character read, or -1 if the end of the stream has been reached
*
* @exception IOException If an I/O error occurs
*/
@Override
public int read() throws IOException {
if (pos < this.buf.length)
return this.buf[pos++];
else
return this.in.read();
}
/**
* Pushes back a single character by copying it to the front of the pushback buffer. After this
* method returns, the next character to be read will have the value <code>(char)c</code>.
*
* @param c The int value representing a character to be pushed back
*
* @exception IOException If the pushback buffer is full, or if some other I/O error occurs
*/
public void unread(int c) throws IOException {
this.buf[--pos] = (char) c;
}
/**
* Marks the present position in the stream. The <code>mark</code> for class
* <code>PushbackReader</code> always throws an exception.
*
* @exception IOException Always, since mark is not supported
*/
@Override
public void mark(int readAheadLimit) throws IOException {
throw new IOException("mark/reset not supported");
}
/**
* Resets the stream. The <code>reset</code> method of <code>PushbackReader</code> always throws
* an exception.
*
* @exception IOException Always, since reset is not supported
*/
@Override
public void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
/**
* Tells whether this stream supports the mark() operation, which it does not.
*/
@Override
public boolean markSupported() {
return false;
}
/**
* Closes the stream and releases any system resources associated with it. Once the stream has
* been closed, further read(), unread(), ready(), or skip() invocations will throw an
* IOException. Closing a previously closed stream has no effect.
*
* @exception IOException If an I/O error occurs
*/
@Override
public void close() throws IOException {
this.in.close();
this.buf = null;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new IOException("read not supported");
}
}