Skip to content

Instantly share code, notes, and snippets.

@xxhdpi
Created March 25, 2017 11:37
Show Gist options
  • Select an option

  • Save xxhdpi/ad67c51608e06f1ca7d0ada9600803b2 to your computer and use it in GitHub Desktop.

Select an option

Save xxhdpi/ad67c51608e06f1ca7d0ada9600803b2 to your computer and use it in GitHub Desktop.
/**
* 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