This isn't a bug in XStream per se. This is actually a problem with the underlying XML parser, which doesn't recognized a BOM (http://en.wikipedia.org/wiki/Byte-order_mark) in the InputStream.
After a bit of experimentation, it looks like only the six Xpp-related drivers (subclasses of AbstractXppDomDriver and AbstractXppDriver) can handle the BOM in your example (which may be because of the XmlHeaderAwareReader used in both classes). Luckily, there's a simple way to make all Drivers work with BOM; wrap them in an InputStreamReader which handles character encoding. The default encoding for the InputStreamReader handles the BOM nicely.
In your test, just change line 34 from:
to
xStream.fromXML(new InputStreamReader(bin));
If you don't want to worry about remembering to do that with every raw InputStream, you can always patch the XStream class:
Change:
/**
* Deserialize an object from an XML InputStream.
*
* @throws XStreamException if the object cannot be deserialized
*/
public Object fromXML(InputStream input) {
return unmarshal(hierarchicalStreamDriver.createReader(input), null);
}
to:
/**
* Deserialize an object from an XML InputStream.
*
* @throws XStreamException if the object cannot be deserialized
*/
public Object fromXML(InputStream input) {
return fromXML(new InputStreamReader(input));
}
Of course, you're now assuming that the default encoding for InputStreamReader will work for all incoming InputStreams. In my experience, it's a pretty good bet, since that's what FileReader, which subclasses InputStreamReader, assumes, but I'd probably just do the InputStreamReader wrapping each time.
This isn't a bug in XStream per se. This is actually a problem with the underlying XML parser, which doesn't recognized a BOM (http://en.wikipedia.org/wiki/Byte-order_mark) in the InputStream.
After a bit of experimentation, it looks like only the six Xpp-related drivers (subclasses of AbstractXppDomDriver and AbstractXppDriver) can handle the BOM in your example (which may be because of the XmlHeaderAwareReader used in both classes). Luckily, there's a simple way to make all Drivers work with BOM; wrap them in an InputStreamReader which handles character encoding. The default encoding for the InputStreamReader handles the BOM nicely.
In your test, just change line 34 from:
to
xStream.fromXML(new InputStreamReader(bin));
If you don't want to worry about remembering to do that with every raw InputStream, you can always patch the XStream class:
Change:
to:
Of course, you're now assuming that the default encoding for InputStreamReader will work for all incoming InputStreams. In my experience, it's a pretty good bet, since that's what FileReader, which subclasses InputStreamReader, assumes, but I'd probably just do the InputStreamReader wrapping each time.