This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

lock/unlock filechannel Win32 implementation (patch file)


Tested, seems to work fine :)

attached the diff patch!

Martin

Index: gnu/java/nio/channels/natFileChannelWin32.cc
===================================================================
--- gnu/java/nio/channels/natFileChannelWin32.cc	(revision 117867)
+++ gnu/java/nio/channels/natFileChannelWin32.cc	(working copy)
@@ -30,6 +30,7 @@
 #include <java/io/InterruptedIOException.h>
 #include <java/io/EOFException.h>
 #include <java/lang/ArrayIndexOutOfBoundsException.h>
+#include <java/lang/IllegalArgumentException.h>
 #include <java/lang/NullPointerException.h>
 #include <java/lang/System.h>
 #include <java/lang/String.h>
@@ -342,19 +343,87 @@
   return size() - position();
 }
 
-jboolean
-FileChannelImpl::lock
-(jlong /*pos*/, jlong /*len*/, jboolean /*shared*/, jboolean /*wait*/)
+jboolean FileChannelImpl::lock (jlong pos, jlong len, jboolean shared, jboolean wait)
 {
-  throw new IOException (JvNewStringLatin1
-    ("FileChannel.lock() not implemented"));
+    DWORD flags = 0;
+    OVERLAPPED ovlpd;
+
+    if(pos<0 || len<0)
+        throw new ::java::lang::IllegalArgumentException
+            (JvNewStringLatin1("pos or len are negative!"));
+
+    ZeroMemory(&ovlpd,sizeof(OVERLAPPED));
+
+    if(!shared)
+        flags |= LOCKFILE_EXCLUSIVE_LOCK;
+    if(!wait)
+        flags |= LOCKFILE_FAIL_IMMEDIATELY;
+
+    ovlpd.Offset = (DWORD)pos;
+    ovlpd.OffsetHigh = pos>>32;
+
+    DWORD lenlow = (DWORD)len;
+    DWORD lenhigh = len>>32;
+
+    BOOL ret = LockFileEx((HANDLE)fd,flags,0,lenlow,lenhigh,&ovlpd);
+
+    if(ret==ERROR_IO_PENDING && !shared && wait)
+    {
+        ret = GetOverlappedResult((HANDLE)fd,&ovlpd,NULL,wait);
+    }
+
+    if(!ret)
+    {
+        DWORD errnum = GetLastError();
+        LPTSTR perrmsg = NULL;
+
+        if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+            FORMAT_MESSAGE_FROM_SYSTEM,0,errnum,0,(LPTSTR)&perrmsg,0,NULL))
+        {
+            jstring jerrstr= JvNewStringLatin1(perrmsg);
+            LocalFree(perrmsg);
+            throw new IOException(jerrstr);
+        }
+        else
+            throw new IOException(JvNewStringLatin1("LockFileEx() failed!"));
+    }
+
+    return true;
 }
 
-void
-FileChannelImpl::unlock (jlong /*pos*/, jlong /*len*/)
+void FileChannelImpl::unlock (jlong pos, jlong len)
 {
-  throw new IOException (JvNewStringLatin1
-    ("FileChannel.unlock() not implemented"));
+    OVERLAPPED ovlpd;
+
+    if(pos<0 || len<0)
+        throw new ::java::lang::IllegalArgumentException
+        (JvNewStringLatin1("pos or len are negative!"));
+
+    ZeroMemory(&ovlpd,sizeof(OVERLAPPED));
+
+    ovlpd.Offset = (DWORD)pos;
+    ovlpd.OffsetHigh = pos>>32;
+
+    DWORD lenlow = (DWORD)len;
+    DWORD lenhigh = len>>32;
+
+    BOOL ret = UnlockFileEx((HANDLE)fd,0,lenlow,lenhigh,&ovlpd);
+
+    if(!ret)
+    {
+        DWORD errnum = GetLastError();
+        LPTSTR perrmsg = NULL;
+
+        if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+            FORMAT_MESSAGE_FROM_SYSTEM,0,errnum,0,(LPTSTR)&perrmsg,0,NULL))
+        {
+            jstring jerrstr= JvNewStringLatin1(perrmsg);
+            LocalFree(perrmsg);
+            throw new IOException(jerrstr);
+        }
+        else
+            throw new IOException(JvNewStringLatin1("UnlockFileEx() failed!"));
+    }
 }
 
 java::nio::MappedByteBuffer *

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]