Skip to content

Instantly share code, notes, and snippets.

@alexfu
Created May 23, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexfu/a68b0f9d9df74e54ae70 to your computer and use it in GitHub Desktop.
Save alexfu/a68b0f9d9df74e54ae70 to your computer and use it in GitHub Desktop.
A utility function to convert an InputStream to String.
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