src/java.base/share/native/libjava/FileInputStream.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 41971 jdk/src/java.base/share/native/libjava/FileInputStream.c@d94dec08aecc
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30784
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 26190
diff changeset
     2
 * Copyright (c) 1997, 2015, 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
30784
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 26190
diff changeset
    26
#include <fcntl.h>
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 26190
diff changeset
    27
#include <limits.h>
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 26190
diff changeset
    28
2
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 "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_io_FileInputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*******************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*  BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*******************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * static methods to store field ID's in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
Java_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
JNIEXPORT void JNICALL
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
    60
Java_java_io_FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    fileOpen(env, this, path, fis_fd, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNIEXPORT jint JNICALL
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 17691
diff changeset
    65
Java_java_io_FileInputStream_read0(JNIEnv *env, jobject this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    return readSingle(env, this, fis_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
Java_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        jbyteArray bytes, jint off, jint len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return readBytes(env, this, bytes, off, len, fis_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
JNIEXPORT jlong JNICALL
41971
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 30784
diff changeset
    76
Java_java_io_FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jlong cur = jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jlong end = jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    FD fd = GET_FD(this, fis_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        JNU_ThrowIOException (env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    return (end - cur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
JNIEXPORT jint JNICALL
41971
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 30784
diff changeset
    93
Java_java_io_FileInputStream_available0(JNIEnv *env, jobject this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    jlong ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    FD fd = GET_FD(this, fis_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        JNU_ThrowIOException (env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    if (IO_Available(fd, &ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (ret > INT_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ret = (jlong) INT_MAX;
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 5506
diff changeset
   103
        } else if (ret < 0) {
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 5506
diff changeset
   104
            ret = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return jlong_to_jint(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    JNU_ThrowIOExceptionWithLastError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}