Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedInputStream stops reading

843790Dec 9 2009 — edited Dec 31 2009
Hi,

I really don't understand what's wrong with my code. I've tried to add Thread.sleep, buffers and everything else I could think about, but the problem persists.
I'm trying to read a file using HTTP, the file size is around 3 MB (although I have smaller and bigger files, all with the same problem). After reading a random number of bytes (usually above 1MB), the socket stops reading and no data is available (socket.available() returns 0). Sometimes, however, the file is being fully read. The code is being run on a new thread, not the GUI thread.

String m_webPath = "http://www.c2b2.columbia.edu/danapeerlab/html/Genatomy/commons.jar";

int buffersize = 4096*100;
FileOutputStream writer = new FileOutputStream(m_localPath);
BufferedInputStream input = new BufferedInputStream( new URI(m_webPath).toURL().openConnection().getInputStream(), buffersize);
Thread.sleep(100); // seems to make it better, although I'm not really sure

long counter = 0;
byte[] array = new byte[buffersize];

while (counter<m_length)
{
if (input.available()>0)
{
int l = input.read(array);
counter += l;
m_percent = (int)(((double)counter/(double)m_length)*100);
writer.write(array, 0, l);
}
else
{
Thread.sleep(20); // Also, seems better
}
}

Any input or insight will be greatly appreciated.

Thanks!

Oren
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 28 2010
Added on Dec 9 2009
23 comments
795 views