public String write(Object in) throws IOException, JAXBException { Source src = (Source) in; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); _transformer.transform(src, new StreamResult(out)); return Base64.encodeFromByteArray(out.toByteArray()); } catch (TransformerException e) { if (src instanceof StreamSource) { StreamSource ss = (StreamSource) src; InputStream is = ss.getInputStream(); if (is != null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int ch = is.read(); ch >= 0; ch = is.read()) out.write(ch); return Base64.encodeFromByteArray(out.toByteArray()); } Reader reader = ss.getReader(); if (reader != null) { CharArrayWriter out = new CharArrayWriter(); for (int ch = reader.read(); ch >= 0; ch = reader.read()) out.write(ch); return Base64.encode(new String(out.toCharArray())); } } throw new JAXBException(e); } }