1 27 package org.archive.util; 28 29 import java.io.IOException ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.text.ParseException ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Calendar ; 35 import java.util.Date ; 36 import java.util.GregorianCalendar ; 37 import java.util.TimeZone ; 38 39 45 public class ArchiveUtils { 46 47 50 public static final SimpleDateFormat TIMESTAMP12; 51 54 public static final SimpleDateFormat TIMESTAMP14; 55 58 public static final SimpleDateFormat TIMESTAMP17; 59 63 public static final SimpleDateFormat TIMESTAMP17ISO8601Z; 64 68 public static final SimpleDateFormat TIMESTAMP14ISO8601Z; 69 72 private static final char DEFAULT_PAD_CHAR = ' '; 73 74 75 private static final int HOUR_IN_MS = 60 * 60 * 1000; 76 77 private static final int DAY_IN_MS = 24 * HOUR_IN_MS; 78 79 static { 81 TimeZone TZ = TimeZone.getTimeZone("GMT"); 82 TIMESTAMP12 = new SimpleDateFormat ("yyyyMMddHHmm"); 83 TIMESTAMP12.setTimeZone(TZ); 84 TIMESTAMP14 = new SimpleDateFormat ("yyyyMMddHHmmss"); 85 TIMESTAMP14.setTimeZone(TZ); 86 TIMESTAMP17 = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); 87 TIMESTAMP17.setTimeZone(TZ); 88 TIMESTAMP17ISO8601Z = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 89 TIMESTAMP14ISO8601Z = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 90 } 91 92 public static int MAX_INT_CHAR_WIDTH = 93 Integer.toString(Integer.MAX_VALUE).length(); 94 95 101 public static String get17DigitDate(){ 102 return TIMESTAMP17.format(new Date ()); 103 } 104 105 111 public static String get14DigitDate(){ 112 return TIMESTAMP14.format(new Date ()); 113 } 114 115 121 public static String get12DigitDate(){ 122 return TIMESTAMP12.format(new Date ()); 123 } 124 125 133 public static String getLog17Date(){ 134 return TIMESTAMP17ISO8601Z.format(new Date ()); 135 } 136 137 146 public static String getLog17Date(long date){ 147 return TIMESTAMP17ISO8601Z.format(new Date (date)); 148 } 149 150 158 public static String getLog14Date(){ 159 return TIMESTAMP14ISO8601Z.format(new Date ()); 160 } 161 162 171 public static String getLog14Date(long date){ 172 return TIMESTAMP14ISO8601Z.format(new Date (date)); 173 } 174 175 183 public static String get17DigitDate(long date){ 184 return TIMESTAMP17.format(new Date (date)); 185 } 186 187 public static String get17DigitDate(Date date){ 188 return TIMESTAMP17.format(date); 189 } 190 191 199 public static String get14DigitDate(long date){ 200 return TIMESTAMP14.format(new Date (date)); 201 } 202 203 public static String get14DigitDate(Date d) { 204 return TIMESTAMP14.format(d); 205 } 206 207 215 public static String get12DigitDate(long date){ 216 return TIMESTAMP12.format(new Date (date)); 217 } 218 219 public static String get12DigitDate(Date d) { 220 return TIMESTAMP12.format(d); 221 } 222 223 233 public static Date getDate(String d) throws ParseException { 234 Date date = null; 235 if (d == null) { 236 throw new IllegalArgumentException ("Passed date is null"); 237 } 238 switch (d.length()) { 239 case 14: 240 date = ArchiveUtils.parse14DigitDate(d); 241 break; 242 243 case 17: 244 date = ArchiveUtils.parse17DigitDate(d); 245 break; 246 247 case 12: 248 date = ArchiveUtils.parse12DigitDate(d); 249 break; 250 251 case 0: 252 case 1: 253 case 2: 254 case 3: 255 throw new ParseException ("Date string must at least contain a" + 256 "year: " + d, d.length()); 257 258 default: 259 if (!(d.startsWith("19") || d.startsWith("20"))) { 260 throw new ParseException ("Unrecognized century: " + d, 0); 261 } 262 if (d.length() < 8 && (d.length() % 2) != 0) { 263 throw new ParseException ("Incomplete month/date: " + d, 264 d.length()); 265 } 266 StringBuilder sb = new StringBuilder (d); 267 if (sb.length() < 8) { 268 for (int i = sb.length(); sb.length() < 8; i += 2) { 269 sb.append("01"); 270 } 271 } 272 if (sb.length() < 12) { 273 for (int i = sb.length(); sb.length() < 12; i++) { 274 sb.append("0"); 275 } 276 } 277 date = ArchiveUtils.parse12DigitDate(sb.toString()); 278 } 279 280 return date; 281 } 282 283 293 public static Date parse17DigitDate(String date) throws ParseException { 294 return TIMESTAMP17.parse(date); 295 } 296 297 307 public static Date parse14DigitDate(String date) throws ParseException { 308 return TIMESTAMP14.parse(date); 309 } 310 311 321 public static Date parse12DigitDate(String date) throws ParseException { 322 return TIMESTAMP12.parse(date); 323 } 324 325 339 public static Calendar timestamp17ToCalendar(String timestamp17String) { 340 GregorianCalendar calendar = new GregorianCalendar (); 341 int year = Integer.parseInt(timestamp17String.substring(0, 4)); 342 int dayOfMonth = Integer.parseInt(timestamp17String.substring(6, 8)); 343 int month = Integer.parseInt(timestamp17String.substring(4, 6)) - 1; 345 int hourOfDay = Integer.parseInt(timestamp17String.substring(8, 10)); 346 int minute = Integer.parseInt(timestamp17String.substring(10, 12)); 347 int second = Integer.parseInt(timestamp17String.substring(12, 14)); 348 int milliseconds = Integer 349 .parseInt(timestamp17String.substring(14, 17)); 350 calendar.set(Calendar.YEAR, year); 351 calendar.set(Calendar.MONTH, month); 352 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 353 calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 354 calendar.set(Calendar.MINUTE, minute); 355 calendar.set(Calendar.SECOND, second); 356 calendar.set(Calendar.MILLISECOND, milliseconds); 357 return calendar; 358 } 359 360 368 public static String secondsSinceEpoch(String timestamp) 369 throws ParseException { 370 return zeroPadInteger((int) 371 (getSecondsSinceEpoch(timestamp).getTime()/1000)); 372 } 373 374 381 public static Date getSecondsSinceEpoch(String timestamp) 382 throws ParseException { 383 if (timestamp.length() < 14) { 384 if (timestamp.length() < 10 && (timestamp.length() % 2) == 1) { 385 throw new IllegalArgumentException ("Must have year, " + 386 "month, date, hour or second granularity: " + timestamp); 387 } 388 if (timestamp.length() == 4) { 389 timestamp = timestamp + "01010000"; 391 } 392 if (timestamp.length() == 6) { 393 timestamp = timestamp + "010000"; 395 } 396 if (timestamp.length() < 14) { 397 timestamp = timestamp + 398 ArchiveUtils.padTo("", 14 - timestamp.length(), '0'); 399 } 400 } 401 return ArchiveUtils.parse14DigitDate(timestamp); 402 } 403 404 411 public static String zeroPadInteger(int i) { 412 return ArchiveUtils.padTo(Integer.toString(i), 413 MAX_INT_CHAR_WIDTH, '0'); 414 } 415 416 423 public static String padTo(final int i, final int pad) { 424 String n = Integer.toString(i); 425 return padTo(n, pad); 426 } 427 428 438 public static String padTo(final String s, final int pad) { 439 return padTo(s, pad, DEFAULT_PAD_CHAR); 440 } 441 442 454 public static String padTo(final String s, final int pad, 455 final char padChar) { 456 String result = s; 457 int l = s.length(); 458 if (l < pad) { 459 StringBuffer sb = new StringBuffer (pad); 460 while(l < pad) { 461 sb.append(padChar); 462 l++; 463 } 464 sb.append(s); 465 result = sb.toString(); 466 } 467 return result; 468 } 469 470 477 public static boolean byteArrayEquals(final byte[] lhs, final byte[] rhs) { 478 if (lhs == null && rhs != null || lhs != null && rhs == null) { 479 return false; 480 } 481 if (lhs==rhs) { 482 return true; 483 } 484 if (lhs.length != rhs.length) { 485 return false; 486 } 487 for(int i = 0; i<lhs.length; i++) { 488 if (lhs[i]!=rhs[i]) { 489 return false; 490 } 491 } 492 return true; 493 } 494 495 501 public static String doubleToString(double val, int precision){ 502 String tmp = Double.toString(val); 503 if(tmp.indexOf(".")!=-1){ 504 if(precision<=0){ 506 tmp = tmp.substring(0,tmp.indexOf('.')); 507 } else { 508 if(tmp.length()>tmp.indexOf('.')+precision+1){ 509 tmp = tmp.substring(0,tmp.indexOf('.')+precision+1); 511 } 512 } 513 } 514 return tmp; 515 } 516 517 534 public static String formatBytesForDisplay(long amount){ 535 long kbStartAt = 1024; 536 long mbStartAt = 1024*1024*2; 537 long gbStartAt = ((long)1024*1024)*1024*4; 538 539 if(amount < 0){ 540 return "0 B"; 541 } 542 else if(amount < kbStartAt){ 543 return amount + " B"; 545 } else if(amount < mbStartAt) { 546 return Long.toString((long)(((double)amount/1024)))+" KB"; 548 } else if(amount < gbStartAt) { 549 return Long.toString((long)(((double)amount/(1024*1024))))+" MB"; 551 } else { 552 return Long.toString((long)(((double)amount/(1024*1024*1024))))+" GB"; 554 } 555 } 556 557 562 public static String formatMillisecondsToConventional(long time) { 563 return formatMillisecondsToConventional(time,true); 564 } 565 566 572 public static String formatMillisecondsToConventional(long time, boolean toMs) { 573 StringBuffer sb = new StringBuffer (); 574 if(time<0) { 575 sb.append("-"); 576 } 577 long absTime = Math.abs(time); 578 if(!toMs && absTime < 1000) { 579 return "0s"; 580 } 581 if(absTime > DAY_IN_MS) { 582 sb.append(absTime / DAY_IN_MS + "d"); 584 absTime = absTime % DAY_IN_MS; 585 } 586 if (absTime > HOUR_IN_MS) { 587 sb.append(absTime / HOUR_IN_MS + "h"); 589 absTime = absTime % HOUR_IN_MS; 590 } 591 if (absTime > 60000) { 592 sb.append(absTime / 60000 + "m"); 593 absTime = absTime % 60000; 594 } 595 if (absTime > 1000) { 596 sb.append(absTime / 1000 + "s"); 597 absTime = absTime % 1000; 598 } 599 if(toMs) { 600 sb.append(absTime + "ms"); 601 } 602 return sb.toString(); 603 } 604 605 606 616 public static long classnameBasedUID(Class class1, int version) { 617 String callingClassname = class1.getName(); 618 return (long)callingClassname.hashCode() << 32 + version; 619 } 620 621 629 public static void longIntoByteArray(long l, byte[] array, int offset) { 630 int i, shift; 631 632 for(i = 0, shift = 56; i < 8; i++, shift -= 8) 633 array[offset+i] = (byte)(0xFF & (l >> shift)); 634 } 635 636 public static long byteArrayIntoLong(byte [] bytearray) { 637 return byteArrayIntoLong(bytearray, 0); 638 } 639 640 648 public static long byteArrayIntoLong(byte [] bytearray, 649 int offset) { 650 long result = 0; 651 for (int i = offset; i < 8 ; i++) { 652 result = (result << 8 ) | 653 (0xff & (byte)(bytearray[i] & 0xff)); 654 } 655 return result; 656 } 657 658 665 public static String addImpliedHttpIfNecessary(String u) { 666 if(u.indexOf(':') == -1 || u.indexOf('.') < u.indexOf(':')) { 667 u = "http://" + u; 669 } 670 return u; 671 } 672 673 681 public static boolean startsWith(byte[] array, byte[] prefix) { 682 if(prefix.length>array.length) { 683 return false; 684 } 685 for(int i = 0; i < prefix.length; i++) { 686 if(array[i]!=prefix[i]) { 687 return false; 688 } 689 } 690 return true; 691 } 692 693 698 public static String singleLineReport(Reporter rep) { 699 StringWriter sw = new StringWriter (); 700 PrintWriter pw = new PrintWriter (sw); 701 try { 702 rep.singleLineReportTo(pw); 703 } catch (IOException e) { 704 e.printStackTrace(); 706 } 707 pw.flush(); 708 return sw.toString(); 709 } 710 711 719 public static String writeReportToString(Reporter rep, String name) { 720 StringWriter sw = new StringWriter (); 721 PrintWriter pw = new PrintWriter (sw); 722 rep.reportTo(name,pw); 723 pw.flush(); 724 return sw.toString(); 725 } 726 } 727 728 | Popular Tags |