diff options
author | Aaron M. Renn <arenn@urbanophile.com> | 2003-03-04 22:40:46 +0000 |
---|---|---|
committer | Aaron M. Renn <arenn@urbanophile.com> | 2003-03-04 22:40:46 +0000 |
commit | 9f69cdbd6de11e75b5dcfba70ba88983fec2952c (patch) | |
tree | 3e1a29a335f5e820e1b7ac16d6d2eeb79d39ebaf /java/io/FileOutputStream.java | |
parent | ea617bbe56a5750e523328e5f4690878de7b6d4f (diff) | |
download | classpath-9f69cdbd6de11e75b5dcfba70ba88983fec2952c.tar.gz |
Converted file based I/O mechanisms in java.io to new
native provider interface. All native methods are now
in the FileDescriptor class.
* java/io/FileDescriptor.java
Re-indent code and fix word-wrapped comments.
native_fd Rename instance variable to nativeFd
Commence primary ignition....
getFileDescriptor(int)
Diked out.
(setNativeFD) Rename to setNativeFd, make private
(getNativeFD) Rename to getNativeFd
(syncInternal) Rename to nativeSync(long)
(validInternal) Rename to nativeValid (long)
New instance methods added:
open(String, String)
close()
write(byte)
write(byte[], long, long)
read()
read(byte[], long, long)
available()
seek()
getFilePointer()
getLength()
setLength()
nativeOpen()
nativeClose()
nativeWriteByte()
nativeWriteBuf()
nativeReadByte()
nativeReadBuf()
nativeAvailable()
nativeSeek()
nativeGetFilePointer()
nativeGetLength()
nativeSetLength()
Added new static methods
nativeInit()
* java/io/FileInputStream.java
Re-ident code and fix word-wrapped comments.
native_fd - Convert from int to FileDescriptor, rename fd
FileInputStream(String) - Use string directly, don't convert to File
and call overloaded constructor
FileInputStream(File) - Call getPath, then invoke overloaded
constructor
(getFD) - Just return fd
(available) - Just call fd.available()
(skip) - Convert to use fd.seek()
(read) - Use fd.read()
(read(byte[])) - Use fd.read(byte[], long, long)
(close) - Use fd.close()
(getChannel) - Use fd.getNativeFd()
Commence primary ignition....
grand_total_read instance variable
skipInternal
readInternal
open
closeInternal
finalize
static initializer block
Diked out.
* java/io/FileOutputStream.java
native_fd - Convert to FileDescriptor and rename fd
(getFD) - Just return fd
(write) - Use fd.write()
(write(byte[], long, long) - Use fd.write(byte[], long, long)
(close) - Use fd.close()
(getChannel) - Use fd.getNativeFd()
Commence primary ignition....
writeInternal
open
closeInternal
finalize
static initializer block
Diked out.
* java/io/RandomAccessFile.java
native_fd - Convert to FileDescriptor and rename fd
(RandomAccessFile(File, String)) - Add modes "rws" and "rwd"
(getFD) - Just return fd
(getFilePointer) - Use fd.getFilePointer()
(length) - Use fd.length()
(seek) - Use fd.seek()
(setLength) - Use fd.setLength()
(read) - Use fd.read()
(write) - Use fd.write()
(skipBytes) - Use fd.getFilePointer() and fd.seek()
(getChannel) - Use fd.getNativeFd()
Commence primary ignition ....
closeInternal
getFilePointerInternal
lengthInternal
seekInternal
setLengthInternal
readInternal
skipInternal
writeInternal
static initializer block
Diked Out.
* native/jni/java-io/FileDescriptor.c
New C module implementing all native methods from FileDescriptor
* native/jni/java-io/Makefile.am
Use new FileDescriptor.c instead of old modules
* native/jni/java-io/java_io_FileDescriptor.c
No longer used
* native/jni/java-io/java_io_FileInputStream.c
No longer used
* native/jni/java-io/java_io_FileOutputStream.c
No longer used
* native/jni/java-io/java_io_RandomAccessFile.c
No longer used.
Diffstat (limited to 'java/io/FileOutputStream.java')
-rw-r--r-- | java/io/FileOutputStream.java | 522 |
1 files changed, 211 insertions, 311 deletions
diff --git a/java/io/FileOutputStream.java b/java/io/FileOutputStream.java index 50ee24022..4e0e4e3b7 100644 --- a/java/io/FileOutputStream.java +++ b/java/io/FileOutputStream.java @@ -1,5 +1,5 @@ /* FileOutputStream.java -- Writes to a file on disk. - Copyright (C) 1998, 2001 Free Software Foundation, Inc. + Copyright (C) 1998, 2001, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -46,341 +46,241 @@ import gnu.java.nio.FileChannelImpl; * This classes allows a stream of data to be written to a disk file or * any open <code>FileDescriptor</code>. * - * @version 0.0 - * * @author Aaron M. Renn (arenn@urbanophile.com) */ public class FileOutputStream extends OutputStream { -/*************************************************************************/ - -/* - * Class Variables and Initializers - */ - - static + /* + * Instance Variables + */ + + private FileDescriptor fd; + + /*************************************************************************/ + + /* + * Constructors + */ + + /** + * This method initializes a <code>FileOutputStream</code> object to write + * to the named file. The file is created if it does not exist, and + * the bytes written are written starting at the beginning of the file. + * <p> + * Before opening a file, a security check is performed by calling the + * <code>checkWrite</code> method of the <code>SecurityManager</code> (if + * one exists) with the name of the file to be opened. An exception is + * thrown if writing is not allowed. + * + * @param name The name of the file this stream should write to + * + * @exception SecurityException If write access to the file is not allowed + * @exception FileNotFoundException If a non-security error occurs + */ + public + FileOutputStream(String name) throws SecurityException, FileNotFoundException { - if (Configuration.INIT_LOAD_LIBRARY) - { - System.loadLibrary ("javaio"); - } + this(name, false); } -/*************************************************************************/ - -/* - * Instance Variables - */ - -/** - * This is the native file handle - */ -private int native_fd = -1; - -/*************************************************************************/ - -/* - * Constructors - */ - -/** - * This method initializes a <code>FileOutputStream</code> object to write - * to the named file. The file is created if it does not exist, and - * the bytes written are written starting at the beginning of the file. - * <p> - * Before opening a file, a security check is performed by calling the - * <code>checkWrite</code> method of the <code>SecurityManager</code> (if - * one exists) with the name of the file to be opened. An exception is - * thrown if writing is not allowed. - * - * @param name The name of the file this stream should write to - * - * @exception SecurityException If write access to the file is not allowed - * @exception FileNotFoundException If a non-security error occurs - */ -public -FileOutputStream(String name) throws SecurityException, FileNotFoundException -{ - this(name, false); -} - -/*************************************************************************/ - -/** - * This method initializes a <code>FileOutputStream</code> object to write - * to the specified <code>File</code> object. The file is created if it - * does not exist, and the bytes written are written starting at the - * beginning of the file. - * <p> - * Before opening a file, a security check is performed by calling the - * <code>checkWrite</code> method of the <code>SecurityManager</code> (if - * one exists) with the name of the file to be opened. An exception is - * thrown if writing is not allowed. - * - * @param file The <code>File</code> object this stream should write to - * - * @exception SecurityException If write access to the file is not allowed - * @exception FileNotFoundException If a non-security error occurs - */ -public -FileOutputStream(File file) throws SecurityException, FileNotFoundException -{ - this(file.getPath(), false); -} - -/*************************************************************************/ - -/** - * This method initializes a <code>FileOutputStream</code> object to write - * to the named file. The file is created if it does not exist, and - * the bytes written are written starting at the beginning of the file if - * the <code>append</code> argument is <code>false</code> or at the end - * of the file if the <code>append</code> argument is true. - * <p> - * Before opening a file, a security check is performed by calling the - * <code>checkWrite</code> method of the <code>SecurityManager</code> (if - * one exists) with the name of the file to be opened. An exception is - * thrown if writing is not allowed. - * - * @param name The name of the file this stream should write to - * @param append <code>true</code> to append bytes to the end of the file, - * or <code>false</code> to write bytes to the beginning - * - * @exception SecurityException If write access to the file is not allowed - * @exception FileNotFoundException If a non-security error occurs - */ -public -FileOutputStream(String name, boolean append) throws SecurityException, - FileNotFoundException -{ - SecurityManager sm = System.getSecurityManager(); - if (sm != null) - { -// try -// { - sm.checkWrite(name); -// } -// catch(AccessControlException e) -// { -// throw new SecurityException(e.getMessage()); -// } - } - - native_fd = open((new File(name)).getAbsolutePath(), append); -} - -/*************************************************************************/ - -/** - * This method initializes a <code>FileOutputStream</code> object to write - * to the file represented by the specified <code>FileDescriptor</code> - * object. This method does not create any underlying disk file or - * reposition the file pointer of the given descriptor. It assumes that - * this descriptor is ready for writing as is. - * <p> - * Before opening a file, a security check is performed by calling the - * <code>checkWrite</code> method of the <code>SecurityManager</code> (if - * one exists) with the specified <code>FileDescriptor</code> as an argument. - * An exception is thrown if writing is not allowed. - * - * @param file The <code>FileDescriptor</code> this stream should write to - * - * @exception SecurityException If write access to the file is not allowed - */ -public -FileOutputStream(FileDescriptor fd) throws SecurityException -{ - // Hmm, no other exception but this one to throw, but if the descriptor - // isn't valid, we surely don't have "permission" to write to it. - if (!fd.valid()) - throw new SecurityException("Invalid FileDescriptor"); - - SecurityManager sm = System.getSecurityManager(); - if (sm != null) - { -// try -// { - // sm.checkWrite(fd); -// } -// catch(AccessControlException e) -// { -// throw new SecurityException(e.getMessage()); -// } - } - - native_fd = fd.getNativeFD(); -} - -/*************************************************************************/ - -/** - * This method returns a <code>FileDescriptor</code> object representing - * the file that is currently being written to - * - * @return A <code>FileDescriptor</code> object for this stream - * - * @exception IOException If an error occurs - */ -public final FileDescriptor -getFD() throws IOException -{ - return(new FileDescriptor(native_fd)); -} - -/*************************************************************************/ - -/** - * This method writes a single byte of data to the file. - * - * @param b The byte of data to write, passed as an <code>int</code> - * - * @exception IOException If an error occurs - */ -public synchronized void -write(int b) throws IOException -{ - byte[] buf = new byte[1]; - - buf[0] = (byte)(b & 0xFF); - writeInternal(native_fd, buf, 0, buf.length); -} - -/*************************************************************************/ - -/** - * This method writes all the bytes in the specified array to the - * file. - * - * @param buf The array of bytes to write to the file - * - * @exception IOException If an error occurs - */ -public synchronized void -write(byte[] buf) throws IOException -{ - writeInternal(native_fd, buf, 0, buf.length); -} + /*************************************************************************/ + + /** + * This method initializes a <code>FileOutputStream</code> object to write + * to the specified <code>File</code> object. The file is created if it + * does not exist, and the bytes written are written starting at the + * beginning of the file. + * <p> + * Before opening a file, a security check is performed by calling the + * <code>checkWrite</code> method of the <code>SecurityManager</code> (if + * one exists) with the name of the file to be opened. An exception is + * thrown if writing is not allowed. + * + * @param file The <code>File</code> object this stream should write to + * + * @exception SecurityException If write access to the file is not allowed + * @exception FileNotFoundException If a non-security error occurs + */ + public + FileOutputStream(File file) throws SecurityException, FileNotFoundException + { + this(file.getPath(), false); + } -/*************************************************************************/ + /*************************************************************************/ + + /** + * This method initializes a <code>FileOutputStream</code> object to write + * to the named file. The file is created if it does not exist, and + * the bytes written are written starting at the beginning of the file if + * the <code>append</code> argument is <code>false</code> or at the end + * of the file if the <code>append</code> argument is true. + * <p> + * Before opening a file, a security check is performed by calling the + * <code>checkWrite</code> method of the <code>SecurityManager</code> (if + * one exists) with the name of the file to be opened. An exception is + * thrown if writing is not allowed. + * + * @param name The name of the file this stream should write to + * @param append <code>true</code> to append bytes to the end of the file, + * or <code>false</code> to write bytes to the beginning + * + * @exception SecurityException If write access to the file is not allowed + * @exception FileNotFoundException If a non-security error occurs + */ + public FileOutputStream(String name, boolean append) + throws SecurityException, FileNotFoundException + { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkWrite(name); -/** - * This method writes <code>len</code> bytes from the byte array - * <code>buf</code> to the file starting at index <code>offset</code>. - * - * @param buf The array of bytes to write to the file - * @param offset The offset into the array to start writing bytes from - * @param len The number of bytes to write to the file - * - * @exception IOException If an error occurs - */ -public synchronized void -write(byte[] buf, int offset, int len) throws IOException -{ - writeInternal(native_fd, buf, offset, len); -} + fd = new FileDescriptor(); -/*************************************************************************/ + try + { + if (append) + fd.open(name, "ra"); + else + fd.open(name, "rw"); + } + catch(IOException e) + { + throw new FileNotFoundException(name + ": " + e.getMessage()); + } + } -/** - * This internal method does the actual writing of bytes to the underlying - * file - * - * @param native_fd The native file descriptor - * @param buf The array of bytes to write to the file - * @param offset The offset into the array to start writing bytes from - * @param len The number of bytes to write to the file - * - * @exception IOException If an error occurs - */ -private synchronized native void -writeInternal(int native_fd, byte[] buf, int offset, int len) throws IOException; + /*************************************************************************/ + + /** + * This method initializes a <code>FileOutputStream</code> object to write + * to the file represented by the specified <code>FileDescriptor</code> + * object. This method does not create any underlying disk file or + * reposition the file pointer of the given descriptor. It assumes that + * this descriptor is ready for writing as is. + * <p> + * Before opening a file, a security check is performed by calling the + * <code>checkWrite</code> method of the <code>SecurityManager</code> (if + * one exists) with the specified <code>FileDescriptor</code> as an argument. + * An exception is thrown if writing is not allowed. + * + * @param file The <code>FileDescriptor</code> this stream should write to + * + * @exception SecurityException If write access to the file is not allowed + */ + public FileOutputStream(FileDescriptor fd) throws SecurityException + { + // Hmm, no other exception but this one to throw, but if the descriptor + // isn't valid, we surely don't have "permission" to write to it. + if (!fd.valid()) + throw new SecurityException("Invalid FileDescriptor"); -/*************************************************************************/ + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkWrite(fd); -/** - * This internal method opens the specfied file for writing. If the - * <code>append</code> variable is <code>true</code> the file is positioned - * for writing at the end, otherwise it is positioned for writing at the - * beginning. - * - * @param name The name of the file to open - * @param append <code>true</code> to write starting at the end of the file, - * <code>false</code> to start writing at the beginning - * - * @return A native file descriptor for this file - * - * @exception FileNotFoundException If an error occurs - */ -private synchronized native int -open(String name, boolean append) throws FileNotFoundException; + this.fd = fd; + } -/*************************************************************************/ + /*************************************************************************/ + + /** + * This method returns a <code>FileDescriptor</code> object representing + * the file that is currently being written to + * + * @return A <code>FileDescriptor</code> object for this stream + * + * @exception IOException If an error occurs + */ + public final FileDescriptor getFD() throws IOException + { + return(fd); + } -/** - * This method closes the underlying file. Any further attempts to - * write to this stream will likely generate an exception since the - * file is closed. - * - * @exception IOException If an error occurs - */ -public synchronized void -close() throws IOException -{ - if (native_fd != -1) - closeInternal(native_fd); - native_fd = -1; -} + /*************************************************************************/ -/*************************************************************************/ + /** + * This method writes a single byte of data to the file. + * + * @param b The byte of data to write, passed as an <code>int</code> + * + * @exception IOException If an error occurs + */ + public void write(int b) throws IOException + { + fd.write(b); + } -/** - * This internal method closes the actual file - * - * @param native_fd The native file descriptor to close - * - * @exception IOException If an error occurs - */ -private synchronized native void -closeInternal(int native_fd) throws IOException; + /*************************************************************************/ + + /** + * This method writes all the bytes in the specified array to the + * file. + * + * @param buf The array of bytes to write to the file + * + * @exception IOException If an error occurs + */ + public void write(byte[] buf) throws IOException + { + write(buf, 0, buf.length); + } -/*************************************************************************/ + /*************************************************************************/ + + /** + * This method writes <code>len</code> bytes from the byte array + * <code>buf</code> to the file starting at index <code>offset</code>. + * + * @param buf The array of bytes to write to the file + * @param offset The offset into the array to start writing bytes from + * @param len The number of bytes to write to the file + * + * @exception IOException If an error occurs + */ + public void write(byte[] buf, int offset, int len) throws IOException + { + fd.write(buf, offset, len); + } -/** - * This method closes the stream when this object is being garbage - * collected. - * - * @exception IOException If an error occurs (ignored by the Java runtime) - */ -protected void -finalize() throws IOException -{ - close(); -} + /*************************************************************************/ -/*************************************************************************/ + /** + * This method closes the underlying file. Any further attempts to + * write to this stream will likely generate an exception since the + * file is closed. + * + * @exception IOException If an error occurs + */ + public void close() throws IOException + { + fd.close(); + } -/** - * This method creates a java.nio.channels.FileChannel. - * Nio does not allow one to create a file channel directly. - * A file channel must be created by first creating an instance of - * Input/Output/RandomAccessFile and invoking the getChannel() method on it. - */ + /*************************************************************************/ -private FileChannel ch; /* cached associated file-channel */ + /** + * This method creates a java.nio.channels.FileChannel. + * Nio does not allow one to create a file channel directly. + * A file channel must be created by first creating an instance of + * Input/Output/RandomAccessFile and invoking the getChannel() method on it. + */ -public FileChannel -getChannel() -{ - synchronized (this) - { - if (ch == null) - ch = new gnu.java.nio.FileChannelImpl(native_fd, - this); - } - return ch; -} + private FileChannel ch; /* cached associated file-channel */ + public FileChannel + getChannel() + { + synchronized (this) + { + // FIXME: Convert NIO to 64 bit + if (ch == null) + ch = new gnu.java.nio.FileChannelImpl( + (int)(fd.getNativeFd() & 0xFFFF), this); + } + return ch; + } } // class FileOutputStream |