1 40 package org.dspace.core; 41 42 import java.io.BufferedInputStream ; 43 import java.io.BufferedOutputStream ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.math.BigInteger ; 48 import java.rmi.dgc.VMID ; 49 import java.security.MessageDigest ; 50 import java.security.NoSuchAlgorithmException ; 51 import java.text.ParseException ; 52 import java.util.Random ; 53 import java.util.regex.Matcher ; 54 import java.util.regex.Pattern ; 55 56 62 public class Utils 63 { 64 private static final Pattern DURATION_PATTERN = Pattern 65 .compile("(\\d+)([smhdwy])"); 66 67 private static final long MS_IN_SECOND = 1000L; 68 69 private static final long MS_IN_MINUTE = 60000L; 70 71 private static final long MS_IN_HOUR = 3600000L; 72 73 private static final long MS_IN_DAY = 86400000L; 74 75 private static final long MS_IN_WEEK = 604800000L; 76 77 private static final long MS_IN_YEAR = 31536000000L; 78 79 private static int counter = 0; 80 81 private static Random random = new Random (); 82 83 private static VMID vmid = new VMID (); 84 85 86 private Utils() 87 { 88 } 89 90 97 public static String getMD5(String data) 98 { 99 return getMD5(data.getBytes()); 100 } 101 102 109 public static String getMD5(byte[] data) 110 { 111 return toHex(getMD5Bytes(data)); 112 } 113 114 121 public static byte[] getMD5Bytes(byte[] data) 122 { 123 try 124 { 125 MessageDigest digest = MessageDigest.getInstance("MD5"); 126 127 return digest.digest(data); 128 } 129 catch (NoSuchAlgorithmException nsae) 130 { 131 } 132 133 return null; 135 } 136 137 144 public static String toHex(byte[] data) 145 { 146 if ((data == null) || (data.length == 0)) 147 { 148 return null; 149 } 150 151 StringBuffer result = new StringBuffer (); 152 153 for (int i = 0; i < data.length; i++) 155 { 156 int low = (int) (data[i] & 0x0F); 157 int high = (int) (data[i] & 0xF0); 158 159 result.append(Integer.toHexString(high).substring(0, 1)); 160 result.append(Integer.toHexString(low)); 161 } 162 163 return result.toString(); 164 } 165 166 172 public static String generateKey() 173 { 174 return new BigInteger (generateBytesKey()).abs().toString(); 175 } 176 177 183 public static String generateHexKey() 184 { 185 return toHex(generateBytesKey()); 186 } 187 188 193 public static synchronized byte[] generateBytesKey() 194 { 195 byte[] junk = new byte[16]; 196 197 random.nextBytes(junk); 198 199 String input = new StringBuffer ().append(vmid).append( 200 new java.util.Date ()).append(junk).append(counter++).toString(); 201 202 return getMD5Bytes(input.getBytes()); 203 } 204 205 207 218 public static void copy(final InputStream input, final OutputStream output) 219 throws IOException 220 { 221 final int BUFFER_SIZE = 1024 * 4; 222 final byte[] buffer = new byte[BUFFER_SIZE]; 223 224 while (true) 225 { 226 final int count = input.read(buffer, 0, BUFFER_SIZE); 227 228 if (-1 == count) 229 { 230 break; 231 } 232 233 output.write(buffer, 0, count); 235 } 236 237 } 240 241 254 public static void bufferedCopy(final InputStream source, 255 final OutputStream destination) throws IOException 256 { 257 final BufferedInputStream input = new BufferedInputStream (source); 258 final BufferedOutputStream output = new BufferedOutputStream ( 259 destination); 260 copy(input, output); 261 output.flush(); 262 } 263 264 277 public static String addEntities(String value) 278 { 279 if (value==null || value.length() == 0) 280 return value; 281 282 value = value.replaceAll("&", "&"); 283 value = value.replaceAll("\"", """); 284 285 value = value.replaceAll("<", "<"); 289 value = value.replaceAll(">", ">"); 290 291 return value; 292 } 293 294 306 public static long parseDuration(String duration) throws ParseException 307 { 308 Matcher m = DURATION_PATTERN.matcher(duration.trim()); 309 if (!m.matches()) 310 { 311 throw new ParseException ("'" + duration 312 + "' is not a valid duration definition", 0); 313 } 314 315 String units = m.group(2); 316 long multiplier = MS_IN_SECOND; 317 318 if ("s".equals(units)) 319 { 320 multiplier = MS_IN_SECOND; 321 } 322 else if ("m".equals(units)) 323 { 324 multiplier = MS_IN_MINUTE; 325 } 326 else if ("h".equals(units)) 327 { 328 multiplier = MS_IN_HOUR; 329 } 330 else if ("d".equals(units)) 331 { 332 multiplier = MS_IN_DAY; 333 } 334 else if ("w".equals(units)) 335 { 336 multiplier = MS_IN_WEEK; 337 } 338 else if ("y".equals(units)) 339 { 340 multiplier = MS_IN_YEAR; 341 } 342 else 343 { 344 throw new ParseException (units 345 + " is not a valid time unit (must be 'y', " 346 + "'w', 'd', 'h', 'm' or 's')", duration.indexOf(units)); 347 } 348 349 long qint = Long.parseLong(m.group(1)); 350 351 return qint * multiplier; 352 } 353 } 354 | Popular Tags |