1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 
  29 /*
  30  * Macros to use the right data type for file descriptors
  31  */
  32 #define FD jint
  33 
  34 /*
  35  * Macros to set/get fd from the java.io.FileDescriptor.  These
  36  * macros rely on having an appropriately defined 'this' object
  37  * within the scope in which they're used.
  38  * If GetObjectField returns null, SET_FD will stop and GET_FD
  39  * will simply return -1 to avoid crashing VM.
  40  */
  41 
  42 #define SET_FD(this, fd, fid) \
  43     if ((*env)->GetObjectField(env, (this), (fid)) != NULL) \
  44         (*env)->SetIntField(env, (*env)->GetObjectField(env, (this), (fid)),IO_fd_fdID, (fd))
  45 
  46 #define GET_FD(this, fid) \
  47     (*env)->GetObjectField(env, (this), (fid)) == NULL ? \
  48         -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)
  49 
  50 /*
  51  * Macros to set/get fd when inside java.io.FileDescriptor
  52  */
  53 #define THIS_FD(obj) (*env)->GetIntField(env, obj, IO_fd_fdID)
  54 
  55 /*
  56  * Route the routines through VM
  57  */
  58 #define IO_Append JVM_Write
  59 #define IO_Write JVM_Write
  60 #define IO_Sync JVM_Sync
  61 #define IO_Read JVM_Read
  62 #define IO_Lseek JVM_Lseek
  63 #define IO_Available JVM_Available
  64 #define IO_SetLength JVM_SetLength
  65 
  66 /*
  67  * On Solaris, the handle field is unused
  68  */
  69 #define SET_HANDLE(fd) return (jlong)-1
  70 
  71 /*
  72  * IO helper function(s)
  73  */
  74 void fileClose(JNIEnv *env, jobject this, jfieldID fid);