Top Description Fields Constructors Methods
org.apache.derby.impl.jdbc

public final Class UTF8Reader

extends Reader
Class Inheritance
Imports
java.io.BufferedInputStream, .InputStream, .Reader, .IOException, .UTFDataFormatException, .EOFException, java.sql.SQLException, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.jdbc.CharacterStreamDescriptor, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.types.PositionedStream

Class for reading characters from streams encoded in the modified UTF-8 format.

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.

See Also
PositionedStoreStream

Field Summary

Modifier and TypeField and Description
private final char[]
buffer

Internal character buffer storing characters read from the stream.

private int
charactersInBuffer

The number of characters in the internal buffer.

private final CharacterStreamDescriptor
csd

Descriptor containing information about the stream.

private InputStream
in

The underlying data stream.

private static final 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 final 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 (positionedIn != null).

private static final String
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.

Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
UTF8Reader(CharacterStreamDescriptor
a description of and reference to the source stream
csd
,
ConnectionChild
the parent object / connection child
conChild
,
Object
synchronization object used when accessing the underlying data stream
sync
)

Constructs a reader on top of the source UTF-8 encoded stream.

Method Summary

Modifier and TypeMethod and Description
private final int

Returns:

An (sub)optimal buffer size.
calculateBufferSize
(CharacterStreamDescriptor
stream descriptor
csd
)

Calculates an optimized buffer size.

public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Close the reader, disallowing further reads.

private void
closeIn()

Close the underlying stream if it is open.

private boolean

Returns:

true if the end of the stream is reached, false if there is apparently more data to be read.
fillBuffer
()

Fills the internal character buffer by decoding bytes from the stream.

private final void
persistentSkip(long
number of characters to skip
toSkip
)

Skips the requested number of characters.

public int

Returns:

A character or -1 if end of stream has been reached.
read
()

Overrides java.io.Reader.read.

Reads a single character from the stream.

public int

Returns:

The number of characters read, or -1 if the end of the stream has been reached.
read
(char[]
Destination buffer
cbuf
,
int
Offset at which to start storing characters
off
,
int
Maximum number of characters to read
len
)

Implements abstract java.io.Reader.read.

Reads characters into an array.

pack-priv int

Returns:

The number of characters read, or -1 if the end of the stream is reached.
readAsciiInto
(byte[]
the buffer to read into
abuf
,
int
the offset into the destination buffer
off
,
int
maximum number of characters to read
len
)

Reads characters into an array as ASCII characters.

public int

Returns:

The number of characters read, or -1 if the end of the stream is reached.
readInto
(StringBuffer
the destination buffer
sb
,
int
maximum number of characters to read
len
)

Reads characters from the stream.

pack-priv void
reposition(long
1-based requested character position
requestedCharPos
)

Repositions the stream so that the next character read will be the character at the requested position.

private void
resetUTF8Reader()

Resets the reader.

public long

Returns:

The number of characters actually skipped.
skip
(long
the numbers of characters to skip
len
)

Overrides java.io.Reader.skip.

Skips characters.

private IOException
utfFormatException(String s)

Convenience method generating an UTFDataFormatException and cleaning up the reader state.

Inherited from java.io.Reader:
markmarkSupportednullReaderreadreadreadyresettransferTo

Field Detail

bufferback to summary
private final char[] buffer

Internal character buffer storing characters read from the stream.

charactersInBufferback to summary
private int charactersInBuffer

The number of characters in the internal buffer.

csdback to summary
private final CharacterStreamDescriptor csd

Descriptor containing information about the stream. Except for the current positions, the information in this object is considered permanent and valid for the life-time of the stream.

inback to summary
private InputStream in

The underlying data stream.

MAXIMUM_BUFFER_SIZEback to summary
private static final int MAXIMUM_BUFFER_SIZE

Maximum size in number of chars for the internal character buffer.

noMoreReadsback to summary
private boolean noMoreReads

Tells if this reader has been closed.

parentback to summary
private ConnectionChild parent

A reference to the parent object of the stream.

The reference is kept so that the parent object can't get garbage collected until we are done with the stream.

positionedInback to summary
private final PositionedStream positionedIn

Stream that can reposition itself on request (may be null).

rawStreamPosback to summary
private long rawStreamPos

Store the last visited position in the store stream, if it is capable of repositioning itself (positionedIn != null).

