Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Helpers/FilesHelper.java
Show All 28 Lines | 28 | public class FilesHelper { | |||
---|---|---|---|---|---|
29 | 29 | | |||
30 | public static String getFileExt(String filename) { | 30 | public static String getFileExt(String filename) { | ||
31 | //return MimeTypeMap.getFileExtensionFromUrl(filename); | 31 | //return MimeTypeMap.getFileExtensionFromUrl(filename); | ||
32 | return filename.substring((filename.lastIndexOf(".") + 1)); | 32 | return filename.substring((filename.lastIndexOf(".") + 1)); | ||
33 | } | 33 | } | ||
34 | 34 | | |||
35 | public static String getFileNameWithoutExt(String filename) { | 35 | public static String getFileNameWithoutExt(String filename) { | ||
36 | int dot = filename.lastIndexOf("."); | 36 | int dot = filename.lastIndexOf("."); | ||
37 | return (dot < 0)? filename : filename.substring(0, dot); | 37 | return (dot < 0) ? filename : filename.substring(0, dot); | ||
38 | } | 38 | } | ||
39 | | ||||
39 | public static String getMimeTypeFromFile(String file) { | 40 | public static String getMimeTypeFromFile(String file) { | ||
40 | String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(getFileExt(file)); | 41 | String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(getFileExt(file)); | ||
41 | if (mime == null) mime = "*/*"; | 42 | if (mime == null) mime = "*/*"; | ||
42 | return mime; | 43 | return mime; | ||
43 | } | 44 | } | ||
44 | 45 | | |||
45 | public static String findNonExistingNameForNewFile(String path, String filename) { | 46 | public static String findNonExistingNameForNewFile(String path, String filename) { | ||
46 | int dot = filename.lastIndexOf("."); | 47 | int dot = filename.lastIndexOf("."); | ||
47 | String name = (dot < 0)? filename : filename.substring(0, dot); | 48 | String name = (dot < 0) ? filename : filename.substring(0, dot); | ||
48 | String ext = (dot < 0)? "" : filename.substring(filename.lastIndexOf(".")); | 49 | String ext = (dot < 0) ? "" : filename.substring(filename.lastIndexOf(".")); | ||
49 | 50 | | |||
50 | int num = 1; | 51 | int num = 1; | ||
51 | while (new File(path+"/"+filename).exists()) { | 52 | while (new File(path + "/" + filename).exists()) { | ||
52 | filename = name+" ("+num+")"+ext; | 53 | filename = name + " (" + num + ")" + ext; | ||
53 | num++; | 54 | num++; | ||
54 | } | 55 | } | ||
55 | 56 | | |||
56 | return filename; | 57 | return filename; | ||
57 | } | 58 | } | ||
58 | 59 | | |||
59 | //Following code from http://activemq.apache.org/maven/5.7.0/kahadb/apidocs/src-html/org/apache/kahadb/util/IOHelper.html | 60 | //Following code from http://activemq.apache.org/maven/5.7.0/kahadb/apidocs/src-html/org/apache/kahadb/util/IOHelper.html | ||
61 | | ||||
60 | /** | 62 | /** | ||
61 | * Converts any string into a string that is safe to use as a file name. | 63 | * Converts any string into a string that is safe to use as a file name. | ||
62 | * The result will only include ascii characters and numbers, and the "-","_", and "." characters. | 64 | * The result will only include ascii characters and numbers, and the "-","_", and "." characters. | ||
63 | */ | 65 | */ | ||
64 | public static String toFileSystemSafeName(String name, boolean dirSeparators, int maxFileLength) { | 66 | public static String toFileSystemSafeName(String name, boolean dirSeparators, int maxFileLength) { | ||
65 | int size = name.length(); | 67 | int size = name.length(); | ||
66 | StringBuilder rc = new StringBuilder(size * 2); | 68 | StringBuilder rc = new StringBuilder(size * 2); | ||
67 | for (int i = 0; i < size; i++) { | 69 | for (int i = 0; i < size; i++) { | ||
68 | char c = name.charAt(i); | 70 | char c = name.charAt(i); | ||
69 | boolean valid = c >= 'a' && c <= 'z'; | 71 | boolean valid = c >= 'a' && c <= 'z'; | ||
70 | valid = valid || (c >= 'A' && c <= 'Z'); | 72 | valid = valid || (c >= 'A' && c <= 'Z'); | ||
71 | valid = valid || (c >= '0' && c <= '9'); | 73 | valid = valid || (c >= '0' && c <= '9'); | ||
72 | valid = valid || (c == '_') || (c == '-') || (c == '.'); | 74 | valid = valid || (c == '_') || (c == '-') || (c == '.'); | ||
73 | valid = valid || (dirSeparators && ( (c == '/') || (c == '\\'))); | 75 | valid = valid || (dirSeparators && ((c == '/') || (c == '\\'))); | ||
74 | 76 | | |||
75 | if (valid) { | 77 | if (valid) { | ||
76 | rc.append(c); | 78 | rc.append(c); | ||
77 | } | 79 | } | ||
78 | } | 80 | } | ||
79 | String result = rc.toString(); | 81 | String result = rc.toString(); | ||
80 | if (result.length() > maxFileLength) { | 82 | if (result.length() > maxFileLength) { | ||
81 | result = result.substring(result.length()-maxFileLength,result.length()); | 83 | result = result.substring(result.length() - maxFileLength, result.length()); | ||
82 | } | 84 | } | ||
83 | return result; | 85 | return result; | ||
84 | } | 86 | } | ||
87 | | ||||
85 | public static String toFileSystemSafeName(String name, boolean dirSeparators) { | 88 | public static String toFileSystemSafeName(String name, boolean dirSeparators) { | ||
86 | return toFileSystemSafeName(name, dirSeparators, 255); | 89 | return toFileSystemSafeName(name, dirSeparators, 255); | ||
87 | } | 90 | } | ||
91 | | ||||
88 | public static String toFileSystemSafeName(String name) { | 92 | public static String toFileSystemSafeName(String name) { | ||
89 | return toFileSystemSafeName(name, true, 255); | 93 | return toFileSystemSafeName(name, true, 255); | ||
90 | } | 94 | } | ||
91 | 95 | | |||
92 | public static int GetOpenFileCount() { | 96 | public static int GetOpenFileCount() { | ||
93 | return new File("/proc/self/fd").listFiles().length; | 97 | return new File("/proc/self/fd").listFiles().length; | ||
94 | } | 98 | } | ||
95 | 99 | | |||
96 | public static void LogOpenFileCount() { | 100 | public static void LogOpenFileCount() { | ||
97 | Log.e("KDE/FileCount",""+GetOpenFileCount()); | 101 | Log.e("KDE/FileCount", "" + GetOpenFileCount()); | ||
98 | } | 102 | } | ||
99 | } | 103 | } |