JDK-4268893 : DataInputStream.readFully doesn't provide num-of-chars on EOF
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 1999-09-03
  • Updated: 2017-07-20
  • Resolved: 2017-07-20
Related Reports
Duplicate :  
Description
In class DataInputStream, the method ReadFully can hit eof and return without reading all the chars requested, BUT you can't find out how many chars it DID
read.

Perhaps we should add a 

   int InputStream.readBlocking( byte[] ba, int offset, int length);



Comments
Request has been fulfilled with readNBytes(), fixed by JDK-8080835.
20-07-2017

EVALUATION This may be addressed in a future release. michael.mccloskey@eng 1999-09-22
22-09-1999

SUGGESTED FIX Perhaps Sun should add a int InputStream.readBlocking( byte[] ba, int offset, int length) /** * Reads exactly <code>len</code> bytes from the input stream * into the byte array. This method reads repeatedly from the * underlying stream until all the bytes are read. * InputStream.read is often documented to block like this, * but in actuality it * does not always do so, and can return early with just a few bytes. * readBlockiyng blocks until all the bytes are read, * the end of the stream is detected, * or an exception is thrown. * You will always get either as many bytes as you * asked for, or you get an eof or other exception. * Unlike readFully, you find out how many bytes you did get. * @author Roedy Green * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the number of bytes to read. * @return number of bytes actually read. * @exception IOException if an I/O error occurs. * */ public final int readBlocking (InputStream in, byte b[], int off, int len) throws IOException { int totalBytesRead = 0; while ( totalBytesRead < len ) { int bytesRead = in.read(b, off + totalBytesRead, len - totalBytesRead); if ( bytesRead < 0 ) { break; } totalBytesRead += bytesRead; } return totalBytesRead; } // end readBlocking peter.vanderlinden@Eng 1999-09-07
07-09-1999