com.ibm.as400.access
Class ReaderInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.ibm.as400.access.ReaderInputStream
-
- All Implemented Interfaces:
- java.io.Closeable
public class ReaderInputStream extends java.io.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
Constructors Constructor and Description ReaderInputStream(java.io.Reader reader, int ccsid)
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified CCSID.ReaderInputStream(java.io.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(java.io.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(java.io.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(java.io.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(java.io.Reader reader, java.lang.String encoding)
Constructs a ReaderInputStream that will convert Unicode characters into bytes of the specified encoding.
-
Method Summary
Methods 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.java.lang.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.
-
-
-
Constructor Detail
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.lang.String encoding) throws java.io.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:
java.io.UnsupportedEncodingException
- If the specified character encoding is not supported.
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, int ccsid) throws java.io.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:
java.io.UnsupportedEncodingException
- If the specified CCSID or its corresponding character encoding is not supported.
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, int ccsid, int bidiStringType) throws java.io.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
- Thebi-directional string type
.- Throws:
java.io.UnsupportedEncodingException
- If the specified CCSID or its corresponding character encoding is not supported.
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, int ccsid, BidiConversionProperties properties) throws java.io.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:
java.io.UnsupportedEncodingException
- If the specified CCSID or its corresponding character encoding is not supported.
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, int ccsid, int bidiStringType, int cacheSize) throws java.io.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
- Thebi-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:
java.io.UnsupportedEncodingException
- If the specified CCSID or its corresponding character encoding is not supported.
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, int ccsid, BidiConversionProperties properties, int cacheSize) throws java.io.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:
java.io.UnsupportedEncodingException
- If the specified CCSID or its corresponding character encoding is not supported.
-
-
Method Detail
-
available
public int available() throws java.io.IOException
Returns the number of bytes stored in this ReaderInputStream's internal cache.- Overrides:
available
in classjava.io.InputStream
- Returns:
- The number of bytes available to be read without calling the underlying Reader.
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Closes this ReaderInputStream and its underlying Reader. Calling close() multiple times will not throw an exception.- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.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 java.lang.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 java.io.IOException
Reads a single byte. If close() is called prior to calling this method, an exception will be thrown.- Specified by:
read
in classjava.io.InputStream
- Returns:
- The byte read, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException
- If an I/O exception occurs.
-
read
public int read(byte[] buffer) throws java.io.IOException
Reads bytes into the specified array. If close() is called prior to calling this method, an exception will be thrown.- Overrides:
read
in classjava.io.InputStream
- Parameters:
buffer
- The destination buffer.- Returns:
- The number of bytes read, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException
- If an I/O exception occurs.
-
read
public int read(byte[] buffer, int offset, int length) throws java.io.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 classjava.io.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:
java.io.IOException
- If an I/O exception occurs.
-
skip
public long skip(long length) throws java.io.IOException
Skips the specified number of bytes. If close() is called prior to calling this method, an exception will be thrown.- Overrides:
skip
in classjava.io.InputStream
- Parameters:
length
- The number of bytes to skip.- Returns:
- The number of bytes actually skipped.
- Throws:
java.io.IOException
- If an I/O exception occurs.
-
-