[Kaffe] bug found and fixed in java.io.File!

Moses DeJong dejong at cs.umn.edu
Fri Jan 22 14:35:19 PST 1999


Hi all.

I ran into a little problem with the java.io.File class in kaffe.
Here is the test code I wrote to check the java.io.File implementation.



//start file TestFile.java

import java.io.File;

public class TestFile {

    public static void exists(File f) {
      String type = "File";
      if (f.isDirectory()) { type = "Directory"; }
      System.out.print(type + " \"" + f.getPath() );
      if (f.exists()) {
          System.out.println("\" does exist");
      } else {
          System.out.println("\" does not exist");
      }
    }

    public static void main(String[] argv) throws Exception {
	File f1 = new File("/", "tmp"); exists(f1);
	File f2 = new File("/"); exists(f2);
	File f3 = new File(f2, "tmp"); exists(f3);
        try {
	File f4 = new File(null); exists(f4);
        } catch (NullPointerException e) {System.out.println("caught 1");}
        try {
	File f5 = new File("/", null); exists(f5);
        } catch (NullPointerException e) {System.out.println("caught 2");}
	File f6 = new File(""); exists(f6);
	File f7 = new File("","file2"); exists(f7);
    }
}

//end file TestFile.java


Here is the output I get running under JDK 1.1.


% java TestFile
Directory "/tmp" does exist
Directory "/" does exist
Directory "/tmp" does exist
caught 1
caught 2
File "" does not exist
File "/file2" does not exist


Here is what I got from Kaffe (latest out of CVS).

% kaffe TestFile
Directory "//tmp" does exist
Directory "/" does exist
Directory "//tmp" does exist
File "null" does not exist
File "//null" does not exist
File "" does not exist
File "/file2" does not exist


Of course the kaffe output was wrong so I created this patch
file to fix these problems. It needs to be applied to the
libraries/javalib/java/io/File.java file.


*** copy_File.java	Thu Jan 21 18:44:21 1999
--- File.java	Thu Jan 21 19:14:43 1999
***************
*** 26,48 ****
  }
  
  public File(File dir, String name) {
! 	if (dir == null) {
! 		this.path = name;
  	}
  	else {
! 		this.path = dir.getPath() + separatorChar + name;
  	}
  }
  
  public File(String path) {
- 	this.path = path;
- }
- 
- public File(String path, String name) {
  	if (path == null) {
! 		path = ".";
! 	}
! 	this.path = path + separatorChar + name;
  }
  
  public boolean canRead() {
--- 26,56 ----
  }
  
  public File(File dir, String name) {
!         this(dir.getPath(), name);
! }
! 
! public File(String path, String name) {
! 	if (name == null) {
!             throw new NullPointerException();
!         }
! 	if (path == null || path.length() == 0) {
! 	    this.path = separatorChar + name;
  	}
  	else {
!             char last = path.charAt( path.length() - 1 );
!             if (last != separatorChar) {
!                 this.path = path + separatorChar + name;
!             } else {
!                 this.path = path + name;
!             }
  	}
  }
  
  public File(String path) {
  	if (path == null) {
!             throw new NullPointerException();
!         }
! 	this.path = path;
  }
  
  public boolean canRead() {





After adding this patch the TestFile class produces the same output in the
JDK and Kaffe.


I hope that helps
mo dejong
dejong at cs.umn.edu



More information about the kaffe mailing list