org.jibx.runtime.impl
Class InputStreamWrapper

java.lang.Object
  extended by org.jibx.runtime.impl.InputStreamWrapper

public class InputStreamWrapper
extends java.lang.Object

Wrapper for input buffer that supports multiple character encodings. This is needed because the XPP3 pull parser does not support detecting the character encoding for a document based on the content of the document. If used with a common encoding this performs the conversion to characters using an inner reader class; otherwise, this creates the appropriate reader type

Author:
Dennis M. Sosnoski

Nested Class Summary
private  class InputStreamWrapper.WrappedStream
          Stream that just uses the enclosing class to buffer input from the wrapped stream.
private  class InputStreamWrapper.WrappedStreamISO88591Reader
          Reader for input stream using ISO8859-1 encoding.
private  class InputStreamWrapper.WrappedStreamUTF8Reader
          Reader for input stream using UTF-8 encoding.
 
Field Summary
private  byte[] m_buffer
          Cached reference to byte array used by buffer.
private  IInByteBuffer m_byteBuffer
          Input buffer.
private  int m_emptyOffset
          Current offset for generating character from buffer.
private  java.lang.String m_encodingName
          Name of encoding to be used for stream.
private  int m_endOffset
          Offset past end of bytes in buffer.
private  int m_scanOffset
          Scan position offset used for lookahead in buffer.
 
Constructor Summary
InputStreamWrapper()
           
 
Method Summary
 void close()
          Close document input.
private  boolean fillBuffer()
          Reads data into the buffer.
 IInByteBuffer getBuffer()
          Get input buffer.
 java.lang.String getEncoding()
          Get encoding for input document.
 java.io.Reader getReader()
          Get reader for wrapped input stream.
private  boolean isWhite(int chr)
          Check if a character is XML whitespace.
private  boolean require(int min)
          Reads data into the buffer to at least a minimum number of bytes.
 void reset()
          Reset to initial state for reuse.
private  java.lang.String scanQuoted()
          Reads a quote delimited token from the scan position in the buffer.
private  java.lang.String scanToken()
          Reads a space or equals ('=') delimited token from the scan position in the buffer.
 void setBuffer(IInByteBuffer buff)
          Set the input buffer.
 void setEncoding(java.lang.String enc)
          Set encoding for stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_byteBuffer

private IInByteBuffer m_byteBuffer
Input buffer.


m_encodingName

private java.lang.String m_encodingName
Name of encoding to be used for stream.


m_buffer

private byte[] m_buffer
Cached reference to byte array used by buffer.


m_endOffset

private int m_endOffset
Offset past end of bytes in buffer.


m_emptyOffset

private int m_emptyOffset
Current offset for generating character from buffer.


m_scanOffset

private int m_scanOffset
Scan position offset used for lookahead in buffer.

Constructor Detail

InputStreamWrapper

public InputStreamWrapper()
Method Detail

setBuffer

public void setBuffer(IInByteBuffer buff)
Set the input buffer.

Parameters:
buff -

getBuffer

public IInByteBuffer getBuffer()
Get input buffer.

Returns:
buffer, null if none set

setEncoding

public void setEncoding(java.lang.String enc)
                 throws java.io.IOException
Set encoding for stream. This call is only valid if the encoding has not been set previously, and if the encoding is a recognized type.

Parameters:
enc - character encoding used for input from stream (null if to be determined from XML input)
Throws:
java.io.IOException - if unknown encoding, or encoding already set

fillBuffer

private boolean fillBuffer()
                    throws java.io.IOException
Reads data into the buffer. The actual number of bytes read by a call to this method is normally between one and the space available in the buffer array.

Returns:
true if data has been read into buffer, false if not
Throws:
java.io.IOException - on error reading from stream

require

private boolean require(int min)
                 throws java.io.IOException
Reads data into the buffer to at least a minimum number of bytes. Any retained data is first copied down to the start of the buffer array. Next, data is read from the wrapped stream into the available space in the buffer until the end of the input stream is reached or at least the requested number of bytes are present in the buffer.

Parameters:
min - number of bytes required
Returns:
true if buffer contains at least the required byte count on return, false if not
Throws:
java.io.IOException - on error reading from wrapped stream

isWhite

private boolean isWhite(int chr)
Check if a character is XML whitespace.

Parameters:
chr -
Returns:
true if whitespace, false if not

scanToken

private java.lang.String scanToken()
                            throws java.io.IOException
Reads a space or equals ('=') delimited token from the scan position in the buffer. This treats bytes in the buffer as equivalent to characters. Besides ending a token on a delimitor, it also ends a token after adding a greater-than ('>') character.

Returns:
token read from buffer
Throws:
java.io.IOException - on error reading from wrapped stream

scanQuoted

private java.lang.String scanQuoted()
                             throws java.io.IOException
Reads a quote delimited token from the scan position in the buffer. This treats bytes in the buffer as equivalent to characters, and skips past any leading whitespace.

Returns:
token read from buffer
Throws:
java.io.IOException - on error reading from wrapped stream

getReader

public java.io.Reader getReader()
                         throws java.io.IOException
Get reader for wrapped input stream. This creates and returns a reader using the appropriate encoding, if necessary reading and examining the first part of the stream (including the XML declaration, if present) to determine the encoding.

Returns:
reader
Throws:
java.io.IOException - if error reading from document or creating a reader for the encoding found

getEncoding

public java.lang.String getEncoding()
Get encoding for input document. This call may not return an accurate result until after getReader() is called.

Returns:
character encoding for input document

close

public void close()
           throws java.io.IOException
Close document input. Completes reading of document input, including closing the input medium.

Throws:
java.io.IOException - on error closing document

reset

public void reset()
Reset to initial state for reuse.



Project Web Site