Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   transferring large amounts of data (http://www.velocityreviews.com/forums/t147368-transferring-large-amounts-of-data.html)

Andersen 10-31-2005 06:15 PM

transferring large amounts of data
 

I have several questions.

I want to transfer a 1MB file between two machines using TCP sockets.
Assume, for some strange reason, that I have all my 1MB stored in a byte
array in memory (inefficient, I know) and want to transfer it over the
socket. Will that work? Or will the receiver side give me trouble?

Is it possible to use PIPEs to connect the File directly to the
OutputStream?

I know that the most sensible way would be neither of the above, but to
probably read chunks at a while and transfer the whole thing in chunks.

regards,
Andersen

frankgerlach22@gmx.de 10-31-2005 07:27 PM

Re: transferring large amounts of data
 
The read() call on the receiving side can use a buffer of arbitrary
length- you might just have to call read() more often if you use a very
small buffer.
Note that the read() call will most probably NOT fill a 1-Mbyte buffer,
but will return after reading less data. So you have to interpret the
return value of your InputStream.read() call ! (If you know that there
is a 1Mbyte chunk coming, you have to call read() in a loop until all
data is read.
The following piece of code might help you:
public static void readCompleteBlocking(InputStream is,byte[] target)
throws IOException
{
int readBytes=0;
while(readBytes<target.length){
int read=is.read(target,readBytes,target.length-readBytes);
if(read<1)throw new IOException("read() returned 0 or
negative");
readBytes+=read;
}
}


Jack 11-03-2005 03:16 AM

Re: transferring large amounts of data
 
On 31 Oct 2005 11:27:10 -0800, "frankgerlach22@gmx.de"
<frankgerlach@gmail.com> wrote:

>Note that the read() call will most probably NOT fill a 1-Mbyte buffer,
>but will return after reading less data.


So true. Yet one might think (at least I did) that calling available()
would give an approximation of the ideal size for the input array.
This being especially true for J2ME where you don't want to just go
around creating large arrays in the limited available memory.

But this was not true, in my limited tests. While available() showed
500, the total read time (in millis) did not suffer (reading from a
server on localhost) until less than ~100 bytes were read at a clip.

Maybe that's just a J2ME thing, though.



All times are GMT. The time now is 04:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.