Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/io/FileInputStream.c
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jlong.h"28#include "io_util.h"2930#include "jvm.h"3132#include "java_io_FileInputStream.h"3334#include <fcntl.h>35#include <limits.h>3637#include "io_util_md.h"3839/*******************************************************************/40/* BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/41/*******************************************************************/4243jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */4445/**************************************************************46* static methods to store field ID's in initializers47*/4849JNIEXPORT void JNICALL50Java_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {51fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");52}5354/**************************************************************55* Input stream56*/5758JNIEXPORT void JNICALL59Java_java_io_FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {60fileOpen(env, this, path, fis_fd, O_RDONLY);61}6263JNIEXPORT jint JNICALL64Java_java_io_FileInputStream_read0(JNIEnv *env, jobject this) {65return readSingle(env, this, fis_fd);66}6768JNIEXPORT jint JNICALL69Java_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this,70jbyteArray bytes, jint off, jint len) {71return readBytes(env, this, bytes, off, len, fis_fd);72}7374JNIEXPORT jlong JNICALL75Java_java_io_FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {76jlong cur = jlong_zero;77jlong end = jlong_zero;78FD fd = GET_FD(this, fis_fd);79if (fd == -1) {80JNU_ThrowIOException (env, "Stream Closed");81return 0;82}83if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {84JNU_ThrowIOExceptionWithLastError(env, "Seek error");85} else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {86JNU_ThrowIOExceptionWithLastError(env, "Seek error");87}88return (end - cur);89}9091JNIEXPORT jint JNICALL92Java_java_io_FileInputStream_available0(JNIEnv *env, jobject this) {93jlong ret;94FD fd = GET_FD(this, fis_fd);95if (fd == -1) {96JNU_ThrowIOException (env, "Stream Closed");97return 0;98}99if (IO_Available(fd, &ret)) {100if (ret > INT_MAX) {101ret = (jlong) INT_MAX;102} else if (ret < 0) {103ret = 0;104}105return jlong_to_jint(ret);106}107JNU_ThrowIOExceptionWithLastError(env, NULL);108return 0;109}110111112