Created
March 25, 2017 11:37
-
-
Save xxhdpi/ad67c51608e06f1ca7d0ada9600803b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment