use of java.io.InputStream in project camel by apache.
the class StaxConverterTest method testToInputSreamByXmlStreamReader.
public void testToInputSreamByXmlStreamReader() throws Exception {
StringReader src = new StringReader(TEST_XML_7000);
XMLStreamReader xreader = null;
InputStream in = null;
try {
xreader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, src);
in = context.getTypeConverter().mandatoryConvertTo(InputStream.class, xreader);
// verify
InputStream expected = new ByteArrayInputStream(TEST_XML_7000.getBytes("utf-8"));
byte[] tmp1 = new byte[512];
byte[] tmp2 = new byte[512];
for (; ; ) {
int n1 = 0;
int n2 = 0;
try {
n1 = expected.read(tmp1, 0, tmp1.length);
n2 = in.read(tmp2, 0, tmp2.length);
} catch (IOException e) {
fail("unable to read data");
}
assertEquals(n1, n2);
if (n2 < 0) {
break;
}
assertTrue(Arrays.equals(tmp1, tmp2));
}
} finally {
if (xreader != null) {
xreader.close();
}
if (in != null) {
in.close();
}
}
}
use of java.io.InputStream in project camel by apache.
the class XmlConverterTest method testToStreamSourceByInputStream.
public void testToStreamSourceByInputStream() throws Exception {
XmlConverter conv = new XmlConverter();
InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
StreamSource out = conv.toStreamSource(is);
assertNotNull(out);
assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
use of java.io.InputStream in project camel by apache.
the class DomConverterTest method testDomConverterToInputStream.
public void testDomConverterToInputStream() throws Exception {
Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
InputStream is = new DomConverter().toInputStream(document.getChildNodes(), null);
assertEquals("<hello>world!</hello>", context.getTypeConverter().convertTo(String.class, is));
}
use of java.io.InputStream in project camel by apache.
the class IOConverterTest method testToInputStreamStringBufferAndBuilderExchange.
public void testToInputStreamStringBufferAndBuilderExchange() throws Exception {
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, ObjectHelper.getDefaultCharacterSet());
StringBuffer buffer = new StringBuffer();
buffer.append("Hello World");
InputStream is = IOConverter.toInputStream(buffer, exchange);
assertNotNull(is);
assertEquals("Hello World", IOConverter.toString(is, exchange));
StringBuilder builder = new StringBuilder();
builder.append("Hello World");
is = IOConverter.toInputStream(builder, exchange);
assertNotNull(is);
assertEquals("Hello World", IOConverter.toString(is, exchange));
}
use of java.io.InputStream in project camel by apache.
the class IOConverterCharsetTest method testToInputStreamFileWithCharsetUTF8.
public void testToInputStreamFileWithCharsetUTF8() throws Exception {
switchToDefaultCharset("UTF-8");
File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt");
InputStream in = IOConverter.toInputStream(file, "UTF-8");
// do read with default charset!
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
try {
String line = reader.readLine();
String naiveLine = naiveReader.readLine();
assertEquals(naiveLine, line);
assertEquals(CONTENT, line);
} finally {
reader.close();
naiveReader.close();
}
}
Aggregations