dcsimg
reading a binary file from remote location using url connection.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rohit_N
Posted On:   Thursday, June 7, 2001 10:15 AM

I tried reading a binary file from a remote location using URLConnection from a servlet. I want the contents of the file to be stored in a byte array which is to be base64 encoded. The available() always returns zero if I don't read atleast a byte of data from the file. Reading a byte and then trying available() gives 512 bytes or the size of the file , if the size of the file is less than 512 bytes. I read the chunk of available bytes into the byte array and store the array in a vector, this is continued until all the bytes are read from the file. In the end I find the total size from all the byte arrays stored in the vector , create a byte array of that size, put all the read bytes from the byte arrays in the vecto   More>>

I tried reading a binary file from a remote location using URLConnection from a servlet.
I want the contents of the file to be stored in a byte array which is to be base64 encoded.
The available() always returns zero if I don't read atleast a byte of data from the file.
Reading a byte and then trying available() gives 512 bytes or the size of the file , if the size of the file is less than 512 bytes.
I read the chunk of available bytes into the byte array and store the array in a vector, this is continued until all the bytes are read from the file.
In the end I find the total size from all the byte arrays stored in the vector , create a byte array of that size,
put all the read bytes from the byte arrays in the vector into the new byte array of the total size.
The bytes read are as same in the orginal file when the size of the file is less than 3kb,if the size is more
the bytes that are read are different from what is actually in the file.

Could you please give a better way to do this.
Since its a binary file i'm not able to use the readLine which would have made it simpler.
Is there a way to find the size of the file so that i can read all the bytes at one go into the byte array?

   <<Less

Re: reading a binary file from remote location using url connection.

Posted By:   Tim_Rohaly  
Posted On:   Thursday, June 7, 2001 02:39 PM

available() returns the number of bytes that
can be read without blocking - please read
the API documentation where this is explicitly stated.
It is certainly not supposed to return the
length of the file.
In your case, reading at least 1 byte initiates a transfer
of a packet, which fills the local buffer (which is apparently
512 bytes).


The way you are doing things is just wrong. You need to
read up on how streams work - see the link
to the Java Tutorial I/O chapter to the right of this post -->

About | Sitemap | Contact