 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
pdnews Guest
|
Posted: Thu Oct 21, 2004 1:36 am Post subject: reading bytes from an inputstream |
|
|
I obtain an InputStream from a java.net.URLConnection and need to read
bytes from it until EOF. If I call available(), create an array
of byte[] of that size and read that amount, I do not get the entire
content. Why doesn't available return the entire size of the response?
Also, what is the best way to read the response up until EOF?
I need binary data, because the content is a PDF file.
TIA
|
|
| Back to top |
|
 |
Jim Guest
|
Posted: Fri Oct 22, 2004 1:09 am Post subject: Re: reading bytes from an inputstream |
|
|
On Thu, 21 Oct 2004 01:36:45 GMT, pdnews <pdnews (AT) verizon (DOT) net> wrote:
| Quote: | I obtain an InputStream from a java.net.URLConnection and need to read
bytes from it until EOF. If I call available(), create an array
of byte[] of that size and read that amount, I do not get the entire
content. Why doesn't available return the entire size of the response?
Also, what is the best way to read the response up until EOF?
I need binary data, because the content is a PDF file.
TIA
|
Try reading within a while loop
InputStream in = url.getInputStream();
int inputdata;
// -1 signals the end of the data stream
while((inputdata = in.read()) > -1) {
Do stuff with the input data here. The data
will range from 0..255
}
When you use Available() remember that all its telling you is
how many bytes it can make available without blocking! You
can read that many but you have to go back for more. And then
you still need to check rour read() for a return of -1 to
ensure the EndOfStream.
Jim
|
|
| Back to top |
|
 |
pdnews Guest
|
Posted: Fri Oct 22, 2004 2:15 am Post subject: Re: reading bytes from an inputstream |
|
|
Jim wrote:
| Quote: | On Thu, 21 Oct 2004 01:36:45 GMT, pdnews <pdnews (AT) verizon (DOT) net> wrote:
I obtain an InputStream from a java.net.URLConnection and need to read
bytes from it until EOF. If I call available(), create an array
of byte[] of that size and read that amount, I do not get the entire
content. Why doesn't available return the entire size of the response?
Also, what is the best way to read the response up until EOF?
I need binary data, because the content is a PDF file.
TIA
Try reading within a while loop
InputStream in = url.getInputStream();
int inputdata;
// -1 signals the end of the data stream
while((inputdata = in.read()) > -1) {
Do stuff with the input data here. The data
will range from 0..255
}
When you use Available() remember that all its telling you is
how many bytes it can make available without blocking! You
can read that many but you have to go back for more. And then
you still need to check rour read() for a return of -1 to
ensure the EndOfStream.
Jim
I tried this, but still got a -1 before all data was read. |
Could it be that a -1 could be something other than EOF?
|
|
| Back to top |
|
 |
Jim Guest
|
Posted: Fri Oct 22, 2004 2:38 am Post subject: Re: reading bytes from an inputstream |
|
|
On Fri, 22 Oct 2004 02:15:54 GMT, pdnews <pdnews (AT) verizon (DOT) net> wrote:
| Quote: | Jim wrote:
On Thu, 21 Oct 2004 01:36:45 GMT, pdnews <pdnews (AT) verizon (DOT) net> wrote:
I obtain an InputStream from a java.net.URLConnection and need to read
bytes from it until EOF. If I call available(), create an array
of byte[] of that size and read that amount, I do not get the entire
content. Why doesn't available return the entire size of the response?
Also, what is the best way to read the response up until EOF?
I need binary data, because the content is a PDF file.
TIA
Try reading within a while loop
InputStream in = url.getInputStream();
int inputdata;
// -1 signals the end of the data stream
while((inputdata = in.read()) > -1) {
Do stuff with the input data here. The data
will range from 0..255
}
When you use Available() remember that all its telling you is
how many bytes it can make available without blocking! You
can read that many but you have to go back for more. And then
you still need to check rour read() for a return of -1 to
ensure the EndOfStream.
Jim
I tried this, but still got a -1 before all data was read.
Could it be that a -1 could be something other than EOF?
|
Here is a complete program that I used to read several html pages
and some jpg images with.
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.io.IOException;
public class URLTest {
public static void main(String [] args) {
try {
// Put a valid url here
URL url = new URL("");
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
int data;
while((data = in.read()) > -1) {
// Just print the data
System.out.print(data + " ");
}
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
} // main()
} // URLTest
Try this on some known file, and then on the PDF you are trying
to retrieve. Is it possible that the PDF is corrupt?
Jim
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|