READER_CLOSEDback to summary
private static final String READER_CLOSED
readerCharCountback to summary
private long readerCharCount

Number of characters read from the stream.

readPositionInBufferback to summary
private int readPositionInBuffer

The position of the next character to read in the internal buffer.

utfCountback to summary
private long utfCount

Number of bytes read from the stream, including any header bytes.

Constructor Detail

UTF8Readerback to summary
public UTF8Reader(CharacterStreamDescriptor csd, ConnectionChild conChild, Object sync) throws IOException

Constructs a reader on top of the source UTF-8 encoded stream.

Parameters
csd:CharacterStreamDescriptor

a description of and reference to the source stream

conChild:ConnectionChild

the parent object / connection child

sync:Object

synchronization object used when accessing the underlying data stream

Exceptions
IOException:
if reading from the underlying stream fails

Method Detail

calculateBufferSizeback to summary
private final int calculateBufferSize(CharacterStreamDescriptor csd)

Calculates an optimized buffer size.

The maximum size allowed is returned if the specified values don't give enough information to say a smaller buffer size is preferable.

Parameters
csd:CharacterStreamDescriptor

stream descriptor

Returns:int

An (sub)optimal buffer size.

closeback to summary
public void close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Close the reader, disallowing further reads.

closeInback to summary
private void closeIn()

Close the underlying stream if it is open.

fillBufferback to summary
private boolean fillBuffer() throws IOException

Fills the internal character buffer by decoding bytes from the stream.

Returns:boolean

true if the end of the stream is reached, false if there is apparently more data to be read.

persistentSkipback to summary
private final void persistentSkip(long toSkip) throws IOException

Skips the requested number of characters.

Parameters
toSkip:long

number of characters to skip

Exceptions
IOException:
if reading from the stream fails
EOFException:
if there are too few characters in the stream
readback to summary
public int read() throws IOException

Overrides java.io.Reader.read.

Reads a single character from the stream.

Returns:int

A character or -1 if end of stream has been reached.

Exceptions
IOException:
if the stream has been closed, or an exception is raised while reading from the underlying stream
readback to summary
public int read(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Reader.read.

Reads characters into an array.

Parameters
cbuf:char[]

Doc from java.io.Reader.read.

Destination buffer

off:int

Doc from java.io.Reader.read.

Offset at which to start storing characters

len:int

Doc from java.io.Reader.read.

Maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream has been reached.

Exceptions
IOException:

Doc from java.io.Reader.read.

If an I/O error occurs

readAsciiIntoback to summary
pack-priv int readAsciiInto(byte[] abuf, int off, int len) throws IOException

Reads characters into an array as ASCII characters.

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.

Parameters
abuf:byte[]

the buffer to read into

off:int

the offset into the destination buffer

len:int

maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream is reached.

readIntoback to summary
public int readInto(StringBuffer sb, int len) throws IOException

Reads characters from the stream.

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.

Parameters
sb:StringBuffer

the destination buffer

len:int

maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream is reached.

repositionback to summary
pack-priv void reposition(long requestedCharPos) throws IOException, StandardException

Repositions the stream so that the next character read will be the character at the requested position.

There are three types of repositioning, ordered after increasing cost:

  1. Reposition within current character buffer (small hops forwards and potentially backwards - in range 1 char to MAXIMUM_BUFFER_SIZE chars)
  2. Forward stream from current position (hops forwards)
  3. Reset stream and skip data (hops backwards)
Parameters
requestedCharPos:long

1-based requested character position

Exceptions
IOException:
if resetting or reading from the stream fails
StandardException:
if resetting the stream fails
resetUTF8Readerback to summary
private void resetUTF8Reader() throws IOException, StandardException

Resets the reader.

This method is used internally to achieve better performance.

Exceptions
IOException:
if resetting or reading from the stream fails
StandardException:
if resetting the stream fails
See Also
reposition(long)
skipback to summary
public long skip(long len) throws IOException

Overrides java.io.Reader.skip.

Skips characters.

Parameters
len:long

the numbers of characters to skip

Returns:long

The number of characters actually skipped.

Exceptions
IOException:
if accessing the underlying stream fails
IllegalArgumentException:
if the number of characters to skip is negative
utfFormatExceptionback to summary
private IOException utfFormatException(String s)

Convenience method generating an UTFDataFormatException and cleaning up the reader state.