jdk/src/share/native/java/io/io_util.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8186 d0e2cc8b3073
child 11116 3e486ce0019d
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8186
diff changeset
     2
 * Copyright (c) 1994, 2011, 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 <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    28
#include <stddef.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/* IO helper functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    38
jint
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
readSingle(JNIEnv *env, jobject this, jfieldID fid) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    40
    jint nread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    char ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
    44
        JNU_ThrowIOException(env, "Stream Closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
    47
    nread = (jint)IO_Read(fd, &ret, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    if (nread == 0) { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    } else if (nread == JVM_IO_ERR) { /* error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        JNU_ThrowIOExceptionWithLastError(env, "Read error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    } else if (nread == JVM_IO_INTR) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    53
        JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    return ret & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/* The maximum size of a stack-allocated buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#define BUF_SIZE 8192
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
1768
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    62
/*
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    63
 * Returns true if the array slice defined by the given offset and length
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    64
 * is out of bounds.
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    65
 */
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    66
static int
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    67
outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    68
    return ((off < 0) ||
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    69
            (len < 0) ||
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    70
            // We are very careful to avoid signed integer overflow,
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    71
            // the result of which is undefined in C.
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    72
            ((*env)->GetArrayLength(env, array) - off < len));
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    73
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    75
jint
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
          jint off, jint len, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
{
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    79
    jint nread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    char stackBuf[BUF_SIZE];
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    81
    char *buf = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    if (IS_NULL(bytes)) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    85
        JNU_ThrowNullPointerException(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
1768
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
    89
    if (outOfBounds(env, off, len, bytes)) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    90
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    } else if (len > BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        buf = malloc(len);
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    98
        if (buf == NULL) {
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
    99
            JNU_ThrowOutOfMemoryError(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        buf = stackBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   108
        JNU_ThrowIOException(env, "Stream Closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        nread = -1;
1769
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   110
    } else {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   111
        nread = (jint)IO_Read(fd, buf, len);
1769
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   112
        if (nread > 0) {
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   113
            (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   114
        } else if (nread == JVM_IO_ERR) {
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   115
            JNU_ThrowIOExceptionWithLastError(env, "Read error");
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   116
        } else if (nread == JVM_IO_INTR) {
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   117
            JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
1769
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   118
        } else { /* EOF */
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   119
            nread = -1;
27a33d1b16ec 6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents: 1768
diff changeset
   120
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (buf != stackBuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
void
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   130
writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   131
    // Discard the 24 high-order bits of byte. See OutputStream#write(int)
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   132
    char c = (char) byte;
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   133
    jint n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   136
        JNU_ThrowIOException(env, "Stream Closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   139
    if (append == JNI_TRUE) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   140
        n = (jint)IO_Append(fd, &c, 1);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   141
    } else {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   142
        n = (jint)IO_Write(fd, &c, 1);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   143
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JNU_ThrowIOExceptionWithLastError(env, "Write error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    } else if (n == JVM_IO_INTR) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   147
        JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   153
           jint off, jint len, jboolean append, jfieldID fid)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   155
    jint n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    char stackBuf[BUF_SIZE];
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   157
    char *buf = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (IS_NULL(bytes)) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   161
        JNU_ThrowNullPointerException(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
1768
27f2d52ea088 6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents: 715
diff changeset
   165
    if (outOfBounds(env, off, len, bytes)) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   166
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    } else if (len > BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        buf = malloc(len);
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   174
        if (buf == NULL) {
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   175
            JNU_ThrowOutOfMemoryError(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        buf = stackBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   189
                JNU_ThrowIOException(env, "Stream Closed");
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   190
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   192
            if (append == JNI_TRUE) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   193
                n = (jint)IO_Append(fd, buf+off, len);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   194
            } else {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   195
                n = (jint)IO_Write(fd, buf+off, len);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   196
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                JNU_ThrowIOExceptionWithLastError(env, "Write error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else if (n == JVM_IO_INTR) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   201
                JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    if (buf != stackBuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
throwFileNotFoundException(JNIEnv *env, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    char buf[256];
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   217
    jint n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    jobject x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jstring why = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    n = JVM_GetLastErrorString(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    if (n > 0) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   223
        why = JNU_NewStringPlatform(env, buf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    x = JNU_NewObjectByName(env,
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   226
                            "java/io/FileNotFoundException",
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   227
                            "(Ljava/lang/String;Ljava/lang/String;)V",
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   228
                            path, why);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    if (x != NULL) {
1773
1be34caf9662 6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents: 1769
diff changeset
   230
        (*env)->Throw(env, x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}