[kaffe] Bug in FileInputStream

Patrick Tullmann tullmann@cs.utah.edu
Sun, 9 Jun 2002 14:40:19 -0600


> I've got the same problem here, and I'll look into a fix (hopefully
> it will be a simple fix).

I fixed this, and some related problems on FileOutputStream (its
constructors were throwing IOException when they may only throw
FileNotFoundException, and a 1.4 constructor taking (File, boolean)
was missing).  I expanded your test case a bit, too, and gave it the
(lame) name "FileChecks.java".

I'll wait on adding FileChecks.java to the Kaffe regression test suite
until I've heard something about how Mauve may or may not test this...

The attached patch covers java/io/File{Input,Output}Stream.java.
Here's a ChangeLog entry:

Sun Jun 9 2002  Patrick Tullmann <pat@tullmann.org>
	* java/io/FileInputStream.java: Fix FileInputStream
	  constructor to fail if passed a directory.  Reported by
	  Martin Lackner.  

	* java/io/FileOutputStream: Fix OutputStream to only throw
	  FileNotFoundException.  Add missing (File, boolean)
	  constructor.

As an aside, I think we need some sort of process for checking in
patches during a "freeze" like this.  Perhaps if two additional people
sign off on it, and there are no other comments for two days then the
patch can be checked in?

-Pat

----- ----- ---- ---  ---  --   -    -      -         -               -
Pat Tullmann                                       tullmann@cs.utah.edu
		   All your base are belong to us.

Index: libraries/javalib/java/io/FileInputStream.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/FileInputStream.java,v
retrieving revision 1.12
diff -u -b -r1.12 FileInputStream.java
--- libraries/javalib/java/io/FileInputStream.java	4 Jan 2002 05:12:22 -0000	1.12
+++ libraries/javalib/java/io/FileInputStream.java	9 Jun 2002 20:28:19 -0000
@@ -20,31 +20,38 @@
         System.loadLibrary("io");
 }
 
-public FileInputStream(File file) throws FileNotFoundException {
-	this(file.getPath());
-}
-
 public FileInputStream(FileDescriptor fdObj) {
 	SecurityManager sm = System.getSecurityManager();
 	if (sm != null)
 		sm.checkRead(fdObj);
+	// XXX check if fd is invalid?  Or points to a non-file object?
 	fd=fdObj;
 }
 
 public FileInputStream(String name) throws FileNotFoundException {
-	final SecurityManager sm = System.getSecurityManager();
-	if (sm != null)
-		sm.checkRead(name);
+	this(new File(name));
+}
+
+public FileInputStream(File file) throws FileNotFoundException {
+	final String fname = file.getName();
+
+	// Note File.isDirectory() will do the required SecurityManager 
+	// canRead() check for us.
+	if (file.isDirectory())
+		throw new FileNotFoundException(fname+ ": Is a directory");
+
 	try {
-		open(name);
+		open(fname);
 	} catch (IOException e) {
 		/* Note that open may throw an IOException, but the spec says
 		 * that this constructor throws only FileNotFoundExceptions,
 		 * hence we must map them.
 		 */
-		throw new FileNotFoundException(name + ": " + e.getMessage());
+		throw new FileNotFoundException(fname + ": " + e.getMessage());
 	}
 }
+
+
 
 native public int available() throws IOException;
 
Index: libraries/javalib/java/io/FileOutputStream.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/FileOutputStream.java,v
retrieving revision 1.8
diff -u -b -r1.8 FileOutputStream.java
--- libraries/javalib/java/io/FileOutputStream.java	4 Jan 2002 05:12:22 -0000	1.8
+++ libraries/javalib/java/io/FileOutputStream.java	9 Jun 2002 20:28:19 -0000
@@ -20,13 +20,8 @@
 
 private FileDescriptor fd = new FileDescriptor();
 
-public FileOutputStream(File file) throws IOException
-{
-	this(file.getPath());
-}
-
 public FileOutputStream(FileDescriptor fdObj)
-	{
+{
 	final SecurityManager sm = System.getSecurityManager();
 	if (sm != null)
 		sm.checkWrite(fdObj);
@@ -38,16 +33,39 @@
 	this(name, false);
 }
 
+public FileOutputStream(File file) throws FileNotFoundException
+{
+	this(file, false);
+}
+
 public FileOutputStream(String name, boolean append) throws FileNotFoundException
 {
+	this(new File(name), append);
+}
+
+public FileOutputStream(File file, boolean append) throws FileNotFoundException
+{
+	final String fname = file.getName();
+
+	// Note, don't need an explicit File.isDirectory() check 
+	// as the open() call in write mode will check that.
+	
 	final SecurityManager sm = System.getSecurityManager();
 	if (sm != null)
-		sm.checkWrite(name);
+		sm.checkWrite(fname);
+
+	try
+	{
 	if (!append) {
-		open(name);
+			open(fname);
 	}
 	else {
-		openAppend(name);
+			openAppend(fname);
+		}
+	}
+	catch (IOException ioe)
+	{
+		throw new FileNotFoundException(fname+ ": " +ioe.getMessage());
 	}
 }
 
@@ -69,9 +87,9 @@
 	return (fd);
 }
 
-native private void open(String name);
+native private void open(String name) throws IOException;
 
-native private void openAppend(String name);
+native private void openAppend(String name) throws IOException;
 
 public void write(byte b[]) throws IOException
 {