[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [cp-patches] Re: FYI: java/io/FileInputStream.java& java/io/FileOutp
From: |
Dalibor Topic |
Subject: |
Re: [cp-patches] Re: FYI: java/io/FileInputStream.java& java/io/FileOutputStream.java |
Date: |
Thu, 10 Mar 2005 01:58:24 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:1.7.5) Gecko/20050105 Debian/1.7.5-1 |
Dalibor Topic wrote:
Jeroen Frijters wrote:
Another strategy to implement this in the FileChannelImpl constructor
would be to call open() first, and then to check whether what we have
opened is a directory, and if it is one to call close() + throw
FileNotFoundException in that case. Does that sound OK?
That sounds like a good approach to me. I would think it would be pretty
efficient too.
OK, I'll post a patch that does that, without the close() part, after
some more testing and writing a muave test case. Thanks for your help!
Attached. ChangeLog:
2005-03-10 Dalibor Topic <address@hidden>
* libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java
(FileChannelImpl(Sting, int)): Removed.
(FileChannelImpl(File, int)) Added. Check if opened file is a
directory.
* libraries/javalib/java/io/FileInputStream.java (FileInputStream):
Fixed javadocs. Call FileChannelImpl(File, int).
* libraries/javalib/java/io/FileOutputStream.java
(FileInputStream):
Call FileChannelImpl(File, int).
* libraries/javalib/java/io/RandomAccessFile.java
(RandomAccessFile):
Call FileChannelImpl(File, int). Switched constructors around.
cheers,
dalibor topic
Index: gnu/java/nio/channels/FileChannelImpl.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/nio/channels/FileChannelImpl.java,v
retrieving revision 1.8
diff -u -r1.8 FileChannelImpl.java
--- gnu/java/nio/channels/FileChannelImpl.java 17 Feb 2005 12:57:27 -0000
1.8
+++ gnu/java/nio/channels/FileChannelImpl.java 10 Mar 2005 01:09:29 -0000
@@ -41,6 +41,7 @@
import gnu.classpath.Configuration;
import gnu.java.nio.FileLockImpl;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -101,10 +102,16 @@
}
/* Open a file. MODE is a combination of the above mode flags. */
- public FileChannelImpl (String path, int mode) throws FileNotFoundException
+ public FileChannelImpl (File file, int mode) throws FileNotFoundException
{
+ final String path = file.getPath();
fd = open (path, mode);
this.mode = mode;
+
+ // First open the file and then check if it is a a directory
+ // to avoid race condition.
+ if (file.isDirectory())
+ throw new FileNotFoundException(file.getPath() + " is a directory");
}
/* Used by init() (native code) */
Index: java/io/FileInputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/FileInputStream.java,v
retrieving revision 1.31
diff -u -r1.31 FileInputStream.java
--- java/io/FileInputStream.java 3 Mar 2005 08:56:02 -0000 1.31
+++ java/io/FileInputStream.java 10 Mar 2005 01:09:33 -0000
@@ -76,7 +76,8 @@
* @param name The name of the file this stream should read from
*
* @exception SecurityException If read access to the file is not allowed
- * @exception FileNotFoundException If the file does not exist.
+ * @exception FileNotFoundException If the file does not exist
+ * or if it is a directory
*/
public FileInputStream(String name) throws FileNotFoundException
{
@@ -97,7 +98,8 @@
* @param file The <code>File</code> object this stream should read from
*
* @exception SecurityException If read access to the file is not allowed
- * @exception FileNotFoundException If the file does not exist.
+ * @exception FileNotFoundException If the file does not exist
+ * or if it is a directory.
*/
public FileInputStream(File file) throws FileNotFoundException
{
@@ -105,7 +107,7 @@
if (s != null)
s.checkRead(file.getPath());
- ch = new FileChannelImpl (file.getPath(), FileChannelImpl.READ);
+ ch = new FileChannelImpl (file, FileChannelImpl.READ);
}
/**
Index: java/io/FileOutputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/FileOutputStream.java,v
retrieving revision 1.34
diff -u -r1.34 FileOutputStream.java
--- java/io/FileOutputStream.java 3 Mar 2005 08:56:02 -0000 1.34
+++ java/io/FileOutputStream.java 10 Mar 2005 01:09:33 -0000
@@ -155,10 +155,10 @@
if (s != null)
s.checkWrite(file.getPath());
- ch = new FileChannelImpl (file.getPath(), (append
- ? FileChannelImpl.WRITE
- | FileChannelImpl.APPEND
- : FileChannelImpl.WRITE));
+ ch = new FileChannelImpl (file, (append
+ ? FileChannelImpl.WRITE
+ | FileChannelImpl.APPEND
+ : FileChannelImpl.WRITE));
}
/**
Index: java/io/RandomAccessFile.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/RandomAccessFile.java,v
retrieving revision 1.44
diff -u -r1.44 RandomAccessFile.java
--- java/io/RandomAccessFile.java 16 Feb 2005 11:18:37 -0000 1.44
+++ java/io/RandomAccessFile.java 10 Mar 2005 01:09:33 -0000
@@ -86,38 +86,12 @@
* illegal value
* @exception SecurityException If the requested access to the file
* is not allowed
- * @exception IOException If any other error occurs
+ * @exception FileNotFoundException If the file is a directory, or
+ * any other error occurs
*/
public RandomAccessFile (File file, String mode)
throws FileNotFoundException
{
- this (file.getPath(), mode);
- }
-
- /**
- * This method initializes a new instance of <code>RandomAccessFile</code>
- * to read from the specified file name with the specified access mode.
- * The access mode is either "r" for read only access, "rw" for read
- * write access, "rws" for synchronized read/write access of both
- * content and metadata, or "rwd" for read/write access
- * where only content is required to be synchronous.
- * <p>
- * Note that a <code>SecurityManager</code> check is made prior to
- * opening the file to determine whether or not this file is allowed to
- * be read or written.
- *
- * @param fileName The name of the file to read and/or write
- * @param mode "r", "rw", "rws", or "rwd"
- *
- * @exception IllegalArgumentException If <code>mode</code> has an
- * illegal value
- * @exception SecurityException If the requested access to the file
- * is not allowed
- * @exception FileNotFoundException If any other error occurs
- */
- public RandomAccessFile (String fileName, String mode)
- throws FileNotFoundException
- {
int fdmode;
if (mode.equals("r"))
fdmode = FileChannelImpl.READ;
@@ -136,6 +110,8 @@
else
throw new IllegalArgumentException ("invalid mode: " + mode);
+ final String fileName = file.getPath();
+
// The obligatory SecurityManager stuff
SecurityManager s = System.getSecurityManager();
if (s != null)
@@ -146,13 +122,41 @@
s.checkWrite(fileName);
}
- ch = new FileChannelImpl (fileName, fdmode);
+ ch = new FileChannelImpl (file, fdmode);
fd = new FileDescriptor(ch);
out = new DataOutputStream (new FileOutputStream (fd));
in = new DataInputStream (new FileInputStream (fd));
}
/**
+ * This method initializes a new instance of <code>RandomAccessFile</code>
+ * to read from the specified file name with the specified access mode.
+ * The access mode is either "r" for read only access, "rw" for read
+ * write access, "rws" for synchronized read/write access of both
+ * content and metadata, or "rwd" for read/write access
+ * where only content is required to be synchronous.
+ * <p>
+ * Note that a <code>SecurityManager</code> check is made prior to
+ * opening the file to determine whether or not this file is allowed to
+ * be read or written.
+ *
+ * @param fileName The name of the file to read and/or write
+ * @param mode "r", "rw", "rws", or "rwd"
+ *
+ * @exception IllegalArgumentException If <code>mode</code> has an
+ * illegal value
+ * @exception SecurityException If the requested access to the file
+ * is not allowed
+ * @exception FileNotFoundException If the file is a directory or
+ * any other error occurs
+ */
+ public RandomAccessFile (String fileName, String mode)
+ throws FileNotFoundException
+ {
+ this (new File(fileName), mode);
+ }
+
+ /**
* This method closes the file and frees up all file related system
* resources. Since most operating systems put a limit on how many files
* may be opened at any given time, it is a good idea to close all files