FileInputStream.c revision 02df4b384e42969ad6f9e211989698e4f9fcde6e
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#include "java_io_FileInputStream.h"
40
41#include <fcntl.h>
42#include <limits.h>
43
44#include "io_util_md.h"
45#include "JNIHelp.h"
46
47#define NATIVE_METHOD(className, functionName, signature) \
48{ #functionName, signature, (void*)(className ## _ ## functionName) }
49
50/*******************************************************************/
51/*  BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
52/*******************************************************************/
53
54jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
55
56/**************************************************************
57 * static methods to store field ID's in initializers
58 */
59
60JNIEXPORT void JNICALL
61FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {
62    fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
63}
64
65/**************************************************************
66 * Input stream
67 */
68
69JNIEXPORT void JNICALL
70FileInputStream_open(JNIEnv *env, jobject this, jstring path) {
71    fileOpen(env, this, path, fis_fd, O_RDONLY);
72}
73
74JNIEXPORT jint JNICALL
75FileInputStream_read0(JNIEnv *env, jobject this) {
76    return readSingle(env, this, fis_fd);
77}
78
79JNIEXPORT jint JNICALL
80FileInputStream_readBytes(JNIEnv *env, jobject this,
81        jbyteArray bytes, jint off, jint len) {
82    return readBytes(env, this, bytes, off, len, fis_fd);
83}
84
85JNIEXPORT jlong JNICALL
86FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {
87    jlong cur = jlong_zero;
88    jlong end = jlong_zero;
89    FD fd = GET_FD(this, fis_fd);
90    if (fd == -1) {
91        JNU_ThrowIOException (env, "Stream Closed");
92        return 0;
93    }
94    if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
95      if (errno == ESPIPE) {
96        JNU_ThrowByName(env, "java/io/FileInputStream$UseManualSkipException", NULL);
97      } else {
98        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
99      }
100    } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
101        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
102    }
103    return (end - cur);
104}
105
106// Android added:
107// TODO: Where does this function come from ? Needs a detailed code review.
108// Why wasn't IO_Available used.
109static int available(int fd, jlong *bytes) {
110    jlong cur, end;
111    int mode;
112    struct stat64 buf64;
113
114    if (fstat64(fd, &buf64) >= 0) {
115        mode = buf64.st_mode;
116        if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
117            /*
118             * XXX: is the following call interruptible? If so, this might
119             * need to go through the INTERRUPT_IO() wrapper as for other
120             * blocking, interruptible calls in this file.
121             */
122             int n;
123             if (ioctl(fd, FIONREAD, &n) >= 0) {
124                 *bytes = n;
125                 return 1;
126             }
127        }
128    }
129    if ((cur = lseek64(fd, 0L, SEEK_CUR)) == -1) {
130        return 0;
131    } else if ((end = lseek64(fd, 0L, SEEK_END)) == -1) {
132        return 0;
133    } else if (lseek64(fd, cur, SEEK_SET) == -1) {
134        return 0;
135    }
136    *bytes = end - cur;
137    return 1;
138}
139
140JNIEXPORT jint JNICALL
141FileInputStream_available(JNIEnv *env, jobject this) {
142    jlong ret;
143    FD fd = GET_FD(this, fis_fd);
144    if (fd == -1) {
145        JNU_ThrowIOException (env, "Stream Closed");
146        return 0;
147    }
148    if (available(fd, &ret)) {
149        if (ret > INT_MAX) {
150            ret = (jlong) INT_MAX;
151        }
152        return jlong_to_jint(ret);
153    }
154    JNU_ThrowIOExceptionWithLastError(env, NULL);
155    return 0;
156}
157
158JNIEXPORT void JNICALL
159FileInputStream_close0(JNIEnv *env, jobject this) {
160    fileClose(env, this, fis_fd);
161}
162
163static JNINativeMethod gMethods[] = {
164  NATIVE_METHOD(FileInputStream, initIDs, "()V"),
165  NATIVE_METHOD(FileInputStream, open, "(Ljava/lang/String;)V"),
166  NATIVE_METHOD(FileInputStream, skip0, "(J)J"),
167  NATIVE_METHOD(FileInputStream, available, "()I"),
168  //NATIVE_METHOD(FileInputStream, read0, "()I"),
169  //NATIVE_METHOD(FileInputStream, readBytes, "([BII)I"),
170  //NATIVE_METHOD(FileInputStream, close0, "()V"),
171};
172
173void register_java_io_FileInputStream(JNIEnv* env) {
174  jniRegisterNativeMethods(env, "java/io/FileInputStream", gMethods, NELEM(gMethods));
175}
176