1 29 package Acme; 30 31 import java.util.*; 32 import java.io.*; 33 import java.net.*; 34 35 53 public class Utils 54 { 55 56 public static String lsDateStr( Date date ) 59 { 60 Calendar cal = new GregorianCalendar(); 61 cal.setTime( date ); 62 long dateTime = date.getTime(); 63 if ( dateTime == -1L ) 64 return "------------"; 65 long nowTime = (new Date()).getTime(); 66 String [] months = { 67 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 68 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 69 String part1 = 70 months[cal.get(Calendar.MONTH)] + 71 Fmt.fmt( cal.get(Calendar.DATE), 3 ); 72 if ( Math.abs( nowTime - dateTime ) < 183L * 24L * 60L * 60L * 1000L ) 73 return part1 + Fmt.fmt( cal.get(Calendar.HOUR_OF_DAY), 3 ) + ":" + 74 Fmt.fmt( cal.get(Calendar.MINUTE), 2, Fmt.ZF ); 75 else 76 return part1 + Fmt.fmt( cal.get(Calendar.YEAR), 6 ); 77 } 78 79 80 public static String pluralStr( long n ) 82 { 83 if ( n == 1 ) 84 return ""; 85 else 86 return "s"; 87 } 88 89 90 public static final long INT_SECOND = 1000L; 92 public static final long INT_MINUTE = INT_SECOND * 60L; 93 public static final long INT_HOUR = INT_MINUTE * 60L; 94 public static final long INT_DAY = INT_HOUR * 24L; 95 public static final long INT_WEEK = INT_DAY * 7L; 96 public static final long INT_MONTH = INT_DAY * 30L; 97 public static final long INT_YEAR = INT_DAY * 365L; 98 public static final long INT_DECADE = INT_DAY * 3652L; 99 100 public static String intervalStr( long interval ) 103 { 104 long decades, years, months, weeks, days, hours, minutes, seconds, millis; 105 106 decades = interval / INT_DECADE; 107 interval -= decades * INT_DECADE; 108 years = interval / INT_YEAR; 109 interval -= years * INT_YEAR; 110 months = interval / INT_MONTH; 111 interval -= months * INT_MONTH; 112 weeks = interval / INT_WEEK; 113 interval -= weeks * INT_WEEK; 114 days = interval / INT_DAY; 115 interval -= days * INT_DAY; 116 hours = interval / INT_HOUR; 117 interval -= hours * INT_HOUR; 118 minutes = interval / INT_MINUTE; 119 interval -= minutes * INT_MINUTE; 120 seconds = interval / INT_SECOND; 121 interval -= seconds * INT_SECOND; 122 millis = interval; 123 124 if ( decades > 0 ) 125 if ( years == 0 ) 126 return decades + " decade" + pluralStr( decades ); 127 else 128 return 129 decades + " decade" + pluralStr( decades ) + ", " + 130 years + " years" + pluralStr( years ); 131 else if ( years > 0 ) 132 if ( months == 0 ) 133 return years + " year" + pluralStr( years ); 134 else 135 return 136 years + " year" + pluralStr( years ) + ", " + 137 months + " month" + pluralStr( months ); 138 else if ( months > 0 ) 139 if ( weeks == 0 ) 140 return months + " month" + pluralStr( months ); 141 else 142 return 143 months + " month" + pluralStr( months ) + ", " + 144 weeks + " week" + pluralStr( weeks ); 145 else if ( weeks > 0 ) 146 if ( days == 0 ) 147 return weeks + " week" + pluralStr( weeks ); 148 else 149 return 150 weeks + " week" + pluralStr( weeks ) + ", " + 151 days + " day" + pluralStr( days ); 152 else if ( days > 0 ) 153 if ( hours == 0 ) 154 return days + " day" + pluralStr( days ); 155 else 156 return 157 days + " day" + pluralStr( days ) + ", " + 158 hours + " hour" + pluralStr( hours ); 159 else if ( hours > 0 ) 160 if ( minutes == 0 ) 161 return hours + " hour" + pluralStr( hours ); 162 else 163 return 164 hours + " hour" + pluralStr( hours ) + ", " + 165 minutes + " minute" + pluralStr( minutes ); 166 else if ( minutes > 0 ) 167 if ( seconds == 0 ) 168 return minutes + " minute" + pluralStr( minutes ); 169 else 170 return 171 minutes + " minute" + pluralStr( minutes ) + ", " + 172 seconds + " second" + pluralStr( seconds ); 173 else if ( seconds > 0 ) 174 if ( millis == 0 ) 175 return seconds + " second" + pluralStr( seconds ); 176 else 177 return 178 seconds + " second" + pluralStr( seconds ) + ", " + 179 millis + " millisecond" + pluralStr( millis ); 180 else 181 return millis + " millisecond" + pluralStr( millis ); 182 } 183 184 185 public static int strSpan( String str, String charSet ) 188 { 189 return strSpan( str, charSet, 0 ); 190 } 191 192 public static int strSpan( String str, String charSet, int fromIdx ) 195 { 196 int i; 197 for ( i = fromIdx; i < str.length(); ++i ) 198 if ( charSet.indexOf( str.charAt( i ) ) == -1 ) 199 break; 200 return i - fromIdx; 201 } 202 203 public static int strCSpan( String str, String charSet ) 206 { 207 return strCSpan( str, charSet, 0 ); 208 } 209 210 public static int strCSpan( String str, String charSet, int fromIdx ) 213 { 214 int i; 215 for ( i = fromIdx; i < str.length(); ++i ) 216 if ( charSet.indexOf( str.charAt( i ) ) != -1 ) 217 break; 218 return i - fromIdx; 219 } 220 221 public static boolean match( String pattern, String string ) 224 { 225 for ( int p = 0; ; ++p ) 226 { 227 for ( int s = 0; ; ++p, ++s ) 228 { 229 boolean sEnd = ( s >= string.length() ); 230 boolean pEnd = ( p >= pattern.length() || 231 pattern.charAt( p ) == '|' ); 232 if ( sEnd && pEnd ) 233 return true; 234 if ( sEnd || pEnd ) 235 break; 236 if ( pattern.charAt( p ) == '?' ) 237 continue; 238 if ( pattern.charAt( p ) == '*' ) 239 { 240 int i; 241 ++p; 242 for ( i = string.length(); i >= s; --i ) 243 if ( match( 244 pattern.substring( p ), 245 string.substring( i ) ) ) 246 return true; 247 break; 248 } 249 if ( pattern.charAt( p ) != string.charAt( s ) ) 250 break; 251 } 252 p = pattern.indexOf( '|', p ); 253 if ( p == -1 ) 254 return false; 255 } 256 } 257 258 266 public static int sameSpan( String str1, String str2 ) 268 { 269 int i; 270 for ( i = 0; 271 i < str1.length() && i < str2.length() && 272 str1.charAt( i ) == str2.charAt( i ); 273 ++i ) 274 ; 275 return i; 276 } 277 278 public static int charCount( String str, char c ) 280 { 281 int n = 0; 282 for ( int i = 0; i < str.length(); ++i ) 283 if ( str.charAt( i ) == c ) 284 ++n; 285 return n; 286 } 287 288 289 public static String [] splitStr( String str ) 292 { 293 StringTokenizer st = new StringTokenizer( str ); 294 int n = st.countTokens(); 295 String [] strs = new String [n]; 296 for ( int i = 0; i < n; ++i ) 297 strs[i] = st.nextToken(); 298 return strs; 299 } 300 301 public static String [] splitStr( String str, char delim ) 305 { 306 int n = 1; 307 int index = -1; 308 while ( true ) 309 { 310 index = str.indexOf( delim, index + 1 ); 311 if ( index == -1 ) 312 break; 313 ++n; 314 } 315 String [] strs = new String [n]; 316 index = -1; 317 for ( int i = 0; i < n - 1; ++i ) 318 { 319 int nextIndex = str.indexOf( delim, index + 1 ); 320 strs[i] = str.substring( index + 1, nextIndex ); 321 index = nextIndex; 322 } 323 strs[n - 1] = str.substring( index + 1 ); 324 return strs; 325 } 326 327 public static String flattenStrarr( String [] strs ) 330 { 331 StringBuffer sb = new StringBuffer (); 332 for ( int i = 0; i < strs.length; ++i ) 333 { 334 if ( i > 0 ) 335 sb.append( ' ' ); 336 sb.append( strs[i] ); 337 } 338 return sb.toString(); 339 } 340 341 public static void sortStrings( String [] strings ) 345 { 346 for ( int i = 0; i < strings.length - 1; ++i ) 348 { 349 for ( int j = i + 1; j < strings.length; ++j ) 350 { 351 if ( strings[i].compareTo( strings[j] ) > 0 ) 352 { 353 String t = strings[i]; 354 strings[i] = strings[j]; 355 strings[j] = t; 356 } 357 } 358 } 359 } 360 361 public static int indexOfString( String [] strings, String string ) 364 { 365 for ( int i = 0; i < strings.length; ++i ) 366 if ( string.equals( strings[i] ) ) 367 return i; 368 return -1; 369 } 370 371 public static int indexOfStringIgnoreCase( String [] strings, String string ) 374 { 375 for ( int i = 0; i < strings.length; ++i ) 376 if ( string.equalsIgnoreCase( strings[i] ) ) 377 return i; 378 return -1; 379 } 380 381 public static boolean equalsStrings( String [] strings1, String [] strings2 ) 383 { 384 if ( strings1.length != strings2.length ) 385 return false; 386 for ( int i = 0; i < strings1.length; ++i ) 387 if ( ! strings1[i].equals( strings2[i] ) ) 388 return false; 389 return true; 390 } 391 392 393 public static long pow( long a, long b ) throws ArithmeticException 396 { 397 if ( b < 0 ) 398 throw new ArithmeticException (); 399 long r = 1; 400 while ( b != 0 ) 401 { 402 if ( odd( b ) ) 403 r *= a; 404 b >>>= 1; 405 a *= a; 406 } 407 return r; 408 } 409 410 411 public static int parseInt( String str, int def ) 413 { 414 try 415 { 416 return Integer.parseInt( str ); 417 } 418 catch ( Exception e ) 419 { 420 return def; 421 } 422 } 423 424 public static long parseLong( String str, long def ) 426 { 427 try 428 { 429 return Long.parseLong( str ); 430 } 431 catch ( Exception e ) 432 { 433 return def; 434 } 435 } 436 437 438 public static String arrayToString( Object o ) 452 { 453 if ( o == null ) 454 return "null"; 455 String cl = o.getClass().getName(); 456 if ( ! cl.startsWith( "[" ) ) 457 return o.toString(); 459 StringBuffer sb = new StringBuffer ( "{ " ); 460 if ( o instanceof byte[] ) 461 { 462 byte[] ba = (byte[]) o; 463 for ( int i = 0; i < ba.length; ++i ) 464 { 465 if ( i > 0 ) sb.append( ", " ); 466 sb.append( "(byte)" ); 467 sb.append( ba[i] ); 468 } 469 } 470 else if ( o instanceof char[] ) 471 { 472 char[] ca = (char[]) o; 473 for ( int i = 0; i < ca.length; ++i ) 474 { 475 if ( i > 0 ) sb.append( ", " ); 476 sb.append( "'" ); 477 sb.append( ca[i] ); 478 sb.append( "'" ); 479 } 480 } 481 else if ( o instanceof short[] ) 482 { 483 short[] sa = (short[]) o; 484 for ( int i = 0; i < sa.length; ++i ) 485 { 486 if ( i > 0 ) sb.append( ", " ); 487 sb.append( "(short)" ); 488 sb.append( sa[i] ); 489 } 490 } 491 else if ( o instanceof int[] ) 492 { 493 int[] ia = (int[]) o; 494 for ( int i = 0; i < ia.length; ++i ) 495 { 496 if ( i > 0 ) sb.append( ", " ); 497 sb.append( ia[i] ); 498 } 499 } 500 else if ( o instanceof long[] ) 501 { 502 long[] la = (long[]) o; 503 for ( int i = 0; i < la.length; ++i ) 504 { 505 if ( i > 0 ) sb.append( ", " ); 506 sb.append( la[i] ); 507 sb.append( "L" ); 508 } 509 } 510 else if ( o instanceof float[] ) 511 { 512 float[] fa = (float[]) o; 513 for ( int i = 0; i < fa.length; ++i ) 514 { 515 if ( i > 0 ) sb.append( ", " ); 516 sb.append( fa[i] ); 517 sb.append( "F" ); 518 } 519 } 520 else if ( o instanceof double[] ) 521 { 522 double[] da = (double[]) o; 523 for ( int i = 0; i < da.length; ++i ) 524 { 525 if ( i > 0 ) sb.append( ", " ); 526 sb.append( da[i] ); 527 sb.append( "D" ); 528 } 529 } 530 else if ( o instanceof String ) 531 { 532 String [] sa = (String []) o; 534 for ( int i = 0; i < sa.length; ++i ) 535 { 536 if ( i > 0 ) sb.append( ", " ); 537 sb.append( "\"" ); 538 sb.append( sa[i] ); 539 sb.append( "\"" ); 540 } 541 } 542 else if ( cl.startsWith( "[L" ) ) 543 { 544 Object [] oa = (Object []) o; 546 for ( int i = 0; i < oa.length; ++i ) 547 { 548 if ( i > 0 ) sb.append( ", " ); 549 sb.append( oa[i] ); 550 } 551 } 552 else if ( cl.startsWith( "[[" ) ) 553 { 554 Object [] aa = (Object []) o; 556 for ( int i = 0; i < aa.length; ++i ) 557 { 558 if ( i > 0 ) sb.append( ", " ); 559 sb.append( arrayToString( aa[i] ) ); 560 } 561 } 562 else 563 sb.append( "(unknown array type)" ); 564 sb.append( " }" ); 565 return sb.toString(); 566 } 567 568 569 public static boolean instanceOf( Object o, Class cl ) 573 { 574 if ( o == null || cl == null ) 576 return false; 577 Class ocl = o.getClass(); 578 if ( ocl.equals( cl ) ) 580 return true; 581 if ( ! cl.isInterface() ) 583 { 584 Class ifs[] = cl.getInterfaces(); 585 for ( int i = 0; i < ifs.length; ++i ) 586 if ( instanceOf( o, ifs[i] ) ) 587 return true; 588 } 589 Class scl = cl.getSuperclass(); 591 if ( scl != null ) 592 if ( instanceOf( o, scl ) ) 593 return true; 594 return false; 596 } 597 598 599 public static boolean even( long n ) 601 { 602 return ( n & 1 ) == 0; 603 } 604 605 public static boolean odd( long n ) 607 { 608 return ( n & 1 ) != 0; 609 } 610 611 612 public static int countOnes( byte n ) 614 { 615 return countOnes( n & 0xffL ); 616 } 617 618 public static int countOnes( int n ) 620 { 621 return countOnes( n & 0xffffffffL ); 622 } 623 624 public static int countOnes( long n ) 626 { 627 int count = 0; 630 while ( n != 0 ) 631 { 632 if ( odd( n ) ) 633 ++count; 634 n >>>= 1; 635 } 636 return count; 637 } 638 639 640 public static int read( InputStream in, byte[] b, int off, int len ) throws IOException 644 { 645 if ( len <= 0 ) 646 return 0; 647 int c = in.read(); 648 if ( c == -1 ) 649 return -1; 650 if ( b != null ) 651 b[off] = (byte) c; 652 int i; 653 for ( i = 1; i < len ; ++i ) 654 { 655 c = in.read(); 656 if ( c == -1 ) 657 break; 658 if ( b != null ) 659 b[off + i] = (byte) c; 660 } 661 return i; 662 } 663 664 public static int readFully( InputStream in, byte[] b, int off, int len ) throws IOException 668 { 669 int l, r; 670 for ( l = 0; l < len; ) 671 { 672 r = read( in, b, l, len - l ); 673 if ( r == -1 ) 674 return -1; 675 l += r; 676 } 677 return len; 678 } 679 680 681 public static URL plainUrl( URL context, String urlStr ) throws MalformedURLException 684 { 685 URL url = new URL( context, urlStr ); 686 String fileStr = url.getFile(); 687 int i = fileStr.indexOf( '?' ); 688 if ( i != -1 ) 689 fileStr = fileStr.substring( 0, i ); 690 url = new URL( 691 url.getProtocol(), url.getHost(), url.getPort(), fileStr ); 692 if ( ( ! fileStr.endsWith( "/" ) ) && 693 urlStrIsDir( url.toExternalForm() ) ) 694 { 695 fileStr = fileStr + "/"; 696 url = new URL( 697 url.getProtocol(), url.getHost(), url.getPort(), fileStr ); 698 } 699 return url; 700 } 701 702 public static URL plainUrl( String urlStr ) throws MalformedURLException 705 { 706 return plainUrl( null, urlStr ); 707 } 708 709 public static String baseUrlStr( String urlStr ) 713 { 714 if ( urlStr.endsWith( "/" ) ) 715 return urlStr; 716 if ( urlStrIsDir( urlStr ) ) 717 return urlStr + "/"; 718 return urlStr.substring( 0, urlStr.lastIndexOf( '/' ) + 1 ); 719 } 720 721 public static String fixDirUrlStr( String urlStr ) 723 { 724 if ( urlStr.endsWith( "/" ) ) 725 return urlStr; 726 if ( urlStrIsDir( urlStr ) ) 727 return urlStr + "/"; 728 return urlStr; 729 } 730 731 public static boolean urlStrIsDir( String urlStr ) 739 { 740 if ( urlStr.endsWith( "/" ) ) 742 return true; 743 744 int lastSlash = urlStr.lastIndexOf( '/' ); 746 int lastPeriod = urlStr.lastIndexOf( '.' ); 747 if ( lastPeriod != -1 && ( lastSlash == -1 || lastPeriod > lastSlash ) ) 748 return false; 749 750 String urlStrWithSlash = urlStr + "/"; 753 try 754 { 755 URL url = new URL( urlStrWithSlash ); 756 InputStream f = url.openStream(); 757 f.close(); 758 return true; 760 } 761 catch ( Exception e ) 762 { 763 return false; 765 } 766 } 767 768 769 public static boolean urlStrIsAbsolute( String urlStr ) 771 { 772 if ( urlStr.startsWith( "/" ) || urlStr.indexOf( ":/" ) != -1 ) 773 return true; 774 return false; 776 } 777 778 public static String absoluteUrlStr( String urlStr, URL contextUrl ) throws MalformedURLException 780 { 781 URL url = new URL( contextUrl, urlStr ); 782 return url.toExternalForm(); 783 } 784 785 786 public static String urlDecoder( String encoded ) 789 { 790 StringBuffer decoded = new StringBuffer (); 791 int len = encoded.length(); 792 for ( int i = 0; i < len; ++i ) 793 { 794 if ( encoded.charAt( i ) == '%' && i + 2 < len ) 795 { 796 int d1 = Character.digit( encoded.charAt( i + 1 ), 16 ); 797 int d2 = Character.digit( encoded.charAt( i + 2 ), 16 ); 798 if ( d1 != -1 && d2 != -1 ) 799 decoded.append( (char) ( ( d1 << 4 ) + d2 ) ); 800 i += 2; 801 } 802 else if ( encoded.charAt( i ) == '+' ) 803 decoded.append( ' ' ); 804 else 805 decoded.append( encoded.charAt( i ) ); 806 } 807 return decoded.toString(); 808 } 809 810 811 public static String base64Encode( byte[] src ) 822 { 823 StringBuffer encoded = new StringBuffer (); 824 int i, phase = 0; 825 char c = 0; 826 827 for ( i = 0; i < src.length; ++i ) 828 { 829 switch ( phase ) 830 { 831 case 0: 832 c = b64EncodeTable[( src[i] >> 2 ) & 0x3f]; 833 encoded.append( c ); 834 c = b64EncodeTable[( src[i] & 0x3 ) << 4]; 835 encoded.append( c ); 836 ++phase; 837 break; 838 case 1: 839 c = b64EncodeTable[ 840 ( b64DecodeTable[c] | ( src[i] >> 4 ) ) & 0x3f]; 841 encoded.setCharAt( encoded.length() - 1, c ); 842 c = b64EncodeTable[( src[i] & 0xf ) << 2]; 843 encoded.append( c ); 844 ++phase; 845 break; 846 case 2: 847 c = b64EncodeTable[ 848 ( b64DecodeTable[c] | ( src[i] >> 6 ) ) & 0x3f]; 849 encoded.setCharAt( encoded.length() - 1, c ); 850 c = b64EncodeTable[src[i] & 0x3f]; 851 encoded.append( c ); 852 phase = 0; 853 break; 854 } 855 } 856 857 while ( phase++ < 3 ) 858 encoded.append( '=' ); 859 return encoded.toString(); 860 } 861 862 private static char b64EncodeTable[] = { 863 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; 872 873 private static int b64DecodeTable[] = { 874 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }; 891 892 public static String base64Encode( String srcString ) 894 { 895 byte[] src = new byte[srcString.length()]; 896 srcString.getBytes( 0, src.length, src, 0 ); 897 return base64Encode( src ); 898 } 899 900 901 public static boolean arraycontains( Object [] array, Object element ) 903 { 904 for ( int i = 0; i < array.length; ++i ) 905 if ( array[i].equals( element ) ) 906 return true; 907 return false; 908 } 909 910 911 public static int system( String cmd ) 918 { 919 try 920 { 921 return runCommand( cmd ).waitFor(); 922 } 923 catch ( IOException e ) 924 { 925 return -1; 926 } 927 catch ( InterruptedException e ) 928 { 929 return -1; 930 } 931 } 932 933 public static InputStream popenr( String cmd ) 945 { 946 try 947 { 948 return runCommand( cmd ).getInputStream(); 949 } 950 catch ( IOException e ) 951 { 952 return null; 953 } 954 } 955 956 public static OutputStream popenw( String cmd ) 961 { 962 try 963 { 964 return runCommand( cmd ).getOutputStream(); 965 } 966 catch ( IOException e ) 967 { 968 return null; 969 } 970 } 971 972 public static Process runCommand( String cmd ) throws IOException 984 { 985 Runtime runtime = Runtime.getRuntime(); 986 String [] shCmd = new String [3]; 987 shCmd[0] = "/bin/sh"; 988 shCmd[1] = "-c"; 989 shCmd[2] = cmd; 990 return runtime.exec( shCmd ); 991 } 992 993 994 public static void copyStream( InputStream in, OutputStream out ) throws IOException 996 { 997 byte[] buf = new byte[4096]; 998 int len; 999 while ( ( len = in.read( buf ) ) != -1 ) 1000 out.write( buf, 0, len ); 1001 } 1002 1003 public static void copyStream( Reader in, Writer out ) throws IOException 1005 { 1006 char[] buf = new char[4096]; 1007 int len; 1008 while ( ( len = in.read( buf ) ) != -1 ) 1009 out.write( buf, 0, len ); 1010 } 1011 1012 public static void copyStream( InputStream in, Writer out ) throws IOException 1014 { 1015 byte[] buf1 = new byte[4096]; 1016 char[] buf2 = new char[4096]; 1017 int len, i; 1018 while ( ( len = in.read( buf1 ) ) != -1 ) 1019 { 1020 for ( i = 0; i < len; ++i ) 1021 buf2[i] = (char) buf1[i]; 1022 out.write( buf2, 0, len ); 1023 } 1024 } 1025 1026 public static void copyStream( Reader in, OutputStream out ) throws IOException 1028 { 1029 char[] buf1 = new char[4096]; 1030 byte[] buf2 = new byte[4096]; 1031 int len, i; 1032 while ( ( len = in.read( buf1 ) ) != -1 ) 1033 { 1034 for ( i = 0; i < len; ++i ) 1035 buf2[i] = (byte) buf1[i]; 1036 out.write( buf2, 0, len ); 1037 } 1038 } 1039 1040 1041 public static void dumpStack( PrintStream p ) 1043 { 1044 (new Throwable ()).printStackTrace( p ); 1045 } 1046 1047 public static void dumpStack() 1049 { 1050 (new Throwable ()).printStackTrace(); 1051 } 1052 1053 } 1054 | Popular Tags |