This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.awilmore.ioutils; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.channels.ReadableByteChannel; | |
import java.nio.channels.SeekableByteChannel; | |
import java.nio.channels.WritableByteChannel; | |
public class IOUtil { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 将数字变为“1st/2nd/3rd/4th...”这种表示”顺序“的形式 | |
*/ | |
public static String toOrdinal(int i) { | |
String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", | |
"th" }; | |
switch (i % 100) { | |
case 11: | |
case 12: | |
case 13: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 用于包装某耗时逻辑,并以指定的时等待其执行完毕,若未执行完毕则抛出TimeoutException; 若在指定的时间内执行完毕则正常返回 | |
* @param logic 将要执行的耗时逻辑, 注意:该逻辑中的耗时操作必须正确响应interrupt操作, 若不明白这句话含义的请咨询 @pf_miles, 切勿在不明白原理的情形下冒然使用 | |
* @param toWait 期望等待的时间,以ms计 | |
* @throws TimeoutException 超时抛出 | |
*/ | |
public static <R> R timedWaiting(Callable<R> logic, final long toWait) throws TimeoutException, | |
Exception { | |
// 原理:主、子线程谁先醒来就interrupt对方 | |
final Thread mainThread = Thread.currentThread(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static ClassLoader getParentClsLoader() { | |
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); | |
if (ctxLoader != null) { | |
try { | |
ctxLoader.loadClass(CurrentClass.class.getName()); | |
return ctxLoader; | |
} catch (ClassNotFoundException e) { | |
} | |
} | |
return CurrentClass.class.getClassLoader(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* NULL类型的值,通常用在cache系统中避免'0值攻击'; 全局唯一值; | |
* 定义为enum省去了hashCode & equals实现的烦恼,且避免了反序列化时潜在的创建多个实例的可能性以及无参public构造函数的要求 | |
* | |
* @author pf-miles Apr 19, 2016 5:50:03 PM | |
*/ | |
public enum Null { | |
NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String... args) { | |
TreeLog log = new TreeLog("test"); | |
log.log("第一层"); | |
TreeLog log1 = log.addChild("test1"); | |
log1.log("第二层"); | |
TreeLog log2 = log1.addChild("test2"); | |
log2.log("第三层"); | |
TreeLog log3 = log.addChild("test3"); | |
log3.log("又一个第二层"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.ThreadFactory; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* 当线程池满且等待任务queue也满时,submit(task)会block的executor | |
* @author pf-miles Feb 25, 2016 1:51:39 PM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.Random; | |
/** | |
* 生成指定长度的随机串,字符范围仅限于ascii可打印字符: https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters | |
* @author pf-miles Nov 16, 2015 3:18:13 PM | |
*/ | |
public abstract class RandomPrintableStringGenerator { | |
private static final Random r = new Random(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* created by pf_miles, 2015-10-13 | |
*/ | |
/** | |
* 根据参数,返回一个执行表单异步上传文件的函数, 该函数可以被绑定到页面控件的事件上使用 | |
* @param {String} fileFormId 要提交的formId | |
* @param {String} formAction 要提交的formAction | |
* @param {Function} successHandler 上传成功处理函数, 接受一个json参数为server端返回信息 | |
* @param {Function} failHandler 上传失败处理函数, 接受一个json参数为server端返回信息 |