-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBacktraceFileSystemProvider.java
More file actions
165 lines (140 loc) · 4.96 KB
/
Copy pathBacktraceFileSystemProvider.java
File metadata and controls
165 lines (140 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package backtrace.library;
import android.os.FileUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
@ReactModule(name = BacktraceFileSystemProvider.NAME)
public class BacktraceFileSystemProvider extends ReactContextBaseJavaModule {
public static final String NAME = "BacktraceFileSystemProvider";
private static final transient String LOG_TAG = BacktraceFileSystemProvider.class.getSimpleName();
public BacktraceFileSystemProvider(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
@NonNull
public String getName() {
return NAME;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public String readFileSync(String path) {
File file = new File(path);
if (!file.exists()) {
return null;
}
try (Scanner scanner = new Scanner(file)) {
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()) {
sb.append(scanner.nextLine());
}
return sb.toString();
} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
return null;
}
}
@ReactMethod
public void readFile(String path, Promise promise) {
File file = new File(path);
if (!file.exists()) {
promise.reject(new FileNotFoundException(path));
return;
}
try (Scanner scanner = new Scanner(file)) {
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()) {
sb.append(scanner.nextLine());
}
scanner.close();
promise.resolve(sb.toString());
} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
promise.reject(e);
}
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean writeFileSync(String path, String content) {
try (FileOutputStream out = new FileOutputStream(path, false)) {
out.write(content.getBytes(StandardCharsets.UTF_8));
return true;
} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
return false;
}
}
@ReactMethod
public void writeFile(String path, String content, Promise promise) {
try (FileOutputStream out = new FileOutputStream(path, false)) {
out.write(content.getBytes(StandardCharsets.UTF_8));
promise.resolve(true);
} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
promise.reject(e);
}
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean unlinkSync(String path) {
return delete(path);
}
@ReactMethod
public void unlink(String filePath, Promise promise) {
promise.resolve(delete(filePath));
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean existsSync(String path) {
return exists(path);
}
@ReactMethod
public void exists(String path, Promise promise) {
promise.resolve(exists(path));
}
private boolean delete(String path) {
try {
File file = new File(path);
return !file.exists() || file.delete();
} catch (RuntimeException e) {
Log.d(LOG_TAG, "Cannot delete " + path + ": " + e.getClass().getName());
return false;
}
}
private boolean exists(String path) {
try {
return new File(path).exists();
} catch (RuntimeException e) {
Log.d(LOG_TAG, "Cannot check " + path + ": " + e.getClass().getName());
return false;
}
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean copySync(String sourcePath, String destinationPath) {
return copy(sourcePath, destinationPath);
}
@ReactMethod
public void copy(String sourcePath, String destinationPath, Promise promise) {
promise.resolve(copy(sourcePath, destinationPath));
}
private boolean copy(String sourcePath, String destinationPath) {
try (FileInputStream inputStream = new FileInputStream(sourcePath);
FileOutputStream outputStream = new FileOutputStream(destinationPath, false)) {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
return true;
} catch (Exception e) {
return false;
}
}
}