com.ibm.as400.access
Class ReaderInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.ibm.as400.access.ReaderInputStream
All Implemented Interfaces:
Closeable

public class ReaderInputStream
extends InputStream

A ReaderInputStream represents a Toolbox converter that uses stateful character conversion to convert characters into bytes. That is, it wraps an underlying Reader and reads/caches the appropriate number of characters to return the requested number of bytes. This is especially useful for mixed byte tables where the number of converted bytes is almost never the same as the number of underlying Unicode characters. This class exists primarily for use with JDBC CLOBs, but other components are free to use it as well.

For example, the following code shows two methods that perform essentially the same conversion, except one uses character converters that are part of the Java runtime, and the other uses character converters that are part of the Toolbox:

  public static InputStream getJavaConversionStream(String data, String encoding)
  {
    byte[] b = data.getBytes(encoding);
    return new java.io.ByteArrayInputStream(b);
  }

  public static InputStream getToolboxConversionStream(String data, String encoding)
  {
    StringReader r = new StringReader(data);
    return new com.ibm.as400.access.ReaderInputStream(r, encoding);
  }
 

See Also:
ConvTableReader, InputStreamReader

Constructor Summary
Constructor and Description
ReaderInputStream(Reader reader, int ccsid)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID.
ReaderInputStream(Reader reader, int ccsid, BidiConversionProperties properties)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.
ReaderInputStream(Reader reader, int ccsid, BidiConversionProperties properties, int cacheSize)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.
ReaderInputStream(Reader reader, int ccsid, int bidiStringType)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.
ReaderInputStream(Reader reader, int ccsid, int bidiStringType, int cacheSize)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.
ReaderInputStream(Reader reader, String encoding)
          Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified encoding.
 
Method Summary
Modifier and Type Method and Description
 int available()
          Returns the number of bytes stored in this ReaderInputStream's internal cache.
 void close()
          Closes this ReaderInputStream and its underlying Reader.
 int getBidiStringType()
          Returns the bi-directional string type in use by this ReaderInputStream.
 int getCacheSize()
          Returns the maximum number of characters that may be stored in the internal buffer.
 int getCcsid()
          Returns the CCSID used by this ReaderInputStream.
 String getEncoding()
          Returns the encoding used by this ReaderInputStream.
 int read()
          Reads a single byte.
 int read(byte[] buffer)
          Reads bytes into the specified array.
 int read(byte[] buffer, int offset, int length)
          Reads bytes into a portion of the specified array.
 long skip(long length)
          Skips the specified number of bytes.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReaderInputStream

public ReaderInputStream(Reader reader,
                         String encoding)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified encoding.

Parameters:
reader - The Reader from which to read characters.
encoding - The name of a supported Java character encoding.
Throws:
UnsupportedEncodingException - If the specified character encoding is not supported.

ReaderInputStream

public ReaderInputStream(Reader reader,
                         int ccsid)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID.

Parameters:
reader - The Reader from which to read characters.
ccsid - The CCSID used to convert characters into bytes.
Throws:
UnsupportedEncodingException - If the specified CCSID or its corresponding character encoding is not supported.

ReaderInputStream

public ReaderInputStream(Reader reader,
                         int ccsid,
                         int bidiStringType)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.

Parameters:
reader - The Reader from which to read characters.
ccsid - The CCSID used to convert characters into bytes.
bidiStringType - The bi-directional string type.
Throws:
UnsupportedEncodingException - If the specified CCSID or its corresponding character encoding is not supported.

ReaderInputStream

public ReaderInputStream(Reader reader,
                         int ccsid,
                         BidiConversionProperties properties)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.

Parameters:
reader - The Reader from which to read characters.
ccsid - The CCSID used to convert characters into bytes.
properties - The bidi conversion properties.
Throws:
UnsupportedEncodingException - If the specified CCSID or its corresponding character encoding is not supported.

ReaderInputStream

public ReaderInputStream(Reader reader,
                         int ccsid,
                         int bidiStringType,
                         int cacheSize)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.

Parameters:
reader - The Reader from which to read characters.
ccsid - The CCSID used to convert characters into bytes.
bidiStringType - The bi-directional string type.
cacheSize - The number of characters to store in the internal buffer. The default is 1024. This number must be greater than zero.
Throws:
UnsupportedEncodingException - If the specified CCSID or its corresponding character encoding is not supported.

ReaderInputStream

public ReaderInputStream(Reader reader,
                         int ccsid,
                         BidiConversionProperties properties,
                         int cacheSize)
                  throws UnsupportedEncodingException
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID and bi-directional string type.

Parameters:
reader - The Reader from which to read characters.
ccsid - The CCSID used to convert characters into bytes.
properties - The bidi conversion properties.
cacheSize - The number of characters to store in the internal buffer. The default is 1024. This number must be greater than zero.
Throws:
UnsupportedEncodingException - If the specified CCSID or its corresponding character encoding is not supported.
Method Detail

available

public int available()
              throws IOException
Returns the number of bytes stored in this ReaderInputStream's internal cache.

Overrides:
available in class InputStream
Returns:
The number of bytes available to be read without calling the underlying Reader.
Throws:
IOException

close

public void close()
           throws IOException
Closes this ReaderInputStream and its underlying Reader. Calling close() multiple times will not throw an exception.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException - If an I/O exception occurs.

getBidiStringType

public int getBidiStringType()
Returns the bi-directional string type in use by this ReaderInputStream.

Returns:
The bi-directional string type.

getCacheSize

public int getCacheSize()
Returns the maximum number of characters that may be stored in the internal buffer. This number represents the number of characters that may be read out of the underlying Reader any time a read() method is called on this ReaderInputStream.

Returns:
The size of the character cache in use by this ReaderInputStream.

getCcsid

public int getCcsid()
Returns the CCSID used by this ReaderInputStream.

Returns:
The CCSID, or -1 if the CCSID is not known.

getEncoding

public String getEncoding()
Returns the encoding used by this ReaderInputStream. If the CCSID is not known, the encoding provided on the constructor is returned. Otherwise, the corresponding encoding for the CCSID is returned, which may be null if no such mapping exists.

Returns:
The encoding, or null if the encoding is not known.

read

public int read()
         throws IOException
Reads a single byte. If close() is called prior to calling this method, an exception will be thrown.

Specified by:
read in class InputStream
Returns:
The byte read, or -1 if the end of the stream has been reached.
Throws:
IOException - If an I/O exception occurs.

read

public int read(byte[] buffer)
         throws IOException
Reads bytes into the specified array. If close() is called prior to calling this method, an exception will be thrown.

Overrides:
read in class InputStream
Parameters:
buffer - The destination buffer.
Returns:
The number of bytes read, or -1 if the end of the stream has been reached.
Throws:
IOException - If an I/O exception occurs.

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws IOException
Reads bytes into a portion of the specified array. If close() is called prior to calling this method, an exception will be thrown.

Overrides:
read in class InputStream
Parameters:
buffer - The destination buffer.
offset - The offset into the buffer at which to begin storing data.
length - The maximum number of bytes to store.
Returns:
The number of bytes read, or -1 if the end of the stream has been reached.
Throws:
IOException - If an I/O exception occurs.

skip

public long skip(long length)
          throws IOException
Skips the specified number of bytes. If close() is called prior to calling this method, an exception will be thrown.

Overrides:
skip in class InputStream
Parameters:
length - The number of bytes to skip.
Returns:
The number of bytes actually skipped.
Throws:
IOException - If an I/O exception occurs.