Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * bca text dari input stream, dan convert menjadi string object
- *
- * @throws IOException
- */
- private String readStream(InputStream stream, int maxLength) throws IOException {
- String result = null;
- // Read InputStream using the UTF-8 charset.
- InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
- // Create temporary buffer to hold Stream data with specified max length.
- char[] buffer = new char[maxLength];
- // Populate temporary buffer with Stream data.
- int numChars = 0;
- int readSize = 0;
- while (numChars < maxLength && readSize != -1) {
- numChars += readSize;
- readSize = reader.read(buffer, numChars, buffer.length - numChars);
- }
- if (numChars != -1) {
- // The stream was not empty.
- // Create String that is actual length of response body if actual length was less than
- // max length.
- numChars = Math.min(numChars, maxLength);
- result = new String(buffer, 0, numChars);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement