Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/file_access_filesystem_jandroid.cpp
10277 views
1
/**************************************************************************/
2
/* file_access_filesystem_jandroid.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "file_access_filesystem_jandroid.h"
32
33
#include "thread_jandroid.h"
34
35
#include "core/os/os.h"
36
#include "core/templates/local_vector.h"
37
38
#include <unistd.h>
39
40
jobject FileAccessFilesystemJAndroid::file_access_handler = nullptr;
41
jclass FileAccessFilesystemJAndroid::cls;
42
43
jmethodID FileAccessFilesystemJAndroid::_file_open = nullptr;
44
jmethodID FileAccessFilesystemJAndroid::_file_get_size = nullptr;
45
jmethodID FileAccessFilesystemJAndroid::_file_seek = nullptr;
46
jmethodID FileAccessFilesystemJAndroid::_file_seek_end = nullptr;
47
jmethodID FileAccessFilesystemJAndroid::_file_read = nullptr;
48
jmethodID FileAccessFilesystemJAndroid::_file_tell = nullptr;
49
jmethodID FileAccessFilesystemJAndroid::_file_eof = nullptr;
50
jmethodID FileAccessFilesystemJAndroid::_file_set_eof = nullptr;
51
jmethodID FileAccessFilesystemJAndroid::_file_close = nullptr;
52
jmethodID FileAccessFilesystemJAndroid::_file_write = nullptr;
53
jmethodID FileAccessFilesystemJAndroid::_file_flush = nullptr;
54
jmethodID FileAccessFilesystemJAndroid::_file_exists = nullptr;
55
jmethodID FileAccessFilesystemJAndroid::_file_last_modified = nullptr;
56
jmethodID FileAccessFilesystemJAndroid::_file_last_accessed = nullptr;
57
jmethodID FileAccessFilesystemJAndroid::_file_resize = nullptr;
58
jmethodID FileAccessFilesystemJAndroid::_file_size = nullptr;
59
60
String FileAccessFilesystemJAndroid::get_path() const {
61
return path_src;
62
}
63
64
String FileAccessFilesystemJAndroid::get_path_absolute() const {
65
return absolute_path;
66
}
67
68
Error FileAccessFilesystemJAndroid::open_internal(const String &p_path, int p_mode_flags) {
69
if (is_open()) {
70
_close();
71
}
72
73
if (_file_open) {
74
JNIEnv *env = get_jni_env();
75
ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
76
77
String path = fix_path(p_path).simplify_path();
78
jstring js = env->NewStringUTF(path.utf8().get_data());
79
int res = env->CallIntMethod(file_access_handler, _file_open, js, p_mode_flags);
80
env->DeleteLocalRef(js);
81
82
if (res < 0) {
83
// Errors are passed back as their negative value to differentiate from the positive file id.
84
return static_cast<Error>(-res);
85
}
86
87
id = res;
88
path_src = p_path;
89
absolute_path = path;
90
return OK;
91
} else {
92
return ERR_UNCONFIGURED;
93
}
94
}
95
96
void FileAccessFilesystemJAndroid::_close() {
97
if (!is_open()) {
98
return;
99
}
100
101
if (_file_close) {
102
JNIEnv *env = get_jni_env();
103
ERR_FAIL_NULL(env);
104
env->CallVoidMethod(file_access_handler, _file_close, id);
105
}
106
id = 0;
107
}
108
109
bool FileAccessFilesystemJAndroid::is_open() const {
110
return id != 0;
111
}
112
113
void FileAccessFilesystemJAndroid::seek(uint64_t p_position) {
114
if (_file_seek) {
115
JNIEnv *env = get_jni_env();
116
ERR_FAIL_NULL(env);
117
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
118
env->CallVoidMethod(file_access_handler, _file_seek, id, p_position);
119
}
120
}
121
122
void FileAccessFilesystemJAndroid::seek_end(int64_t p_position) {
123
if (_file_seek_end) {
124
JNIEnv *env = get_jni_env();
125
ERR_FAIL_NULL(env);
126
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
127
env->CallVoidMethod(file_access_handler, _file_seek_end, id, p_position);
128
}
129
}
130
131
uint64_t FileAccessFilesystemJAndroid::get_position() const {
132
if (_file_tell) {
133
JNIEnv *env = get_jni_env();
134
ERR_FAIL_NULL_V(env, 0);
135
ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
136
return env->CallLongMethod(file_access_handler, _file_tell, id);
137
} else {
138
return 0;
139
}
140
}
141
142
uint64_t FileAccessFilesystemJAndroid::get_length() const {
143
if (_file_get_size) {
144
JNIEnv *env = get_jni_env();
145
ERR_FAIL_NULL_V(env, 0);
146
ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
147
return env->CallLongMethod(file_access_handler, _file_get_size, id);
148
} else {
149
return 0;
150
}
151
}
152
153
bool FileAccessFilesystemJAndroid::eof_reached() const {
154
if (_file_eof) {
155
JNIEnv *env = get_jni_env();
156
ERR_FAIL_NULL_V(env, false);
157
ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
158
return env->CallBooleanMethod(file_access_handler, _file_eof, id);
159
} else {
160
return false;
161
}
162
}
163
164
void FileAccessFilesystemJAndroid::_set_eof(bool eof) {
165
if (_file_set_eof) {
166
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
167
168
JNIEnv *env = get_jni_env();
169
ERR_FAIL_NULL(env);
170
env->CallVoidMethod(file_access_handler, _file_set_eof, id, eof);
171
}
172
}
173
174
String FileAccessFilesystemJAndroid::get_line() const {
175
ERR_FAIL_COND_V_MSG(!is_open(), String(), "File must be opened before use.");
176
177
const size_t buffer_size_limit = 2048;
178
const uint64_t file_size = get_length();
179
const uint64_t start_position = get_position();
180
181
String result;
182
LocalVector<uint8_t> line_buffer;
183
size_t current_buffer_size = 0;
184
uint64_t line_buffer_position = 0;
185
186
while (true) {
187
size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
188
if (line_buffer_size <= 0) {
189
const_cast<FileAccessFilesystemJAndroid *>(this)->_set_eof(true);
190
break;
191
}
192
193
current_buffer_size += line_buffer_size;
194
line_buffer.resize(current_buffer_size);
195
196
uint64_t bytes_read = get_buffer(&line_buffer[line_buffer_position], current_buffer_size - line_buffer_position);
197
if (bytes_read <= 0) {
198
break;
199
}
200
201
for (; bytes_read > 0; line_buffer_position++, bytes_read--) {
202
uint8_t elem = line_buffer[line_buffer_position];
203
if (elem == '\n' || elem == '\0') {
204
// Found the end of the line
205
const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + 1);
206
if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
207
return String();
208
}
209
return result;
210
}
211
}
212
}
213
214
if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
215
return String();
216
}
217
return result;
218
}
219
220
uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
221
if (_file_read) {
222
ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
223
if (p_length == 0) {
224
return 0;
225
}
226
227
JNIEnv *env = get_jni_env();
228
ERR_FAIL_NULL_V(env, 0);
229
230
jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
231
int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
232
env->DeleteLocalRef(j_buffer);
233
return length;
234
} else {
235
return 0;
236
}
237
}
238
239
bool FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
240
if (_file_write) {
241
ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
242
ERR_FAIL_COND_V(!p_src && p_length > 0, false);
243
if (p_length == 0) {
244
return true;
245
}
246
247
JNIEnv *env = get_jni_env();
248
ERR_FAIL_NULL_V(env, false);
249
250
jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
251
bool ok = env->CallBooleanMethod(file_access_handler, _file_write, id, j_buffer);
252
env->DeleteLocalRef(j_buffer);
253
return ok;
254
} else {
255
return false;
256
}
257
}
258
259
Error FileAccessFilesystemJAndroid::get_error() const {
260
if (eof_reached()) {
261
return ERR_FILE_EOF;
262
}
263
return OK;
264
}
265
266
Error FileAccessFilesystemJAndroid::resize(int64_t p_length) {
267
if (_file_resize) {
268
JNIEnv *env = get_jni_env();
269
ERR_FAIL_NULL_V(env, FAILED);
270
ERR_FAIL_COND_V_MSG(!is_open(), FAILED, "File must be opened before use.");
271
int res = env->CallIntMethod(file_access_handler, _file_resize, id, p_length);
272
return static_cast<Error>(res);
273
} else {
274
return ERR_UNAVAILABLE;
275
}
276
}
277
278
void FileAccessFilesystemJAndroid::flush() {
279
if (_file_flush) {
280
JNIEnv *env = get_jni_env();
281
ERR_FAIL_NULL(env);
282
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
283
env->CallVoidMethod(file_access_handler, _file_flush, id);
284
}
285
}
286
287
bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
288
if (_file_exists) {
289
JNIEnv *env = get_jni_env();
290
ERR_FAIL_NULL_V(env, false);
291
292
String path = fix_path(p_path).simplify_path();
293
jstring js = env->NewStringUTF(path.utf8().get_data());
294
bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
295
env->DeleteLocalRef(js);
296
return result;
297
} else {
298
return false;
299
}
300
}
301
302
uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
303
if (_file_last_modified) {
304
JNIEnv *env = get_jni_env();
305
ERR_FAIL_NULL_V(env, 0);
306
307
String path = fix_path(p_file).simplify_path();
308
jstring js = env->NewStringUTF(path.utf8().get_data());
309
uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
310
env->DeleteLocalRef(js);
311
return result;
312
} else {
313
return 0;
314
}
315
}
316
317
uint64_t FileAccessFilesystemJAndroid::_get_access_time(const String &p_file) {
318
if (_file_last_accessed) {
319
JNIEnv *env = get_jni_env();
320
ERR_FAIL_NULL_V(env, 0);
321
322
String path = fix_path(p_file).simplify_path();
323
jstring js = env->NewStringUTF(path.utf8().get_data());
324
uint64_t result = env->CallLongMethod(file_access_handler, _file_last_accessed, js);
325
env->DeleteLocalRef(js);
326
return result;
327
} else {
328
return 0;
329
}
330
}
331
332
int64_t FileAccessFilesystemJAndroid::_get_size(const String &p_file) {
333
if (_file_size) {
334
JNIEnv *env = get_jni_env();
335
ERR_FAIL_NULL_V(env, -1);
336
337
String path = fix_path(p_file).simplify_path();
338
jstring js = env->NewStringUTF(path.utf8().get_data());
339
int64_t result = env->CallLongMethod(file_access_handler, _file_size, js);
340
env->DeleteLocalRef(js);
341
return result;
342
} else {
343
return -1;
344
}
345
}
346
347
void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
348
JNIEnv *env = get_jni_env();
349
file_access_handler = env->NewGlobalRef(p_file_access_handler);
350
351
jclass c = env->GetObjectClass(file_access_handler);
352
cls = (jclass)env->NewGlobalRef(c);
353
354
_file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
355
_file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
356
_file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
357
_file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
358
_file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
359
_file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
360
_file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
361
_file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
362
_file_close = env->GetMethodID(cls, "fileClose", "(I)V");
363
_file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)Z");
364
_file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
365
_file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
366
_file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
367
_file_last_accessed = env->GetMethodID(cls, "fileLastAccessed", "(Ljava/lang/String;)J");
368
_file_resize = env->GetMethodID(cls, "fileResize", "(IJ)I");
369
_file_size = env->GetMethodID(cls, "fileSize", "(Ljava/lang/String;)J");
370
}
371
372
void FileAccessFilesystemJAndroid::terminate() {
373
JNIEnv *env = get_jni_env();
374
ERR_FAIL_NULL(env);
375
376
env->DeleteGlobalRef(cls);
377
env->DeleteGlobalRef(file_access_handler);
378
}
379
380
void FileAccessFilesystemJAndroid::close() {
381
if (is_open()) {
382
_close();
383
}
384
}
385
386
FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
387
id = 0;
388
}
389
390
FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
391
if (is_open()) {
392
_close();
393
}
394
}
395
396