Q:

two examples for sockets read() inputstream. which is better?

shown here are two code samples on how to read in an inputStream for a socket connection, which of these is best and why?
CODE SAMPLE 1
   while(totalBytesRead < fileSizeFromClient){
            int bytesRemaining = fileSizeFromClient - totalBytesRead;
            int bytesRead = dataInputStream.read(buffer, 0, (int)Math.min(buffer.length, bytesRemaining));

            if(bytesRead == -1){
               break;
             }else{

                dataOutputSream.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;

            }
   }

CODE SAMPLE 2
    while(totalBytesRead < fileSizeFromClient){
          int bytesRemaining = fileSizeFromClient - totalBytesRead;
          int bytesRead = dataInputStream.read(buffer, totalBytesRead, bytesRemaining);

          if(bytesRead == -1){
             break;
             }else{

                dataOutputStream.write(buffer, totalBytesRead, bytesRead);
                totalBytesRead += bytesRead;
            }
   }

A:

They are not equivalent, so you cannot compare them. The second example doesn't work beyond a certain file size and buffer size, because 'totalBytesRead' will overflow the buffer. It also wastes massive amounts of space. Unless you need the file contents in the buffer it is a pointless alternative

