Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference between read and readFully???  RSS feed

 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in DataInputStream there're read(byte []) and readFully(byte []), from their names, readFully seems like it read all bytes, so does it mean read can just read partially?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HaoZhe,
From the API:
read(byte[]) - 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.

readFully(byte[]) - 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.

So readFully() guarantees you the array will be filled up or an exception will be thrown. read() can get away with reading one byte and returning to the caller.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!