?
Solved

Taking a LONG time for BufferedInputStream to read end of stream

Posted on 2004-04-13
11
Medium Priority
?
438 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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 

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 500 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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