1 39 40 package com.sun.japex; 41 42 import java.io.*; 43 import java.util.*; 44 import java.lang.reflect.*; 45 import java.text.DecimalFormat ; 46 47 public class Util { 48 49 static final int KB = 1024; 50 static final String spaces = " "; 51 52 static Method currentTime; 53 static boolean unitIsMillis; 54 55 static { 56 try { 58 currentTime = System .class.getMethod("nanoTime", null); 59 unitIsMillis = false; 60 } 61 catch (NoSuchMethodException e) { 62 try { 63 currentTime = System .class.getMethod("currentTimeMillis", null); 64 unitIsMillis = true; 65 } 66 catch (NoSuchMethodException ee) { 67 e.printStackTrace(); 68 System.exit(1); 69 } 70 } 71 } 72 73 74 public Util() { 75 } 76 77 static String getSpaces(int length) { 78 return spaces.substring(0, length); 79 } 80 81 static public byte[] streamToByteArray(InputStream is) { 82 ByteArrayOutputStream bos = new ByteArrayOutputStream(8 * KB); 83 int c; 84 try { 85 while ((c = is.read()) != -1) { 86 bos.write(c); 87 } 88 } catch (IOException e) { 89 throw new RuntimeException (e); 90 } 91 return bos.toByteArray(); 92 } 93 94 public static long parseDuration(String duration) { 95 try { 96 int length = duration.length(); 97 switch (length) { 98 case 1: 99 case 2: 100 return Integer.parseInt(duration.substring(0, length)) 102 * 1000; 103 case 5: 104 if (duration.charAt(2) == ':') { 105 return Integer.parseInt(duration.substring(0, 2)) 107 * 60 * 1000 + 108 Integer.parseInt(duration.substring(3, 5)) 109 * 1000; 110 } 111 break; 112 case 8: 113 if (duration.charAt(2) == ':' && duration.charAt(5) == ':') { 115 return Integer.parseInt(duration.substring(0, 2)) 116 * 60 * 60 * 1000 + 117 Integer.parseInt(duration.substring(3, 5)) 118 * 60 * 1000 + 119 Integer.parseInt(duration.substring(6, 8)) 120 * 1000; 121 } 122 break; 123 } 124 } 125 catch (NumberFormatException e) { 126 } 128 throw new RuntimeException ("Duration '" + duration 129 + "' does not conform to pattern '((HH:)?MM:)?SS'"); 130 } 131 132 public static long currentTimeNanos() { 133 try { 134 long t = ((Number ) currentTime.invoke(null, null)).longValue(); 135 return unitIsMillis ? millisToNanos(t) : t; 136 } 137 catch (Exception e) { 138 e.printStackTrace(); 139 System.exit(1); 140 } 141 return 0; 142 } 143 144 public static long currentTimeMillis() { 145 try { 146 long t = ((Number ) currentTime.invoke(null, null)).longValue(); 147 return unitIsMillis ? t : (long) nanosToMillis(t); 148 } 149 catch (Exception e) { 150 e.printStackTrace(); 151 System.exit(1); 152 } 153 return 0; 154 } 155 156 public static long millisToNanos(long millis) { 157 return millis * 1000000L; 158 } 159 160 public static double nanosToMillis(long nanos) { 161 return nanos / 1000000.0; 162 } 163 164 public static double arithmeticMean(double[] sample) { 165 double mean = 0.0; 166 for (int i = 0; i < sample.length; i++) { 167 mean += sample[i]; 168 } 169 return (mean / sample.length); 170 } 171 172 public static double standardDev(double[] sample) { 173 double mean = arithmeticMean(sample); 174 175 double variance = 0.0; 177 for (int i = 0; i < sample.length; i++) { 178 variance += (sample[i] - mean) * (sample[i] - mean); 179 } 180 variance /= sample.length; 181 182 return Math.sqrt(variance); 184 } 185 186 static DecimalFormat _decimalFormat = new DecimalFormat ("0.00"); 187 188 public static String formatDouble(double value) { 189 return _decimalFormat.format(value); 190 } 191 } 192 | Popular Tags |