Skip to content

Commit ad4841a

Browse files
author
Stephen Colebourne
committed
Add toInputStream(String) methods
bug 32958, from Ian Springer git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@160345 13f79535-47bb-0310-9956-ffa450edef68
1 parent 745375d commit ad4841a

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.BufferedInputStream;
1919
import java.io.BufferedReader;
20+
import java.io.ByteArrayInputStream;
2021
import java.io.CharArrayWriter;
2122
import java.io.IOException;
2223
import java.io.InputStream;
@@ -65,12 +66,13 @@
6566
* @author Matthew Hawthorne
6667
* @author Stephen Colebourne
6768
* @author Gareth Davis
69+
* @author Ian Springer
6870
* @version CVS $Revision$ $Date$
6971
*/
7072
public class IOUtils {
7173
// NOTE: This class is focussed on InputStream, OutputStream, Reader and
72-
// Writer. Each method should take at least one of these as a parameter.
73-
// NOTE: This class should not depend on any other classes
74+
// Writer. Each method should take at least one of these as a parameter,
75+
// or return one of them.
7476

7577
/**
7678
* The default buffer size to use.
@@ -390,6 +392,35 @@ public static String toString(byte[] input, String encoding)
390392
}
391393
}
392394

395+
/**
396+
* Convert the specified string to an input stream, encoded as bytes
397+
* using the default character encoding of the platform.
398+
*
399+
* @param input the string to convert
400+
* @return an input stream
401+
*/
402+
public static InputStream toInputStream(String input) {
403+
byte[] bytes = input.getBytes();
404+
return new ByteArrayInputStream(bytes);
405+
}
406+
407+
/**
408+
* Convert the specified string to an input stream, encoded as bytes
409+
* using the specified character encoding.
410+
* <p>
411+
* Character encoding names can be found at
412+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
413+
*
414+
* @param input the string to convert
415+
* @param encoding the encoding to use, null means platform default
416+
* @throws IOException if the encoding is invalid
417+
* @return an input stream
418+
*/
419+
public static InputStream toInputStream(String input, String encoding) throws IOException {
420+
byte[] bytes = encoding != null ? input.getBytes(encoding) : input.getBytes();
421+
return new ByteArrayInputStream(bytes);
422+
}
423+
393424
// write byte[]
394425
//-----------------------------------------------------------------------
395426
/**

src/test/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.FileReader;
2323
import java.io.FileWriter;
2424
import java.io.IOException;
25+
import java.io.InputStream;
2526
import java.util.Arrays;
2627

2728
import org.apache.commons.io.testtools.*;
@@ -42,6 +43,7 @@
4243
*
4344
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
4445
* @author Gareth Davis
46+
* @author Ian Springer
4547
*/
4648
public class IOUtilsTestCase extends FileBasedTestCase {
4749
/*
@@ -252,6 +254,26 @@ public void testByteArrayToString()
252254
}
253255
}
254256

257+
/**
258+
* Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String, String)}.
259+
* Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on
260+
* {@link #testInputStreamToByteArray()} to ensure this method functions correctly.
261+
*
262+
* @throws Exception on error
263+
*/
264+
public void testStringToInputStream() throws Exception {
265+
String str = "Abc123Xyz!";
266+
InputStream inStream = IOUtils.toInputStream(str);
267+
byte[] bytes = IOUtils.toByteArray(inStream);
268+
assertEqualContent(str.getBytes(), bytes);
269+
inStream = IOUtils.toInputStream(str, null);
270+
bytes = IOUtils.toByteArray(inStream);
271+
assertEqualContent(str.getBytes(), bytes);
272+
inStream = IOUtils.toInputStream(str, "UTF-8");
273+
bytes = IOUtils.toByteArray(inStream);
274+
assertEqualContent(str.getBytes("UTF-8"), bytes);
275+
}
276+
255277
public void testByteArrayToOutputStream()
256278
throws Exception
257279
{

0 commit comments

Comments
 (0)