J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | #if !defined(O_DSYNC) || !defined(O_SYNC) |
| 33 | #define O_SYNC (0x0800) |
| 34 | #define O_DSYNC (0x2000) |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | * IO helper functions |
| 39 | */ |
| 40 | |
| 41 | int readSingle(JNIEnv *env, jobject this, jfieldID fid); |
| 42 | int readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off, |
| 43 | jint len, jfieldID fid); |
| 44 | void writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid); |
| 45 | void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off, |
| 46 | jint len, jfieldID fid); |
| 47 | void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags); |
| 48 | void throwFileNotFoundException(JNIEnv *env, jstring path); |
| 49 | |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * Macros for managing platform strings. The typical usage pattern is: |
| 54 | * |
| 55 | * WITH_PLATFORM_STRING(env, string, var) { |
| 56 | * doSomethingWith(var); |
| 57 | * } END_PLATFORM_STRING(env, var); |
| 58 | * |
| 59 | * where env is the prevailing JNIEnv, |
| 60 | * string is a JNI reference to a java.lang.String object, and |
| 61 | * var is the char * variable that will point to the string, |
| 62 | * after being converted into the platform encoding. |
| 63 | * |
| 64 | * The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from |
| 65 | * a given field of a given object: |
| 66 | * |
| 67 | * WITH_FIELD_PLATFORM_STRING(env, object, id, var) { |
| 68 | * doSomethingWith(var); |
| 69 | * } END_PLATFORM_STRING(env, var); |
| 70 | * |
| 71 | * where env is the prevailing JNIEnv, |
| 72 | * object is a jobject, |
| 73 | * id is the field ID of the String field to be extracted, and |
| 74 | * var is the char * variable that will point to the string. |
| 75 | * |
| 76 | * Uses of these macros may be nested as long as each WITH_.._STRING macro |
| 77 | * declares a unique variable. |
| 78 | */ |
| 79 | |
| 80 | #define WITH_PLATFORM_STRING(env, strexp, var) \ |
| 81 | if (1) { \ |
| 82 | const char *var; \ |
| 83 | jstring _##var##str = (strexp); \ |
| 84 | if (_##var##str == NULL) { \ |
| 85 | JNU_ThrowNullPointerException((env), NULL); \ |
| 86 | goto _##var##end; \ |
| 87 | } \ |
| 88 | var = JNU_GetStringPlatformChars((env), _##var##str, NULL); \ |
| 89 | if (var == NULL) goto _##var##end; |
| 90 | |
| 91 | #define WITH_FIELD_PLATFORM_STRING(env, object, id, var) \ |
| 92 | WITH_PLATFORM_STRING(env, \ |
| 93 | ((object == NULL) \ |
| 94 | ? NULL \ |
| 95 | : (*(env))->GetObjectField((env), (object), (id))), \ |
| 96 | var) |
| 97 | |
| 98 | #define END_PLATFORM_STRING(env, var) \ |
| 99 | JNU_ReleaseStringPlatformChars(env, _##var##str, var); \ |
| 100 | _##var##end: ; \ |
| 101 | } else ((void)NULL) |
| 102 | |
| 103 | /* Macros for transforming Java Strings into native Unicode strings. |
| 104 | * Works analogously to WITH_PLATFORM_STRING. |
| 105 | */ |
| 106 | |
| 107 | #define WITH_UNICODE_STRING(env, strexp, var) \ |
| 108 | if (1) { \ |
| 109 | const jchar *var; \ |
| 110 | jstring _##var##str = (strexp); \ |
| 111 | if (_##var##str == NULL) { \ |
| 112 | JNU_ThrowNullPointerException((env), NULL); \ |
| 113 | goto _##var##end; \ |
| 114 | } \ |
| 115 | var = (*(env))->GetStringChars((env), _##var##str, NULL); \ |
| 116 | if (var == NULL) goto _##var##end; |
| 117 | |
| 118 | #define END_UNICODE_STRING(env, var) \ |
| 119 | (*(env))->ReleaseStringChars(env, _##var##str, var); \ |
| 120 | _##var##end: ; \ |
| 121 | } else ((void)NULL) |