This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
FYI: Patch: java nio file locking
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Fri, 23 Jan 2004 15:42:25 +0100
- Subject: FYI: Patch: java nio file locking
Hi list,
I commited the attached patch to add java NIO file locking to trunk.
Michael
2004-01-23 Michael Koch <konqueror@gmx.de>
* gnu/java/nio/FileLockImpl.java:
Fixed filename in copyright.
(released): Removed.
(finalize): New method.
* gnu/java/nio/natFileLockImpl.cc
(releaseImpl): Implemented.
* java/nio/channels/FileChannelImpl.java:
Reworked imports.
(lock): Implemented.
(lockImpl): New method.
(tryLock): Implemented.
(tryLockImpl): New method.
* java/nio/channels/natFileChannelImpl.cc
(lockImpl): New method.
(tryLockImpl): New method.
Index: gnu/java/nio/FileLockImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/FileLockImpl.java,v
retrieving revision 1.5
diff -u -b -B -r1.5 FileLockImpl.java
--- gnu/java/nio/FileLockImpl.java 2 Dec 2003 15:03:21 -0000 1.5
+++ gnu/java/nio/FileLockImpl.java 23 Jan 2004 14:35:36 -0000
@@ -1,5 +1,5 @@
-/* FileChannelImpl.java --
- Copyright (C) 2002 Free Software Foundation, Inc.
+/* FileLockImpl.java --
+ Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -60,14 +60,24 @@
}
private FileDescriptor fd;
- private boolean released;
public FileLockImpl (FileDescriptor fd, FileChannel channel, long position,
long size, boolean shared)
{
super (channel, position, size, shared);
this.fd = fd;
- this.released = false;
+ }
+
+ public void finalize()
+ {
+ try
+ {
+ release();
+ }
+ catch (IOException e)
+ {
+ // Ignore this.
+ }
}
public boolean isValid ()
Index: gnu/java/nio/natFileLockImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/natFileLockImpl.cc,v
retrieving revision 1.1
diff -u -b -B -r1.1 natFileLockImpl.cc
--- gnu/java/nio/natFileLockImpl.cc 14 May 2003 06:37:59 -0000 1.1
+++ gnu/java/nio/natFileLockImpl.cc 23 Jan 2004 14:35:36 -0000
@@ -20,6 +20,5 @@
void
gnu::java::nio::FileLockImpl::releaseImpl ()
{
- throw new ::java::io::IOException
- (JvNewStringUTF ("releaseImpl not implemented"));
+ fd->unlock(position(), size());
}
Index: java/nio/channels/FileChannelImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/channels/FileChannelImpl.java,v
retrieving revision 1.4
diff -u -b -B -r1.4 FileChannelImpl.java
--- java/nio/channels/FileChannelImpl.java 19 Dec 2003 19:06:34 -0000 1.4
+++ java/nio/channels/FileChannelImpl.java 23 Jan 2004 14:35:36 -0000
@@ -38,6 +38,9 @@
package java.nio.channels;
+import gnu.classpath.Configuration;
+import gnu.gcj.RawData;
+import gnu.java.nio.FileLockImpl;
import java.io.EOFException;
import java.io.FileDescriptor;
import java.io.FileInputStream;
@@ -47,8 +50,6 @@
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.MappedByteBufferImpl;
-import gnu.classpath.Configuration;
-import gnu.gcj.RawData;
/**
* This file is not user visible !
@@ -354,9 +355,23 @@
file_obj instanceof FileInputStream)
throw new NonWritableChannelException ();
- throw new Error ("Not implemented");
+ boolean completed = false;
+
+ try
+ {
+ begin();
+ lockImpl(position, size, shared);
+ completed = true;
+ return new FileLockImpl(fd, this, position, size, shared);
+ }
+ finally
+ {
+ end(completed);
+ }
}
+ private native void lockImpl(long position, long size, boolean shared);
+
public FileLock tryLock (long position, long size, boolean shared)
throws IOException
{
@@ -367,8 +382,26 @@
if (!isOpen ())
throw new ClosedChannelException ();
- throw new Error ("Not implemented");
+ if (! tryLockImpl(position, size, shared))
+ return null;
+
+ boolean completed = false;
+
+ try
+ {
+ boolean lockable = tryLockImpl(position, size, shared);
+ completed = true;
+ return (lockable
+ ? new FileLockImpl(fd, this, position, size, shared)
+ : null);
}
+ finally
+ {
+ end(completed);
+ }
+ }
+
+ private native boolean tryLockImpl(long position, long size, boolean shared);
public long position ()
throws IOException
Index: java/nio/channels/natFileChannelImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/channels/natFileChannelImpl.cc,v
retrieving revision 1.1
diff -u -b -B -r1.1 natFileChannelImpl.cc
--- java/nio/channels/natFileChannelImpl.cc 13 Jul 2003 16:53:05 -0000 1.1
+++ java/nio/channels/natFileChannelImpl.cc 23 Jan 2004 14:35:36 -0000
@@ -25,11 +25,13 @@
#endif
#include <gnu/gcj/RawData.h>
+#include <gnu/java/nio/FileLockImpl.h>
#include <java/io/FileDescriptor.h>
#include <java/io/IOException.h>
#include <java/nio/ByteBuffer.h>
#include <java/nio/channels/FileChannel.h>
#include <java/nio/channels/FileChannelImpl.h>
+#include <java/nio/channels/FileLock.h>
jlong
java::nio::channels::FileChannelImpl::size ()
@@ -91,4 +93,18 @@
jint /*length*/)
{
throw new ::java::io::IOException (JvNewStringUTF ("msync not implemented"));
+}
+
+void
+java::nio::channels::FileChannelImpl::lockImpl(jlong position, jlong size, jboolean shared)
+{
+ // FIXME: shared is unused, write is always true.
+ fd->lock(position, size, true);
+}
+
+jboolean
+java::nio::channels::FileChannelImpl::tryLockImpl(jlong position, jlong size, jboolean shared)
+{
+ // FIXME: shared is unused, write is always true.
+ return fd->tryLock(position, size, true);
}