Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/native/libjava/FileInputStream.c
41149 views
1
/*
2
* Copyright (c) 1997, 2021, 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
46
/**************************************************************
47
* static methods to store field ID's in initializers
48
*/
49
50
JNIEXPORT void JNICALL
51
Java_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {
52
fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
53
}
54
55
/**************************************************************
56
* Input stream
57
*/
58
59
JNIEXPORT void JNICALL
60
Java_java_io_FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {
61
fileOpen(env, this, path, fis_fd, O_RDONLY);
62
}
63
64
JNIEXPORT jint JNICALL
65
Java_java_io_FileInputStream_read0(JNIEnv *env, jobject this) {
66
return readSingle(env, this, fis_fd);
67
}
68
69
JNIEXPORT jint JNICALL
70
Java_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this,
71
jbyteArray bytes, jint off, jint len) {
72
return readBytes(env, this, bytes, off, len, fis_fd);
73
}
74
75
JNIEXPORT jlong JNICALL
76
Java_java_io_FileInputStream_length0(JNIEnv *env, jobject this) {
77
78
FD fd;
79
jlong length = jlong_zero;
80
81
fd = getFD(env, this, fis_fd);
82
if (fd == -1) {
83
JNU_ThrowIOException(env, "Stream Closed");
84
return -1;
85
}
86
if ((length = IO_GetLength(fd)) == -1) {
87
JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
88
}
89
return length;
90
}
91
92
JNIEXPORT jlong JNICALL
93
Java_java_io_FileInputStream_position0(JNIEnv *env, jobject this) {
94
FD fd;
95
jlong ret;
96
97
fd = getFD(env, this, fis_fd);
98
if (fd == -1) {
99
JNU_ThrowIOException(env, "Stream Closed");
100
return -1;
101
}
102
if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
103
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
104
}
105
return ret;
106
}
107
108
JNIEXPORT jlong JNICALL
109
Java_java_io_FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {
110
jlong cur = jlong_zero;
111
jlong end = jlong_zero;
112
FD fd = getFD(env, this, fis_fd);
113
if (fd == -1) {
114
JNU_ThrowIOException (env, "Stream Closed");
115
return 0;
116
}
117
if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
118
JNU_ThrowIOExceptionWithLastError(env, "Seek error");
119
} else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
120
JNU_ThrowIOExceptionWithLastError(env, "Seek error");
121
}
122
return (end - cur);
123
}
124
125
JNIEXPORT jint JNICALL
126
Java_java_io_FileInputStream_available0(JNIEnv *env, jobject this) {
127
jlong ret;
128
FD fd = getFD(env, this, fis_fd);
129
if (fd == -1) {
130
JNU_ThrowIOException (env, "Stream Closed");
131
return 0;
132
}
133
if (IO_Available(fd, &ret)) {
134
if (ret > INT_MAX) {
135
ret = (jlong) INT_MAX;
136
} else if (ret < 0) {
137
ret = 0;
138
}
139
return jlong_to_jint(ret);
140
}
141
JNU_ThrowIOExceptionWithLastError(env, NULL);
142
return 0;
143
}
144
145