Path: blob/master/src/java.base/share/native/libjava/FileInputStream.c
41149 views
/*1* Copyright (c) 1997, 2021, 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 <fcntl.h>26#include <limits.h>2728#include "jni.h"29#include "jni_util.h"30#include "jlong.h"31#include "io_util.h"3233#include "jvm.h"3435#include "java_io_FileInputStream.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_length0(JNIEnv *env, jobject this) {7677FD fd;78jlong length = jlong_zero;7980fd = getFD(env, this, fis_fd);81if (fd == -1) {82JNU_ThrowIOException(env, "Stream Closed");83return -1;84}85if ((length = IO_GetLength(fd)) == -1) {86JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");87}88return length;89}9091JNIEXPORT jlong JNICALL92Java_java_io_FileInputStream_position0(JNIEnv *env, jobject this) {93FD fd;94jlong ret;9596fd = getFD(env, this, fis_fd);97if (fd == -1) {98JNU_ThrowIOException(env, "Stream Closed");99return -1;100}101if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {102JNU_ThrowIOExceptionWithLastError(env, "Seek failed");103}104return ret;105}106107JNIEXPORT jlong JNICALL108Java_java_io_FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {109jlong cur = jlong_zero;110jlong end = jlong_zero;111FD fd = getFD(env, this, fis_fd);112if (fd == -1) {113JNU_ThrowIOException (env, "Stream Closed");114return 0;115}116if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {117JNU_ThrowIOExceptionWithLastError(env, "Seek error");118} else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {119JNU_ThrowIOExceptionWithLastError(env, "Seek error");120}121return (end - cur);122}123124JNIEXPORT jint JNICALL125Java_java_io_FileInputStream_available0(JNIEnv *env, jobject this) {126jlong ret;127FD fd = getFD(env, this, fis_fd);128if (fd == -1) {129JNU_ThrowIOException (env, "Stream Closed");130return 0;131}132if (IO_Available(fd, &ret)) {133if (ret > INT_MAX) {134ret = (jlong) INT_MAX;135} else if (ret < 0) {136ret = 0;137}138return jlong_to_jint(ret);139}140JNU_ThrowIOExceptionWithLastError(env, NULL);141return 0;142}143144145