📄 utilities.java
字号:
/** * Copyright (C) 2003 Alexander Kout * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.yaofahua.jleapftp;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.text.DecimalFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Vector;import java.util.regex.Pattern;import org.apache.commons.net.ftp.FTPFile;/** * Utility class * *@author Alexander Kout *@created 30. M鋜z 2002 */public class Utilities { /** * verbose output */ public static boolean debug = false; /** * the FileWriter for the debug mode */ public static FileWriter logFile = null; private static final String[] monthsWS = {" Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ", " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec "}; private static final String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; /** * transforms a long into a String like 4KiB * *@param size Description of Parameter *@return Description of the Returned Value */ public static String humanReadable(double size) { DecimalFormat df = new DecimalFormat("###0.00"); double f1; double f2; double f3; if ((f1 = size / 1024.0) > 1.0) { if ((f2 = f1 / 1024.0) > 1.0) { if ((f3 = f2 / 1024) > 1.0) { return df.format(f3) + "GiB"; } return df.format(f2) + "MiB"; } return df.format(f1) + "KiB"; } return df.format(size) + "B"; } /** * Description of the Method * *@param size Description of Parameter *@return Description of the Returned Value */ public static String humanReadable(long size) { return humanReadable(size * 1.0); } public static String humanReadableTime(double time) { DecimalFormat df = new DecimalFormat("###0"); double min, hour, day, sec; sec = time *1.0; StringBuffer buf = new StringBuffer(50); // minutes if ((min = sec /60.0) >= 1.0) { // hours if ((hour = min /60.0) >= 1.0) { // days if ((day = hour/24.0) >= 1.0) { buf.append(df.format(day)). append("d "). append(df.format(hour-Math.floor(day)*24.0)). append("h "). append(df.format(min-Math.floor(hour)*60.0)). append("min "). append(df.format(sec-Math.floor(min)*60.0)). append("s"); return buf.toString(); } buf.append(df.format(hour)). append("h "). append(df.format(min-Math.floor(hour)*60.0)). append("min "). append(df.format(sec-Math.floor(min)*60.0)). append("s"); return buf.toString(); } buf.append(df.format(min)). append("min "). append(df.format(sec-Math.floor(min)*60.0)). append("s"); return buf.toString(); } buf.append(df.format(sec)).append("s"); return buf.toString(); } public static String humanReadableTime2(double time) { DecimalFormat df = new DecimalFormat("00"); double min, hour, day, sec; sec = time *1.0; StringBuffer buf = new StringBuffer(50); // minutes if ((min = sec /60.0) >= 1.0) { // hours if ((hour = min /60.0) >= 1.0) { // days if ((day = hour/24.0) >= 1.0) { buf.append(df.format(day)). append("d:"). append(df.format(hour-Math.floor(day)*24.0)). append(":"). append(df.format(min-Math.floor(hour)*60.0)). append(":"). append(df.format(sec-Math.floor(min)*60.0)); return buf.toString(); } buf.append(df.format(hour)). append(":"). append(df.format(min-Math.floor(hour)*60.0)). append(":"). append(df.format(sec-Math.floor(min)*60.0)); return buf.toString(); } buf.append(df.format(min)). append(":"). append(df.format(sec-Math.floor(min)*60.0)); return buf.toString(); } buf.append(new DecimalFormat("#0").format(sec)); return buf.toString(); } /** * Heapsort "with bottom-up linear search" algorithm * *@param list must be a list of Strings */ public static void sortList(String list[]) { // Heap creation int n = list.length; for (int i = n / 2; i > 0; i--) { reheap(list, i, n); } for (int m = n; m > 0; m--) { String t = list[0]; list[0] = list[m - 1]; list[m - 1] = t; reheap(list, 1, m - 1); } } /** * reheap method for heapsort * *@param array String array to be sorted *@param root position of root *@param end position of end */ private static void reheap(String[] array, int root, int end) { int[] stack = new int[new Double(Math.log(array.length) / Math.log(2)).intValue() + 10]; int s = 0; int pos = root; stack[s++] = pos; while (2 * pos <= end) { if (2 * pos + 1 > end) { stack[s++] = 2 * pos; break; } if (array[2 * pos - 1].compareToIgnoreCase(array[2 * pos]) > 0) { stack[s++] = 2 * pos; } else { stack[s++] = 2 * pos + 1; } pos = stack[s - 1]; } pos = root; for (int i = s - 1; i >= 0; i--) { if (array[stack[i] - 1].compareToIgnoreCase(array[root - 1]) > -1) { pos = stack[i]; s = i + 1; break; } } String temp = array[root - 1]; for (int i = 1; i < s; i++) { array[stack[i] / 2 - 1] = array[stack[i] - 1]; } array[pos - 1] = temp; } /** * Heapsort "with linear bottom-up search" algorithm * *@param list FtpFile array which is going to be sorted *@param sortBy Description of the Parameter */ public static void sortFiles(Vector list, String sortBy, boolean prio, Vector prioList) { // Heap creation int n = list.size(); for (int i = n / 2; i > 0; i--) { reheap(list, i, n, sortBy, prio, prioList); } for (int m = n; m > 0; m--) { FTPFile t = (FTPFile) list.elementAt(0); list.setElementAt(list.elementAt(m - 1), 0); list.setElementAt(t, m-1); reheap(list, 1, m - 1, sortBy, prio, prioList); } } public static void sortFiles2(Vector list) { for(int i=0;i<list.size();i++) { if( ((FTPFile)list.elementAt(i)).getName().equals("..") ) { FTPFile f = (FTPFile) list.elementAt(i); list.remove(i); list.add(0,f); } } } /** * reheap method for heapsort * *@param array array to be sorted *@param root position of root *@param end position of end *@param sortBy Description of the Parameter */ private static void reheap(Vector array, int root, int end, String sortBy, boolean prio, Vector prioList) { int[] stack = new int[new Double(Math.log(array.size()) / Math.log(2)).intValue() + 10]; int s = 0; int pos = root; stack[s++] = pos; while (2 * pos <= end) { if (2 * pos + 1 > end) { stack[s++] = 2 * pos; break; } if (compareFiles(array.elementAt(2 * pos - 1), array.elementAt(2 * pos), sortBy, prio, prioList) > 0) { stack[s++] = 2 * pos; } else { stack[s++] = 2 * pos + 1; } pos = stack[s - 1]; } pos = root; for (int i = s - 1; i >= 0; i--) { if (compareFiles(array.elementAt(stack[i] - 1), array.elementAt(root - 1), sortBy, prio, prioList) > -1) { pos = stack[i]; s = i + 1; break; } } Object temp = array.elementAt(root - 1); for (int i = 1; i < s; i++) { array.setElementAt(array.elementAt(stack[i] - 1), stack[i] / 2 - 1); } array.setElementAt(temp, pos - 1); } static public String toLocaleString(String str) { //String tt = new String(name.getBytes("ISO-8859-1"),"GBK") String encoding = System.getProperty("file.encoding"); try { return new String(str.getBytes("ISO-8859-1"),encoding); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // return str; } /** * compare method for sorting * *@param f1 first file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -