Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

transferring large amounts of data

Thread view: 
Andersen - 31 Oct 2005 19:15 GMT
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 - 31 Oct 2005 20:27 GMT
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 - 03 Nov 2005 04:16 GMT
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.