Guest User

Java convert InputStream to String

a guest
Nov 28th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1.     public static String convertInputStreamToString(InputStream inputStream) {
  2.         try {
  3.             int chunkSize = 128;
  4.             byte[] bytes = new byte[chunkSize];
  5.             ByteArrayOutputStream baos = new ByteArrayOutputStream(chunkSize);
  6.             int bytesRead;
  7.             do {
  8.                 bytesRead = inputStream.read(bytes, 0, chunkSize);
  9.                 if (bytesRead == -1) break;
  10.                 baos.write(bytes, 0, bytesRead);
  11.             } while (true);
  12.             return baos.toString("UTF-8");
  13.         } catch (IOException ex) {
  14.             Logger.getLogger(InputStreamToString.class.getName()).log(Level.SEVERE, null, ex);
  15.             return "";
  16.         }
  17.     }
Add Comment
Please, Sign In to add comment