Search in sources :

Example 96 with InputStream

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();
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 97 with InputStream

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));
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource)

Example 98 with InputStream

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));
}
Also used : InputStream(java.io.InputStream) Document(org.w3c.dom.Document)

Example 99 with InputStream

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));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 100 with InputStream

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InputStream (java.io.InputStream)12635 IOException (java.io.IOException)4626 ByteArrayInputStream (java.io.ByteArrayInputStream)2818 Test (org.junit.Test)2722 FileInputStream (java.io.FileInputStream)2443 File (java.io.File)1750 OutputStream (java.io.OutputStream)1173 InputStreamReader (java.io.InputStreamReader)1091 BufferedInputStream (java.io.BufferedInputStream)1075 URL (java.net.URL)1032 FileOutputStream (java.io.FileOutputStream)821 ByteArrayOutputStream (java.io.ByteArrayOutputStream)782 BufferedReader (java.io.BufferedReader)742 FileNotFoundException (java.io.FileNotFoundException)587 Properties (java.util.Properties)580 ArrayList (java.util.ArrayList)562 HttpURLConnection (java.net.HttpURLConnection)424 HashMap (java.util.HashMap)397 GZIPInputStream (java.util.zip.GZIPInputStream)342 Metadata (org.apache.tika.metadata.Metadata)319