Index: lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.cpp
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.cpp	(revision 1143955)
+++ lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.cpp	(working copy)
@@ -15,10 +15,14 @@
  * the License.
  */
 
-#include <jni.h>
-#include <fcntl.h>   // posix_fadvise, constants for open
-#include <string.h>   // strerror
-#include <errno.h>   // errno
+#if linux
+  #include "onlylinux.h"
+#elif __APPLE__
+  #include "onlyosx.h"
+#else
+  #include "onlybsd.h"    // __unix__ is not used as even Linux falls under it.  
+#endif
+
 #include <unistd.h>   // pread
 #include <sys/mman.h>   // posix_madvise, madvise
 #include <sys/types.h>  // constants for open
@@ -28,124 +32,6 @@
 
 /*
  * Class:     org_apache_lucene_store_NativePosixUtil
- * Method:    posix_fadvise
- * Signature: (Ljava/io/FileDescriptor;JJI)V
- */
-extern "C"
-JNIEXPORT jint JNICALL Java_org_apache_lucene_store_NativePosixUtil_posix_1fadvise(JNIEnv *env, jclass _ignore, jobject fileDescriptor, jlong offset, jlong len, jint advice)
-{
-  jfieldID field_fd;
-  jmethodID const_fdesc;
-
-  jclass ioex = env->FindClass("java/io/IOException");
-  if (ioex == NULL) {
-    return -1;
-  }
-
-  jclass fdesc = env->FindClass("java/io/FileDescriptor");
-  if (fdesc == NULL) {
-    return -2;
-  }
-
-  // read the int fd field
-  jfieldID fdField = env->GetFieldID(fdesc, "fd", "I");
-  if (fdField == NULL) {
-    return -3;
-  }
-
-  int fd = env->GetIntField(fileDescriptor, fdField);
-  //printf("fd=%d\n", fd);  fflush(stdout);
-
-  int osAdvice;
-  switch(advice) {
-
-  case 0:
-    osAdvice = POSIX_FADV_NORMAL;
-    break;
-  case 1:
-    osAdvice = POSIX_FADV_SEQUENTIAL;
-    break;
-  case 2:
-    osAdvice = POSIX_FADV_RANDOM;
-    break;
-  case 3:
-    osAdvice = POSIX_FADV_WILLNEED;
-    break;
-  case 4:
-    osAdvice = POSIX_FADV_DONTNEED;
-    break;
-  case 5:
-    osAdvice = POSIX_FADV_NOREUSE;
-    break;
-  }
-
-  int result = posix_fadvise(fd, (off_t) offset, (off_t) len, osAdvice);
-  if (result == 0) {
-    // ok
-  } else {
-    env->ThrowNew(ioex, strerror(errno));
-    return -1;
-  }
-
-  return 0;
-}
-
-
-/*
- * Class:     org_apache_lucene_store_NativePosixUtil
- * Method:    open_direct
- * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
- */
-extern "C"
-JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1direct(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
-{
-  jfieldID field_fd;
-  jmethodID const_fdesc;
-  jclass class_fdesc, class_ioex;
-  jobject ret;
-  int fd;
-  char *fname;
-
-  class_ioex = env->FindClass("java/io/IOException");
-  if (class_ioex == NULL) return NULL;
-  class_fdesc = env->FindClass("java/io/FileDescriptor");
-  if (class_fdesc == NULL) return NULL;
-
-  fname = (char *) env->GetStringUTFChars(filename, NULL);
-
-  if (readOnly) {
-    fd = open(fname, O_RDONLY | O_DIRECT | O_NOATIME);
-  } else {
-    fd = open(fname, O_RDWR | O_CREAT | O_DIRECT | O_NOATIME, 0666);
-  }
-
-  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
-
-  env->ReleaseStringUTFChars(filename, fname);
-
-  if (fd < 0) {
-    // open returned an error. Throw an IOException with the error string
-    env->ThrowNew(class_ioex, strerror(errno));
-    return NULL;
-  }
-
-  // construct a new FileDescriptor
-  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
-  if (const_fdesc == NULL) return NULL;
-  ret = env->NewObject(class_fdesc, const_fdesc);
-
-  // poke the "fd" field with the file descriptor
-  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
-  if (field_fd == NULL) return NULL;
-  env->SetIntField(ret, field_fd, fd);
-
-  // and return it
-  return ret;
-}
-
-
-/*
- * Class:     org_apache_lucene_store_NativePosixUtil
  * Method:    pread
  * Signature: (Ljava/io/FileDescriptor;JLjava/nio/ByteBuffer;)I
  */
Index: lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.java
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.java	(revision 1143955)
+++ lucene/contrib/misc/src/java/org/apache/lucene/store/NativePosixUtil.java	(working copy)
@@ -37,6 +37,7 @@
   public static native int posix_madvise(ByteBuffer buf, int advise) throws IOException;
   public static native int madvise(ByteBuffer buf, int advise) throws IOException;
   public static native FileDescriptor open_direct(String filename, boolean read) throws IOException;
+  public static native FileDescriptor open_normal(String filename, boolean read) throws IOException; 
   public static native long pread(FileDescriptor fd, long pos, ByteBuffer byteBuf) throws IOException;
 
   public static void advise(FileDescriptor fd, long offset, long len, int advise) throws IOException {
Index: lucene/contrib/misc/src/java/org/apache/lucene/store/onlybsd.h
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/store/onlybsd.h	(revision 0)
+++ lucene/contrib/misc/src/java/org/apache/lucene/store/onlybsd.h	(revision 0)
@@ -0,0 +1,193 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.
+ */
+
+#include <jni.h>
+#include <fcntl.h>   // posix_fadvise, constants for open
+#include <string.h>   // strerror
+#include <errno.h>   // errno
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_direct
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1direct(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY | O_DIRECT);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT | O_DIRECT, 0666);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
+
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_normal
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1normal(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT, 0666);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
+
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    posix_fadvise
+ * Signature: (Ljava/io/FileDescriptor;JJI)V
+ */
+extern "C"
+JNIEXPORT jint JNICALL Java_org_apache_lucene_store_NativePosixUtil_posix_1fadvise(JNIEnv *env, jclass _ignore, jobject fileDescriptor, jlong offset, jlong len, jint advice)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+
+  jclass ioex = env->FindClass("java/io/IOException");
+  if (ioex == NULL) {
+    return -1;
+  }
+
+  jclass fdesc = env->FindClass("java/io/FileDescriptor");
+  if (fdesc == NULL) {
+    return -2;
+  }
+
+  // read the int fd field
+  jfieldID fdField = env->GetFieldID(fdesc, "fd", "I");
+  if (fdField == NULL) {
+    return -3;
+  }
+
+  int fd = env->GetIntField(fileDescriptor, fdField);
+  //printf("fd=%d\n", fd);  fflush(stdout);
+
+  int osAdvice;
+  switch(advice) {
+
+  case 0:
+    osAdvice = POSIX_FADV_NORMAL;
+    break;
+  case 1:
+    osAdvice = POSIX_FADV_SEQUENTIAL;
+    break;
+  case 2:
+    osAdvice = POSIX_FADV_RANDOM;
+    break;
+  case 3:
+    osAdvice = POSIX_FADV_WILLNEED;
+    break;
+  case 4:
+    osAdvice = POSIX_FADV_DONTNEED;
+    break;
+  case 5:
+    osAdvice = POSIX_FADV_NOREUSE;
+    break;
+  }
+
+  int result = posix_fadvise(fd, (off_t) offset, (off_t) len, osAdvice);
+  if (result == 0) {
+    // ok
+  } else {
+    env->ThrowNew(ioex, strerror(errno));
+    return -1;
+  }
+
+  return 0;
+}
Index: lucene/contrib/misc/src/java/org/apache/lucene/store/onlylinux.h
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/store/onlylinux.h	(revision 0)
+++ lucene/contrib/misc/src/java/org/apache/lucene/store/onlylinux.h	(revision 0)
@@ -0,0 +1,193 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.
+ */
+
+#include <jni.h>
+#include <fcntl.h>   // posix_fadvise, constants for open
+#include <string.h>   // strerror
+#include <errno.h>   // errno
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_direct
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1direct(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY | O_DIRECT | O_NOATIME);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT | O_DIRECT | O_NOATIME, 0666);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
+
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_normal
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1normal(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY | O_NOATIME);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT | O_NOATIME, 0666);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
+
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    posix_fadvise
+ * Signature: (Ljava/io/FileDescriptor;JJI)V
+ */
+extern "C"
+JNIEXPORT jint JNICALL Java_org_apache_lucene_store_NativePosixUtil_posix_1fadvise(JNIEnv *env, jclass _ignore, jobject fileDescriptor, jlong offset, jlong len, jint advice)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+
+  jclass ioex = env->FindClass("java/io/IOException");
+  if (ioex == NULL) {
+    return -1;
+  }
+
+  jclass fdesc = env->FindClass("java/io/FileDescriptor");
+  if (fdesc == NULL) {
+    return -2;
+  }
+
+  // read the int fd field
+  jfieldID fdField = env->GetFieldID(fdesc, "fd", "I");
+  if (fdField == NULL) {
+    return -3;
+  }
+
+  int fd = env->GetIntField(fileDescriptor, fdField);
+  //printf("fd=%d\n", fd);  fflush(stdout);
+
+  int osAdvice;
+  switch(advice) {
+
+  case 0:
+    osAdvice = POSIX_FADV_NORMAL;
+    break;
+  case 1:
+    osAdvice = POSIX_FADV_SEQUENTIAL;
+    break;
+  case 2:
+    osAdvice = POSIX_FADV_RANDOM;
+    break;
+  case 3:
+    osAdvice = POSIX_FADV_WILLNEED;
+    break;
+  case 4:
+    osAdvice = POSIX_FADV_DONTNEED;
+    break;
+  case 5:
+    osAdvice = POSIX_FADV_NOREUSE;
+    break;
+  }
+
+  int result = posix_fadvise(fd, (off_t) offset, (off_t) len, osAdvice);
+  if (result == 0) {
+    // ok
+  } else {
+    env->ThrowNew(ioex, strerror(errno));
+    return -1;
+  }
+
+  return 0;
+}
Index: lucene/contrib/misc/src/java/org/apache/lucene/store/onlyosx.h
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/store/onlyosx.h	(revision 0)
+++ lucene/contrib/misc/src/java/org/apache/lucene/store/onlyosx.h	(revision 0)
@@ -0,0 +1,130 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.
+ */
+
+#include <jni.h>
+#include <fcntl.h>   // posix_fadvise, constants for open
+#include <string.h>   // strerror
+#include <errno.h>   // errno
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_direct
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1direct(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY);
+    fcntl(fd, F_NOCACHE, 1);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT, 0666);
+    fcntl(fd, F_NOCACHE, 1);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
+
+
+/*
+ * Class:     org_apache_lucene_store_NativePosixUtil
+ * Method:    open_normal
+ * Signature: (Ljava/lang/String;Z)Ljava/io/FileDescriptor;
+ */
+
+extern "C"
+JNIEXPORT jobject JNICALL Java_org_apache_lucene_store_NativePosixUtil_open_1normal(JNIEnv *env, jclass _ignore, jstring filename, jboolean readOnly)
+{
+  jfieldID field_fd;
+  jmethodID const_fdesc;
+  jclass class_fdesc, class_ioex;
+  jobject ret;
+  int fd;
+  char *fname;
+
+  class_ioex = env->FindClass("java/io/IOException");
+  if (class_ioex == NULL) return NULL;
+  class_fdesc = env->FindClass("java/io/FileDescriptor");
+  if (class_fdesc == NULL) return NULL;
+
+  fname = (char *) env->GetStringUTFChars(filename, NULL);
+
+  if (readOnly) {
+    fd = open(fname, O_RDONLY);
+  } else {
+    fd = open(fname, O_RDWR | O_CREAT, 0666);
+  }
+
+  //printf("open %s -> %d; ro %d\n", fname, fd, readOnly); fflush(stdout);
+
+  env->ReleaseStringUTFChars(filename, fname);
+
+  if (fd < 0) {
+    // open returned an error. Throw an IOException with the error string
+    env->ThrowNew(class_ioex, strerror(errno));
+    return NULL;
+  }
+
+  // construct a new FileDescriptor
+  const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
+  if (const_fdesc == NULL) return NULL;
+  ret = env->NewObject(class_fdesc, const_fdesc);
+
+  // poke the "fd" field with the file descriptor
+  field_fd = env->GetFieldID(class_fdesc, "fd", "I");
+  if (field_fd == NULL) return NULL;
+  env->SetIntField(ret, field_fd, fd);
+
+  // and return it
+  return ret;
+}
