Search in sources :

Example 11 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class IOConverterCharsetTest method testToReaderFileWithCharsetLatin1.

public void testToReaderFileWithCharsetLatin1() throws Exception {
    File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt");
    BufferedReader reader = IOConverter.toReader(file, "ISO-8859-1");
    BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
    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) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with InputStreamReader

use of java.io.InputStreamReader 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)

Example 13 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class IOConverterCharsetTest method testToInputStreamFileWithCharsetLatin1.

public void testToInputStreamFileWithCharsetLatin1() throws Exception {
    switchToDefaultCharset("UTF-8");
    File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt");
    InputStream in = IOConverter.toInputStream(file, "ISO-8859-1");
    // do read with default charset!
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
    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)

Example 14 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class MultiCastParallelAndStreamCachingTest method testReaderCache.

/**
     * Tests the ReaderCache.
     * 
     * The sent InputStreamReader is transformed to a ReaderCache before the
     * multi-cast processor is called.
     * 
     * @throws Exception
     */
public void testReaderCache() throws Exception {
    // sharp-s
    String abcScharpS = "ABCß";
    MockEndpoint mock = getMockEndpoint("mock:resulta");
    mock.expectedBodiesReceived(abcScharpS);
    mock = getMockEndpoint("mock:resultb");
    mock.expectedBodiesReceived(abcScharpS);
    InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(abcScharpS.getBytes("ISO-8859-1")), "ISO-8859-1");
    template.sendBody("direct:start", isr);
    assertMockEndpointsSatisfied();
}
Also used : InputStreamReader(java.io.InputStreamReader) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 15 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class BindyKeyValuePairDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
    BindyKeyValuePairFactory factory = (BindyKeyValuePairFactory) getFactory();
    // List of Pojos
    List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
    // Pojos of the model
    Map<String, Object> model;
    // Map to hold the model @OneToMany classes while binding
    Map<String, List<Object>> lists = new HashMap<String, List<Object>>();
    InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
    // Scanner is used to read big file
    Scanner scanner = new Scanner(in);
    // Retrieve the pair separator defined to split the record
    ObjectHelper.notNull(factory.getPairSeparator(), "The pair separator property of the annotation @Message");
    String separator = factory.getPairSeparator();
    int count = 0;
    try {
        while (scanner.hasNextLine()) {
            // Read the line
            String line = scanner.nextLine().trim();
            if (ObjectHelper.isEmpty(line)) {
                // skip if line is empty
                continue;
            }
            // Increment counter
            count++;
            // Create POJO
            model = factory.factory();
            // Split the message according to the pair separator defined in
            // annotated class @Message
            List<String> result = Arrays.asList(line.split(separator));
            if (result.size() == 0 || result.isEmpty()) {
                throw new java.lang.IllegalArgumentException("No records have been defined in the KVP");
            }
            if (result.size() > 0) {
                // Bind data from message with model classes
                // Counter is used to detect line where error occurs
                factory.bind(result, model, count, lists);
                // Link objects together
                factory.link(model);
                // Add objects graph to the list
                models.add(model);
                LOG.debug("Graph of objects created: {}", model);
            }
        }
        // If this is the case (correspond to an empty stream, ...)
        if (models.size() == 0) {
            throw new java.lang.IllegalArgumentException("No records have been defined in the CSV");
        } else {
            return extractUnmarshalResult(models);
        }
    } finally {
        scanner.close();
        IOHelper.close(in, "in", LOG);
    }
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BindyKeyValuePairFactory(org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InputStreamReader (java.io.InputStreamReader)4181 BufferedReader (java.io.BufferedReader)2938 IOException (java.io.IOException)1843 InputStream (java.io.InputStream)1083 FileInputStream (java.io.FileInputStream)767 ArrayList (java.util.ArrayList)494 URL (java.net.URL)471 File (java.io.File)448 Reader (java.io.Reader)446 Test (org.junit.Test)336 HttpURLConnection (java.net.HttpURLConnection)253 ByteArrayInputStream (java.io.ByteArrayInputStream)231 OutputStreamWriter (java.io.OutputStreamWriter)218 FileNotFoundException (java.io.FileNotFoundException)210 HashMap (java.util.HashMap)171 Socket (java.net.Socket)170 OutputStream (java.io.OutputStream)150 URLConnection (java.net.URLConnection)148 StringWriter (java.io.StringWriter)135 Path (org.apache.hadoop.fs.Path)123