Solved

Taking a LONG time for BufferedInputStream to read end of stream

Posted on 2004-04-13
11
433 Views
Last Modified: 2008-01-16
I have created a simple program which will download an image file from a url and write it to a file (yahoo banner gif file).  The problem is that when I go to read the BufferedInputStream from the socket, it takes forever to return -1 (or end of stream) so the program will sit there waiting.  That is the while loop at the end of this program does not halt in a short time.  Any ideas on this one?  Thanks.

try {
      File newFile = new File("test.gif");
      FileOutputStream outstream = new FileOutputStream(newFile);      

      Socket sock = new Socket("us.i1.yimg.com",80);

      DataOutputStream out = new DataOutputStream(sock.getOutputStream());
      InputStream in = sock.getInputStream();
      BufferedInputStream input = new BufferedInputStream(in);            
      byte buffer[] = new byte[8192];      

      String command = "GET http://us.i1.yimg.com/us.yimg.com/i/ww/m6v8c.gif HTTP/1.1 \n";
      command += "Host: us.i1.yimg.com \n\n";                  

      out.write(command.getBytes());

              while((bytesRead = input .read(buffer)) > -1){      //This while loop does not seem to want to end as bytesRead sits there waiting for end of stream
                    outstream.write(buffer,0,bytesRead);
       }
0
Comment
Question by:aomega
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10820362
Does it (the loop) halt at all?
It may be that the server is still waiting for the request.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820419
Why don't you use URL and URLConnection to do that?

That would take care of all these problems.


Regards,
Muruga
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820448
File newFile = new File("test.gif");
FileOutputStream outstream = new FileOutputStream(newFile);    

URL url = new URL("http://us.i1.yimg.com/us.yimg.com/i/ww/m6v8c.gif");
URLConnection conn = url.openConnection();

byte buffer[] = new byte[8192];
BufferedInputStream input = new BufferedInputStream(conn.getInputStream());

while((bytesRead = input .read(buffer)) > -1)
{
    outstream.write(buffer,0,bytesRead);
}

input.close();
outstream.close();



Regards,
Muruga
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:aomega
ID: 10820485
objects, in this particular case I am not sure if the loop halts at all.  However, I have tried connecting to other addresses and some may halt in say 5 minutes.

mmuruganandam, yes using the HttpURLConnection class would work.  However, this needs to be implemented using sockets.  Thanks.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820496
use socket.soTimeOut(<milliseconds>); // this will timeout when the connection is idle for that many milliseconds.
0
 
LVL 92

Expert Comment

by:objects
ID: 10820520
Add some debug to your loop to check if any data is being read:

      System.out.println("Reading response");
      while((bytesRead = input .read(buffer)) > -1){      //This while loop does not seem to want to end as bytesRead sits there waiting for end of stream
           System.out.println("Read: "+bytesRead);
           outstream.write(buffer,0,bytesRead);
      }

If none is being read then I'd say the server has not returned response.
0
 

Author Comment

by:aomega
ID: 10820585
objects, I have tried printing out bytesRead and it is indeed reading all the bytes that make up the image.  When the loop goes back to read more from the stream when there is none left, it will sit and wait.  When I force an end with ctrl-C i can even successfully view the image.

mmuruganandam, I have tried to implement this with the socket timeout and it will throw the exception when the socket indeed times out.  However I was wondering if there was another solution to this (aside from using URL's) since I am wondering myself.  Thanks.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820653
If the problem persists, you can think of running this in a thread and timeout the thread after some idle period

0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10820726
you may need to read the response to determine the content length to know when all the data has been read.
0
 

Author Comment

by:aomega
ID: 10820965
objects, thanks for the suggestion.  That is exactly what I did and it works perfectly now.
0
 
LVL 92

Expert Comment

by:objects
ID: 10821002
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question