transferring large amounts of data

Discussion in 'Java' started by Andersen, Oct 31, 2005.

  1. Andersen

    Andersen Guest

    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
     
    Andersen, Oct 31, 2005
    #1
    1. Advertising

  2. Andersen

    Guest

    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;
    }
    }
     
    , Oct 31, 2005
    #2
    1. Advertising

  3. Andersen

    Jack Guest

    On 31 Oct 2005 11:27:10 -0800, ""
    <> 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.
     
    Jack, Nov 3, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brent
    Replies:
    3
    Views:
    507
    Raymond Lewallen
    Apr 7, 2004
  2. Bint
    Replies:
    1
    Views:
    1,947
    Benjamin Niemann
    Mar 19, 2006
  3. David
    Replies:
    0
    Views:
    339
    David
    Jun 21, 2006
  4. flamesrock
    Replies:
    2
    Views:
    392
    flamesrock
    Mar 12, 2005
  5. Wayne Marsh

    Handling large amounts of data

    Wayne Marsh, Nov 20, 2004, in forum: C Programming
    Replies:
    3
    Views:
    443
    Malcolm
    Nov 20, 2004
Loading...

Share This Page