JDK-4017497 : java.io.RandomAccessFile.readFully() hangs on EOF
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1,1.1.1
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_2.5.1
  • CPU: generic,sparc
  • Submitted: 1996-12-03
  • Updated: 1997-10-23
  • Resolved: 1997-10-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.1.1 1.1.1Fixed
Related Reports
Duplicate :  
Relates :  
Description
RandomAccessFile.readFully() hangs when you attempt to read n bytes and less than n bytes remain in the file.  The Java Application Programming Interface says that an EOFException should be thrown if end-of-file is reached while attempting to read from this file.  

Test case output:

sumba 85 =>java RAFTest
Length of file: 4
Contents of file: >0123<
readByte: 0
readByte: 1
Request 3 more bytes from the raf: readFully(0,3)...


Test case:

import java.io.*;

class RAFTest {
   public static void main(String[] args) {
	try {
		RandomAccessFile raf = new RandomAccessFile("test_raf", "rw");
		// Write 0123 into the raf.
		byte[] b = {0,1,2,3};
		raf.write(b);
		raf.close();

		// Print contents of the file.
		raf = new RandomAccessFile("test_raf", "r");
		byte[] b4 = {9,9,9,9};
		raf.read(b4);
		System.err.println("Length of file: " +raf.length());
		System.err.println("Contents of file: >" +b4[0]+b4[1]+b4[2]+b4[3]+ "<");
		raf.close();

		raf = new RandomAccessFile("test_raf", "r");
		byte[] b3 = {9,9,9};
		int off = 0;
		int len = 3;

		// Read first two bytes (01)
		System.err.println("readByte: " +raf.readByte());
		System.err.println("readByte: " +raf.readByte());

		// Attempt to read 3 bytes from the raf and put them into 
		// b3 starting at b3[0]. Note:  There are only two bytes 
		// left in the raf.
		//
		System.err.println("Request " +len+ " more bytes from the raf: readFully(" +off+ "," +len+ ")...");
		raf.readFully(b3, off, len);
		System.err.println("Result: >" + b3[0] + b3[1] + b3[2] + "<");
	}
	catch (IOException e) {
		System.err.println(e);
	}
   }
}

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.1 FIXED IN: 1.1.1 INTEGRATED IN: 1.1.1
14-06-2004

EVALUATION This bug was fixed in JDK 1.1.1.
11-06-2004

WORK AROUND Use RandomAccessFile.read() which returns -1 when eof is reached.
11-06-2004

PUBLIC COMMENTS The read(byte buf[]) and read(byte buf[], int offset, int length) methods of java.io.RandomAccessFile return zero at end-of-file rather than -1. To work around this bug, treat a return value of zero as an indication of EOF. The corresponding readFully() methods may hang at end-of-file. To work around this bug, use the following method: <pre> void readFully(RandomAccessFile f, byte b[], int off, int len) throws IOException { int n = 0; while (n &lt; len) { int count = f.read(b, off + n, len - n); if (count &lt;= 0) throw new EOFException(); n += count; } } </pre>
10-06-2004