org.apache.derby.iapi.types
Class ReaderToUTF8Stream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.derby.iapi.types.ReaderToUTF8Stream
All Implemented Interfaces:
java.io.Closeable

public final class ReaderToUTF8Stream
extends java.io.InputStream

Converts a java.io.Reader to the on-disk UTF8 format used by Derby for character types.


Field Summary
private  int blen
           
private  int boff
           
private  byte[] buffer
           
private static int BUFSIZE
           
private  int charsToTruncate
          Number of characters to truncate from this stream The SQL standard allows for truncation of trailing spaces for clobs,varchar,char.
private  boolean eof
           
private  int maximumLength
          The maximum allowed length of the stream.
private  boolean multipleBuffer
           
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
          Length of the final value, after truncation if any, in characters.
 
Constructor Summary
ReaderToUTF8Stream(java.io.Reader appReader, int valueLength, int numCharsToTruncate, java.lang.String typeName)
          Create a stream that will truncate trailing blanks if required/allowed.
ReaderToUTF8Stream(java.io.Reader appReader, int maximumLength, java.lang.String typeName)
          Create a UTF-8 stream for a length less application reader.
 
Method Summary
 int available()
          Return an optimized version of bytes available to read from the stream Note, it is not exactly per java.io.InputStream#available()
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)
           
 int read()
          read from stream; characters converted to utf-8 derby specific encoding.
 int read(byte[] b, int off, int len)
           
private  void truncate()
          Attempt to truncate the stream by removing trailing blanks.
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

reader

private LimitReader reader
Application's reader wrapped in a LimitReader.


buffer

private byte[] buffer

boff

private int boff

blen

private int blen

eof

private boolean eof

multipleBuffer

private boolean multipleBuffer

BUFSIZE

private static final int BUFSIZE
See Also:
Constant Field Values

charsToTruncate

private final int charsToTruncate
Number of characters to truncate from this stream The SQL standard allows for truncation of trailing spaces for clobs,varchar,char. If zero, no characters are truncated.


SPACE

private static final char SPACE
See Also:
Constant Field Values

valueLength

private final int valueLength
Length of the final value, after truncation if any, in characters. this stream needs to fit into a column of colWidth if truncation error happens ,then the error message includes information about the column width.


maximumLength

private final int maximumLength
The maximum allowed length of the stream.


typeName

private final java.lang.String typeName
The type name for the column data is inserted into.

Constructor Detail

ReaderToUTF8Stream

public ReaderToUTF8Stream(java.io.Reader appReader,
                          int valueLength,
                          int numCharsToTruncate,
                          java.lang.String typeName)
Create a stream that will truncate trailing blanks if required/allowed. If the stream must be truncated, the number of blanks to truncate is specified to allow the stream to be checked for exact length, as required by JDBC 3.0. If the stream is shorter or longer than specified, an exception is thrown during read.

Parameters:
appReader - application reader
valueLength - the length of the reader in characters
numCharsToTruncate - the number of trailing blanks to truncate
typeName - type name of the column data is inserted into

ReaderToUTF8Stream

public ReaderToUTF8Stream(java.io.Reader appReader,
                          int maximumLength,
                          java.lang.String typeName)
Create a UTF-8 stream for a length less application reader. A limit is placed on the length of the reader. If the reader exceeds the maximum length, truncation of trailing blanks is attempted. If truncation fails, an exception is thrown.

Parameters:
appReader - application reader
maximumLength - maximum allowed length in number of characters for the reader
typeName - type name of the column data is inserted into
Throws:
java.lang.IllegalArgumentException - if maximum length is negative, or type name is null
Method Detail

read

public int read()
         throws java.io.IOException
read from stream; characters converted to utf-8 derby specific encoding. If stream has been read, and eof reached, in that case any subsequent read will throw an EOFException

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.read()

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

fillBuffer

private void fillBuffer(int startingOffset)
                 throws java.io.IOException
Throws:
java.io.IOException

checkSufficientData

private void checkSufficientData()
                          throws java.io.IOException
Validate the length of the stream, take corrective action if allowed. JDBC 3.0 (from tutorial book) requires that an input stream has the correct number of bytes in the stream. If the stream is too long, trailing blank truncation is attempted if allowed. If truncation fails, or is disallowed, an exception is thrown.

Throws:
java.io.IOException - if an errors occurs in the application stream
DerbyIOException - 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 length

canTruncate

private boolean canTruncate()
Determine if trailing blank truncation is allowed.


truncate

private void truncate()
               throws java.io.IOException
Attempt to truncate the stream by removing trailing blanks.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
return resources

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

available

public final int available()
Return an optimized version of bytes available to read from the stream Note, it is not exactly per java.io.InputStream#available()

Overrides:
available in class java.io.InputStream

Built on Tue 2008-05-20 18:49:31-0700, from revision 658522

Apache Derby V10.5 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.