Created
May 23, 2015 14:27
-
-
Save alexfu/a68b0f9d9df74e54ae70 to your computer and use it in GitHub Desktop.
A utility function to convert an InputStream to String.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class StringUtils { | |
private StringUtils() { /* No op */ } | |
public static String from(InputStream is) { | |
byte[] buffer = new byte[1024]; | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
try { | |
for (int count = is.read(buffer); count != -1; count = is.read(buffer)) { | |
baos.write(buffer, 0, count); | |
} | |
baos.close(); | |
return new String(baos.toByteArray()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment