Search in sources :

Example 6 with BufferedReader

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

the class IOHelper method loadText.

/**
     * Loads the entire stream into memory as a String and returns it.
     * <p/>
     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
     * terminator at the of the text.
     * <p/>
     * Warning, don't use for crazy big streams :)
     */
public static String loadText(InputStream in) throws IOException {
    StringBuilder builder = new StringBuilder();
    InputStreamReader isr = new InputStreamReader(in);
    try {
        BufferedReader reader = buffered(isr);
        while (true) {
            String line = reader.readLine();
            if (line != null) {
                builder.append(line);
                builder.append("\n");
            } else {
                break;
            }
        }
        return builder.toString();
    } finally {
        close(isr, in);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 7 with BufferedReader

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

the class IOConverterTest method testStringByBufferedReader.

public void testStringByBufferedReader() throws Exception {
    BufferedReader br = IOHelper.buffered(new StringReader("Hello World"));
    assertEquals("Hello World", IOConverter.toString(br));
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Example 8 with BufferedReader

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

the class IOConverterTest method testToStringBufferReader.

public void testToStringBufferReader() throws Exception {
    BufferedReader br = IOHelper.buffered(new StringReader("Hello World"));
    String s = IOConverter.toString(br);
    assertNotNull(s);
    assertEquals("Hello World", s);
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Example 9 with BufferedReader

use of java.io.BufferedReader 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 10 with BufferedReader

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

BufferedReader (java.io.BufferedReader)4790 InputStreamReader (java.io.InputStreamReader)2944 IOException (java.io.IOException)2260 FileReader (java.io.FileReader)1112 File (java.io.File)818 InputStream (java.io.InputStream)730 ArrayList (java.util.ArrayList)676 FileInputStream (java.io.FileInputStream)626 URL (java.net.URL)390 Test (org.junit.Test)367 FileNotFoundException (java.io.FileNotFoundException)329 StringReader (java.io.StringReader)282 BufferedWriter (java.io.BufferedWriter)205 HashMap (java.util.HashMap)199 Matcher (java.util.regex.Matcher)196 OutputStreamWriter (java.io.OutputStreamWriter)190 HttpURLConnection (java.net.HttpURLConnection)190 PrintWriter (java.io.PrintWriter)187 Reader (java.io.Reader)169 Socket (java.net.Socket)151