Solved

Taking a LONG time for BufferedInputStream to read end of stream

Posted on 2004-04-13
11
412 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
rhino JavaScript import, load 25 82
servlet filter example 37 61
use lov values 2 50
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

770 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