joebar
May 28th, 2010, 07:51 AM
Hello,
I get some response from calling a website, basically I do:
InputStream response = null;
HttpSessionToken = (HttpURLConnection) new URL (MyUrl).openConnection(proxy);
response = (InputStream)HttpSessionToken.getContent();
That works fine. Now in the following code I have
//some code here...
result = stringifyInputStream(response) ; // line 1
//some more code here...
response.close(); // line 2
return response;
and this is the method being called
public static String stringifyInputStream(InputStream inputStream) throws IOException
{
int ichar;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(;;) {
ichar = inputStream.read();
if(ichar < 0) {
break;
}
baos.write(ichar);
}
return baos.toString("UTF-8");
}
In the above code I have marked two lines as line 1 and line 2, these are the lines in question. The "return response" returns nothing as long as line 1 and 2 exist. If I remove line 1 and 2 then the return statement works fine.
The stringify Method only reads the inputStream why does it destroy it? Why does a close() destroys my stream. I need the stream later on and if I don't close it it will stay open for ever, not sure if that really harms, but I guess I have to close what I open before.
Any help is appreciated. Thanks.
I get some response from calling a website, basically I do:
InputStream response = null;
HttpSessionToken = (HttpURLConnection) new URL (MyUrl).openConnection(proxy);
response = (InputStream)HttpSessionToken.getContent();
That works fine. Now in the following code I have
//some code here...
result = stringifyInputStream(response) ; // line 1
//some more code here...
response.close(); // line 2
return response;
and this is the method being called
public static String stringifyInputStream(InputStream inputStream) throws IOException
{
int ichar;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(;;) {
ichar = inputStream.read();
if(ichar < 0) {
break;
}
baos.write(ichar);
}
return baos.toString("UTF-8");
}
In the above code I have marked two lines as line 1 and line 2, these are the lines in question. The "return response" returns nothing as long as line 1 and 2 exist. If I remove line 1 and 2 then the return statement works fine.
The stringify Method only reads the inputStream why does it destroy it? Why does a close() destroys my stream. I need the stream later on and if I don't close it it will stay open for ever, not sure if that really harms, but I guess I have to close what I open before.
Any help is appreciated. Thanks.