-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.java
More file actions
273 lines (247 loc) · 10.6 KB
/
FileSystem.java
File metadata and controls
273 lines (247 loc) · 10.6 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import java.util.Enumeration;
import java.util.Vector;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
/**
* FileSystem - JSR-75 FileConnection wrapper
*
* CLDC 1.1 has NO java.lang.reflect - all reflection removed.
* Uses direct JSR-75 imports and casts.
*
* Supported devices: Nokia S40/S60, Sony Ericsson JP7+,
* Motorola, Samsung SGH, BlackBerry 4.2+, WTK 2.5.2 emulator.
*
* Build requirement: jsr75.jar in bootclasspath (see build.xml)
*/
public class FileSystem {
private static FileSystem instance;
private FileSystem() {}
public static FileSystem getInstance() {
if (instance == null) instance = new FileSystem();
return instance;
}
/** Check JSR-75 availability via Class.forName (CLDC 1.1 supports this) */
public boolean isAvailable() {
try {
Class.forName("javax.microedition.io.file.FileConnection");
return true;
} catch (Exception e) {
return false;
}
}
/** List all storage roots, returns full file:/// URLs */
public String[] listRoots() {
try {
Enumeration e = FileSystemRegistry.listRoots();
Vector v = new Vector();
while (e.hasMoreElements()) {
String root = (String) e.nextElement();
if (!root.startsWith("file:")) root = "file:///" + root;
v.addElement(root);
}
String[] result = new String[v.size()];
for (int i = 0; i < v.size(); i++) result[i] = (String) v.elementAt(i);
return result;
} catch (Exception ex) {
return getFallbackRoots();
}
}
/** List directory contents at given URL (must end with /). Dirs first. */
public FileEntry[] listDir(String url) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ);
Enumeration e = fc.list();
Vector v = new Vector();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String fullUrl = url + name;
boolean isDir = name.endsWith("/");
String displayName = isDir ? name.substring(0, name.length()-1) : name;
long size = 0;
if (!isDir) {
FileConnection fc2 = null;
try {
fc2 = (FileConnection) Connector.open(fullUrl, Connector.READ);
size = fc2.fileSize();
} catch (Exception e2) { size = 0; }
finally { if (fc2!=null) try{fc2.close();}catch(Exception e3){} }
}
v.addElement(new FileEntry(displayName, fullUrl, isDir, size, 0));
}
FileEntry[] result = new FileEntry[v.size()];
for (int i = 0; i < v.size(); i++) result[i] = (FileEntry) v.elementAt(i);
sortEntries(result);
return result;
} catch (Exception ex) {
return new FileEntry[0];
} finally {
if (fc != null) try { fc.close(); } catch (Exception e) {}
}
}
/** Read a text file into String (up to maxBytes bytes) */
public String readTextFile(String url, int maxBytes) throws Exception {
FileConnection fc = (FileConnection) Connector.open(url, Connector.READ);
try {
InputStream is = fc.openInputStream();
byte[] buf = new byte[maxBytes];
int read = 0, b;
while (read < maxBytes && (b = is.read()) != -1) buf[read++] = (byte)b;
is.close();
return new String(buf, 0, read, "UTF-8");
} finally { fc.close(); }
}
/** Rename a file. newName = filename only, no path. */
public boolean renameFile(String url, String newName) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
fc.rename(newName);
return true;
} catch (Exception ex) { return false; }
finally { if (fc!=null) try{fc.close();}catch(Exception e){} }
}
/** Delete a file or empty directory */
public boolean deleteFile(String url) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
fc.delete();
return true;
} catch (Exception ex) { return false; }
finally { if (fc!=null) try{fc.close();}catch(Exception e){} }
}
/** Get detailed file metadata */
public FileInfo getFileInfo(String url) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ);
long size=0, modified=0;
boolean readable=false, writable=false, hidden=false;
try { size = fc.fileSize(); } catch (Exception e) {}
try { modified = fc.lastModified(); } catch (Exception e) {}
try { readable = fc.canRead(); } catch (Exception e) {}
try { writable = fc.canWrite(); } catch (Exception e) {}
try { hidden = fc.isHidden(); } catch (Exception e) {}
return new FileInfo(size, modified, readable, writable, hidden, guessMime(url));
} catch (Exception ex) {
return new FileInfo(0, 0, false, false, false, "unknown");
} finally { if (fc!=null) try{fc.close();}catch(Exception e){} }
}
/** Create a new directory */
public boolean mkdir(String url) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
fc.mkdir();
return true;
} catch (Exception ex) { return false; }
finally { if (fc!=null) try{fc.close();}catch(Exception e){} }
}
/** Write text to a file (create or overwrite) */
public boolean writeTextFile(String url, String content) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
if (!fc.exists()) fc.create();
else fc.truncate(0);
OutputStream os = fc.openOutputStream();
byte[] bytes = content.getBytes("UTF-8");
os.write(bytes);
os.flush();
os.close();
return true;
} catch (Exception ex) { return false; }
finally { if (fc!=null) try{fc.close();}catch(Exception e){} }
}
// ===== STATIC HELPERS =====
private String[] getFallbackRoots() {
return new String[]{
"file:///root1/", "file:///root2/",
"file:///Card/", "file:///SDCard/",
"file:///Memory card/", "file:///Phone memory/",
"file:///C:/", "file:///E:/", "file:///a/", "file:///b/"
};
}
public static String guessMime(String url) {
if (url == null) return "file";
String lo = url.toLowerCase();
if (endsAny(lo, new String[]{".txt",".log",".csv",".ini",".cfg",".nfo",".md"})) return "text";
if (endsAny(lo, new String[]{".jpg",".jpeg",".png",".gif",".bmp",".webp"})) return "image";
if (endsAny(lo, new String[]{".mp3",".aac",".wav",".amr",".m4a",".ogg"})) return "audio";
if (endsAny(lo, new String[]{".mp4",".3gp",".avi",".mov",".mkv",".wmv"})) return "video";
if (endsAny(lo, new String[]{".jar",".jad"})) return "midlet";
if (endsAny(lo, new String[]{".java",".xml",".json",".html",".htm",".css",".js",".py",".c",".cpp",".h"})) return "code";
if (lo.endsWith(".pdf")) return "pdf";
if (endsAny(lo, new String[]{".zip",".rar",".gz",".tar",".7z"})) return "archive";
if (lo.endsWith(".vcf")) return "contact";
if (lo.endsWith(".ics")) return "calendar";
return "file";
}
private static boolean endsAny(String s, String[] exts) {
for (int i = 0; i < exts.length; i++) if (s.endsWith(exts[i])) return true;
return false;
}
public static String formatSize(long bytes) {
if (bytes < 0) return "?";
if (bytes < 1024) return bytes + "B";
if (bytes < 1024L * 1024) return (bytes/1024) + "KB";
return (bytes/(1024L*1024)) + "MB";
}
private void sortEntries(FileEntry[] arr) {
// Insertion sort: dirs first, then alpha
for (int i = 1; i < arr.length; i++) {
FileEntry key = arr[i];
int j = i - 1;
while (j >= 0 && shouldSwap(arr[j], key)) { arr[j+1] = arr[j]; j--; }
arr[j+1] = key;
}
}
private boolean shouldSwap(FileEntry a, FileEntry b) {
if (!a.isDir && b.isDir) return true;
if (a.isDir && !b.isDir) return false;
return a.name.toLowerCase().compareTo(b.name.toLowerCase()) > 0;
}
// ===== DATA CLASSES =====
public static class FileEntry {
public final String name;
public final String url;
public final boolean isDir;
public final long size;
public final long modified;
public FileEntry(String name, String url, boolean isDir, long size, long modified) {
this.name=name; this.url=url; this.isDir=isDir;
this.size=size; this.modified=modified;
}
public String getIcon() {
if (isDir) return "[D]";
String m = FileSystem.guessMime(url);
if (m.equals("text")) return "[T]";
if (m.equals("image")) return "[I]";
if (m.equals("audio")) return "[A]";
if (m.equals("video")) return "[V]";
if (m.equals("midlet")) return "[J]";
if (m.equals("code")) return "[C]";
if (m.equals("archive")) return "[Z]";
if (m.equals("contact")) return "[P]";
return "[F]";
}
}
public static class FileInfo {
public final long size;
public final long modified;
public final boolean readable;
public final boolean writable;
public final boolean hidden;
public final String mimeType;
public FileInfo(long size, long modified, boolean readable,
boolean writable, boolean hidden, String mimeType) {
this.size=size; this.modified=modified;
this.readable=readable; this.writable=writable;
this.hidden=hidden; this.mimeType=mimeType;
}
}
}