src/share/native/java/io/io_util.h

Print this page




   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 #ifdef MACOSX
  29 char* convertToNFDIfNeeded(const char *origPath, char *buf, size_t bufsize);
  30 #endif
  31 
  32 extern jfieldID IO_fd_fdID;
  33 extern jfieldID IO_handle_fdID;
  34 
  35 #ifdef _ALLBSD_SOURCE
  36 #include <fcntl.h>
  37 #ifndef O_SYNC
  38 #define O_SYNC  O_FSYNC
  39 #endif
  40 #ifndef O_DSYNC
  41 #define O_DSYNC O_FSYNC
  42 #endif
  43 #elif !defined(O_DSYNC) || !defined(O_SYNC)
  44 #define O_SYNC  (0x0800)
  45 #define O_DSYNC (0x2000)
  46 #endif
  47 
  48 /*
  49  * IO helper functions
  50  */
  51 
  52 jint readSingle(JNIEnv *env, jobject this, jfieldID fid);
  53 jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  54                jint len, jfieldID fid);
  55 void writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid);
  56 void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  57                 jint len, jboolean append, jfieldID fid);
  58 void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags);
  59 void throwFileNotFoundException(JNIEnv *env, jstring path);
  60 
  61 
  62 
  63 /*
  64  * Macros for managing platform strings.  The typical usage pattern is:
  65  *
  66  *     WITH_PLATFORM_STRING(env, string, var) {
  67  *         doSomethingWith(var);
  68  *     } END_PLATFORM_STRING(env, var);
  69  *
  70  *  where  env      is the prevailing JNIEnv,
  71  *         string   is a JNI reference to a java.lang.String object, and
  72  *         var      is the char * variable that will point to the string,
  73  *                  after being converted into the platform encoding.
  74  *
  75  * The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from
  76  * a given field of a given object:
  77  *
  78  *     WITH_FIELD_PLATFORM_STRING(env, object, id, var) {
  79  *         doSomethingWith(var);
  80  *     } END_PLATFORM_STRING(env, var);
  81  *
  82  *  where  env      is the prevailing JNIEnv,
  83  *         object   is a jobject,
  84  *         id       is the field ID of the String field to be extracted, and
  85  *         var      is the char * variable that will point to the string.
  86  *
  87  * Uses of these macros may be nested as long as each WITH_.._STRING macro
  88  * declares a unique variable.
  89  */
  90 
  91 #ifdef MACOSX
  92 
  93 #define WITH_PLATFORM_STRING(env, strexp, var)                                \
  94     if (1) {                                                                  \
  95         const char *var;                                                      \
  96         jstring _##var##str = (strexp);                                       \
  97         if (_##var##str == NULL) {                                            \
  98             JNU_ThrowNullPointerException((env), NULL);                       \
  99             goto _##var##end;                                                 \
 100         }                                                                     \
 101         const char *temp_var = JNU_GetStringPlatformChars((env), _##var##str, NULL);      \
 102         if (temp_var == NULL) goto _##var##end;                               \
 103         char buf[MAXPATHLEN];                                                 \
 104         var = convertToNFDIfNeeded(temp_var, buf, sizeof(buf));
 105 
 106 #define WITH_FIELD_PLATFORM_STRING(env, object, id, var)                      \
 107     WITH_PLATFORM_STRING(env,                                                 \
 108                          ((object == NULL)                                    \
 109                           ? NULL                                              \
 110                           : (*(env))->GetObjectField((env), (object), (id))), \
 111                         var)
 112 
 113 #define END_PLATFORM_STRING(env, var)                                         \
 114         JNU_ReleaseStringPlatformChars(env, _##var##str, temp_var);           \
 115     _##var##end: ;                                                            \
 116     } else ((void)NULL)
 117 
 118 #else
 119 
 120 #define WITH_PLATFORM_STRING(env, strexp, var)                                \
 121     if (1) {                                                                  \
 122         const char *var;                                                      \
 123         jstring _##var##str = (strexp);                                       \
 124         if (_##var##str == NULL) {                                            \
 125             JNU_ThrowNullPointerException((env), NULL);                       \
 126             goto _##var##end;                                                 \
 127         }                                                                     \
 128         var = JNU_GetStringPlatformChars((env), _##var##str, NULL);           \
 129         if (var == NULL) goto _##var##end;
 130 
 131 #define WITH_FIELD_PLATFORM_STRING(env, object, id, var)                      \
 132     WITH_PLATFORM_STRING(env,                                                 \
 133                          ((object == NULL)                                    \
 134                           ? NULL                                              \
 135                           : (*(env))->GetObjectField((env), (object), (id))), \
 136                          var)
 137 
 138 #define END_PLATFORM_STRING(env, var)                                         \
 139         JNU_ReleaseStringPlatformChars(env, _##var##str, var);                \
 140     _##var##end: ;                                                            \
 141     } else ((void)NULL)
 142 
 143 #endif
 144 
 145 
 146 /* Macros for transforming Java Strings into native Unicode strings.
 147  * Works analogously to WITH_PLATFORM_STRING.
 148  */
 149 
 150 #define WITH_UNICODE_STRING(env, strexp, var)                                 \
 151     if (1) {                                                                  \
 152         const jchar *var;                                                     \
 153         jstring _##var##str = (strexp);                                       \
 154         if (_##var##str == NULL) {                                            \
 155             JNU_ThrowNullPointerException((env), NULL);                       \
 156             goto _##var##end;                                                 \
 157         }                                                                     \
 158         var = (*(env))->GetStringChars((env), _##var##str, NULL);             \
 159         if (var == NULL) goto _##var##end;
 160 
 161 #define END_UNICODE_STRING(env, var)                                          \
 162         (*(env))->ReleaseStringChars(env, _##var##str, var);                  \
 163     _##var##end: ;                                                            \
 164     } else ((void)NULL)


   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 extern jfieldID IO_fd_fdID;
  30 extern jfieldID IO_handle_fdID;
  31 
  32 #ifdef _ALLBSD_SOURCE
  33 #include <fcntl.h>
  34 #ifndef O_SYNC
  35 #define O_SYNC  O_FSYNC
  36 #endif
  37 #ifndef O_DSYNC
  38 #define O_DSYNC O_FSYNC
  39 #endif
  40 #elif !defined(O_DSYNC) || !defined(O_SYNC)
  41 #define O_SYNC  (0x0800)
  42 #define O_DSYNC (0x2000)
  43 #endif
  44 
  45 /*
  46  * IO helper functions
  47  */
  48 
  49 jint readSingle(JNIEnv *env, jobject this, jfieldID fid);
  50 jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  51                jint len, jfieldID fid);
  52 void writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid);
  53 void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  54                 jint len, jboolean append, jfieldID fid);
  55 void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags);
  56 void throwFileNotFoundException(JNIEnv *env, jstring path);
  57 
  58 

  59 /*
  60  * Macros for managing platform strings.  The typical usage pattern is:
  61  *
  62  *     WITH_PLATFORM_STRING(env, string, var) {
  63  *         doSomethingWith(var);
  64  *     } END_PLATFORM_STRING(env, var);
  65  *
  66  *  where  env      is the prevailing JNIEnv,
  67  *         string   is a JNI reference to a java.lang.String object, and
  68  *         var      is the char * variable that will point to the string,
  69  *                  after being converted into the platform encoding.
  70  *
  71  * The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from
  72  * a given field of a given object:
  73  *
  74  *     WITH_FIELD_PLATFORM_STRING(env, object, id, var) {
  75  *         doSomethingWith(var);
  76  *     } END_PLATFORM_STRING(env, var);
  77  *
  78  *  where  env      is the prevailing JNIEnv,
  79  *         object   is a jobject,
  80  *         id       is the field ID of the String field to be extracted, and
  81  *         var      is the char * variable that will point to the string.
  82  *
  83  * Uses of these macros may be nested as long as each WITH_.._STRING macro
  84  * declares a unique variable.
  85  */
  86 





























  87 #define WITH_PLATFORM_STRING(env, strexp, var)                                \
  88     if (1) {                                                                  \
  89         const char *var;                                                      \
  90         jstring _##var##str = (strexp);                                       \
  91         if (_##var##str == NULL) {                                            \
  92             JNU_ThrowNullPointerException((env), NULL);                       \
  93             goto _##var##end;                                                 \
  94         }                                                                     \
  95         var = JNU_GetStringPlatformChars((env), _##var##str, NULL);           \
  96         if (var == NULL) goto _##var##end;
  97 
  98 #define WITH_FIELD_PLATFORM_STRING(env, object, id, var)                      \
  99     WITH_PLATFORM_STRING(env,                                                 \
 100                          ((object == NULL)                                    \
 101                           ? NULL                                              \
 102                           : (*(env))->GetObjectField((env), (object), (id))), \
 103                          var)
 104 
 105 #define END_PLATFORM_STRING(env, var)                                         \
 106         JNU_ReleaseStringPlatformChars(env, _##var##str, var);                \
 107     _##var##end: ;                                                            \
 108     } else ((void)NULL)
 109 


 110 
 111 /* Macros for transforming Java Strings into native Unicode strings.
 112  * Works analogously to WITH_PLATFORM_STRING.
 113  */
 114 
 115 #define WITH_UNICODE_STRING(env, strexp, var)                                 \
 116     if (1) {                                                                  \
 117         const jchar *var;                                                     \
 118         jstring _##var##str = (strexp);                                       \
 119         if (_##var##str == NULL) {                                            \
 120             JNU_ThrowNullPointerException((env), NULL);                       \
 121             goto _##var##end;                                                 \
 122         }                                                                     \
 123         var = (*(env))->GetStringChars((env), _##var##str, NULL);             \
 124         if (var == NULL) goto _##var##end;
 125 
 126 #define END_UNICODE_STRING(env, var)                                          \
 127         (*(env))->ReleaseStringChars(env, _##var##str, var);                  \
 128     _##var##end: ;                                                            \
 129     } else ((void)NULL)