Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. /**
  2. * bca text dari input stream, dan convert menjadi string object
  3. *
  4. * @throws IOException
  5. */
  6. private String readStream(InputStream stream, int maxLength) throws IOException {
  7. String result = null;
  8. // Read InputStream using the UTF-8 charset.
  9. InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
  10. // Create temporary buffer to hold Stream data with specified max length.
  11. char[] buffer = new char[maxLength];
  12. // Populate temporary buffer with Stream data.
  13. int numChars = 0;
  14. int readSize = 0;
  15. while (numChars < maxLength && readSize != -1) {
  16. numChars += readSize;
  17. readSize = reader.read(buffer, numChars, buffer.length - numChars);
  18. }
  19. if (numChars != -1) {
  20. // The stream was not empty.
  21. // Create String that is actual length of response body if actual length was less than
  22. // max length.
  23. numChars = Math.min(numChars, maxLength);
  24. result = new String(buffer, 0, numChars);
  25. }
  26. return result;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement