KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > ArchiveUtils


1 /*
2  * ArchiveUtils
3  *
4  * $Header: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/java/org/archive/util/ArchiveUtils.java,v 1.36.2.1 2007/01/13 01:31:39 stack-sf Exp $
5  *
6  * Created on Jul 7, 2003
7  *
8  * Copyright (C) 2003 Internet Archive.
9  *
10  * This file is part of the Heritrix web crawler (crawler.archive.org).
11  *
12  * Heritrix is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * any later version.
16  *
17  * Heritrix is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser Public License
23  * along with Heritrix; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */

27 package org.archive.util;
28
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.text.ParseException JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Calendar JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.GregorianCalendar JavaDoc;
37 import java.util.TimeZone JavaDoc;
38
39 /**
40  * Miscellaneous useful methods.
41  *
42  *
43  * @author gojomo
44  */

45 public class ArchiveUtils {
46
47     /**
48      * Arc-style date stamp in the format yyyyMMddHHmm and UTC time zone.
49      */

50     public static final SimpleDateFormat JavaDoc TIMESTAMP12;
51     /**
52      * Arc-style date stamp in the format yyyyMMddHHmmss and UTC time zone.
53      */

54     public static final SimpleDateFormat JavaDoc TIMESTAMP14;
55     /**
56      * Arc-style date stamp in the format yyyyMMddHHmmssSSS and UTC time zone.
57      */

58     public static final SimpleDateFormat JavaDoc TIMESTAMP17;
59     /**
60      * Log-style date stamp in the format yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
61      * UTC time zone is assumed.
62      */

63     public static final SimpleDateFormat JavaDoc TIMESTAMP17ISO8601Z;
64     /**
65      * Log-style date stamp in the format yyyy-MM-dd'T'HH:mm:ss'Z'
66      * UTC time zone is assumed.
67      */

68     public static final SimpleDateFormat JavaDoc TIMESTAMP14ISO8601Z;
69     /**
70      * Default character to use padding strings.
71      */

72     private static final char DEFAULT_PAD_CHAR = ' ';
73
74     /** milliseconds in an hour */
75     private static final int HOUR_IN_MS = 60 * 60 * 1000;
76     /** milliseconds in a day */
77     private static final int DAY_IN_MS = 24 * HOUR_IN_MS;
78
79     // Initialize fomatters with pattern and time zone
80
static {
81         TimeZone JavaDoc TZ = TimeZone.getTimeZone("GMT");
82         TIMESTAMP12 = new SimpleDateFormat JavaDoc("yyyyMMddHHmm");
83         TIMESTAMP12.setTimeZone(TZ);
84         TIMESTAMP14 = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
85         TIMESTAMP14.setTimeZone(TZ);
86         TIMESTAMP17 = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssSSS");
87         TIMESTAMP17.setTimeZone(TZ);
88         TIMESTAMP17ISO8601Z = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
89         TIMESTAMP14ISO8601Z = new SimpleDateFormat JavaDoc("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     /**
96      * Utility function for creating arc-style date stamps
97      * in the format yyyMMddHHmmssSSS.
98      * Date stamps are in the UTC time zone
99      * @return the date stamp
100      */

101     public static String JavaDoc get17DigitDate(){
102         return TIMESTAMP17.format(new Date JavaDoc());
103     }
104
105     /**
106      * Utility function for creating arc-style date stamps
107      * in the format yyyMMddHHmmss.
108      * Date stamps are in the UTC time zone
109      * @return the date stamp
110      */

111     public static String JavaDoc get14DigitDate(){
112         return TIMESTAMP14.format(new Date JavaDoc());
113     }
114
115     /**
116      * Utility function for creating arc-style date stamps
117      * in the format yyyMMddHHmm.
118      * Date stamps are in the UTC time zone
119      * @return the date stamp
120      */

121     public static String JavaDoc get12DigitDate(){
122         return TIMESTAMP12.format(new Date JavaDoc());
123     }
124
125     /**
126      * Utility function for creating log timestamps, in
127      * W3C/ISO8601 format, assuming UTC. Use current time.
128      *
129      * Format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
130      *
131      * @return the date stamp
132      */

133     public static String JavaDoc getLog17Date(){
134         return TIMESTAMP17ISO8601Z.format(new Date JavaDoc());
135     }
136     
137     /**
138      * Utility function for creating log timestamps, in
139      * W3C/ISO8601 format, assuming UTC.
140      *
141      * Format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
142      * @param date Date to format.
143      *
144      * @return the date stamp
145      */

146     public static String JavaDoc getLog17Date(long date){
147         return TIMESTAMP17ISO8601Z.format(new Date JavaDoc(date));
148     }
149     
150     /**
151      * Utility function for creating log timestamps, in
152      * W3C/ISO8601 format, assuming UTC. Use current time.
153      *
154      * Format is yyyy-MM-dd'T'HH:mm:ss'Z'
155      *
156      * @return the date stamp
157      */

158     public static String JavaDoc getLog14Date(){
159         return TIMESTAMP14ISO8601Z.format(new Date JavaDoc());
160     }
161     
162     /**
163      * Utility function for creating log timestamps, in
164      * W3C/ISO8601 format, assuming UTC.
165      *
166      * Format is yyyy-MM-dd'T'HH:mm:ss'Z'
167      * @param date Date to format.
168      *
169      * @return the date stamp
170      */

171     public static String JavaDoc getLog14Date(long date){
172         return TIMESTAMP14ISO8601Z.format(new Date JavaDoc(date));
173     }
174     
175     /**
176      * Utility function for creating arc-style date stamps
177      * in the format yyyyMMddHHmmssSSS.
178      * Date stamps are in the UTC time zone
179      *
180      * @param date milliseconds since epoc
181      * @return the date stamp
182      */

183     public static String JavaDoc get17DigitDate(long date){
184         return TIMESTAMP17.format(new Date JavaDoc(date));
185     }
186     
187     public static String JavaDoc get17DigitDate(Date JavaDoc date){
188         return TIMESTAMP17.format(date);
189     }
190
191     /**
192      * Utility function for creating arc-style date stamps
193      * in the format yyyyMMddHHmmss.
194      * Date stamps are in the UTC time zone
195      *
196      * @param date milliseconds since epoc
197      * @return the date stamp
198      */

199     public static String JavaDoc get14DigitDate(long date){
200         return TIMESTAMP14.format(new Date JavaDoc(date));
201     }
202
203     public static String JavaDoc get14DigitDate(Date JavaDoc d) {
204         return TIMESTAMP14.format(d);
205     }
206
207     /**
208      * Utility function for creating arc-style date stamps
209      * in the format yyyyMMddHHmm.
210      * Date stamps are in the UTC time zone
211      *
212      * @param date milliseconds since epoc
213      * @return the date stamp
214      */

215     public static String JavaDoc get12DigitDate(long date){
216         return TIMESTAMP12.format(new Date JavaDoc(date));
217     }
218     
219     public static String JavaDoc get12DigitDate(Date JavaDoc d) {
220         return TIMESTAMP12.format(d);
221     }
222     
223     /**
224      * Parses an ARC-style date. If passed String is < 12 characters in length,
225      * we pad. At a minimum, String should contain a year (>=4 characters).
226      * Parse will also fail if day or month are incompletely specified. Depends
227      * on the above getXXDigitDate methods.
228      * @param A 4-17 digit date in ARC style (<code>yyyy</code> to
229      * <code>yyyyMMddHHmmssSSS</code>) formatting.
230      * @return A Date object representing the passed String.
231      * @throws ParseException
232      */

233     public static Date JavaDoc getDate(String JavaDoc d) throws ParseException JavaDoc {
234         Date JavaDoc date = null;
235         if (d == null) {
236             throw new IllegalArgumentException JavaDoc("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 JavaDoc("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 JavaDoc("Unrecognized century: " + d, 0);
261             }
262             if (d.length() < 8 && (d.length() % 2) != 0) {
263                 throw new ParseException JavaDoc("Incomplete month/date: " + d,
264                     d.length());
265             }
266             StringBuilder JavaDoc sb = new StringBuilder JavaDoc(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     /**
284      * Utility function for parsing arc-style date stamps
285      * in the format yyyMMddHHmmssSSS.
286      * Date stamps are in the UTC time zone. The whole string will not be
287      * parsed, only the first 17 digits.
288      *
289      * @param date an arc-style formatted date stamp
290      * @return the Date corresponding to the date stamp string
291      * @throws ParseException if the inputstring was malformed
292      */

293     public static Date JavaDoc parse17DigitDate(String JavaDoc date) throws ParseException JavaDoc {
294         return TIMESTAMP17.parse(date);
295     }
296
297     /**
298      * Utility function for parsing arc-style date stamps
299      * in the format yyyMMddHHmmss.
300      * Date stamps are in the UTC time zone. The whole string will not be
301      * parsed, only the first 14 digits.
302      *
303      * @param date an arc-style formatted date stamp
304      * @return the Date corresponding to the date stamp string
305      * @throws ParseException if the inputstring was malformed
306      */

307     public static Date JavaDoc parse14DigitDate(String JavaDoc date) throws ParseException JavaDoc{
308         return TIMESTAMP14.parse(date);
309     }
310
311     /**
312      * Utility function for parsing arc-style date stamps
313      * in the format yyyMMddHHmm.
314      * Date stamps are in the UTC time zone. The whole string will not be
315      * parsed, only the first 12 digits.
316      *
317      * @param date an arc-style formatted date stamp
318      * @return the Date corresponding to the date stamp string
319      * @throws ParseException if the inputstring was malformed
320      */

321     public static Date JavaDoc parse12DigitDate(String JavaDoc date) throws ParseException JavaDoc{
322         return TIMESTAMP12.parse(date);
323     }
324     
325     /**
326      * Convert 17-digit date format timestamps (as found in crawl.log, for
327      * example) into a GregorianCalendar object. + * Useful so you can convert
328      * into milliseconds-since-epoch. Note: it is possible to compute
329      * milliseconds-since-epoch + * using {@link #parse17DigitDate}.UTC(), but
330      * that method is deprecated in favor of using Calendar.getTimeInMillis(). + *
331      * <p/>I probably should have dug into all the utility methods in
332      * DateFormat.java to parse the timestamp, but this was + * easier. If
333      * someone wants to fix this to use those methods, please have at it! <p/>
334      * Mike Schwartz, schwartz at CodeOnTheRoad dot com.
335      *
336      * @param timestamp17String
337      * @return Calendar set to <code>timestamp17String</code>.
338      */

339     public static Calendar JavaDoc timestamp17ToCalendar(String JavaDoc timestamp17String) {
340         GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
341         int year = Integer.parseInt(timestamp17String.substring(0, 4));
342         int dayOfMonth = Integer.parseInt(timestamp17String.substring(6, 8));
343         // Month is 0-based
344
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     /**
361      * @param timestamp A 14-digit timestamp or the suffix for a 14-digit
362      * timestamp: E.g. '20010909014640' or '20010101' or '1970'.
363      * @return Seconds since the epoch as a string zero-pre-padded so always
364      * Integer.MAX_VALUE wide (Makes it so sorting of resultant string works
365      * properly).
366      * @throws ParseException
367      */

368     public static String JavaDoc secondsSinceEpoch(String JavaDoc timestamp)
369     throws ParseException JavaDoc {
370         return zeroPadInteger((int)
371             (getSecondsSinceEpoch(timestamp).getTime()/1000));
372     }
373     
374     /**
375      * @param timestamp A 14-digit timestamp or the suffix for a 14-digit
376      * timestamp: E.g. '20010909014640' or '20010101' or '1970'.
377      * @return A date.
378      * @see #secondsSinceEpoch(String)
379      * @throws ParseException
380      */

381     public static Date JavaDoc getSecondsSinceEpoch(String JavaDoc timestamp)
382     throws ParseException JavaDoc {
383         if (timestamp.length() < 14) {
384             if (timestamp.length() < 10 && (timestamp.length() % 2) == 1) {
385                 throw new IllegalArgumentException JavaDoc("Must have year, " +
386                     "month, date, hour or second granularity: " + timestamp);
387             }
388             if (timestamp.length() == 4) {
389                 // Add first month and first date.
390
timestamp = timestamp + "01010000";
391             }
392             if (timestamp.length() == 6) {
393                 // Add a date of the first.
394
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     /**
405      * @param i Integer to add prefix of zeros too. If passed
406      * 2005, will return the String <code>0000002005</code>. String
407      * width is the width of Integer.MAX_VALUE as a string (10
408      * digits).
409      * @return Padded String version of <code>i</code>.
410      */

411     public static String JavaDoc zeroPadInteger(int i) {
412         return ArchiveUtils.padTo(Integer.toString(i),
413                 MAX_INT_CHAR_WIDTH, '0');
414     }
415
416     /**
417      * Convert an <code>int</code> to a <code>String</code>, and pad it to
418      * <code>pad</code> spaces.
419      * @param i the int
420      * @param pad the width to pad to.
421      * @return String w/ padding.
422      */

423     public static String JavaDoc padTo(final int i, final int pad) {
424         String JavaDoc n = Integer.toString(i);
425         return padTo(n, pad);
426     }
427     
428     /**
429      * Pad the given <code>String</code> to <code>pad</code> characters wide
430      * by pre-pending spaces. <code>s</code> should not be <code>null</code>.
431      * If <code>s</code> is already wider than <code>pad</code> no change is
432      * done.
433      *
434      * @param s the String to pad
435      * @param pad the width to pad to.
436      * @return String w/ padding.
437      */

438     public static String JavaDoc padTo(final String JavaDoc s, final int pad) {
439         return padTo(s, pad, DEFAULT_PAD_CHAR);
440     }
441
442     /**
443      * Pad the given <code>String</code> to <code>pad</code> characters wide
444      * by pre-pending <code>padChar</code>.
445      *
446      * <code>s</code> should not be <code>null</code>. If <code>s</code> is
447      * already wider than <code>pad</code> no change is done.
448      *
449      * @param s the String to pad
450      * @param pad the width to pad to.
451      * @param padChar The pad character to use.
452      * @return String w/ padding.
453      */

454     public static String JavaDoc padTo(final String JavaDoc s, final int pad,
455             final char padChar) {
456         String JavaDoc result = s;
457         int l = s.length();
458         if (l < pad) {
459             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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     /** check that two byte arrays are equal. They may be <code>null</code>.
471      *
472      * @param lhs a byte array
473      * @param rhs another byte array.
474      * @return <code>true</code> if they are both equal (or both
475      * <code>null</code>)
476      */

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     /**
496      * Converts a double to a string.
497      * @param val The double to convert
498      * @param precision How many characters to include after '.'
499      * @return the double as a string.
500      */

501     public static String JavaDoc doubleToString(double val, int precision){
502         String JavaDoc tmp = Double.toString(val);
503         if(tmp.indexOf(".")!=-1){
504             // Need to handle the part after '.'
505
if(precision<=0){
506                 tmp = tmp.substring(0,tmp.indexOf('.'));
507             } else {
508                 if(tmp.length()>tmp.indexOf('.')+precision+1){
509                     // Need to trim
510
tmp = tmp.substring(0,tmp.indexOf('.')+precision+1);
511                 }
512             }
513         }
514         return tmp;
515     }
516
517     /**
518      * Takes an amount of bytes and formats it for display. This involves
519      * converting it to Kb, Mb or Gb if the amount is enough to qualify for
520      * the next level.
521      * <p>
522      * Displays as bytes (B): 0-1023
523      * Displays as kilobytes (KB): 1024 - 2097151 (~2Mb)
524      * Displays as megabytes (MB): 2097152 - 4294967295 (~4Gb)
525      * Displays as gigabytes (GB): 4294967296 - infinity
526      * <p>
527      * Negative numbers will be returned as '0 B'.
528      * <p>
529      * All values will be approximated down (i.e. 2047 bytes are 1 KB)
530      *
531      * @param amount the amount of bytes
532      * @return A string containing the amount, properly formated.
533      */

534     public static String JavaDoc 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             // Display as bytes.
544
return amount + " B";
545         } else if(amount < mbStartAt) {
546             // Display as kilobytes
547
return Long.toString((long)(((double)amount/1024)))+" KB";
548         } else if(amount < gbStartAt) {
549             // Display as megabytes
550
return Long.toString((long)(((double)amount/(1024*1024))))+" MB";
551         } else {
552             // Display as gigabytes
553
return Long.toString((long)(((double)amount/(1024*1024*1024))))+" GB";
554         }
555     }
556
557     /**
558      * Convert milliseconds value to a human-readable duration
559      * @param time
560      * @return Human readable string version of passed <code>time</code>
561      */

562     public static String JavaDoc formatMillisecondsToConventional(long time) {
563         return formatMillisecondsToConventional(time,true);
564     }
565     
566     /**
567      * Convert milliseconds value to a human-readable duration
568      * @param time
569      * @param toMs whether to print to the ms
570      * @return Human readable string version of passed <code>time</code>
571      */

572     public static String JavaDoc formatMillisecondsToConventional(long time, boolean toMs) {
573         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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             // days
583
sb.append(absTime / DAY_IN_MS + "d");
584             absTime = absTime % DAY_IN_MS;
585         }
586         if (absTime > HOUR_IN_MS) {
587             //got hours.
588
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     /**
607      * Generate a long UID based on the given class and version number.
608      * Using this instead of the default will assume serialization
609      * compatibility across class changes unless version number is
610      * intentionally bumped.
611      *
612      * @param class1
613      * @param version
614      * @return UID based off class and version number.
615      */

616     public static long classnameBasedUID(Class JavaDoc class1, int version) {
617         String JavaDoc callingClassname = class1.getName();
618         return (long)callingClassname.hashCode() << 32 + version;
619     }
620     
621     /**
622      * Copy the raw bytes of a long into a byte array, starting at
623      * the specified offset.
624      *
625      * @param l
626      * @param array
627      * @param offset
628      */

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     /**
641      * Byte array into long.
642      * @param bytearray Array to convert to a long.
643      * @param offset Offset into array at which we start decoding the long.
644      * @return Long made of the bytes of <code>array</code> beginning at
645      * offset <code>offset</code>.
646      * @see #longIntoByteArray(long, byte[], int)
647      */

648     public static long byteArrayIntoLong(byte [] bytearray,
649             int offset) {
650         long result = 0;
651         for (int i = offset; i < 8 /*Bytes in long*/; i++) {
652             result = (result << 8 /*Bits in byte*/) |
653                 (0xff & (byte)(bytearray[i] & 0xff));
654         }
655         return result;
656     }
657
658     /**
659      * Given a string that may be a plain host or host/path (without
660      * URI scheme), add an implied http:// if necessary.
661      *
662      * @param u string to evaluate
663      * @return string with http:// added if no scheme already present
664      */

665     public static String JavaDoc addImpliedHttpIfNecessary(String JavaDoc u) {
666         if(u.indexOf(':') == -1 || u.indexOf('.') < u.indexOf(':')) {
667             // No scheme present; prepend "http://"
668
u = "http://" + u;
669         }
670         return u;
671     }
672
673     /**
674      * Verify that the array begins with the prefix.
675      *
676      * @param array
677      * @param prefix
678      * @return true if array is identical to prefix for the first prefix.length
679      * positions
680      */

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     /**
694      * Utility method to get a String singleLineReport from Reporter
695      * @param rep Reporter to get singleLineReport from
696      * @return String of report
697      */

698     public static String JavaDoc singleLineReport(Reporter rep) {
699         StringWriter JavaDoc sw = new StringWriter JavaDoc();
700         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
701         try {
702             rep.singleLineReportTo(pw);
703         } catch (IOException JavaDoc e) {
704             // not really possible
705
e.printStackTrace();
706         }
707         pw.flush();
708         return sw.toString();
709     }
710
711     /**
712      * Compose the requested report into a String. DANGEROUS IF REPORT
713      * CAN BE LARGE.
714      *
715      * @param rep Reported
716      * @param name String name of report to compose
717      * @return String of report
718      */

719     public static String JavaDoc writeReportToString(Reporter rep, String JavaDoc name) {
720         StringWriter JavaDoc sw = new StringWriter JavaDoc();
721         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
722         rep.reportTo(name,pw);
723         pw.flush();
724         return sw.toString();
725     }
726 }
727
728
Popular Tags