transferring large amounts of data

A

Andersen

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
 
F

frankgerlach22

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;
}
}
 
J

Jack

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top