📄 utilities.java
字号:
case '.': if (lastChar == '.') { String parent = (new File(newPath.toString())).getParent(); if (parent != null) newPath = new StringBuffer(parent); } else if ((lastChar != '\0' && lastChar != '\\' && lastChar != '/') || (i < change.length() - 1 && change.charAt(i + 1) != '.')) buf.append('.'); lastChar = '.'; break; case '\\': case '/': if (lastChar == '\0') { newPath = new StringBuffer(getRoot(newPath.toString())); } else { char c = newPath.charAt(newPath.length() - 1); if (c != '\\' && c != '/') newPath.append(File.separator).append(buf.toString()); else newPath.append(buf.toString()); buf = new StringBuffer(); toAdd = false; } lastChar = '\\'; break; case '~': if (i < change.length() - 1) { if (change.charAt(i + 1) == '\\' || change.charAt(i + 1) == '/') newPath = new StringBuffer(getHomeDirectory()); else buf.append('~'); } else if (i == 0) newPath = new StringBuffer(getHomeDirectory()); else buf.append('~'); lastChar = '~'; break; default: lastChar = current; buf.append(current); toAdd = true; break; } } if (toAdd) { char c = newPath.charAt(newPath.length() - 1); if (c != '\\' && c != '/') newPath.append(File.separator).append(buf.toString()); else newPath.append(buf.toString()); } return newPath.toString(); } /** * It can be necessary to check if a path specified by the user is an absolute * path (i.e C:\Gfx\3d\Utils is absolute whereas ..\Jext is relative). * @param path The path to check * @return <code>true</code> if <code>path</code> begins with a root name */ public static boolean beginsWithRoot(String path) { if (path.length() == 0) return false; File file = new File(path); File[] roots = file.listRoots(); for (int i = 0; i < roots.length; i++) if (path.regionMatches(true, 0, roots[i].getPath(), 0, roots[i].getPath().length())) return true; return false; } /** * Returns user directory. */ public static String getUserDirectory() { return System.getProperty("user.dir"); } /** * Returns user's home directory. */ public static String getHomeDirectory() { return System.getProperty("user.home"); } /** * It can be necessary to determine which is the root of a path. * For example, the root of D:\Projects\Java is D:\. * @param path The path used to get a root * @return The root which contais the specified path */ public static String getRoot(String path) { File file = new File(path); File[] roots = file.listRoots(); for (int i = 0; i < roots.length; i++) if (path.startsWith(roots[i].getPath())) return roots[i].getPath(); return path; } /** * When the user has to specify file names, he can use wildcards (*, ?). This methods * handles the usage of these wildcards. * @param s Wilcards * @param sort Set to true will sort file names * @return An array of String which contains all files matching <code>s</code> * in current directory. */ public static String[] getWildCardMatches(String s, boolean sort) { return getWildCardMatches(null, s, sort); } /** * When the user has to specify file names, he can use wildcards (*, ?). This methods * handles the usage of these wildcards. * @param path The path were to search * @param s Wilcards * @param sort Set to true will sort file names * @return An array of String which contains all file names matching <code>s</code> * in <code>path</code> directory. */ public static String[] getWildCardMatches(String path, String s, boolean sort) { if (s == null) return null; String files[]; String filesThatMatch[]; String args = new String(s.trim()); ArrayList filesThatMatchVector = new ArrayList(); File fPath; if (path == null || path == "") fPath = new File(getUserDirectory()); else { fPath = new File(path); if (! fPath.isAbsolute()) fPath = new File(getUserDirectory(), path); } files = fPath.list(); if (files == null) return null; for (int i = 0; i < files.length; i++) { if (match(args, files[i])) { //File temp = new File(getUserDirectory(), files[i]); File temp = new File(path, files[i]); filesThatMatchVector.add(new String(temp.getName())); } } filesThatMatch = (String[]) filesThatMatchVector.toArray(new String[filesThatMatchVector.size()]); filesThatMatchVector = null; if (sort) Arrays.sort(filesThatMatch); return filesThatMatch; } /** * This method can determine if a String matches a pattern of wildcards * @param pattern The pattern used for comparison * @param string The String to be checked * @return true if <code>string</code> matches <code>pattern</code> */ public static boolean match(String pattern, String string) { for (int p = 0; ; p++) { for (int s = 0; ; p++, s++) { boolean sEnd = (s >= string.length()); boolean pEnd = (p >= pattern.length() || pattern.charAt(p) == '|'); if (sEnd && pEnd) return true; int end = pattern.indexOf('|', p); if (end == -1) end = pattern.length(); if (sEnd && ! pEnd && pattern.substring(p, end).equals("*")) return true; if (sEnd || pEnd) break; if (pattern.charAt(p) == '?') continue; if (pattern.charAt(p) == '*') { int i; p++; for (i = string.length(); i >= s; --i) if (match(pattern.substring(p), string.substring(i))) return true; break; } if (pattern.charAt(p) != string.charAt(s)) break; } p = pattern.indexOf('|', p); if (p == -1) return false; } } /** * Quick sort an array of Strings. * @param string Strings to be sorted * @deprecated Use the standard Java java.util.Array.sort instead. */ public static void sortStrings(String[] strings) { Arrays.sort(strings); } //The below code is left here, even if it is useless, because maybe there are //plugins which need it. sortStrings is needed by jftp, so it's there. //Remember that sortStrings is listed in the docs, so you can't remove it. /* * Quick sort an array of Strings. * @param a Strings to be sorted * @param lo0 Lower bound * @param hi0 Higher bound */ /*public static void sortStrings(String a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; String mid; if (hi0 > lo0) { mid = a[(lo0 + hi0) / 2]; while (lo <= hi) { while (lo < hi0 && a[lo].compareTo(mid) < 0) ++lo; while (hi > lo0 && a[hi].compareTo(mid) > 0) --hi; if (lo <= hi) { swap(a, lo, hi); ++lo; --hi; } } if (lo0 < hi) sortStrings(a, lo0, hi); if (lo < hi0) sortStrings(a, lo, hi0); } }*/ /* * Swaps two Strings. * @param a The array to be swapped * @param i First String index * @param j Second String index */ /*public static void swap(String a[], int i, int j) { String T; T = a[i]; a[i] = a[j]; a[j] = T; }*/ /** * Lists content of a directory. * @param names Names of the files * @param construct Set it to true if names does not contain full paths * @return An array of Files */ public static File[] listFiles(String[] names, boolean construct) { return listFiles(names, null, construct); } /** * Lists content of a directory. * @param names Names of the files * @param path Base path for files * @param construct Set it to true if names do not contain full paths * @return An array of Files */ public static File[] listFiles(String[] names, String path, boolean construct) { if (names == null) return null; File fPath; if (path == null || path == "") fPath = new File(getUserDirectory()); else { fPath = new File(path); if (! fPath.isAbsolute()) fPath = new File(getUserDirectory(), path); } File[] files = new File[names.length]; for (int i = 0; i < files.length; i++) { if (construct) files[i] = new File(fPath, names[i]); else files[i] = new File(names[i]); } return files; } /** * Turns a Un*x glob filter to regexp one * @param glob Globbed filter */ public static String globToRE(String glob) { char c = '\0'; boolean escape = false, enclosed = false; StringBuffer _buf = new StringBuffer(glob.length()); for (int i = 0; i < glob.length(); i++) { c = glob.charAt(i); if (escape) { _buf.append('\\'); _buf.append(c); escape = false; continue; } switch(c) { case '*': _buf.append('.').append('*'); break; case '?': _buf.append('.'); break; case '\\': escape = true; break; case '.': _buf.append('\\').append('.'); break; case '{': _buf.append('('); enclosed = true; break; case '}': _buf.append(')'); enclosed = false; break; case ',': if (enclosed) _buf.append('|'); else _buf.append(','); break; default: _buf.append(c); } } return _buf.toString(); } /*public static void downloadFile(URL source, String outPath, boolean threaded, HandlingRunnable notify) throws IOException { try { //String tempPath = outPath + "__FRAG__"; //final File outFile = new File(outPath), tempFile = new File(tempPath); //FIXME: think about the case below. The caller must avoid that we download the file 2 times. //Not us! /*if (tempFile.exists()) tempFile.renameTo(new File(tempPath + ".bak")); //Could fail and return false!*/ /*HandlingRunnable renamer = new HandlingRunnable() { public void run() { if (expectedLen != -1 && expectedLen != tempFile.length()) { //exceptional condition: if (excep == null && notify != null) notify.setException(new IOException("The download was not completed")); } if (outFile.exists()) { outFile.renameTo(new File(outPath + ".bak")); //Could fail and return false! outFile.delete(); } tempFile.renameTo(outFile); if (notify != null) { notify.setException(excep); //we won't handle it: this will be done by notify. notify.run(); } } //run method end }; copy(threaded, renamer); } catch (IOException ioe) { try { if (in != null) in.close();//if things go well, copy() closes the streams! } catch (IOException ioe2) {} throw ioe; } }*/ /** * @since Jext 3.2pre4 */ /*public static void downloadFile(URL source, String outPath, boolean threaded) throws IOException { downloadFile(source, outPath, threaded, null); }*/ /** * Downloads the file specified in the URL to the File with the <code>outPath</code> path using * copy() (so see copy() for infos about notify and threaded). * @since Jext 3.2pre4 */ public static void downloadFile(URL source, String outPath, boolean threaded, HandlingRunnable notify) /*throws IOException*/ { DownloaderThread downloader = new DownloaderThread(source, notify, outPath); /*try { if (threaded) downloader.start(); else downloader.run(); } catch (Throwable t) { throw (IOException) t; }*/ downloader.start(threaded); } /** * Convenience method for calling * @link{#copy(java.io.InputStream,java.io.OutputStream,boolean,org.jext.misc.HandlingRunnable)} passing a null * <code>notify</code>callback. * @since Jext 3.2pre4 */ /*public static void copy(final InputStream in, final OutputStream out, boolean threaded) throws IOException { copy(in, out, threaded, null); }*/ /** * This method copy the content of the InputStream in to the OutputStream out, in the calling thread or * in a new one. * If threaded is true, a new thread is created, and notify will be called at the end, and will have passed * the exception eventually thrown while doing the copy. * Otherwise, the copy is done synchronously; if an exception is thrown during the copy it is dispatched to the calling * method, otherwise the notify is called. * @since Jext 3.2pre4 */ public static void copy(InputStream in, OutputStream out, boolean threaded, HandlingRunnable notify) throws IOException { CopyThread copier = new CopyThread(in, out, notify); /*try { if (threaded) copier.start(); else copier.run(); } catch (Throwable t) { throw (IOException) t; }*/ copier.start(threaded); }}// End of Utilities.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -