1 package nl.justobjects.pushlet.util; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.PrintWriter ; 6 import java.util.Random ; 7 8 14 public class Rand { 15 private static char CHARS[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z'}; 16 private static char NON_VOWELS[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'z'}; 17 private static char VOWELS[] = {'a', 'e', 'i', 'o', 'u', 'y'}; 18 private static Random random = new Random (); 19 20 public static char randomChar() { 21 return CHARS[randomInt(0, CHARS.length - 1)]; 22 } 23 24 public static char randomVowel() { 25 return VOWELS[randomInt(0, VOWELS.length - 1)]; 26 } 27 28 public static char randomNonVowel() { 29 return NON_VOWELS[randomInt(0, NON_VOWELS.length - 1)]; 30 } 31 32 public static File randomTempDir() throws Exception { 33 File file = new File (System.getProperty("java.io.tmpdir") + File.separator + "oasetest" + File.separator + randomString(12)); 34 file.mkdirs(); 35 file.deleteOnExit(); 36 return file; 37 } 38 39 public static File randomTempFile() throws Exception { 40 File file = new File (System.getProperty("java.io.tmpdir") + File.separator + "oase-" + randomString(6)); 41 file.createNewFile(); 42 file.deleteOnExit(); 43 return file; 44 } 45 46 public static File randomBinaryFile(int aSize) throws Exception { 47 File file = randomTempFile(); 48 FileOutputStream fos = new FileOutputStream (file); 49 fos.write(randomBytes(aSize)); 50 fos.close(); 51 return file; 52 } 53 54 public static File randomTextFile(int aSize) throws Exception { 55 File file = randomTempFile(); 56 PrintWriter pw = new PrintWriter (new FileOutputStream (file)); 57 pw.write(randomString(aSize)); 58 pw.close(); 59 return file; 60 } 61 62 public static byte[] randomBytes(int aSize) { 63 return randomBlob(aSize); 64 } 65 66 public static byte[] randomBlob(int aSize) { 67 byte[] retval = new byte[aSize]; 68 for (int i = 0; i < retval.length; i++) { 69 retval[i] = randomByte(); 70 } 71 return retval; 72 } 73 74 public static byte randomByte() { 75 return (byte) random.nextInt(); 76 } 77 78 public static double randomDouble() { 79 return random.nextLong(); 80 } 81 82 83 public static int randomInt() { 84 return random.nextInt(); 85 } 86 87 public static int randomInt(int min, int max) { 88 return (int) ((Math.random() * (double) (max + 1 - min)) + min); 89 } 90 91 public static long randomLong() { 92 return random.nextLong(); 93 } 94 95 public static long randomLong(long min, long max) { 96 return (long) ((Math.random() * (double) (max + 1L - min)) + min); 97 } 98 99 public static String randomName(int aLength) { 100 StringBuffer sb = new StringBuffer (aLength); 101 for (int i = 0; i < aLength; i++) { 102 sb.append(i % 2 == 0 ? randomNonVowel() : randomVowel()); 103 } 104 return sb.toString(); 105 } 106 107 public static void randomSleep(long min, long max) { 108 try { 109 Thread.sleep(randomLong(min, max)); 110 } catch (InterruptedException ie) { 111 112 } 113 } 114 115 public static String randomString(int aLength) { 116 StringBuffer sb = new StringBuffer (aLength); 117 for (int i = 0; i < aLength; i++) { 118 sb.append(randomChar()); 119 } 120 return sb.toString(); 121 } 122 123 public static String randomString() { 124 return "" + randomLong(); 125 } 126 127 128 } 129 130 | Popular Tags |