You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/09/04 11:23:55 UTC

git commit: CAMEL-6701: Improved javadoc for behavior of loadText. Added test with thanks to Jan Materne

Updated Branches:
  refs/heads/master 1a2075d62 -> 78ab9c1ba


CAMEL-6701: Improved javadoc for behavior of loadText. Added test with thanks to Jan Materne


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/78ab9c1b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/78ab9c1b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/78ab9c1b

Branch: refs/heads/master
Commit: 78ab9c1bac18d8df232c19450c43e0d8fce1e3df
Parents: 1a2075d
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Sep 4 11:22:01 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Sep 4 11:22:01 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/util/IOHelper.java    | 10 ++++--
 .../org/apache/camel/util/IOHelperTest.java     | 36 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/78ab9c1b/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/IOHelper.java b/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
index 3f24e09..001d1e4 100644
--- a/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
@@ -443,13 +443,17 @@ public final class IOHelper {
 
     /**
      * 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(new InputStreamReader(in));
+            BufferedReader reader = buffered(isr);
             while (true) {
                 String line = reader.readLine();
                 if (line != null) {
@@ -461,7 +465,7 @@ public final class IOHelper {
             }
             return builder.toString();
         } finally {
-            close(in);
+            close(isr, in);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/78ab9c1b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
index 0e2231f..9329ace 100644
--- a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
@@ -18,9 +18,12 @@ package org.apache.camel.util;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PrintWriter;
 
 import junit.framework.TestCase;
 
@@ -63,4 +66,37 @@ public class IOHelperTest extends TestCase {
         assertEquals("UTF-8", IOHelper.normalizeCharset("\"UTF-8 \""));
         assertEquals("UTF-8", IOHelper.normalizeCharset("\' UTF-8\'"));
     }
+
+    public void testLine1() throws Exception {
+        assertReadAsWritten("line1",   "line1", "line1\n");
+    }
+
+    public void testLine1LF() throws Exception {
+        assertReadAsWritten("line1LF", "line1\n", "line1\n");
+    }
+
+    public void testLine2() throws Exception {
+        assertReadAsWritten("line2",   "line1\nline2", "line1\nline2\n");
+    }
+
+    public void testLine2LF() throws Exception {
+        assertReadAsWritten("line2LF", "line1\nline2\n", "line1\nline2\n");
+    }
+
+    private void assertReadAsWritten(String testname, String text, String compareText) throws Exception {
+        File file = tempFile(testname);
+        write(file, text);
+        String loadText = IOHelper.loadText(new FileInputStream(file));
+        assertEquals(compareText, loadText);
+    }
+
+    private File tempFile(String testname) throws Exception {
+        return File.createTempFile(testname, "");
+    }
+
+    private void write(File file, String text) throws Exception {
+        PrintWriter out = new PrintWriter(file);
+        out.print(text);
+        out.close();
+    }
 }