jdk/src/share/native/java/io/io_util.c
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 47 c8f0e41aea68
child 1768 27f2d52ea088
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 47
diff changeset
     2
 * Copyright 1994-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/* IO helper functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
readSingle(JNIEnv *env, jobject this, jfieldID fid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    char ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
    43
        JNU_ThrowIOException(env, "Stream Closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    nread = IO_Read(fd, &ret, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    if (nread == 0) { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    } else if (nread == JVM_IO_ERR) { /* error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        JNU_ThrowIOExceptionWithLastError(env, "Read error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    } else if (nread == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    return ret & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/* The maximum size of a stack-allocated buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#define BUF_SIZE 8192
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
          jint off, jint len, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int nread, datalen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    char stackBuf[BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    char *buf = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (IS_NULL(bytes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    datalen = (*env)->GetArrayLength(env, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if ((off < 0) || (off > datalen) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    } else if (len > BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        buf = malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (buf == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        buf = stackBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
    97
        JNU_ThrowIOException(env, "Stream Closed");
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 44
diff changeset
    98
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    nread = IO_Read(fd, buf, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    } else if (nread == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        JNU_ThrowIOExceptionWithLastError(env, "Read error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    } else if (nread == JVM_IO_INTR) { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    } else { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        nread = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (buf != stackBuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    char c = byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   124
        JNU_ThrowIOException(env, "Stream Closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    n = IO_Write(fd, &c, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        JNU_ThrowIOExceptionWithLastError(env, "Write error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
          jint off, jint len, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    int n, datalen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    char stackBuf[BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    char *buf = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (IS_NULL(bytes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    datalen = (*env)->GetArrayLength(env, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if ((off < 0) || (off > datalen) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    } else if (len > BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        buf = malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (buf == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            JNU_ThrowOutOfMemoryError(env, 0);
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
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        buf = stackBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (fd == -1) {
44
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   175
                JNU_ThrowIOException(env, "Stream Closed");
9291315d799d 6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents: 2
diff changeset
   176
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            n = IO_Write(fd, buf+off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                JNU_ThrowIOExceptionWithLastError(env, "Write error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (buf != stackBuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
throwFileNotFoundException(JNIEnv *env, jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    char buf[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    jobject x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    jstring why = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    n = JVM_GetLastErrorString(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    why = JNU_NewStringPlatform(env, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    x = JNU_NewObjectByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                "java/io/FileNotFoundException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                "(Ljava/lang/String;Ljava/lang/String;)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                path, why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    if (x != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    (*env)->Throw(env, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}