jdk/src/share/native/java/io/io_util.h
author alanb
Wed, 01 Dec 2010 13:49:02 +0000
changeset 7515 43202796198e
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
     2
 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1773
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
extern jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
extern jfieldID IO_handle_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#if !defined(O_DSYNC) || !defined(O_SYNC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define O_SYNC  (0x0800)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#define O_DSYNC (0x2000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * IO helper functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 2
diff changeset
    41
jint readSingle(JNIEnv *env, jobject this, jfieldID fid);
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 2
diff changeset
    42
jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 2
diff changeset
    43
               jint len, jfieldID fid);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    44
void writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    46
                jint len, jboolean append, jfieldID fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
void throwFileNotFoundException(JNIEnv *env, jstring path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Macros for managing platform strings.  The typical usage pattern is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     WITH_PLATFORM_STRING(env, string, var) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *         doSomethingWith(var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     } END_PLATFORM_STRING(env, var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  where  env      is the prevailing JNIEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *         string   is a JNI reference to a java.lang.String object, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *         var      is the char * variable that will point to the string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *                  after being converted into the platform encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * a given field of a given object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     WITH_FIELD_PLATFORM_STRING(env, object, id, var) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         doSomethingWith(var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     } END_PLATFORM_STRING(env, var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *  where  env      is the prevailing JNIEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         object   is a jobject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         id       is the field ID of the String field to be extracted, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         var      is the char * variable that will point to the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Uses of these macros may be nested as long as each WITH_.._STRING macro
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * declares a unique variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#define WITH_PLATFORM_STRING(env, strexp, var)                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (1) {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        const char *var;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        jstring _##var##str = (strexp);                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (_##var##str == NULL) {                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            JNU_ThrowNullPointerException((env), NULL);                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            goto _##var##end;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        var = JNU_GetStringPlatformChars((env), _##var##str, NULL);           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (var == NULL) goto _##var##end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#define WITH_FIELD_PLATFORM_STRING(env, object, id, var)                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    WITH_PLATFORM_STRING(env,                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                         ((object == NULL)                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                          ? NULL                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                          : (*(env))->GetObjectField((env), (object), (id))), \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                         var)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
#define END_PLATFORM_STRING(env, var)                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JNU_ReleaseStringPlatformChars(env, _##var##str, var);                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    _##var##end: ;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    } else ((void)NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
/* Macros for transforming Java Strings into native Unicode strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Works analogously to WITH_PLATFORM_STRING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#define WITH_UNICODE_STRING(env, strexp, var)                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (1) {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        const jchar *var;                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        jstring _##var##str = (strexp);                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (_##var##str == NULL) {                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            JNU_ThrowNullPointerException((env), NULL);                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            goto _##var##end;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        var = (*(env))->GetStringChars((env), _##var##str, NULL);             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (var == NULL) goto _##var##end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
#define END_UNICODE_STRING(env, var)                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        (*(env))->ReleaseStringChars(env, _##var##str, var);                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    _##var##end: ;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    } else ((void)NULL)