Top Description Fields Constructors Methods
com.thoughtworks.xstream.core.util

public final Class XmlHeaderAwareReader

extends Reader
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .InputStream, .InputStreamReader, .PushbackInputStream, .Reader, .UnsupportedEncodingException, java.util.HashMap, .Map

A Reader that evaluates the XML header. It selects its encoding based on the encoding read with the XML header of the provided InputStream. The default encoding is UTF-8 and the version is 1.0 if the stream does not contain an XML header or the attributes are not set within the header.
Author
Jörg Schaible
Since
1.3

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private final InputStreamReader
private static final int
private static final int
private static final int
private static final int
private static final int
private final double
private static final String
Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
XmlHeaderAwareReader(final InputStream in)

Constructs an XmlHeaderAwareReader.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Closes the stream and releases any system resources associated with it.
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public String
private Map<K, V>
public double
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public void
mark(final int
Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
readAheadLimit
)

Overrides java.io.Reader.mark.

Marks the present position in the stream.
public boolean
markSupported()

Overrides java.io.Reader.markSupported.

Tells whether this stream supports the mark() operation.
public int
read()

Overrides java.io.Reader.read.

Reads a single character.
public int
read(final char[]
Destination buffer
cbuf
,
final int
Offset at which to start storing characters
offset
,
final int
Maximum number of characters to read
length
)

Implements abstract java.io.Reader.read.

Reads characters into a portion of an array.
public int
read(final char[]
Destination buffer
cbuf
)

Overrides java.io.Reader.read.

Reads characters into an array.
public boolean
ready()

Overrides java.io.Reader.ready.

Tells whether this stream is ready to be read.
public void
reset()

Overrides java.io.Reader.reset.

Resets the stream.
public long
skip(final long
The number of characters to skip
n
)

Overrides java.io.Reader.skip.

Skips characters.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.io.Reader:
nullReaderreadtransferTo

Field Detail

KEY_ENCODINGback to summary
private static final String KEY_ENCODING
KEY_VERSIONback to summary
private static final String KEY_VERSION
readerback to summary
private final InputStreamReader reader
STATE_ATTR_NAMEback to summary
private static final int STATE_ATTR_NAME
STATE_ATTR_VALUEback to summary
private static final int STATE_ATTR_VALUE
STATE_AWAIT_XML_HEADERback to summary
private static final int STATE_AWAIT_XML_HEADER
STATE_BOMback to summary
private static final int STATE_BOM
STATE_STARTback to summary
private static final int STATE_START
versionback to summary
private final double version
XML_TOKENback to summary
private static final String XML_TOKEN

Constructor Detail

XmlHeaderAwareReaderback to summary
public XmlHeaderAwareReader(final InputStream in) throws UnsupportedEncodingException, IOException

Constructs an XmlHeaderAwareReader.

Parameters
in:InputStream

the InputStream

Exceptions
UnsupportedEncodingException:
if the encoding is not supported
IOException:
occurred while reading the XML header
Since
1.3

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Doc from java.io.Reader.close.

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#close()
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

See Also
java.lang.Object#equals(java.lang.Object)
getEncodingback to summary
public String getEncoding()
Since
1.3
See Also
InputStreamReader#getEncoding()
getHeaderback to summary
private Map<K, V> getHeader(final PushbackInputStream[] in) throws IOException
getVersionback to summary
public double getVersion()
Since
1.3
See Also
InputStreamReader#getEncoding()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

See Also
java.lang.Object#hashCode()
markback to summary
public void mark(final int readAheadLimit) throws IOException

Overrides java.io.Reader.mark.

Doc from java.io.Reader.mark.

Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.

Parameters
readAheadLimit:int

Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.

Exceptions
IOException:
If the stream does not support mark(), or if some other I/O error occurs
See Also
java.io.Reader#mark(int)
markSupportedback to summary
public boolean markSupported()

Overrides java.io.Reader.markSupported.

Doc from java.io.Reader.markSupported.

Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.

Returns:boolean

true if and only if this stream supports the mark operation.

See Also
java.io.Reader#markSupported()
readback to summary
public int read() throws IOException

Overrides java.io.Reader.read.

Doc from java.io.Reader.read.

Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.

Subclasses that intend to support efficient single-character input should override this method.

Returns:int

The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#read()
readback to summary
public int read(final char[] cbuf, final int offset, final int length) throws IOException

Implements abstract java.io.Reader.read.

Doc from java.io.Reader.read.

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Parameters
cbuf:char[]

Destination buffer

offset:int

Offset at which to start storing characters

length:int

Maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#read(char[], int, int)
readback to summary
public int read(final char[] cbuf) throws IOException

Overrides java.io.Reader.read.

Doc from java.io.Reader.read.

Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If the length of cbuf is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Parameters
cbuf:char[]

Destination buffer

Returns:int

The number of characters read, or -1 if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#read(char[])
readyback to summary
public boolean ready() throws IOException

Overrides java.io.Reader.ready.

Doc from java.io.Reader.ready.

Tells whether this stream is ready to be read.

Returns:boolean

True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#ready()
resetback to summary
public void reset() throws IOException

Overrides java.io.Reader.reset.

Doc from java.io.Reader.reset.

Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

Exceptions
IOException:
If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurs
See Also
java.io.Reader#reset()
skipback to summary
public long skip(final long n) throws IOException

Overrides java.io.Reader.skip.

Doc from java.io.Reader.skip.

Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. If the stream is already at its end before this method is invoked, then no characters are skipped and zero is returned.

Parameters
n:long

The number of characters to skip

Returns:long

The number of characters actually skipped

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Reader#skip(long)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

See Also
java.lang.Object#toString()