DataInputStream.read() vs DataInputStream.readFully()
Asked Answered
D

2

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?

Drunk answered 17/9, 2014 at 18:17 Comment(3)
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.Hilliary
@Hilliary 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?Drunk
No. read() returns when at least one byte has been read; readFully() when the buffer has been filled.Elyssa
C
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.

Cosmopolitan answered 17/9, 2014 at 20:48 Comment(6)
perfect, that's what I thought. Thanks for the clarificationDrunk
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 ! ;-)Enmesh
How to interrupt readFully() method if I have to do it after timeout?Honeydew
@Enmesh 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.Elyssa
@Honeydew 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.Elyssa
@Elyssa or the ? could be multi-byte decoding of incomplete strings.Exciseman
S
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.

Strohbehn answered 11/2, 2018 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.