List of usage examples for javax.xml.transform.stream StreamSource setInputStream
public void setInputStream(InputStream inputStream)
From source file:Main.java
public static Source createSource(final String message, final String charFormat) throws IOException { StreamSource source = new StreamSource(); byte[] msgByte = message.getBytes(charFormat); ByteArrayInputStream in = new ByteArrayInputStream(msgByte); source.setInputStream(in); return source; }
From source file:com.meltmedia.rodimus.RodimusCli.java
public static StreamSource createStreamSource(URL url) throws IOException { StreamSource source = new StreamSource(); source.setSystemId(url.toExternalForm()); source.setInputStream(url.openStream()); return source; }
From source file:org.dataconservancy.packaging.tool.ser.XStreamMarshallerFactoryTest.java
/** * Insures that the XStreamMarshaller produced by the XStreamMarshallerFactory uses the specified encoding setting; * that is, the platform default is <em>not</em> being used. * * @throws Exception/*from w w w. j a va 2 s.c o m*/ */ @Test public void testEncodingTest() throws Exception { // Holders ByteArrayOutputStream sink = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(sink); StreamSource source = new StreamSource(); // Byte sequences for XML elements byte[] openString = "<string>".getBytes(); byte[] closeString = "</string>".getBytes(); underTest.setEncoding("UTF-8"); XStreamMarshaller utf8Marshaller = underTest.newInstance(); underTest.setEncoding("ISO-8859-1"); XStreamMarshaller iso8859Marshaller = underTest.newInstance(); // Our literal, a "LATIN SMALL LETTER N WITH TILDE" // The two byte sequence 0xC3 0xB1 in UTF-8 // The single byte sequence 0xF1 in ISO-8859-1 // Java Unicode literal: \u00F1 String literal = "\u00F1"; // "<string>".length = 8 characters, so check the 8th and 9th element of the // byte array // 0xF1 written in UTF-8 should be 0xC3B1 utf8Marshaller.marshal(literal, streamResult); assertEquals(0xC3, 0x000000FF & sink.toByteArray()[openString.length]); assertEquals(0xB1, 0x000000FF & sink.toByteArray()[openString.length + 1]); sink.reset(); // 0xF1 written in ISO-8859-1 should be 0xF1 iso8859Marshaller.marshal(literal, streamResult); assertEquals(0xF1, 0x000000FF & sink.toByteArray()[openString.length]); // Bytes representing 0xF1 encoded as UTF-8 byte[] utf8SourceBytes = new byte[openString.length + 2 + closeString.length]; System.arraycopy(openString, 0, utf8SourceBytes, 0, openString.length); utf8SourceBytes[openString.length] = (byte) 0xc3; utf8SourceBytes[openString.length + 1] = (byte) 0xb1; System.arraycopy(closeString, 0, utf8SourceBytes, openString.length + 2, closeString.length); // Bytes representing 0xF1 encoded as ISO-8859-1 byte[] iso88591bytes = new byte[openString.length + 2 + closeString.length]; System.arraycopy(openString, 0, iso88591bytes, 0, openString.length); iso88591bytes[openString.length] = (byte) 0xF1; System.arraycopy(closeString, 0, iso88591bytes, openString.length + 1, closeString.length); // The UTF-8 configured marshaller should be able to unmarshal the utf-8 bytes source.setInputStream(new ByteArrayInputStream(utf8SourceBytes)); assertEquals(literal, utf8Marshaller.unmarshal(source)); // The ISO-8859-1 configured marshaller should be able to unmarshal the ISO-8859-1 bytes source.setInputStream(new ByteArrayInputStream(iso88591bytes)); assertEquals(literal, iso8859Marshaller.unmarshal(source)); // But if the ISO-8859-1 marshaller tries to unmarshal utf-8 bytes... source.setInputStream(new ByteArrayInputStream(utf8SourceBytes)); assertNotEquals(literal, iso8859Marshaller.unmarshal(source)); // Or if the UTF-8 marshaller tries to unmarshal iso-8859-1 bytes... source.setInputStream(new ByteArrayInputStream(iso88591bytes)); assertNotEquals(literal, utf8Marshaller.unmarshal(source)); }
From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java
/** * Removes any references to InputStreams or Readers from the given Source to prohibit * accidental/unwanted use by a component further downstream. * @param src the Source object//from w w w . ja v a 2s.c o m */ public static void removeStreams(Source src) { if (src instanceof ImageSource) { ImageSource isrc = (ImageSource) src; isrc.setImageInputStream(null); } else if (src instanceof StreamSource) { StreamSource ssrc = (StreamSource) src; ssrc.setInputStream(null); ssrc.setReader(null); } else if (src instanceof SAXSource) { InputSource is = ((SAXSource) src).getInputSource(); if (is != null) { is.setByteStream(null); is.setCharacterStream(null); } } }
From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java
/** * Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring * while closing the stream is ignored./*from ww w . j a v a 2 s . com*/ * @param src the Source object */ public static void closeQuietly(Source src) { if (src == null) { return; } else if (src instanceof StreamSource) { StreamSource streamSource = (StreamSource) src; IOUtils.closeQuietly(streamSource.getInputStream()); streamSource.setInputStream(null); IOUtils.closeQuietly(streamSource.getReader()); streamSource.setReader(null); } else if (src instanceof ImageSource) { ImageSource imageSource = (ImageSource) src; if (imageSource.getImageInputStream() != null) { try { imageSource.getImageInputStream().close(); } catch (IOException ioe) { //ignore } imageSource.setImageInputStream(null); } } else if (src instanceof SAXSource) { InputSource is = ((SAXSource) src).getInputSource(); if (is != null) { IOUtils.closeQuietly(is.getByteStream()); is.setByteStream(null); IOUtils.closeQuietly(is.getCharacterStream()); is.setCharacterStream(null); } } }