Solved

Taking a LONG time for BufferedInputStream to read end of stream

Posted on 2004-04-13
11
395 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:aomega
Comment Utility
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
Comment Utility
use socket.soTimeOut(<milliseconds>); // this will timeout when the connection is idle for that many milliseconds.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
objects, thanks for the suggestion.  That is exactly what I did and it works perfectly now.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now