How-to articles, tricks, and solutions about STREAM

Easy way to write contents of a Java InputStream to an OutputStream

One way to write the contents of a Java InputStream to an OutputStream is to use the read and write methods of the InputStream and OutputStream classes.

Get an OutputStream into a String

To get an OutputStream into a String, you can use a ByteArrayOutputStream as the OutputStream and then use the toString() method of the ByteArrayOutputStream to get the contents of the stream as a string.

How do I read / convert an InputStream into a String in Java?

There are several ways to read an InputStream and convert it to a String in Java. One way is to use the BufferedReader and InputStreamReader classes to read the InputStream line by line, and use a StringBuilder to construct the final String:

HTTP requests with file_get_contents, getting the response code

To make an HTTP request using the file_get_contents function in PHP and retrieve the response code, you can use the following code snippet: