Guava equivalent for IOUtils.toString(InputStream)

You stated in your comment on Calum’s answer that you were going to use CharStreams.toString(new InputStreamReader(supplier.get(), Charsets.UTF_8)) This code is problematic because the overload CharStreams.toString(Readable) states: Does not close the Readable. This means that your InputStreamReader, and by extension the InputStream returned by supplier.get(), will not be closed after this code completes. If, on the … Read more

Idiomatic way to convert an InputStream to a String in Scala

For Scala >= 2.11 scala.io.Source.fromInputStream(is).mkString For Scala < 2.11: scala.io.Source.fromInputStream(is).getLines().mkString(“\n”) does pretty much the same thing. Not sure why you want to get lines and then glue them all back together, though. If you can assume the stream’s nonblocking, you could just use .available, read the whole thing into a byte array, and create a … Read more

FileNotFoundException while getting the InputStream object from HttpURLConnection

I don’t know about your Spring/JAXB combination, but the average REST webservice won’t return a response body on POST/PUT, just a response status. You’d like to determine it instead of the body. Replace InputStream response = con.getInputStream(); by int status = con.getResponseCode(); All available status codes and their meaning are available in the HTTP spec, … Read more

How can I read a text file in Android?

Try this : I assume your text file is on sd card //Find the directory for the SD Card using the API //*Don’t* hardcode “/sdcard” File sdcard = Environment.getExternalStorageDirectory(); //Get the text file File file = new File(sdcard,”file.txt”); //Read text from file StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); … Read more

Byte[] to InputStream or OutputStream

You create and use byte array I/O streams as follows: byte[] source = …; ByteArrayInputStream bis = new ByteArrayInputStream(source); // read bytes from bis … ByteArrayOutputStream bos = new ByteArrayOutputStream(); // write bytes to bos … byte[] sink = bos.toByteArray(); Assuming that you are using a JDBC driver that implements the standard JDBC Blob interface … Read more

In Kotlin, how do I read the entire contents of an InputStream into a String?

Kotlin has a specific extension just for this purpose. The simplest: val inputAsString = input.bufferedReader().use { it.readText() } // defaults to UTF-8 And in this example, you could decide between bufferedReader() or just reader(). The call to the function Closeable.use() will automatically close the input at the end of the lambda’s execution. Further reading: If … Read more

InputStream from a URL

Use java.net.URL#openStream() with a proper URL (including the protocol!). E.g. InputStream input = new URL(“http://www.somewebsite.com/a.txt”).openStream(); // … See also: Using java.net.URLConnection to fire and handle HTTP requests

Can you explain the HttpURLConnection connection process?

String message = URLEncoder.encode(“my message”, “UTF-8”); try { // instantiate the URL object with the target URL of the resource to // request URL url = new URL(“http://www.example.com/comment”); // instantiate the HttpURLConnection with the URL object – A new // connection is opened every time by calling the openConnection // method of the protocol handler … Read more

Read input stream twice

You can use org.apache.commons.io.IOUtils.copy to copy the contents of the InputStream to a byte array, and then repeatedly read from the byte array using a ByteArrayInputStream. E.g.: ByteArrayOutputStream baos = new ByteArrayOutputStream(); org.apache.commons.io.IOUtils.copy(in, baos); byte[] bytes = baos.toByteArray(); // either while (needToReadAgain) { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); yourReadMethodHere(bais); } // or ByteArrayInputStream bais = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)