-
May 28th, 2010, 07:51 AM
#1
InputStream vanishes...
Hello,
I get some response from calling a website, basically I do:
Code:
InputStream response = null;
HttpSessionToken = (HttpURLConnection) new URL (MyUrl).openConnection(proxy);
response = (InputStream)HttpSessionToken.getContent();
That works fine. Now in the following code I have
Code:
//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
Code:
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.
-
May 28th, 2010, 09:27 AM
#2
Re: InputStream vanishes...
Close is defined to: Closes this input stream and releases any system resources associated with the stream.
Why do you expect the input stream to exist after you close it?
You should leave the input stream open until you don't need it anymore.
Norm
-
May 28th, 2010, 09:55 AM
#3
Re: InputStream vanishes...
close was to me rather "cannot add anything" but if it is defined like that okay. thanks.
now this stringify function...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|