commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r160345 - in jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/IOUtils.java test/org/apache/commons/io/IOUtilsTestCase.java
Date Wed, 06 Apr 2005 23:58:20 GMT
Author: scolebourne
Date: Wed Apr  6 16:58:19 2005
New Revision: 160345

URL: http://svn.apache.org/viewcvs?view=rev&rev=160345
Log:
Add toInputStream(String) methods
bug 32958, from Ian Springer 

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java?view=diff&r1=160344&r2=160345
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java Wed Apr  6
16:58:19 2005
@@ -17,6 +17,7 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.CharArrayWriter;
 import java.io.IOException;
 import java.io.InputStream;
@@ -65,12 +66,13 @@
  * @author Matthew Hawthorne
  * @author Stephen Colebourne
  * @author Gareth Davis
+ * @author Ian Springer
  * @version CVS $Revision$ $Date$
  */
 public class IOUtils {
     // NOTE: This class is focussed on InputStream, OutputStream, Reader and
-    // Writer. Each method should take at least one of these as a parameter.
-    // NOTE: This class should not depend on any other classes
+    // Writer. Each method should take at least one of these as a parameter,
+    // or return one of them.
 
     /**
      * The default buffer size to use.
@@ -388,6 +390,35 @@
         } else {
             return new String(input, encoding);
         }
+    }
+
+    /**
+     * Convert the specified string to an input stream, encoded as bytes
+     * using the default character encoding of the platform.
+     *
+     * @param input the string to convert
+     * @return an input stream
+     */
+    public static InputStream toInputStream(String input) {
+        byte[] bytes = input.getBytes();
+        return new ByteArrayInputStream(bytes);
+    }
+
+    /**
+     * Convert the specified string to an input stream, encoded as bytes
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     *
+     * @param input the string to convert
+     * @param encoding the encoding to use, null means platform default
+     * @throws IOException if the encoding is invalid
+     * @return an input stream
+     */
+    public static InputStream toInputStream(String input, String encoding) throws IOException
{
+        byte[] bytes = encoding != null ? input.getBytes(encoding) : input.getBytes();
+        return new ByteArrayInputStream(bytes);
     }
 
     // write byte[]

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java?view=diff&r1=160344&r2=160345
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java Wed
Apr  6 16:58:19 2005
@@ -22,6 +22,7 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 
 import org.apache.commons.io.testtools.*;
@@ -42,6 +43,7 @@
  *
  * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
  * @author Gareth Davis
+ * @author Ian Springer
  */
 public class IOUtilsTestCase extends FileBasedTestCase {
     /*
@@ -250,6 +252,26 @@
         } finally {
             fin.close();
         }
+    }
+
+    /**
+     * Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String,
String)}.
+     * Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so
relies on
+     * {@link #testInputStreamToByteArray()} to ensure this method functions correctly.
+     *
+     * @throws Exception on error
+     */
+    public void testStringToInputStream() throws Exception {
+        String str = "Abc123Xyz!";
+        InputStream inStream = IOUtils.toInputStream(str);
+        byte[] bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(str.getBytes(), bytes);
+        inStream = IOUtils.toInputStream(str, null);
+        bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(str.getBytes(), bytes);
+        inStream = IOUtils.toInputStream(str, "UTF-8");
+        bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(str.getBytes("UTF-8"), bytes);
     }
 
     public void testByteArrayToOutputStream()



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message