public final class ReaderToUTF8Stream
extends java.io.InputStream
java.io.Reader
to a stream
returning the data in the on-disk modified UTF-8 encoded representation used
by Derby.
Length validation is performed. If required and allowed by the target column type, truncation of blanks will also be performed.
Modifier and Type | Field and Description |
---|---|
private int |
blen |
private int |
boff |
private byte[] |
buffer
Buffer to hold the data read from stream and converted to the modified
UTF-8 format.
|
private int |
charCount
The number of chars encoded.
|
private int |
charsToTruncate
Number of characters to truncate from this stream.
|
private boolean |
eof |
private static int |
FIRST_READ
Constant indicating the first iteration of
fillBuffer . |
private StreamHeaderGenerator |
hdrGen
The generator for the stream header to use for this stream.
|
private int |
headerLength
The length of the header.
|
private int |
mark
Stream mark, set through mark(int).
|
private static int |
MARK_UNSET_OR_EXCEEDED
Constant indicating that no mark is set in the stream, or that the read
ahead limit of the mark has been exceeded.
|
private boolean |
multipleBuffer
Tells if the stream content is/was larger than the buffer size.
|
private static int |
READ_BUFFER_RESERVATION
Buffer space reserved for one 3 byte encoded char and the EOF marker.
|
private int |
readAheadLimit
Read ahead limit for mark, set through mark(int).
|
private LimitReader |
reader
Application's reader wrapped in a LimitReader.
|
private static char |
SPACE |
private java.lang.String |
typeName
The type name for the column data is inserted into.
|
private int |
valueLength
If positive, length of the expected final value, after truncation if any,
in characters.
|
Constructor and Description |
---|
ReaderToUTF8Stream(java.io.Reader appReader,
int valueLength,
int numCharsToTruncate,
java.lang.String typeName,
StreamHeaderGenerator headerGenerator)
Create a stream that will truncate trailing blanks if required/allowed.
|
ReaderToUTF8Stream(java.io.Reader appReader,
int maximumLength,
java.lang.String typeName,
StreamHeaderGenerator headerGenerator)
Creates a UTF-8 stream for an application reader whose length isn't
known at insertion time.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Return an optimized version of bytes available to read from
the stream.
|
private boolean |
canTruncate()
Determine if trailing blank truncation is allowed.
|
private void |
checkSufficientData()
Validate the length of the stream, take corrective action if allowed.
|
void |
close()
return resources
|
private void |
fillBuffer(int startingOffset)
Fills the internal buffer with data read from the source stream.
|
void |
mark(int readAheadLimit)
Marks the current position in the stream.
|
boolean |
markSupported()
Tests if this stream supports mark/reset.
|
int |
read()
Reads a byte from the stream.
|
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes from the stream. |
void |
reset()
Repositions this stream to the position at the time the mark method was
last called on this input stream.
|
private void |
truncate()
Attempt to truncate the stream by removing trailing blanks.
|
private LimitReader reader
private static final int FIRST_READ
fillBuffer
.private static final int READ_BUFFER_RESERVATION
private static final int MARK_UNSET_OR_EXCEEDED
private byte[] buffer
private int boff
private int blen
private int mark
private int readAheadLimit
private boolean eof
private boolean multipleBuffer
private final StreamHeaderGenerator hdrGen
checkSufficientData()
private int headerLength
private final int charsToTruncate
private static final char SPACE
private final int valueLength
private final java.lang.String typeName
private int charCount
public ReaderToUTF8Stream(java.io.Reader appReader, int valueLength, int numCharsToTruncate, java.lang.String typeName, StreamHeaderGenerator headerGenerator)
appReader
- application readervalueLength
- the expected length of the reader in characters
(positive), or the inverse (maxColWidth * -1) of the maximum column
width if the expected stream length is unknownnumCharsToTruncate
- the number of trailing blanks to truncatetypeName
- type name of the column data is inserted intoheaderGenerator
- the stream header generatorpublic ReaderToUTF8Stream(java.io.Reader appReader, int maximumLength, java.lang.String typeName, StreamHeaderGenerator headerGenerator)
The application reader is coming in through one of the "lengthless overrides" added in JDBC 4.0, for instance java.sql.PreparedStatement.setCharacterStream(int,Reader). A limit is placed on the length of the application reader. If the reader exceeds the maximum length, truncation of trailing blanks is attempted. If truncation fails, an exception is thrown.
appReader
- application readermaximumLength
- maximum allowed length in number of characters for
the reader, typically the maximum field sizetypeName
- type name of the column data is inserted intoheaderGenerator
- the stream header generatorjava.lang.IllegalArgumentException
- if maximum length is negativepublic int read() throws java.io.IOException
Characters read from the source stream are converted to the UTF-8 Derby specific encoding.
read
in class java.io.InputStream
-1
if the end-of-stream is reached.java.io.EOFException
- if the end-of-stream has already been reached or
the stream has been closedjava.io.IOException
- if reading from the source stream failsInputStream.read()
public int read(byte[] b, int off, int len) throws java.io.IOException
len
bytes from the stream.
Characters read from the source stream are converted to the UTF-8 Derby specific encoding.
read
in class java.io.InputStream
-1
if the end-of-stream is
reached.java.io.EOFException
- if the end-of-stream has already been reached or
the stream has been closedjava.io.IOException
- if reading from the source stream failsInputStream.read(byte[],int,int)
private void fillBuffer(int startingOffset) throws java.io.IOException
The characters read from the source are converted to the modified UTF-8 encoding, used as the on-disk format by Derby.
startingOffset
- offset at which to start filling the buffer, used
to avoid overwriting the stream header data on the first iterationDerbyIOException
- if the source stream has an invalid length
(different than specified), or if truncation of blanks failsjava.io.IOException
- if reading from the source stream failsprivate void checkSufficientData() throws java.io.IOException
java.io.IOException
- if an errors occurs in the application streamDerbyIOException
- if Derby finds a problem with the stream;
stream is too long and cannot be truncated, or the stream length
does not match the specified lengthprivate boolean canTruncate()
private void truncate() throws java.io.IOException
java.io.IOException
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
public final int available()
Note, it is not exactly per java.io.InputStream#available()
.
available
in class java.io.InputStream
public void mark(int readAheadLimit)
Note that this stream is not marked at position zero by default (i.e. in the constructor).
mark
in class java.io.InputStream
readAheadLimit
- the maximum limit of bytes that can be read before
the mark position becomes invalidpublic void reset() throws java.io.IOException
reset
in class java.io.InputStream
java.io.EOFException
- if the stream has been closedjava.io.IOException
- if no mark has been set, or the read ahead limit of
the mark has been exceededpublic boolean markSupported()
The markSupported
method of ByteArrayInputStream
always
returns true
.
markSupported
in class java.io.InputStream
true
, mark/reset is always supported.Apache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.