harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r376689 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io: Closeable.java FileChannelFactory.java FileInputStream.java FileOutputStream.java RandomAccessFile.java
Date Fri, 10 Feb 2006 13:57:19 GMT
Author: tellison
Date: Fri Feb 10 05:56:51 2006
New Revision: 376689

URL: http://svn.apache.org/viewcvs?rev=376689&view=rev
Log:
Applying patches received as HARMONY-42 (com.ibm.io.nio.FileChannel is not fully implemented)
 - refactoring of some java platform code
 - additional behavior on NIO file channels
 - refactoring of some IO code
 - work in progress on memory mapping

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Closeable.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileChannelFactory.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Closeable.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Closeable.java?rev=376689&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Closeable.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Closeable.java
Fri Feb 10 05:56:51 2006
@@ -0,0 +1,20 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+public interface Closeable {
+    public void close() throws IOException;
+}
\ No newline at end of file

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileChannelFactory.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileChannelFactory.java?rev=376689&r1=376688&r2=376689&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileChannelFactory.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileChannelFactory.java
Fri Feb 10 05:56:51 2006
@@ -1,4 +1,4 @@
-/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2005,2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,20 +18,28 @@
 
 import java.nio.channels.FileChannel;
 
-import com.ibm.io.nio.FileChannelImpl;
+import com.ibm.io.nio.ReadOnlyFileChannel;
+import com.ibm.io.nio.ReadWriteFileChannel;
+import com.ibm.io.nio.WriteOnlyFileChannel;
+import com.ibm.platform.IFileSystem;
 
 /**
  * A simple factory to provide a generic way to create FileChannel
  * implementation from within the java.io package.
  */
 class FileChannelFactory {
-	static final int O_RDONLY = 0x00000000;
-
-	static final int O_WRONLY = 0x00000001;
-
-	static final int O_RDWR = 0x00000010;
-
-	static FileChannel getFileChannel(long fd, int mode) {
-		return new FileChannelImpl(fd, mode);
+	static FileChannel getFileChannel(Object stream, long fd, int mode) {
+        switch(mode){
+        case IFileSystem.O_RDONLY:
+            return new ReadOnlyFileChannel(stream, fd);
+        case IFileSystem.O_WRONLY:
+            return new WriteOnlyFileChannel(stream, fd);
+        case IFileSystem.O_RDWR:
+            return new ReadWriteFileChannel(stream, fd);
+        case IFileSystem.O_APPEND:
+            return new WriteOnlyFileChannel(stream, fd, true);
+        default:
+            throw new RuntimeException("Unknown file channel type: "+mode); //$NON-NLS-1$
+        }
 	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java?rev=376689&r1=376688&r2=376689&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
Fri Feb 10 05:56:51 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
 
 import java.nio.channels.FileChannel;
 
+import com.ibm.platform.IFileSystem;
+import com.ibm.platform.Platform;
+
 /**
  * FileInputStream is a class for reading bytes from a file. This class may also
  * be used with other InputStreams, ie: BufferedInputStream, to read data from a
@@ -25,7 +28,7 @@
  * 
  * @see FileOutputStream
  */
-public class FileInputStream extends InputStream {
+public class FileInputStream extends InputStream implements Closeable{
 	/**
 	 * The FileDescriptor representing this FileInputStream.
 	 */
@@ -35,12 +38,9 @@
 	// initialized).
 	private FileChannel channel;
 
-	// Fill in the JNI id caches
-	private static native void oneTimeInitialization();
+    private IFileSystem fileSystem = Platform.getFileSystem();
 
-	static {
-		oneTimeInitialization();
-	}
+    private Object repositioningLock = new Object();
 
 	/**
 	 * Constructs a new FileInputStream on the File <code>file</code>. If the
@@ -62,8 +62,10 @@
 		if (security != null)
 			security.checkRead(file.getPath());
 		fd = new FileDescriptor();
-		if (openImpl(file.properPath(true)) != 0)
-			throw new FileNotFoundException(file.getPath());
+		fd.descriptor = fileSystem.open(file.properPath(true),
+                IFileSystem.O_RDONLY);
+        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+                IFileSystem.O_RDONLY);
 	}
 
 	/**
@@ -80,13 +82,15 @@
 	 */
 	public FileInputStream(FileDescriptor fd) {
 		super();
-		if (fd != null) {
-			SecurityManager security = System.getSecurityManager();
-			if (security != null)
-				security.checkRead(fd);
-			this.fd = fd;
-		} else
-			throw new NullPointerException();
+		if (fd == null) {
+            throw new NullPointerException();
+        }
+        SecurityManager security = System.getSecurityManager();
+        if (security != null)
+            security.checkRead(fd);
+        this.fd = fd;
+        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+                IFileSystem.O_RDONLY);
 	}
 
 	/**
@@ -102,12 +106,7 @@
 	 *             If the <code>fileName</code> is not found.
 	 */
 	public FileInputStream(String fileName) throws FileNotFoundException {
-		SecurityManager security = System.getSecurityManager();
-		if (security != null)
-			security.checkRead(fileName);
-		fd = new FileDescriptor();
-		if (openImpl(new File(fileName).properPath(true)) != 0)
-			throw new FileNotFoundException(fileName);
+		this(new File(fileName));
 	}
 
 	/**
@@ -120,7 +119,18 @@
 	 * @throws IOException
 	 *             If an error occurs in this stream.
 	 */
-	public native int available() throws IOException;
+	public int available() throws IOException {
+        openCheck();
+        synchronized (repositioningLock) {
+            long currentPosition = fileSystem.seek(fd.descriptor, 0L,
+                    IFileSystem.SEEK_CUR);
+            long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
+                    IFileSystem.SEEK_END);
+            fileSystem.seek(fd.descriptor, currentPosition,
+                    IFileSystem.SEEK_SET);
+            return (int) (endOfFilePosition - currentPosition);
+        }
+    }
 
 	/**
 	 * Close the FileInputStream.
@@ -129,11 +139,17 @@
 	 *             If an error occurs attempting to close this FileInputStream.
 	 */
 	public void close() throws IOException {
-		closeImpl();
+		synchronized (channel) {
+            synchronized (this) {
+                //FIXME: System.in, out, err may not want to be closed?                
+                if (channel.isOpen() && fd.descriptor >= 0) {
+                    channel.close();
+                }
+                fd.descriptor = -1;
+            }
+        }
 	}
 
-	private native void closeImpl() throws IOException;
-
 	/**
 	 * This method ensures that all resources for this file are released when it
 	 * is about to be garbage collected.
@@ -143,8 +159,7 @@
 	 *             FileInputStream.
 	 */
 	protected void finalize() throws IOException {
-		if (this.fd != null)
-			close();
+		close();
 	}
 
 	/**
@@ -158,11 +173,7 @@
 	 * 
 	 * @return the file channel representation for this FileInputStream.
 	 */
-	public synchronized FileChannel getChannel() {
-		if (channel == null) {
-			channel = FileChannelFactory.getFileChannel(fd.descriptor,
-					FileChannelFactory.O_RDONLY);
-		}
+	public FileChannel getChannel() {
 		return channel;
 	}
 
@@ -177,13 +188,9 @@
 	 *             this FileInputStream.
 	 */
 	public final FileDescriptor getFD() throws IOException {
-		if (fd != null)
-			return fd;
-		throw new IOException();
+		return fd;
 	}
 
-	private native int openImpl(byte[] fileName);
-
 	/**
 	 * Reads a single byte from this FileInputStream and returns the result as
 	 * an int. The low-order byte is returned or -1 of the end of stream was
@@ -196,13 +203,11 @@
 	 *             occurs.
 	 */
 	public int read() throws IOException {
-		if (fd != null)
-			return readByteImpl(getFD().descriptor);
-		throw new IOException();
+		byte[] readed = new byte[1];
+        int result = read(readed, 0, 1);
+        return result == -1 ? -1 : readed[0] & 0xff;
 	}
 
-	private native int readByteImpl(long descriptor) throws IOException;
-
 	/**
 	 * Reads bytes from the FileInputStream and stores them in byte array
 	 * <code>buffer</code>. Answer the number of bytes actually read or -1 if
@@ -239,14 +244,12 @@
 	 *             occurs.
 	 */
 	public int read(byte[] buffer, int offset, int count) throws IOException {
-		if (fd != null)
-			return readImpl(buffer, offset, count, getFD().descriptor);
-		throw new IOException();
+		openCheck();
+        synchronized (repositioningLock) {
+            return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
+        }
 	}
 
-	private native int readImpl(byte[] buffer, int offset, int count,
-			long descriptor) throws IOException;
-
 	/**
 	 * Skips <code>count</code> number of bytes in this FileInputStream.
 	 * Subsequent <code>read()</code>'s will not return these bytes unless
@@ -262,5 +265,20 @@
 	 *             If the stream is already closed or another IOException
 	 *             occurs.
 	 */
-	public native long skip(long count) throws IOException;
+    public long skip(long count) throws IOException {
+        openCheck();
+        synchronized (repositioningLock) {
+            final long currentPosition = fileSystem.seek(fd.descriptor, 0L,
+                    IFileSystem.SEEK_CUR);
+            final long newPosition = fileSystem.seek(fd.descriptor,
+                    currentPosition + count, IFileSystem.SEEK_SET);
+            return newPosition - currentPosition;
+        }
+    }
+
+    private synchronized void openCheck() throws IOException {
+        if (fd.descriptor < 0) {
+            throw new IOException();
+        }
+    }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java?rev=376689&r1=376688&r2=376689&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
Fri Feb 10 05:56:51 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
 
 import java.nio.channels.FileChannel;
 
+import com.ibm.platform.IFileSystem;
+import com.ibm.platform.Platform;
+
 /**
  * FileOutputStream is a class whose underlying stream is represented by a file
  * in the operating system. The bytes that are written to this stream are passed
@@ -30,7 +33,7 @@
  * 
  * @see FileInputStream
  */
-public class FileOutputStream extends OutputStream {
+public class FileOutputStream extends OutputStream implements Closeable{
 
 	/**
 	 * The FileDescriptor representing this FileOutputStream.
@@ -41,12 +44,7 @@
 	// initialized).
 	private FileChannel channel;
 
-	// Fill in the JNI id caches
-	private static native void oneTimeInitialization();
-
-	static {
-		oneTimeInitialization();
-	}
+    private IFileSystem fileSystem = Platform.getFileSystem();
 
 	/**
 	 * Constructs a new FileOutputStream on the File <code>file</code>. If
@@ -90,9 +88,9 @@
 		if (security != null)
 			security.checkWrite(file.getPath());
 		fd = new FileDescriptor();
-		if (openImpl(file.properPath(true), append) != 0)
-			throw new FileNotFoundException(file.getPath());
-
+        fd.descriptor = fileSystem.open(file.properPath(true), append?IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
+        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+                append? IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
 	}
 
 	/**
@@ -107,14 +105,16 @@
 	 */
 	public FileOutputStream(FileDescriptor fd) {
 		super();
-		if (fd != null) {
-			SecurityManager security = System.getSecurityManager();
-			if (security != null)
-				security.checkWrite(fd);
-			this.fd = fd;
-		} else
-			throw new NullPointerException(com.ibm.oti.util.Msg
-					.getString("K006c")); //$NON-NLS-1$
+        if (fd == null) {
+            throw new NullPointerException(com.ibm.oti.util.Msg
+                    .getString("K006c")); //$NON-NLS-1$
+        }
+        SecurityManager security = System.getSecurityManager();
+        if (security != null)
+            security.checkWrite(fd);
+        this.fd = fd;
+        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+                IFileSystem.O_WRONLY);
 	}
 
 	/**
@@ -151,15 +151,7 @@
 	 */
 	public FileOutputStream(String filename, boolean append)
 			throws FileNotFoundException {
-		super();
-		SecurityManager security = System.getSecurityManager();
-		if (security != null)
-			security.checkWrite(filename);
-		fd = new FileDescriptor();
-		File f = new File(filename);
-		if (openImpl(f.properPath(true), append) != 0)
-			throw new FileNotFoundException(filename);
-
+		this(new File(filename), append);
 	}
 
 	/**
@@ -170,11 +162,17 @@
 	 *             If an error occurs attempting to close this FileOutputStream.
 	 */
 	public void close() throws IOException {
-		closeImpl();
+        synchronized (channel) {
+            synchronized (this) {
+                //FIXME: System.in, out, err may not want to be closed?                
+                if(channel.isOpen() && fd.descriptor >= 0){
+                    channel.close();
+                }
+                fd.descriptor = -1;
+            }
+        }
 	}
 
-	private native void closeImpl() throws IOException;
-
 	/**
 	 * Frees any resources allocated to represent this FileOutputStream before
 	 * it is garbage collected. This method is called from the Java Virtual
@@ -185,8 +183,7 @@
 	 *             FileOutputStream.
 	 */
 	protected void finalize() throws IOException {
-		if (fd != null)
-			close();
+		close();
 	}
 
 	/**
@@ -200,12 +197,8 @@
 	 * 
 	 * @return the file channel representation for this FileOutputStream.
 	 */
-	public synchronized FileChannel getChannel() {
-		if (channel == null) {
-			channel = FileChannelFactory.getFileChannel(fd.descriptor,
-					FileChannelFactory.O_WRONLY);
-		}
-		return channel;
+	public FileChannel getChannel() {
+        return channel;
 	}
 
 	/**
@@ -219,13 +212,9 @@
 	 *             FileDescriptor.
 	 */
 	public final FileDescriptor getFD() throws IOException {
-		if (fd != null)
-			return fd;
-		throw new IOException();
+		return fd;
 	}
 
-	private native int openImpl(byte[] fileName, boolean openAppend);
-
 	/**
 	 * Writes the entire contents of the byte array <code>buffer</code> to
 	 * this FileOutputStream.
@@ -262,14 +251,10 @@
 	 *             If buffer is <code>null</code>.
 	 */
 	public void write(byte[] buffer, int offset, int count) throws IOException {
-		if (fd == null)
-			throw new IOException();
-		writeImpl(buffer, offset, count, getFD().descriptor);
+		openCheck();
+        fileSystem.write(fd.descriptor, buffer, offset, count);
 	}
 
-	private native void writeImpl(byte[] buffer, int offset, int count,
-			long descriptor) throws IOException;
-
 	/**
 	 * Writes the specified byte <code>oneByte</code> to this
 	 * FileOutputStream. Only the low order byte of <code>oneByte</code> is
@@ -283,13 +268,16 @@
 	 *             FileOutputStream.
 	 */
 	public void write(int oneByte) throws IOException {
-		if (fd != null) {
-			writeByteImpl(oneByte, getFD().descriptor);
-		} else
-			throw new IOException();
+		openCheck();
+        byte[] byteArray = new byte[1];
+        byteArray[0] = (byte)oneByte;
+        fileSystem.write(fd.descriptor, byteArray, 0, 1);
 	}
 
-	private native void writeByteImpl(int oneByte, long descriptor)
-			throws IOException;
+	private synchronized void openCheck() throws IOException {
+        if (fd.descriptor < 0) {
+            throw new IOException();
+        }
+    }
 
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java?rev=376689&r1=376688&r2=376689&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
Fri Feb 10 05:56:51 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
 
 import java.nio.channels.FileChannel;
 
+import com.ibm.platform.IFileSystem;
+import com.ibm.platform.Platform;
+
 /**
  * RandomAccessFile is a class which allows positioning of the next read
  * anywhere in the file. This is useful for reading specific locations of files
@@ -26,7 +29,7 @@
  * 
  */
 
-public class RandomAccessFile implements DataInput, DataOutput {
+public class RandomAccessFile implements DataInput, DataOutput, Closeable{
 	/**
 	 * The FileDescriptor representing this RandomAccessFile.
 	 */
@@ -36,14 +39,11 @@
 	// initialized).
 	private FileChannel channel;
 
-	private boolean isReadOnly;
+    private IFileSystem fileSystem = Platform.getFileSystem();
 
-	// Fill in the JNI id caches
-	private static native void oneTimeInitialization();
+    private boolean isReadOnly;
 
-	static {
-		oneTimeInitialization();
-	}
+    private Object repositionLock = new Object();
 
 	/**
 	 * Constructs a new RandomAccessFile on the File <code>file</code> and
@@ -66,22 +66,28 @@
 	public RandomAccessFile(File file, String mode)
 			throws FileNotFoundException {
 		super();
-		if (mode.equals("r") || mode.equals("rw")) { //$NON-NLS-1$ //$NON-NLS-2$
-			SecurityManager security = System.getSecurityManager();
-			if (security != null) {
-				security.checkRead(file.getPath());
-				if (mode.equals("rw")) //$NON-NLS-1$
-					security.checkWrite(file.getPath());
-			}
-			fd = new FileDescriptor();
-			if (openImpl(file.properPath(true), mode.equals("rw")) != 0) //$NON-NLS-1$
-				throw new FileNotFoundException(file.getPath());
-
-			isReadOnly = mode.equals("r"); //$NON-NLS-1$
-
-		} else
-			throw new IllegalArgumentException(com.ibm.oti.util.Msg
-					.getString("K0081")); //$NON-NLS-1$
+		if (mode.equals("r")) { //$NON-NLS-1$
+            isReadOnly = true;
+        } else if (mode.equals("rw") || mode.equals("rws") || mode.equals("rwd")) { //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
+            isReadOnly = false;
+        } else {
+            throw new IllegalArgumentException(com.ibm.oti.util.Msg
+                    .getString("K0081")); //$NON-NLS-1$
+        }
+
+        SecurityManager security = System.getSecurityManager();
+        if (security != null) {
+            security.checkRead(file.getPath());
+            if (!isReadOnly)
+                security.checkWrite(file.getPath());
+        }
+
+        fd = new FileDescriptor();
+        // FIXME: add support to "rwd", "rws"
+        fd.descriptor = fileSystem.open(file.properPath(true),
+                isReadOnly ? IFileSystem.O_RDONLY : IFileSystem.O_RDWR);
+        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+                isReadOnly ? IFileSystem.O_RDONLY : IFileSystem.O_RDWR);
 	}
 
 	/**
@@ -107,22 +113,7 @@
 	 */
 	public RandomAccessFile(String fileName, String mode)
 			throws FileNotFoundException {
-		super();
-		if (mode.equals("r") || mode.equals("rw")) {  //$NON-NLS-1$//$NON-NLS-2$
-			SecurityManager security = System.getSecurityManager();
-			if (security != null) {
-				security.checkRead(fileName);
-				if (mode.equals("rw")) //$NON-NLS-1$
-					security.checkWrite(fileName);
-			}
-			fd = new FileDescriptor();
-			File f = new File(fileName);
-			if (openImpl(f.properPath(true), mode.equals("rw")) != 0) //$NON-NLS-1$
-				throw new FileNotFoundException(fileName);
-
-		} else
-			throw new IllegalArgumentException(com.ibm.oti.util.Msg
-					.getString("K0081")); //$NON-NLS-1$
+		this(new File(fileName), mode);
 	}
 
 	/**
@@ -132,11 +123,16 @@
 	 *             If an error occurs attempting to close this RandomAccessFile.
 	 */
 	public void close() throws IOException {
-		closeImpl();
+		synchronized (channel) {
+            synchronized (this) {
+                if(channel.isOpen() && fd.descriptor >= 0){
+                    channel.close();
+                }
+                fd.descriptor = -1;
+            }
+        }
 	}
 
-	private native void closeImpl() throws IOException;
-
 	/**
 	 * Answers the FileChannel equivalent to this stream.
 	 * <p>
@@ -149,11 +145,6 @@
 	 * @return the file channel representation for this FileOutputStream.
 	 */
 	public final synchronized FileChannel getChannel() {
-		if (channel == null) {
-			channel = FileChannelFactory.getFileChannel(fd.descriptor,
-					(isReadOnly ? FileChannelFactory.O_RDONLY
-							: FileChannelFactory.O_RDWR));
-		}
 		return channel;
 	}
 
@@ -181,7 +172,16 @@
 	 *             If an error occurs attempting to get the file pointer
 	 *             position of this RandomAccessFile.
 	 */
-	public native long getFilePointer() throws IOException;
+    public long getFilePointer() throws IOException {
+        openCheck();
+        return fileSystem.seek(fd.descriptor, 0L, IFileSystem.SEEK_CUR);
+    }
+
+    private synchronized void openCheck() throws IOException {
+        if (fd.descriptor < 0) {
+            throw new IOException();
+        }
+    }
 
 	/**
 	 * Answers the current length of this RandomAccessFile in bytes.
@@ -192,9 +192,18 @@
 	 *             If an error occurs attempting to get the file length of this
 	 *             RandomAccessFile.
 	 */
-	public native long length() throws IOException;
-
-	private native int openImpl(byte[] fileName, boolean writable);
+    public long length() throws IOException {
+        openCheck();
+        synchronized (repositionLock) {
+            long currentPosition = fileSystem.seek(fd.descriptor, 0L,
+                    IFileSystem.SEEK_CUR);
+            long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
+                    IFileSystem.SEEK_END);
+            fileSystem.seek(fd.descriptor, currentPosition,
+                    IFileSystem.SEEK_SET);
+            return endOfFilePosition;
+        }
+    }
 
 	/**
 	 * Reads a single byte from this RandomAccessFile and returns the result as
@@ -212,13 +221,14 @@
 	 * @see #write(int)
 	 */
 	public int read() throws IOException {
-		if (fd != null)
-			return readByteImpl(fd.descriptor);
-		throw new IOException();
+		openCheck();
+        byte[] bytes = new byte[1];
+        synchronized (repositionLock) {
+            long readed = fileSystem.read(fd.descriptor, bytes, 0, 1);
+            return readed == -1 ? -1 : bytes[0] & 0xff;
+        }
 	}
 
-	private native int readByteImpl(long descriptor) throws IOException;
-
 	/**
 	 * Reads bytes from this RandomAccessFile into the byte array
 	 * <code>buffer</code>. The number of bytes actually read is returned.
@@ -262,14 +272,12 @@
 	 * @see #write(int)
 	 */
 	public int read(byte[] buffer, int offset, int count) throws IOException {
-		if (fd != null)
-			return readImpl(buffer, offset, count, fd.descriptor);
-		throw new IOException();
+		openCheck();
+        synchronized (repositionLock) {
+            return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
+        }
 	}
 
-	private native int readImpl(byte[] buffer, int offset, int count,
-			long descriptor) throws IOException;
-
 	/**
 	 * Reads a boolean from this stream.
 	 * 
@@ -578,7 +586,12 @@
 	 *             If the stream is already closed or another IOException
 	 *             occurs.
 	 */
-	public native void seek(long pos) throws IOException;
+	public void seek(long pos) throws IOException {
+        openCheck();
+        synchronized (repositionLock) {
+            fileSystem.seek(fd.descriptor, pos, IFileSystem.SEEK_SET);
+        }
+    }
 
 	/**
 	 * Set the length of this file to be <code>newLength</code>. If the
@@ -594,11 +607,17 @@
 	 *             occurs.
 	 */
 	public void setLength(long newLength) throws IOException {
-		setLengthImpl(newLength);
+		openCheck();
+        if (newLength < 0) {
+            throw new IllegalArgumentException();
+        }
+        synchronized (repositionLock) {
+            long position = fileSystem.seek(fd.descriptor, 0, IFileSystem.SEEK_CUR);
+            fileSystem.truncate(fd.descriptor, newLength);            
+            seek(position > newLength ? newLength : position);
+        }
 	}
 
-	private native void setLengthImpl(long newLength) throws IOException;
-
 	/**
 	 * Skips <code>count</code> number of bytes in this stream. Subsequent
 	 * <code>read()</code>'s will not return these bytes unless
@@ -665,15 +684,12 @@
 	 * 
 	 */
 	public void write(byte[] buffer, int offset, int count) throws IOException {
-		if (fd != null) {
-			writeImpl(buffer, offset, count, fd.descriptor);
-		} else
-			throw new IOException();
+		openCheck();
+        synchronized (repositionLock) {
+            fileSystem.write(fd.descriptor, buffer, offset, count);
+        }
 	}
 
-	private native void writeImpl(byte[] buffer, int offset, int count,
-			long descriptor) throws IOException;
-
 	/**
 	 * Writes the specified byte <code>oneByte</code> to this RandomAccessFile
 	 * starting at the current file pointer. Only the low order byte of
@@ -691,14 +707,13 @@
 	 * @see #read(byte[], int, int)
 	 */
 	public void write(int oneByte) throws IOException {
-		if (fd != null) {
-			writeByteImpl(oneByte, fd.descriptor);
-		} else
-			throw new IOException();
+		openCheck();
+        byte[] bytes = new byte[1];
+        bytes[0] = (byte)(oneByte & 0xff);
+        synchronized (repositionLock) {
+            fileSystem.write(fd.descriptor, bytes, 0, 1);
+        }
 	}
-
-	private native void writeByteImpl(int oneByte, long descriptor)
-			throws IOException;
 
 	/**
 	 * Writes a boolean to this output stream.



Mime
View raw message