Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
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
Marshal
Posts: 34837
369
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic