1 46 47 package com.rift.coad.lib.thirdparty.base64; 49 50 51 public class Base64 52 { 53 54 55 56 57 58 public final static int NO_OPTIONS = 0; 59 60 61 public final static int ENCODE = 1; 62 63 64 65 public final static int DECODE = 0; 66 67 68 69 public final static int GZIP = 2; 70 71 72 73 public final static int DONT_BREAK_LINES = 8; 74 75 76 77 78 79 80 private final static int MAX_LINE_LENGTH = 76; 81 82 83 84 private final static byte EQUALS_SIGN = (byte)'='; 85 86 87 88 private final static byte NEW_LINE = (byte)'\n'; 89 90 91 92 private final static String PREFERRED_ENCODING = "UTF-8"; 93 94 95 96 private final static byte[] ALPHABET; 97 private final static byte[] _NATIVE_ALPHABET = 98 { 99 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', 100 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', 101 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', 102 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', 103 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', 104 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', 105 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', 106 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', 107 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', 108 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' 109 }; 110 111 112 static 113 { 114 byte[] __bytes; 115 try 116 { 117 __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes( PREFERRED_ENCODING ); 118 } catch (java.io.UnsupportedEncodingException use) 120 { 121 __bytes = _NATIVE_ALPHABET; } ALPHABET = __bytes; 124 } 126 127 131 private final static byte[] DECODABET = 132 { 133 -9,-9,-9,-9,-9,-9,-9,-9,-9, -5,-5, -9,-9, -5, -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, -9,-9,-9,-9,-9, -5, -9,-9,-9,-9,-9,-9,-9,-9,-9,-9, 62, -9,-9,-9, 63, 52,53,54,55,56,57,58,59,60,61, -9,-9,-9, -1, -9,-9,-9, 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, -9,-9,-9,-9,-9,-9, 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, -9,-9,-9,-9 164 }; 165 166 private final static byte WHITE_SPACE_ENC = -5; private final static byte EQUALS_SIGN_ENC = -1; 171 172 173 private Base64(){} 174 175 176 177 178 179 180 195 private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes ) 196 { 197 encode3to4( threeBytes, 0, numSigBytes, b4, 0 ); 198 return b4; 199 } 201 202 223 private static byte[] encode3to4( 224 byte[] source, int srcOffset, int numSigBytes, 225 byte[] destination, int destOffset ) 226 { 227 234 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 ) 239 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 ) 240 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 ); 241 242 switch( numSigBytes ) 243 { 244 case 3: 245 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 246 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 247 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 248 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ]; 249 return destination; 250 251 case 2: 252 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 253 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 254 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 255 destination[ destOffset + 3 ] = EQUALS_SIGN; 256 return destination; 257 258 case 1: 259 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 260 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 261 destination[ destOffset + 2 ] = EQUALS_SIGN; 262 destination[ destOffset + 3 ] = EQUALS_SIGN; 263 return destination; 264 265 default: 266 return destination; 267 } } 270 271 272 283 public static String encodeObject( java.io.Serializable serializableObject ) 284 { 285 return encodeObject( serializableObject, NO_OPTIONS ); 286 } 288 289 290 313 public static String encodeObject( java.io.Serializable serializableObject, int options ) 314 { 315 java.io.ByteArrayOutputStream baos = null; 317 java.io.OutputStream b64os = null; 318 java.io.ObjectOutputStream oos = null; 319 java.util.zip.GZIPOutputStream gzos = null; 320 321 int gzip = (options & GZIP); 323 int dontBreakLines = (options & DONT_BREAK_LINES); 324 325 try 326 { 327 baos = new java.io.ByteArrayOutputStream (); 329 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 330 331 if( gzip == GZIP ) 333 { 334 gzos = new java.util.zip.GZIPOutputStream ( b64os ); 335 oos = new java.io.ObjectOutputStream ( gzos ); 336 } else 338 oos = new java.io.ObjectOutputStream ( b64os ); 339 340 oos.writeObject( serializableObject ); 341 } catch( java.io.IOException e ) 343 { 344 e.printStackTrace(); 345 return null; 346 } finally 348 { 349 try{ oos.close(); } catch( Exception e ){} 350 try{ gzos.close(); } catch( Exception e ){} 351 try{ b64os.close(); } catch( Exception e ){} 352 try{ baos.close(); } catch( Exception e ){} 353 } 355 try 357 { 358 return new String ( baos.toByteArray(), PREFERRED_ENCODING ); 359 } catch (java.io.UnsupportedEncodingException uue) 361 { 362 return new String ( baos.toByteArray() ); 363 } 365 } 367 368 369 376 public static String encodeBytes( byte[] source ) 377 { 378 return encodeBytes( source, 0, source.length, NO_OPTIONS ); 379 } 381 382 383 403 public static String encodeBytes( byte[] source, int options ) 404 { 405 return encodeBytes( source, 0, source.length, options ); 406 } 408 409 418 public static String encodeBytes( byte[] source, int off, int len ) 419 { 420 return encodeBytes( source, off, len, NO_OPTIONS ); 421 } 423 424 425 447 public static String encodeBytes( byte[] source, int off, int len, int options ) 448 { 449 int dontBreakLines = ( options & DONT_BREAK_LINES ); 451 int gzip = ( options & GZIP ); 452 453 if( gzip == GZIP ) 455 { 456 java.io.ByteArrayOutputStream baos = null; 457 java.util.zip.GZIPOutputStream gzos = null; 458 Base64.OutputStream b64os = null; 459 460 461 try 462 { 463 baos = new java.io.ByteArrayOutputStream (); 465 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 466 gzos = new java.util.zip.GZIPOutputStream ( b64os ); 467 468 gzos.write( source, off, len ); 469 gzos.close(); 470 } catch( java.io.IOException e ) 472 { 473 e.printStackTrace(); 474 return null; 475 } finally 477 { 478 try{ gzos.close(); } catch( Exception e ){} 479 try{ b64os.close(); } catch( Exception e ){} 480 try{ baos.close(); } catch( Exception e ){} 481 } 483 try 485 { 486 return new String ( baos.toByteArray(), PREFERRED_ENCODING ); 487 } catch (java.io.UnsupportedEncodingException uue) 489 { 490 return new String ( baos.toByteArray() ); 491 } } 494 else 496 { 497 boolean breakLines = dontBreakLines == 0; 499 500 int len43 = len * 4 / 3; 501 byte[] outBuff = new byte[ ( len43 ) + ( (len % 3) > 0 ? 4 : 0 ) + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; int d = 0; 505 int e = 0; 506 int len2 = len - 2; 507 int lineLength = 0; 508 for( ; d < len2; d+=3, e+=4 ) 509 { 510 encode3to4( source, d+off, 3, outBuff, e ); 511 512 lineLength += 4; 513 if( breakLines && lineLength == MAX_LINE_LENGTH ) 514 { 515 outBuff[e+4] = NEW_LINE; 516 e++; 517 lineLength = 0; 518 } } 521 if( d < len ) 522 { 523 encode3to4( source, d+off, len - d, outBuff, e ); 524 e += 4; 525 } 527 528 try 530 { 531 return new String ( outBuff, 0, e, PREFERRED_ENCODING ); 532 } catch (java.io.UnsupportedEncodingException uue) 534 { 535 return new String ( outBuff, 0, e ); 536 } 538 } 540 } 542 543 544 545 546 547 548 549 571 private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset ) 572 { 573 if( source[ srcOffset + 2] == EQUALS_SIGN ) 575 { 576 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 580 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 ); 581 582 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 583 return 1; 584 } 585 586 else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) 588 { 589 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 594 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 595 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 ); 596 597 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 598 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 ); 599 return 2; 600 } 601 602 else 604 { 605 try{ 606 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 612 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 613 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6) 614 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) ); 615 616 617 destination[ destOffset ] = (byte)( outBuff >> 16 ); 618 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 ); 619 destination[ destOffset + 2 ] = (byte)( outBuff ); 620 621 return 3; 622 }catch( Exception e){ 623 System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) ); 624 System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) ); 625 System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) ); 626 System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) ); 627 return -1; 628 } } 630 } 632 633 634 635 646 public static byte[] decode( byte[] source, int off, int len ) 647 { 648 int len34 = len * 3 / 4; 649 byte[] outBuff = new byte[ len34 ]; int outBuffPosn = 0; 651 652 byte[] b4 = new byte[4]; 653 int b4Posn = 0; 654 int i = 0; 655 byte sbiCrop = 0; 656 byte sbiDecode = 0; 657 for( i = off; i < off+len; i++ ) 658 { 659 sbiCrop = (byte)(source[i] & 0x7f); sbiDecode = DECODABET[ sbiCrop ]; 661 662 if( sbiDecode >= WHITE_SPACE_ENC ) { 664 if( sbiDecode >= EQUALS_SIGN_ENC ) 665 { 666 b4[ b4Posn++ ] = sbiCrop; 667 if( b4Posn > 3 ) 668 { 669 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn ); 670 b4Posn = 0; 671 672 if( sbiCrop == EQUALS_SIGN ) 674 break; 675 } 677 } 679 } else 681 { 682 System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" ); 683 return null; 684 } } 687 byte[] out = new byte[ outBuffPosn ]; 688 System.arraycopy( outBuff, 0, out, 0, outBuffPosn ); 689 return out; 690 } 692 693 694 695 703 public static byte[] decode( String s ) 704 { 705 byte[] bytes; 706 try 707 { 708 bytes = s.getBytes( PREFERRED_ENCODING ); 709 } catch( java.io.UnsupportedEncodingException uee ) 711 { 712 bytes = s.getBytes(); 713 } 716 bytes = decode( bytes, 0, bytes.length ); 718 719 720 if( bytes != null && bytes.length >= 4 ) 723 { 724 725 int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); 726 if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head ) 727 { 728 java.io.ByteArrayInputStream bais = null; 729 java.util.zip.GZIPInputStream gzis = null; 730 java.io.ByteArrayOutputStream baos = null; 731 byte[] buffer = new byte[2048]; 732 int length = 0; 733 734 try 735 { 736 baos = new java.io.ByteArrayOutputStream (); 737 bais = new java.io.ByteArrayInputStream ( bytes ); 738 gzis = new java.util.zip.GZIPInputStream ( bais ); 739 740 while( ( length = gzis.read( buffer ) ) >= 0 ) 741 { 742 baos.write(buffer,0,length); 743 } 745 bytes = baos.toByteArray(); 747 748 } catch( java.io.IOException e ) 750 { 751 } finally 754 { 755 try{ baos.close(); } catch( Exception e ){} 756 try{ gzis.close(); } catch( Exception e ){} 757 try{ bais.close(); } catch( Exception e ){} 758 } 760 } } 763 return bytes; 764 } 766 767 768 769 777 public static Object decodeToObject( String encodedObject ) 778 { 779 byte[] objBytes = decode( encodedObject ); 781 782 java.io.ByteArrayInputStream bais = null; 783 java.io.ObjectInputStream ois = null; 784 Object obj = null; 785 786 try 787 { 788 bais = new java.io.ByteArrayInputStream ( objBytes ); 789 ois = new java.io.ObjectInputStream ( bais ); 790 791 obj = ois.readObject(); 792 } catch( java.io.IOException e ) 794 { 795 e.printStackTrace(); 796 obj = null; 797 } catch( java.lang.ClassNotFoundException e ) 799 { 800 e.printStackTrace(); 801 obj = null; 802 } finally 804 { 805 try{ bais.close(); } catch( Exception e ){} 806 try{ ois.close(); } catch( Exception e ){} 807 } 809 return obj; 810 } 812 813 814 823 public static boolean encodeToFile( byte[] dataToEncode, String filename ) 824 { 825 boolean success = false; 826 Base64.OutputStream bos = null; 827 try 828 { 829 bos = new Base64.OutputStream( 830 new java.io.FileOutputStream ( filename ), Base64.ENCODE ); 831 bos.write( dataToEncode ); 832 success = true; 833 } catch( java.io.IOException e ) 835 { 836 837 success = false; 838 } finally 840 { 841 try{ bos.close(); } catch( Exception e ){} 842 } 844 return success; 845 } 847 848 857 public static boolean decodeToFile( String dataToDecode, String filename ) 858 { 859 boolean success = false; 860 Base64.OutputStream bos = null; 861 try 862 { 863 bos = new Base64.OutputStream( 864 new java.io.FileOutputStream ( filename ), Base64.DECODE ); 865 bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) ); 866 success = true; 867 } catch( java.io.IOException e ) 869 { 870 success = false; 871 } finally 873 { 874 try{ bos.close(); } catch( Exception e ){} 875 } 877 return success; 878 } 880 881 882 883 892 public static byte[] decodeFromFile( String filename ) 893 { 894 byte[] decodedData = null; 895 Base64.InputStream bis = null; 896 try 897 { 898 java.io.File file = new java.io.File ( filename ); 900 byte[] buffer = null; 901 int length = 0; 902 int numBytes = 0; 903 904 if( file.length() > Integer.MAX_VALUE ) 906 { 907 System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." ); 908 return null; 909 } buffer = new byte[ (int)file.length() ]; 911 912 bis = new Base64.InputStream( 914 new java.io.BufferedInputStream ( 915 new java.io.FileInputStream ( file ) ), Base64.DECODE ); 916 917 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 919 length += numBytes; 920 921 decodedData = new byte[ length ]; 923 System.arraycopy( buffer, 0, decodedData, 0, length ); 924 925 } catch( java.io.IOException e ) 927 { 928 System.err.println( "Error decoding from file " + filename ); 929 } finally 931 { 932 try{ bis.close(); } catch( Exception e) {} 933 } 935 return decodedData; 936 } 938 939 940 949 public static String encodeFromFile( String filename ) 950 { 951 String encodedData = null; 952 Base64.InputStream bis = null; 953 try 954 { 955 java.io.File file = new java.io.File ( filename ); 957 byte[] buffer = new byte[ (int)(file.length() * 1.4) ]; 958 int length = 0; 959 int numBytes = 0; 960 961 bis = new Base64.InputStream( 963 new java.io.BufferedInputStream ( 964 new java.io.FileInputStream ( file ) ), Base64.ENCODE ); 965 966 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 968 length += numBytes; 969 970 encodedData = new String ( buffer, 0, length, Base64.PREFERRED_ENCODING ); 972 973 } catch( java.io.IOException e ) 975 { 976 System.err.println( "Error encoding from file " + filename ); 977 } finally 979 { 980 try{ bis.close(); } catch( Exception e) {} 981 } 983 return encodedData; 984 } 986 987 988 989 990 991 992 993 1001 public static class InputStream extends java.io.FilterInputStream 1002 { 1003 private boolean encode; private int position; private byte[] buffer; private int bufferLength; private int numSigBytes; private int lineLength; 1009 private boolean breakLines; 1011 1012 1018 public InputStream( java.io.InputStream in ) 1019 { 1020 this( in, DECODE ); 1021 } 1023 1024 1045 public InputStream( java.io.InputStream in, int options ) 1046 { 1047 super( in ); 1048 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES; 1049 this.encode = (options & ENCODE) == ENCODE; 1050 this.bufferLength = encode ? 4 : 3; 1051 this.buffer = new byte[ bufferLength ]; 1052 this.position = -1; 1053 this.lineLength = 0; 1054 } 1056 1063 public int read() throws java.io.IOException 1064 { 1065 if( position < 0 ) 1067 { 1068 if( encode ) 1069 { 1070 byte[] b3 = new byte[3]; 1071 int numBinaryBytes = 0; 1072 for( int i = 0; i < 3; i++ ) 1073 { 1074 try 1075 { 1076 int b = in.read(); 1077 1078 if( b >= 0 ) 1080 { 1081 b3[i] = (byte)b; 1082 numBinaryBytes++; 1083 } 1085 } catch( java.io.IOException e ) 1087 { 1088 if( i == 0 ) 1090 throw e; 1091 1092 } } 1095 if( numBinaryBytes > 0 ) 1096 { 1097 encode3to4( b3, 0, numBinaryBytes, buffer, 0 ); 1098 position = 0; 1099 numSigBytes = 4; 1100 } else 1102 { 1103 return -1; 1104 } } 1107 else 1109 { 1110 byte[] b4 = new byte[4]; 1111 int i = 0; 1112 for( i = 0; i < 4; i++ ) 1113 { 1114 int b = 0; 1116 do{ b = in.read(); } 1117 while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC ); 1118 1119 if( b < 0 ) 1120 break; 1122 b4[i] = (byte)b; 1123 } 1125 if( i == 4 ) 1126 { 1127 numSigBytes = decode4to3( b4, 0, buffer, 0 ); 1128 position = 0; 1129 } else if( i == 0 ){ 1131 return -1; 1132 } else 1134 { 1135 throw new java.io.IOException ( "Improperly padded Base64 input." ); 1137 } 1139 } } 1142 if( position >= 0 ) 1144 { 1145 if( position >= numSigBytes ) 1147 return -1; 1148 1149 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH ) 1150 { 1151 lineLength = 0; 1152 return '\n'; 1153 } else 1155 { 1156 lineLength++; 1160 int b = buffer[ position++ ]; 1161 1162 if( position >= bufferLength ) 1163 position = -1; 1164 1165 return b & 0xFF; } } 1170 else 1172 { 1173 throw new java.io.IOException ( "Error in Base64 code reading stream." ); 1175 } } 1178 1179 1191 public int read( byte[] dest, int off, int len ) throws java.io.IOException 1192 { 1193 int i; 1194 int b; 1195 for( i = 0; i < len; i++ ) 1196 { 1197 b = read(); 1198 1199 1202 if( b >= 0 ) 1203 dest[off + i] = (byte)b; 1204 else if( i == 0 ) 1205 return -1; 1206 else 1207 break; } return i; 1210 } 1212 } 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1231 public static class OutputStream extends java.io.FilterOutputStream 1232 { 1233 private boolean encode; 1234 private int position; 1235 private byte[] buffer; 1236 private int bufferLength; 1237 private int lineLength; 1238 private boolean breakLines; 1239 private byte[] b4; private boolean suspendEncoding; 1241 1242 1248 public OutputStream( java.io.OutputStream out ) 1249 { 1250 this( out, ENCODE ); 1251 } 1253 1254 1274 public OutputStream( java.io.OutputStream out, int options ) 1275 { 1276 super( out ); 1277 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES; 1278 this.encode = (options & ENCODE) == ENCODE; 1279 this.bufferLength = encode ? 3 : 4; 1280 this.buffer = new byte[ bufferLength ]; 1281 this.position = 0; 1282 this.lineLength = 0; 1283 this.suspendEncoding = false; 1284 this.b4 = new byte[4]; 1285 } 1287 1288 1300 public void write(int theByte) throws java.io.IOException 1301 { 1302 if( suspendEncoding ) 1304 { 1305 super.out.write( theByte ); 1306 return; 1307 } 1309 if( encode ) 1311 { 1312 buffer[ position++ ] = (byte)theByte; 1313 if( position >= bufferLength ) { 1315 out.write( encode3to4( b4, buffer, bufferLength ) ); 1316 1317 lineLength += 4; 1318 if( breakLines && lineLength >= MAX_LINE_LENGTH ) 1319 { 1320 out.write( NEW_LINE ); 1321 lineLength = 0; 1322 } 1324 position = 0; 1325 } } 1328 else 1330 { 1331 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC ) 1333 { 1334 buffer[ position++ ] = (byte)theByte; 1335 if( position >= bufferLength ) { 1337 int len = Base64.decode4to3( buffer, 0, b4, 0 ); 1338 out.write( b4, 0, len ); 1339 position = 0; 1341 } } else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC ) 1344 { 1345 throw new java.io.IOException ( "Invalid character in Base64 data." ); 1346 } } } 1350 1351 1352 1361 public void write( byte[] theBytes, int off, int len ) throws java.io.IOException 1362 { 1363 if( suspendEncoding ) 1365 { 1366 super.out.write( theBytes, off, len ); 1367 return; 1368 } 1370 for( int i = 0; i < len; i++ ) 1371 { 1372 write( theBytes[ off + i ] ); 1373 } 1375 } 1377 1378 1379 1383 public void flushBase64() throws java.io.IOException 1384 { 1385 if( position > 0 ) 1386 { 1387 if( encode ) 1388 { 1389 out.write( encode3to4( b4, buffer, position ) ); 1390 position = 0; 1391 } else 1393 { 1394 throw new java.io.IOException ( "Base64 input not properly padded." ); 1395 } } 1398 } 1400 1401 1406 public void close() throws java.io.IOException 1407 { 1408 flushBase64(); 1410 1411 super.close(); 1414 1415 buffer = null; 1416 out = null; 1417 } 1419 1420 1421 1428 public void suspendEncoding() throws java.io.IOException 1429 { 1430 flushBase64(); 1431 this.suspendEncoding = true; 1432 } 1434 1435 1442 public void resumeEncoding() 1443 { 1444 this.suspendEncoding = false; 1445 } 1447 1448 1449 } 1451 1452} | Popular Tags |