• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1997, 2007, 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 <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include "jni.h"
32 #include "jni_util.h"
33 #include "jlong.h"
34 #include "io_util.h"
35 #include "io_util_md.h"
36 
37 #include "jvm.h"
38 
39 
40 #include <fcntl.h>
41 #include <limits.h>
42 
43 #include "io_util_md.h"
44 #include <nativehelper/JNIHelp.h>
45 
46 #define NATIVE_METHOD(className, functionName, signature) \
47 { #functionName, signature, (void*)(className ## _ ## functionName) }
48 
49 /*******************************************************************/
50 /*  BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
51 /*******************************************************************/
52 
53 jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
54 
55 /**************************************************************
56  * Input stream
57  */
58 
59 
FileInputStream_initIDs(JNIEnv * env)60 static void FileInputStream_initIDs(JNIEnv *env) {
61     jclass clazz = (*env)->FindClass(env, "java/io/FileInputStream");
62     fis_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
63 }
64 
65 // BEGIN Android-removed: Open files using IoBridge to share BlockGuard & StrictMode logic.
66 // http://b/112107427
67 /*
68 JNIEXPORT void JNICALL
69 FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {
70     fileOpen(env, this, path, fis_fd, O_RDONLY);
71 }
72 */
73 // END Android-removed: Open files using IoBridge to share BlockGuard & StrictMode logic.
74 
75 JNIEXPORT jlong JNICALL
FileInputStream_skip0(JNIEnv * env,jobject this,jlong toSkip)76 FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {
77     jlong cur = jlong_zero;
78     jlong end = jlong_zero;
79     FD fd = GET_FD(this, fis_fd);
80     if (fd == -1) {
81         JNU_ThrowIOException (env, "Stream Closed");
82         return 0;
83     }
84     if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
85       if (errno == ESPIPE) {
86         JNU_ThrowByName(env, "java/io/FileInputStream$UseManualSkipException", NULL);
87       } else {
88         JNU_ThrowIOExceptionWithLastError(env, "Seek error");
89       }
90     } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
91         JNU_ThrowIOExceptionWithLastError(env, "Seek error");
92     }
93     return (end - cur);
94 }
95 
available(int fd,jlong * bytes)96 static int available(int fd, jlong *bytes) {
97 // BEGIN Android-added: Fuchsia does not support FIONREAD. http://b/120566512
98 #if defined(__Fuchsia__)
99   *bytes = 0;
100   return 1;
101 #else
102 // END Android-added: Fuchsia does not support FIONREAD. http://b/120566512
103   int n;
104   // Unlike the original OpenJdk implementation, we use FIONREAD for all file
105   // types. For regular files, this is specified to return the difference
106   // between the current position and the file size. Note that this can be
107   // negative if we're positioned past the end of the file. We must return 0
108   // in that case.
109   if (ioctl(fd, FIONREAD, &n) != -1) {
110     if (n < 0) {
111       n = 0;
112     }
113     *bytes = n;
114     return 1;
115   }
116 
117   // FIONREAD is specified to return ENOTTY when fd refers to a file
118   // type for which this ioctl isn't implemented.
119   if (errno == ENOTTY) {
120     *bytes = 0;
121     return 1;
122   }
123 
124   // Raise an exception for all other error types.
125   return 0;
126 // Android-added: Fuchsia does not support the FIONREAD code. http://b/120566512
127 #endif
128 }
129 
130 JNIEXPORT jint JNICALL
FileInputStream_available0(JNIEnv * env,jobject this)131 FileInputStream_available0(JNIEnv *env, jobject this) {
132     jlong ret;
133     FD fd = GET_FD(this, fis_fd);
134     if (fd == -1) {
135         JNU_ThrowIOException (env, "Stream Closed");
136         return 0;
137     }
138     if (available(fd, &ret)) {
139         if (ret > INT_MAX) {
140             ret = (jlong) INT_MAX;
141         }
142         return jlong_to_jint(ret);
143     }
144     JNU_ThrowIOExceptionWithLastError(env, NULL);
145     return 0;
146 }
147 
148 static JNINativeMethod gMethods[] = {
149   NATIVE_METHOD(FileInputStream, skip0, "(J)J"),
150   NATIVE_METHOD(FileInputStream, available0, "()I"),
151 };
152 
register_java_io_FileInputStream(JNIEnv * env)153 void register_java_io_FileInputStream(JNIEnv* env) {
154     jniRegisterNativeMethods(env, "java/io/FileInputStream", gMethods, NELEM(gMethods));
155     FileInputStream_initIDs(env);
156 }
157