public class ReversibleReader
extends java.io.FilterReader
Modifier and Type | Field and Description |
---|---|
private int |
idx
Index of the "front" of the queue (the next char to be read).
|
private static int |
INCREMENT
Number of elements by which to grow or shrink the array.
|
private char[] |
queue
LIFO queue for data that has been "unread".
|
private static int |
THRESHOLD
Number of empty elements at which the array will be shrunk.
|
Constructor and Description |
---|
ReversibleReader(java.io.Reader reader)
Creates an instance with the specified reader as the primary data
source.
|
Modifier and Type | Method and Description |
---|---|
char[] |
clear()
Obtain the current contents of the queued (unread) data and clear
the queue completely.
|
void |
close()
Closes the stream and releases any system resources associated with
it.
|
private void |
grow(int amount)
Grow the array by the standard increment amount if it is full.
|
private int |
nextIncrement(int size)
Calculate the next multiple of INCREMENT that can hold the given
size elements. |
int |
queueSize()
Reports on the current amount of queued data.
|
int |
read()
Reads the next byte from the current stream.
|
int |
read(char[] buf,
int off,
int len)
Reads characters into a portion of an array.
|
boolean |
ready()
Tells whether this stream is ready to be read.
|
private void |
shrink()
Shrink the array to the smallest multiple of the standard increment
amount if there is enough space to reclaim while still leaving some
open slots (so new additions won't cause array growth immediately).
|
void |
unread(char[] buf)
Queue the given data on a last-in first-out (LIFO) basis.
|
void |
unread(int ch)
Queue the given data on a last-in first-out (LIFO) basis.
|
private static final int INCREMENT
private static final int THRESHOLD
private char[] queue
private int idx
public ReversibleReader(java.io.Reader reader)
reader
- The primary data source.public void unread(int ch)
ch
- The character to be unread.public void unread(char[] buf)
buf
- The data to be unread.public int queueSize()
unread(int)
AND which also has not yet
been re-read using read()
.public char[] clear()
null
if there is no queued
data.public int read() throws java.io.IOException
-1
as the indication of the
EOF
condition.read
in class java.io.FilterReader
java.io.IOException
- If any error occurs in reading from the underlying reader.public int read(char[] buf, int off, int len) throws java.io.IOException
read
in class java.io.FilterReader
buf
- Destination bufferoff
- Offset at which to start writing characterslen
- Maximum number of characters to readjava.io.IOException
- If an I/O error occurspublic boolean ready() throws java.io.IOException
ready
in class java.io.FilterReader
true
if a read request will not block.java.io.IOException
- If an I/O error occurspublic void close() throws java.io.IOException
Closing a previously closed stream has no effect.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterReader
java.io.IOException
- If an I/O error occursprivate void grow(int amount)
amount
- The amount of space needed.private void shrink()
private int nextIncrement(int size)
size
elements.size
- The amount of space needed.size
.