jdk/src/java.base/share/native/libjava/RandomAccessFile.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 22946 jdk/src/share/native/java/io/RandomAccessFile.c@0a1379d15976
child 26190 d183677673d9
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19438
7de6ae3cecad 8023203: Wrap RandomAccessFile.seek native method into a Java helper method
dxu
parents: 7668
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_io_RandomAccessFile.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * static method to store field ID's in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
Java_java_io_RandomAccessFile_open(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                   jobject this, jstring path, jint mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (mode & java_io_RandomAccessFile_O_RDONLY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        flags = O_RDONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    else if (mode & java_io_RandomAccessFile_O_RDWR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        flags = O_RDWR | O_CREAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (mode & java_io_RandomAccessFile_O_SYNC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            flags |= O_SYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        else if (mode & java_io_RandomAccessFile_O_DSYNC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            flags |= O_DSYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    fileOpen(env, this, path, raf_fd, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
JNIEXPORT jint JNICALL
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 19438
diff changeset
    67
Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    return readSingle(env, this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    jobject this, jbyteArray bytes, jint off, jint len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    return readBytes(env, this, bytes, off, len, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
JNIEXPORT void JNICALL
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 19438
diff changeset
    78
Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    79
    writeSingle(env, this, byte, JNI_FALSE, raf_fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jobject this, jbyteArray bytes, jint off, jint len) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    85
    writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    jlong ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    jlong cur = jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jlong end = jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    } else if ((end = IO_Lseek(fd, 0L, SEEK_END)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    } else if (IO_Lseek(fd, cur, SEEK_SET) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    return end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
JNIEXPORT void JNICALL
19438
7de6ae3cecad 8023203: Wrap RandomAccessFile.seek native method into a Java helper method
dxu
parents: 7668
diff changeset
   126
Java_java_io_RandomAccessFile_seek0(JNIEnv *env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    jobject this, jlong pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (pos < jlong_zero) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        JNU_ThrowIOException(env, "Negative seek offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                        jlong newLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    jlong cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        JNU_ThrowIOException(env, "Stream Closed");
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
    if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if (IO_SetLength(fd, newLength) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (cur > newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
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
 fail:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    JNU_ThrowIOExceptionWithLastError(env, "setLength failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}