public final class UTF8Reader
extends java.io.Reader
Note that we often operate on a special Derby stream.
A Derby stream is possibly different from a "normal" stream in two ways;
an encoded length is inserted at the head of the stream, and if the encoded
length is 0
a Derby-specific end of stream marker is appended
to the data.
If the underlying stream is capable of repositioning itself on request, this class supports multiple readers on the same source stream in such a way that the various readers do not interfere with each other (except for serializing access). Each reader instance will have its own pointer into the stream, and request that the stream repositions itself before calling read/skip on the stream.
PositionedStoreStream
Modifier and Type | Field and Description |
---|---|
private char[] |
buffer
Internal character buffer storing characters read from the stream.
|
private int |
charactersInBuffer
The number of characters in the internal buffer.
|
private CharacterStreamDescriptor |
csd
Descriptor containing information about the stream.
|
private java.io.InputStream |
in
The underlying data stream.
|
private static int |
MAXIMUM_BUFFER_SIZE
Maximum size in number of chars for the internal character buffer.
|
private boolean |
noMoreReads
Tells if this reader has been closed.
|
private ConnectionChild |
parent
A reference to the parent object of the stream.
|
private PositionedStream |
positionedIn
Stream that can reposition itself on request (may be
null ). |
private long |
rawStreamPos
Store the last visited position in the store stream, if it is capable of
repositioning itself ({@code positionedIn !
|
private static java.lang.String |
READER_CLOSED |
private long |
readerCharCount
Number of characters read from the stream.
|
private int |
readPositionInBuffer
The position of the next character to read in the internal buffer.
|
private long |
utfCount
Number of bytes read from the stream, including any header bytes.
|
Constructor and Description |
---|
UTF8Reader(CharacterStreamDescriptor csd,
ConnectionChild conChild,
java.lang.Object sync)
Constructs a reader on top of the source UTF-8 encoded stream.
|
Modifier and Type | Method and Description |
---|---|
private int |
calculateBufferSize(CharacterStreamDescriptor csd)
Calculates an optimized buffer size.
|
void |
close()
Close the reader, disallowing further reads.
|
private void |
closeIn()
Close the underlying stream if it is open.
|
private boolean |
fillBuffer()
Fills the internal character buffer by decoding bytes from the stream.
|
private void |
persistentSkip(long toSkip)
Skips the requested number of characters.
|
int |
read()
Reads a single character from the stream.
|
int |
read(char[] cbuf,
int off,
int len)
Reads characters into an array.
|
(package private) int |
readAsciiInto(byte[] abuf,
int off,
int len)
Reads characters into an array as ASCII characters.
|
int |
readInto(java.lang.StringBuffer sb,
int len)
Reads characters from the stream.
|
(package private) void |
reposition(long requestedCharPos)
Repositions the stream so that the next character read will be the
character at the requested position.
|
private void |
resetUTF8Reader()
Resets the reader.
|
long |
skip(long len)
Skips characters.
|
private java.io.IOException |
utfFormatException(java.lang.String s)
Convenience method generating an
UTFDataFormatException and
cleaning up the reader state. |
private static final java.lang.String READER_CLOSED
private static final int MAXIMUM_BUFFER_SIZE
private java.io.InputStream in
private final PositionedStream positionedIn
null
).private long rawStreamPos
positionedIn != null
).private long utfCount
private long readerCharCount
private final char[] buffer
private int charactersInBuffer
private int readPositionInBuffer
private boolean noMoreReads
private ConnectionChild parent
The reference is kept so that the parent object can't get garbage collected until we are done with the stream.
private final CharacterStreamDescriptor csd
public UTF8Reader(CharacterStreamDescriptor csd, ConnectionChild conChild, java.lang.Object sync) throws java.io.IOException
csd
- a description of and reference to the source streamconChild
- the parent object / connection childsync
- synchronization object used when accessing the underlying
data streamjava.io.IOException
- if reading from the underlying stream failspublic int read() throws java.io.IOException
read
in class java.io.Reader
-1
if end of stream has been reached.java.io.IOException
- if the stream has been closed, or an exception is
raised while reading from the underlying streampublic int read(char[] cbuf, int off, int len) throws java.io.IOException
read
in class java.io.Reader
-1
if the end of
the stream has been reached.java.io.IOException
public long skip(long len) throws java.io.IOException
skip
in class java.io.Reader
len
- the numbers of characters to skipjava.lang.IllegalArgumentException
- if the number of characters to skip is
negativejava.io.IOException
- if accessing the underlying stream failspublic void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Reader
public int readInto(java.lang.StringBuffer sb, int len) throws java.io.IOException
Due to internal buffering a smaller number of characters than what is
requested might be returned. To ensure that the request is fulfilled,
call this method in a loop until the requested number of characters is
read or -1
is returned.
sb
- the destination bufferlen
- maximum number of characters to read-1
if the end of
the stream is reached.java.io.IOException
int readAsciiInto(byte[] abuf, int off, int len) throws java.io.IOException
Due to internal buffering a smaller number of characters than what is
requested might be returned. To ensure that the request is fulfilled,
call this method in a loop until the requested number of characters is
read or -1
is returned.
Characters outside the ASCII range are replaced with an out of range marker.
abuf
- the buffer to read intooff
- the offset into the destination bufferlen
- maximum number of characters to read-1
if the end of
the stream is reached.java.io.IOException
private void closeIn()
private java.io.IOException utfFormatException(java.lang.String s)
UTFDataFormatException
and
cleaning up the reader state.private boolean fillBuffer() throws java.io.IOException
true
if the end of the stream is reached,
false
if there is apparently more data to be read.java.io.IOException
private void resetUTF8Reader() throws java.io.IOException, StandardException
This method is used internally to achieve better performance.
java.io.IOException
- if resetting or reading from the stream failsStandardException
- if resetting the stream failsreposition(long)
void reposition(long requestedCharPos) throws java.io.IOException, StandardException
There are three types of repositioning, ordered after increasing cost:
MAXIMUM_BUFFER_SIZE
chars)requestedCharPos
- 1-based requested character positionjava.io.IOException
- if resetting or reading from the stream failsStandardException
- if resetting the stream failsprivate final int calculateBufferSize(CharacterStreamDescriptor csd)
The maximum size allowed is returned if the specified values don't give enough information to say a smaller buffer size is preferable.
csd
- stream descriptorprivate final void persistentSkip(long toSkip) throws java.io.IOException
toSkip
- number of characters to skipjava.io.EOFException
- if there are too few characters in the streamjava.io.IOException
- if reading from the stream failsApache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.