|
17 | 17 |
|
18 | 18 | import java.io.BufferedInputStream; |
19 | 19 | import java.io.BufferedReader; |
| 20 | +import java.io.ByteArrayInputStream; |
20 | 21 | import java.io.CharArrayWriter; |
21 | 22 | import java.io.IOException; |
22 | 23 | import java.io.InputStream; |
|
65 | 66 | * @author Matthew Hawthorne |
66 | 67 | * @author Stephen Colebourne |
67 | 68 | * @author Gareth Davis |
| 69 | + * @author Ian Springer |
68 | 70 | * @version CVS $Revision$ $Date$ |
69 | 71 | */ |
70 | 72 | public class IOUtils { |
71 | 73 | // 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. |
74 | 76 |
|
75 | 77 | /** |
76 | 78 | * The default buffer size to use. |
@@ -390,6 +392,35 @@ public static String toString(byte[] input, String encoding) |
390 | 392 | } |
391 | 393 | } |
392 | 394 |
|
| 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 | + |
393 | 424 | // write byte[] |
394 | 425 | //----------------------------------------------------------------------- |
395 | 426 | /** |
|
0 commit comments