archiveutils.java
来自「一个搜索引擎,希望对大家有用」· Java 代码 · 共 668 行 · 第 1/2 页
JAVA
668 行
} if (timestamp.length() < 14) { timestamp = timestamp + ArchiveUtils.padTo("", 14 - timestamp.length(), '0'); } } return ArchiveUtils.parse14DigitDate(timestamp); } /** * @param i Integer to add prefix of zeros too. If passed * 2005, will return the String <code>0000002005</code>. String * width is the width of Integer.MAX_VALUE as a string (10 * digits). * @return Padded String version of <code>i</code>. */ public static String zeroPadInteger(int i) { return ArchiveUtils.padTo(Integer.toString(i), MAX_INT_CHAR_WIDTH, '0'); } /** * Convert an <code>int</code> to a <code>String</code>, and pad it to * <code>pad</code> spaces. * @param i the int * @param pad the width to pad to. * @return String w/ padding. */ public static String padTo(final int i, final int pad) { String n = Integer.toString(i); return padTo(n, pad); } /** * Pad the given <code>String</code> to <code>pad</code> characters wide * by pre-pending spaces. <code>s</code> should not be <code>null</code>. * If <code>s</code> is already wider than <code>pad</code> no change is * done. * * @param s the String to pad * @param pad the width to pad to. * @return String w/ padding. */ public static String padTo(final String s, final int pad) { return padTo(s, pad, DEFAULT_PAD_CHAR); } /** * Pad the given <code>String</code> to <code>pad</code> characters wide * by pre-pending <code>padChar</code>. * * <code>s</code> should not be <code>null</code>. If <code>s</code> is * already wider than <code>pad</code> no change is done. * * @param s the String to pad * @param pad the width to pad to. * @param padChar The pad character to use. * @return String w/ padding. */ public static String padTo(final String s, final int pad, final char padChar) { String result = s; int l = s.length(); if (l < pad) { StringBuffer sb = new StringBuffer(pad); while(l < pad) { sb.append(padChar); l++; } sb.append(s); result = sb.toString(); } return result; } /** check that two byte arrays are equal. They may be <code>null</code>. * * @param lhs a byte array * @param rhs another byte array. * @return <code>true</code> if they are both equal (or both * <code>null</code>) */ public static boolean byteArrayEquals(final byte[] lhs, final byte[] rhs) { if (lhs == null && rhs != null || lhs != null && rhs == null) { return false; } if (lhs==rhs) { return true; } if (lhs.length != rhs.length) { return false; } for(int i = 0; i<lhs.length; i++) { if (lhs[i]!=rhs[i]) { return false; } } return true; } /** * Converts a double to a string. * @param val The double to convert * @param precision How many characters to include after '.' * @return the double as a string. */ public static String doubleToString(double val, int precision){ String tmp = Double.toString(val); if(tmp.indexOf(".")!=-1){ // Need to handle the part after '.' if(precision<=0){ tmp = tmp.substring(0,tmp.indexOf('.')); } else { if(tmp.length()>tmp.indexOf('.')+precision+1){ // Need to trim tmp = tmp.substring(0,tmp.indexOf('.')+precision+1); } } } return tmp; } /** * Takes an amount of bytes and formats it for display. This involves * converting it to Kb, Mb or Gb if the amount is enough to qualify for * the next level. * <p> * Displays as bytes (B): 0-1023 * Displays as kilobytes (KB): 1024 - 2097151 (~2Mb) * Displays as megabytes (MB): 2097152 - 4294967295 (~4Gb) * Displays as gigabytes (GB): 4294967296 - infinity * <p> * Negative numbers will be returned as '0 B'. * <p> * All values will be approximated down (i.e. 2047 bytes are 1 KB) * * @param amount the amount of bytes * @return A string containing the amount, properly formated. */ public static String formatBytesForDisplay(long amount){ long kbStartAt = 1024; long mbStartAt = 1024*1024*2; long gbStartAt = ((long)1024*1024)*1024*4; if(amount < 0){ return "0 B"; } else if(amount < kbStartAt){ // Display as bytes. return amount + " B"; } else if(amount < mbStartAt) { // Display as kilobytes return Long.toString((long)(((double)amount/1024)))+" KB"; } else if(amount < gbStartAt) { // Display as megabytes return Long.toString((long)(((double)amount/(1024*1024))))+" MB"; } else { // Display as gigabytes return Long.toString((long)(((double)amount/(1024*1024*1024))))+" GB"; } } /** * Convert milliseconds value to a human-readable duration * @param time * @return Human readable string version of passed <code>time</code> */ public static String formatMillisecondsToConventional(long time) { return formatMillisecondsToConventional(time,true); } /** * Convert milliseconds value to a human-readable duration * @param time * @param toMs whether to print to the ms * @return Human readable string version of passed <code>time</code> */ public static String formatMillisecondsToConventional(long time, boolean toMs) { StringBuffer sb = new StringBuffer(); if(time<0) { sb.append("-"); } long absTime = Math.abs(time); if(!toMs && absTime < 1000) { return "0s"; } if(absTime > DAY_IN_MS) { // days sb.append(absTime / DAY_IN_MS + "d"); absTime = absTime % DAY_IN_MS; } if (absTime > HOUR_IN_MS) { //got hours. sb.append(absTime / HOUR_IN_MS + "h"); absTime = absTime % HOUR_IN_MS; } if (absTime > 60000) { sb.append(absTime / 60000 + "m"); absTime = absTime % 60000; } if (absTime > 1000) { sb.append(absTime / 1000 + "s"); absTime = absTime % 1000; } if(toMs) { sb.append(absTime + "ms"); } return sb.toString(); } /** * Generate a long UID based on the given class and version number. * Using this instead of the default will assume serialization * compatibility across class changes unless version number is * intentionally bumped. * * @param class1 * @param version * @return UID based off class and version number. */ public static long classnameBasedUID(Class class1, int version) { String callingClassname = class1.getName(); return (long)callingClassname.hashCode() << 32 + version; } /** * Copy the raw bytes of a long into a byte array, starting at * the specified offset. * * @param l * @param array * @param offset */ public static void longIntoByteArray(long l, byte[] array, int offset) { int i, shift; for(i = 0, shift = 56; i < 8; i++, shift -= 8) array[offset+i] = (byte)(0xFF & (l >> shift)); } public static long byteArrayIntoLong(byte [] bytearray) { return byteArrayIntoLong(bytearray, 0); } /** * Byte array into long. * @param bytearray Array to convert to a long. * @param offset Offset into array at which we start decoding the long. * @return Long made of the bytes of <code>array</code> beginning at * offset <code>offset</code>. * @see #longIntoByteArray(long, byte[], int) */ public static long byteArrayIntoLong(byte [] bytearray, int offset) { long result = 0; for (int i = offset; i < 8 /*Bytes in long*/; i++) { result = (result << 8 /*Bits in byte*/) | (0xff & (byte)(bytearray[i] & 0xff)); } return result; } /** * Given a string that may be a plain host or host/path (without * URI scheme), add an implied http:// if necessary. * * @param u string to evaluate * @return string with http:// added if no scheme already present */ public static String addImpliedHttpIfNecessary(String u) { if(u.indexOf(':') == -1 || u.indexOf('.') < u.indexOf(':')) { // No scheme present; prepend "http://" u = "http://" + u; } return u; } /** * Verify that the array begins with the prefix. * * @param array * @param prefix * @return true if array is identical to prefix for the first prefix.length * positions */ public static boolean startsWith(byte[] array, byte[] prefix) { if(prefix.length>array.length) { return false; } for(int i = 0; i < prefix.length; i++) { if(array[i]!=prefix[i]) { return false; } } return true; } /** * Utility method to get a String singleLineReport from Reporter * @param rep Reporter to get singleLineReport from * @return String of report */ public static String singleLineReport(Reporter rep) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { rep.singleLineReportTo(pw); } catch (IOException e) { // not really possible e.printStackTrace(); } pw.flush(); return sw.toString(); } /** * Compose the requested report into a String. DANGEROUS IF REPORT * CAN BE LARGE. * * @param rep Reported * @param name String name of report to compose * @return String of report */ public static String writeReportToString(Reporter rep, String name) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); rep.reportTo(name,pw); pw.flush(); return sw.toString(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?