← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11000: Adding util method to convert InputStream to String

 

------------------------------------------------------------
revno: 11000
committer: Saptarshi <sunbiz@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2013-05-25 16:37:30 +0200
message:
  Adding util method to convert InputStream to String
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java	2013-04-26 13:15:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java	2013-05-25 14:37:30 +0000
@@ -52,6 +52,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Scanner;
 import java.util.zip.GZIPInputStream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
@@ -592,4 +593,17 @@
     {
         return new File( path ).exists();
     }
+    
+    /**
+     * Converts an InputStream to String with encoding as UTF-8
+     * 
+     * @since 2.12
+     * @param inputStream the InputStream
+     * @return String after reading the InputStream
+     */
+    public static String convertStreamToString(InputStream inputStream)
+    {
+        Scanner s = new Scanner(inputStream, ENCODING_UTF8).useDelimiter("\\A");
+        return s.hasNext() ? s.next() : "";
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java	2012-12-21 12:59:39 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java	2013-05-25 14:37:30 +0000
@@ -105,4 +105,13 @@
         assertEquals( 'm', reader.read() );
         assertEquals( 'l', reader.read() );
     }
+    
+    @Test
+    public void testConvertStreamToString()
+        throws Exception
+    {
+        String str = StreamUtils.convertStreamToString(plainStream);
+        assertTrue( str.contains("<dxf>") );
+        assertTrue( str.startsWith("<?xml version=\'1.0\'?>") );
+    }
 }