Wednesday, September 4, 2013

Java - Invalid byte 2 of 2-byte UTF-8 sequence

ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
Use UTF-8:
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));

Likewise don't use ByteArrayOutputStream.toString(), which again uses the platform default encoding. Indeed, you don't need to convert the output to a string at all:

ByteArrayOutputStream os = new ByteArrayOutputStream();
marshaller.marshal(c, os);
byte[] xml = os.toByteArray();
jc = JAXBContext.newInstance(Candidate.class);
Unmarshaller jaxb = jc.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(xml);

0 comments:

Post a Comment