16

Im making a simple TCP/IP Socket app

Whats the different between doing this:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

byte[] buffer = new byte[100];
in.readFully(buffer);

versus doing this:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

byte[] buffer = new byte[100];
in.read(buffer);

I had a look at the documentation, they have the same exact description. readFully() and read() So can I assume its the same thing?

River
  • 8,585
  • 14
  • 54
  • 67
Krimson
  • 7,386
  • 11
  • 60
  • 97
  • 1
    How do you want to handle EOF (meaning when there are no more bytes to be read on the socket)? Look at the definitions of the two again and not just the descriptions. One returns a value and one throws an exception. – scrappedcola Sep 17 '14 at 18:28
  • @scrappedcola oh ok, I see now. So basically, `readFully()` returns when the buffer is full regardless of if there there are more bytes remaining to be read. And `read()` returns when all the bytes have been read. Correct? – Krimson Sep 17 '14 at 18:33
  • 1
    No. `read()` returns when at least one byte has been read; `readFully()` when the buffer has been filled. – user207421 Dec 05 '17 at 04:46

2 Answers2

19

The Javadoc for DataInput.readFully(byte[] b) states:

Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b.

The Javadoc for DataInputStream.read(byte[] b) states:

Reads some number of bytes from the contained input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

Basically, readFully() will read exactly b.length bytes, whereas read() will read up to b.length, maybe less, whatever is available from the input stream.

River
  • 8,585
  • 14
  • 54
  • 67
Lolo
  • 4,277
  • 2
  • 25
  • 24
  • 1
    perfect, that's what I thought. Thanks for the clarification – Krimson Sep 17 '14 at 20:50
  • 4
    I might add that `readFully()` is better in **network based operations**. I myself had troubles with only `read()` when sending long messages (buffer **over 5K of bytes** for example). When using only `read()` part of the message was missing and replaced by `?` characters. Using `readFully()` solved my issue : the complete message is read, no characters is missing at all. ;-) Hope it helps ! ;-) – Mackovich Jun 15 '15 at 14:39
  • How to interrupt readFully() method if I have to do it after timeout? – user2602807 May 07 '17 at 11:09
  • @Mackovich It wasn't replaced by ? characters. They were whatever was left over in the buffer beyond what was read. The count returned by `read()` did not include them. – user207421 Dec 05 '17 at 04:48
  • 1
    @user2602807 You set a socket read timeout. But you will lose any data that had already been read if the timeout triggers, or rather you won't know how much data was read if any. Best to use `read()` in association with timeouts. – user207421 Dec 05 '17 at 04:49
  • @user207421 or the ? could be multi-byte decoding of incomplete strings. – Barry Staes May 03 '19 at 06:38
0

Using read you need to check return value to know how many bytes have been really read

byte[] buffer = new byte[100];
if (in.read(buffer) < 100){
   // fail
   }

instead with readFully a IOException will be thrown if the 100 bytes could not be read, don't need to check return value, simplify a bit.

from56
  • 3,976
  • 2
  • 13
  • 23