Posted By

laurenceosx on 10/01/10


Tagged

java


Versions (?)

java streamToString


 / Published in: Java
 

URL: http://www.kodejava.org/examples/266.html

  1. public static String streamToString(java.io.InputStream is_)
  2. {
  3. // based on http://www.kodejava.org/examples/266.html
  4. try
  5. {
  6. //////////////////////////////////////
  7. if (is_ != null) {
  8. StringBuilder sb = new StringBuilder();
  9. String line;
  10. try {
  11. java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(is_, "UTF-8"));
  12. while ((line = reader.readLine()) != null) {
  13. sb.append(line).append("\n");
  14. }
  15. } finally {
  16. is_.close();
  17. }
  18. return sb.toString();
  19. } else {
  20. return "";
  21. }
  22. }
  23. catch( Exception ex ) {
  24. //////////////////////////////////////
  25. return null;
  26. //////////////////////////////////////
  27. }
  28. }

Report this snippet  

You need to login to post a comment.