Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/file_access_filesystem_jandroid.cpp
11351 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 == '\r' || elem == '\n' || elem == '\0') {
204
// Found the end of the line
205
const bool is_crlf = elem == '\r' && line_buffer_position + 1 < current_buffer_size && line_buffer[line_buffer_position + 1] == '\n';
206
const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + (is_crlf ? 2 : 1));
207
if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position)) {
208
return String();
209
}
210
return result;
211
}
212
}
213
}
214
215
if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position)) {
216
return String();
217
}
218
return result;
219
}
220
221
uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
222
if (_file_read) {
223
ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
224
if (p_length == 0) {
225
return 0;
226
}
227
228
JNIEnv *env = get_jni_env();
229
ERR_FAIL_NULL_V(env, 0);
230
231
jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
232
int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
233
env->DeleteLocalRef(j_buffer);
234
return length;
235
} else {
236
return 0;
237
}
238
}
239
240
bool FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
241
if (_file_write) {
242
ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
243
ERR_FAIL_COND_V(!p_src && p_length > 0, false);
244
if (p_length == 0) {
245
return true;
246
}
247
248
JNIEnv *env = get_jni_env();
249
ERR_FAIL_NULL_V(env, false);
250
251
jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
252
bool ok = env->CallBooleanMethod(file_access_handler, _file_write, id, j_buffer);
253
env->DeleteLocalRef(j_buffer);
254
return ok;
255
} else {
256
return false;
257
}
258
}
259
260
Error FileAccessFilesystemJAndroid::get_error() const {
261
if (eof_reached()) {
262
return ERR_FILE_EOF;
263
}
264
return OK;
265
}
266
267
Error FileAccessFilesystemJAndroid::resize(int64_t p_length) {
268
if (_file_resize) {
269
JNIEnv *env = get_jni_env();
270
ERR_FAIL_NULL_V(env, FAILED);
271
ERR_FAIL_COND_V_MSG(!is_open(), FAILED, "File must be opened before use.");
272
int res = env->CallIntMethod(file_access_handler, _file_resize, id, p_length);
273
return static_cast<Error>(res);
274
} else {
275
return ERR_UNAVAILABLE;
276
}
277
}
278
279
void FileAccessFilesystemJAndroid::flush() {
280
if (_file_flush) {
281
JNIEnv *env = get_jni_env();
282
ERR_FAIL_NULL(env);
283
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
284
env->CallVoidMethod(file_access_handler, _file_flush, id);
285
}
286
}
287
288
bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
289
if (_file_exists) {
290
JNIEnv *env = get_jni_env();
291
ERR_FAIL_NULL_V(env, false);
292
293
String path = fix_path(p_path).simplify_path();
294
jstring js = env->NewStringUTF(path.utf8().get_data());
295
bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
296
env->DeleteLocalRef(js);
297
return result;
298
} else {
299
return false;
300
}
301
}
302
303
uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
304
if (_file_last_modified) {
305
JNIEnv *env = get_jni_env();
306
ERR_FAIL_NULL_V(env, 0);
307
308
String path = fix_path(p_file).simplify_path();
309
jstring js = env->NewStringUTF(path.utf8().get_data());
310
uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
311
env->DeleteLocalRef(js);
312
return result;
313
} else {
314
return 0;
315
}
316
}
317
318
uint64_t FileAccessFilesystemJAndroid::_get_access_time(const String &p_file) {
319
if (_file_last_accessed) {
320
JNIEnv *env = get_jni_env();
321
ERR_FAIL_NULL_V(env, 0);
322
323
String path = fix_path(p_file).simplify_path();
324
jstring js = env->NewStringUTF(path.utf8().get_data());
325
uint64_t result = env->CallLongMethod(file_access_handler, _file_last_accessed, js);
326
env->DeleteLocalRef(js);
327
return result;
328
} else {
329
return 0;
330
}
331
}
332
333
int64_t FileAccessFilesystemJAndroid::_get_size(const String &p_file) {
334
if (_file_size) {
335
JNIEnv *env = get_jni_env();
336
ERR_FAIL_NULL_V(env, -1);
337
338
String path = fix_path(p_file).simplify_path();
339
jstring js = env->NewStringUTF(path.utf8().get_data());
340
int64_t result = env->CallLongMethod(file_access_handler, _file_size, js);
341
env->DeleteLocalRef(js);
342
return result;
343
} else {
344
return -1;
345
}
346
}
347
348
void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
349
JNIEnv *env = get_jni_env();
350
file_access_handler = env->NewGlobalRef(p_file_access_handler);
351
352
jclass c = env->GetObjectClass(file_access_handler);
353
cls = (jclass)env->NewGlobalRef(c);
354
355
_file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
356
_file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
357
_file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
358
_file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
359
_file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
360
_file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
361
_file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
362
_file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
363
_file_close = env->GetMethodID(cls, "fileClose", "(I)V");
364
_file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)Z");
365
_file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
366
_file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
367
_file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
368
_file_last_accessed = env->GetMethodID(cls, "fileLastAccessed", "(Ljava/lang/String;)J");
369
_file_resize = env->GetMethodID(cls, "fileResize", "(IJ)I");
370
_file_size = env->GetMethodID(cls, "fileSize", "(Ljava/lang/String;)J");
371
}
372
373
void FileAccessFilesystemJAndroid::terminate() {
374
JNIEnv *env = get_jni_env();
375
ERR_FAIL_NULL(env);
376
377
env->DeleteGlobalRef(cls);
378
env->DeleteGlobalRef(file_access_handler);
379
}
380
381
void FileAccessFilesystemJAndroid::close() {
382
if (is_open()) {
383
_close();
384
}
385
}
386
387
FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
388
id = 0;
389
}
390
391
FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
392
if (is_open()) {
393
_close();
394
}
395
}
396
397