AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reading bytes from an inputstream

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api
View previous topic :: View next topic  
Author Message
pdnews
Guest





PostPosted: Thu Oct 21, 2004 1:36 am    Post subject: reading bytes from an inputstream Reply with 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
Back to top
Jim
Guest





PostPosted: Fri Oct 22, 2004 1:09 am    Post subject: Re: reading bytes from an inputstream Reply with quote



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





PostPosted: Fri Oct 22, 2004 2:15 am    Post subject: Re: reading bytes from an inputstream Reply with quote



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





PostPosted: Fri Oct 22, 2004 2:38 am    Post subject: Re: reading bytes from an inputstream Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.