[Title]Add readFromFile method which get the inputstream as an
authorhyunsik.noh <hyunsik.noh@samsung.com>
Tue, 27 Aug 2013 12:11:10 +0000 (21:11 +0900)
committerhyunsik.noh <hyunsik.noh@samsung.com>
Tue, 27 Aug 2013 12:11:10 +0000 (21:11 +0900)
argument
[Type]
[Module]common
[Priority]
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]

org.tizen.common/src/org/tizen/common/util/FileUtil.java

index 15d4a03..2f6bc82 100755 (executable)
@@ -222,12 +222,12 @@ public class FileUtil {
     public static
     String
     readTextStream(
-       final InputStream input,
-       String encoding
+        final InputStream input,
+        String encoding
     )
     throws IOException
     {
-       Assert.notNull( input );
+        Assert.notNull( input );
         Reader in = null;
 
         // if encoding is not set then use default encoding
@@ -235,8 +235,8 @@ public class FileUtil {
 
         try {
             return getString( new BufferedReader(
-               in = new InputStreamReader( input, encoding ),
-               BUFFER_SIZE
+                in = new InputStreamReader( input, encoding ),
+                BUFFER_SIZE
             ) );
         } finally {
             tryClose( in );
@@ -436,7 +436,7 @@ public class FileUtil {
         File fromDir = new File(from);
         File toDir = new File(to);
         if (fromDir.exists() == false) {
-               logger.warn( "Directory {} does not exist.", fromDir.getCanonicalPath() );
+            logger.warn( "Directory {} does not exist.", fromDir.getCanonicalPath() );
             return;
         }
         checkDirectory(fromDir, toDir);
@@ -620,24 +620,37 @@ public class FileUtil {
      * @throws IOException
      */
     public static String readFromFile(URL source) throws IOException {
-        char[] chars = new char[4092];
-        InputStreamReader contentsReader = null;
-        StringBuffer buffer = new StringBuffer();
         if (!new java.io.File(source.getFile()).exists()) {
             throw new FileNotFoundException();
         } else {
-            contentsReader = new InputStreamReader(source.openStream());
-            int c;
-            do {
-                c = contentsReader.read(chars);
-                if (c == -1)
-                {
-                    break;
-                }
-                buffer.append(chars, 0, c);
-            } while (c != -1);
-            contentsReader.close();
+            return readFromFile(source.openStream());
         }
+    }
+    
+    /**
+     * This method takes a InputStream as parameter to read the contents, and to add
+     * into a string buffer.
+     * 
+     * @param source
+     *            InputStream to read the contents.
+     * @return string, contents of a file from inputstream.
+     * @throws IOException
+     */
+    public static String readFromFile(InputStream in) throws IOException {
+        char[] chars = new char[4092];
+        InputStreamReader contentsReader = null;
+        StringBuffer buffer = new StringBuffer();
+        contentsReader = new InputStreamReader(in);
+        int c;
+        do {
+            c = contentsReader.read(chars);
+            if (c == -1)
+            {
+                break;
+            }
+            buffer.append(chars, 0, c);
+        } while (c != -1);
+        tryClose(contentsReader);
         return buffer.toString();
     }