[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Taking a LONG time for BufferedInputStream to read end of stream

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
aomega
Asked:
aomega
  • 4
  • 4
  • 3
1 Solution
 
objectsCommented:
Does it (the loop) halt at all?
It may be that the server is still waiting for the request.
0
 
mmuruganandamCommented:
Why don't you use URL and URLConnection to do that?

That would take care of all these problems.


Regards,
Muruga
0
 
mmuruganandamCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aomegaAuthor Commented:
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
 
mmuruganandamCommented:
use socket.soTimeOut(<milliseconds>); // this will timeout when the connection is idle for that many milliseconds.
0
 
objectsCommented:
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
 
aomegaAuthor Commented:
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
 
mmuruganandamCommented:
If the problem persists, you can think of running this in a thread and timeout the thread after some idle period

0
 
objectsCommented:
you may need to read the response to determine the content length to know when all the data has been read.
0
 
aomegaAuthor Commented:
objects, thanks for the suggestion.  That is exactly what I did and it works perfectly now.
0
 
objectsCommented:
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now