0

I'm trying to build a 3G connection speed test for Android

I got stuck with the IOException being shown in the Logcat:

"IOException : BufferedInputStream is closed"

Here's the code:

   OutputStream output = null;
    InputStream input = null;
    try{  
           URL url = new URL(myFullPathUrl);
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           urlConnection.setDoInput(true);
           urlConnection.setDoOutput(true);


           output = urlConnection.getOutputStream();
           input = urlConnection.getInputStream();

    //note: the guy who worked on this project before me use socket, and he requested the file like this:
    //          output.write( ("GET " + filePath + " HTTP/1.0\r\n").getBytes() );
    //          output.write("Accept: */*\r\n".getBytes());
    //          output.write("\r\n".getBytes());
    // where filePath is something like "/directory/image.jpg"
    // and when he open the socket connecion, he use only host name like "my.domain.com"
    // But I changed his code to use HttpURLConnection instead and use myFullPathUrl =            
    // "my.domain.com/directory/image.jpg"

        catch (Exception e){
             e.printStackTrace();
        }finally {
        urlConnection.disconnect(); //edit 3 : this is the culprit. Sorry I forgot to include this. Please all accept my apology and let's see this question as my little contribution that you can't use the input/output stream object after you call .disconnect()
             }  
while ((DownloadedByte += input.read(BufferData,0,BufferData.length)) != -1)//*** Exception at this line
            {.......... //Connection speed test code here

According to Logcat, the Exception happen on the line with while() loop (I marked * above)

I've confirmed that the inputStream object is not null. Anyone have an idea what could be the cause?

Thanks!

Best, Kitti

edit: Here's the full Logcat as requested:

07-30 15:58:00.349: W/System.err(26626): java.io.IOException: BufferedInputStream is closed
07-30 15:58:00.349: W/System.err(26626):    at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
07-30 15:58:00.359: W/System.err(26626):    at java.io.BufferedInputStream.read(BufferedInputStream.java:271)
07-30 15:58:00.359: W/System.err(26626):    at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:202)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:1)
07-30 15:58:00.359: W/System.err(26626):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 15:58:00.359: W/System.err(26626):    at java.lang.Thread.run(Thread.java:864)

edit 2:

I just tried calling input.available() to get an estimate number of bytes that can be read, and just got the same Exception... -*-

3
  • do you get the exception on the first try or later? Commented Jul 30, 2013 at 8:15
  • I got it on first try. =( Commented Jul 30, 2013 at 9:05
  • This exception means that you closed the stream and then tried to read from it. There is obviously something that closes the stream in the code you've omitted. You exception handling is also up the pole. If you get an exception creating the streams you shouldn't be continuing as though you didn't. Commented Jul 30, 2013 at 9:50

4 Answers 4

2

Your condition in the while loop is incorrect. InputStream#read returns the number of bytes read or -1 if at the end of the stream. However, you keep adding the number of bytes read to DownloadedByte. Use the following condition instead (mind the = instead of +=):

while ( ( bytesRead = inputStream.read( result, 0, result.length ) ) != -1 ) {
   // TODO
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks for answering. =D Actually the DownloadedByte got reset to 0 at the end of while loop. It is used in speed test calculation. Sorry for not including it in the question.
The information in this answer is correct but it doesn't answer the question. @Kakyoin Don't write obscure code, and, if you must, don't post part of it here and expect people to understand it.
1

The culprit is urlConnection.disconnect();

Remove it and the problem is solved. See 'edit 3' in the question. Thanks All!! =)

1 Comment

Well really. You omit code and you expect other people to identify problems in it? You call a disconnect() method and you expect the connection to remain connected?
1

i would suggest you to put your code of the while-loop into the try block. Because you only can process this code if there is no Exception thrown.

Would be nice if you could post the whole exeption that you are getting

Best wishes

3 Comments

Hi, Thanks for answering =) I did as you recommended. Also added the stacktrace in my question.
I guess you may have to call the connect() method of the HTTPUrlConnection. I'm not but you could give it a try
Hi, thanks!! Holger. Your comment about .connect() lead me to solution. Also please accept my apology (see the 'edit 3' in the question)
-2

Try to set a timeout:

urlConnection.setConnectTimeout(10000); //set timeout to 10 seconds

2 Comments

Done. Thanks for the recommendation =D I forgot to tell that I got the Exception immediately though.
I got this error with ksoap2 and I did not have a read problem. I solved it setting timeout. But you are very intelligent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.