1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <fcntl.h> 27 #include <limits.h> 28 29 #include "jni.h" 30 #include "jni_util.h" 31 #include "jlong.h" 32 #include "io_util.h" 33 34 #include "jvm.h" 35 36 #include "java_io_FileInputStream.h" 37 38 #include "io_util_md.h" 39 40 /*******************************************************************/ 41 /* BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/ 42 /*******************************************************************/ 43 44 jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */ 45 jfieldID fis_pgsz; /* id for jobject 'pageSize' in java.io.FileInputStream */ 46 47 /************************************************************** 48 * static methods to store field ID's in initializers 49 */ 50 51 JNIEXPORT void JNICALL 52 Java_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) { 53 fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;"); 54 fis_pgsz = (*env)->GetFieldID(env, fdClass, "pageSize", "I"); 55 } 56 57 /************************************************************** 58 * Input stream 59 */ 60 61 JNIEXPORT jint JNICALL 62 Java_java_io_FileInputStream_read0(JNIEnv *env, jobject this) { 63 return readSingle(env, this, fis_fd); 64 } 65 66 JNIEXPORT jint JNICALL 67 Java_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this, 68 jbyteArray bytes, jint off, jint len) { 69 return readBytes(env, this, bytes, off, len, fis_fd); 70 } 71 72 JNIEXPORT jlong JNICALL 73 Java_java_io_FileInputStream_skip(JNIEnv *env, jobject this, jlong toSkip) { 74 jlong cur = jlong_zero; 75 jlong end = jlong_zero; 76 FD fd = GET_FD(this, fis_fd); 77 if (fd == -1) { 78 JNU_ThrowIOException (env, "Stream Closed"); 79 return 0; 80 } 81 if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) { 82 JNU_ThrowIOExceptionWithLastError(env, "Seek error"); 83 } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) { 84 JNU_ThrowIOExceptionWithLastError(env, "Seek error"); 85 } 86 return (end - cur); 87 } 88 89 JNIEXPORT jint JNICALL 90 Java_java_io_FileInputStream_available(JNIEnv *env, jobject this) { 91 jlong ret; 92 FD fd = GET_FD(this, fis_fd); 93 if (fd == -1) { 94 JNU_ThrowIOException (env, "Stream Closed"); 95 return 0; 96 } 97 if (IO_Available(fd, &ret)) { 98 if (ret > INT_MAX) { 99 ret = (jlong) INT_MAX; 100 } else if (ret < 0) { 101 ret = 0; 102 } 103 return jlong_to_jint(ret); 104 } 105 JNU_ThrowIOExceptionWithLastError(env, NULL); 106 return 0; 107 }