1 package polyglot.util; 2 3 50 public class Base64 51 { 52 53 54 55 56 57 public final static int NO_OPTIONS = 0; 58 59 60 public final static int ENCODE = 1; 61 62 63 64 public final static int DECODE = 0; 65 66 67 68 public final static int GZIP = 2; 69 70 71 72 public final static int DONT_BREAK_LINES = 8; 73 74 75 76 77 78 79 private final static int MAX_LINE_LENGTH = 76; 80 81 82 83 private final static byte EQUALS_SIGN = (byte)'='; 84 85 86 87 private final static byte NEW_LINE = (byte)'\n'; 88 89 90 91 private final static String PREFERRED_ENCODING = "UTF-8"; 92 93 94 95 private final static byte[] ALPHABET; 96 private final static byte[] _NATIVE_ALPHABET = 97 { 98 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', 99 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', 100 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', 101 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', 102 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', 103 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', 104 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', 105 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', 106 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', 107 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' 108 }; 109 110 111 static 112 { 113 byte[] __bytes; 114 try 115 { 116 __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes( PREFERRED_ENCODING ); 117 } catch (java.io.UnsupportedEncodingException use) 119 { 120 __bytes = _NATIVE_ALPHABET; } ALPHABET = __bytes; 123 } 125 126 130 private final static byte[] DECODABET = 131 { 132 -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 163 }; 164 165 private final static byte WHITE_SPACE_ENC = -5; private final static byte EQUALS_SIGN_ENC = -1; 170 171 172 private Base64(){} 173 174 175 176 177 178 179 194 private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes ) 195 { 196 encode3to4( threeBytes, 0, numSigBytes, b4, 0 ); 197 return b4; 198 } 200 201 222 private static byte[] encode3to4( 223 byte[] source, int srcOffset, int numSigBytes, 224 byte[] destination, int destOffset ) 225 { 226 233 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 ) 238 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 ) 239 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 ); 240 241 switch( numSigBytes ) 242 { 243 case 3: 244 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 245 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 246 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 247 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ]; 248 return destination; 249 250 case 2: 251 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 252 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 253 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 254 destination[ destOffset + 3 ] = EQUALS_SIGN; 255 return destination; 256 257 case 1: 258 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 259 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 260 destination[ destOffset + 2 ] = EQUALS_SIGN; 261 destination[ destOffset + 3 ] = EQUALS_SIGN; 262 return destination; 263 264 default: 265 return destination; 266 } } 269 270 271 282 public static String encodeObject( java.io.Serializable serializableObject ) 283 { 284 return encodeObject( serializableObject, NO_OPTIONS ); 285 } 287 288 289 312 public static String encodeObject( java.io.Serializable serializableObject, int options ) 313 { 314 java.io.ByteArrayOutputStream baos = null; 316 java.io.OutputStream b64os = null; 317 java.io.ObjectOutputStream oos = null; 318 java.util.zip.GZIPOutputStream gzos = null; 319 320 int gzip = (options & GZIP); 322 int dontBreakLines = (options & DONT_BREAK_LINES); 323 324 try 325 { 326 baos = new java.io.ByteArrayOutputStream (); 328 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 329 330 if( gzip == GZIP ) 332 { 333 gzos = new java.util.zip.GZIPOutputStream ( b64os ); 334 oos = new java.io.ObjectOutputStream ( gzos ); 335 } else 337 oos = new java.io.ObjectOutputStream ( b64os ); 338 339 oos.writeObject( serializableObject ); 340 } catch( java.io.IOException e ) 342 { 343 e.printStackTrace(); 344 return null; 345 } finally 347 { 348 try{ oos.close(); } catch( Exception e ){} 349 try{ gzos.close(); } catch( Exception e ){} 350 try{ b64os.close(); } catch( Exception e ){} 351 try{ baos.close(); } catch( Exception e ){} 352 } 354 try 356 { 357 return new String ( baos.toByteArray(), PREFERRED_ENCODING ); 358 } catch (java.io.UnsupportedEncodingException uue) 360 { 361 return new String ( baos.toByteArray() ); 362 } 364 } 366 367 368 375 public static String encodeBytes( byte[] source ) 376 { 377 return encodeBytes( source, 0, source.length, NO_OPTIONS ); 378 } 380 381 401 public static String encodeBytes( byte[] source, int options ) 402 { 403 return encodeBytes( source, 0, source.length, options ); 404 } 406 407 416 public static String encodeBytes( byte[] source, int off, int len ) 417 { 418 return encodeBytes( source, off, len, NO_OPTIONS ); 419 } 421 422 423 445 public static String encodeBytes( byte[] source, int off, int len, int options ) 446 { 447 int dontBreakLines = ( options & DONT_BREAK_LINES ); 449 int gzip = ( options & GZIP ); 450 451 if( gzip == GZIP ) 453 { 454 java.io.ByteArrayOutputStream baos = null; 455 java.util.zip.GZIPOutputStream gzos = null; 456 Base64.OutputStream b64os = null; 457 458 459 try 460 { 461 baos = new java.io.ByteArrayOutputStream (); 463 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 464 gzos = new java.util.zip.GZIPOutputStream ( b64os ); 465 466 gzos.write( source, off, len ); 467 gzos.close(); 468 } catch( java.io.IOException e ) 470 { 471 e.printStackTrace(); 472 return null; 473 } finally 475 { 476 try{ gzos.close(); } catch( Exception e ){} 477 try{ b64os.close(); } catch( Exception e ){} 478 try{ baos.close(); } catch( Exception e ){} 479 } 481 try 483 { 484 return new String ( baos.toByteArray(), PREFERRED_ENCODING ); 485 } catch (java.io.UnsupportedEncodingException uue) 487 { 488 return new String ( baos.toByteArray() ); 489 } } 492 else 494 { 495 boolean breakLines = dontBreakLines == 0; 497 498 int len43 = len * 4 / 3; 499 byte[] outBuff = new byte[ ( len43 ) + ( (len % 3) > 0 ? 4 : 0 ) + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; int d = 0; 503 int e = 0; 504 int len2 = len - 2; 505 int lineLength = 0; 506 for( ; d < len2; d+=3, e+=4 ) 507 { 508 encode3to4( source, d+off, 3, outBuff, e ); 509 510 lineLength += 4; 511 if( breakLines && lineLength == MAX_LINE_LENGTH ) 512 { 513 outBuff[e+4] = NEW_LINE; 514 e++; 515 lineLength = 0; 516 } } 519 if( d < len ) 520 { 521 encode3to4( source, d+off, len - d, outBuff, e ); 522 e += 4; 523 } 525 526 try 528 { 529 return new String ( outBuff, 0, e, PREFERRED_ENCODING ); 530 } catch (java.io.UnsupportedEncodingException uue) 532 { 533 return new String ( outBuff, 0, e ); 534 } 536 } 538 } 540 541 542 543 544 545 546 547 569 private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset ) 570 { 571 if( source[ srcOffset + 2] == EQUALS_SIGN ) 573 { 574 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 578 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 ); 579 580 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 581 return 1; 582 } 583 584 else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) 586 { 587 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 592 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 593 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 ); 594 595 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 596 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 ); 597 return 2; 598 } 599 600 else 602 { 603 try{ 604 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 610 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 611 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6) 612 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) ); 613 614 615 destination[ destOffset ] = (byte)( outBuff >> 16 ); 616 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 ); 617 destination[ destOffset + 2 ] = (byte)( outBuff ); 618 619 return 3; 620 }catch( Exception e){ 621 System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) ); 622 System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) ); 623 System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) ); 624 System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) ); 625 return -1; 626 } } 628 } 630 631 632 633 644 public static byte[] decode( byte[] source, int off, int len ) 645 { 646 int len34 = len * 3 / 4; 647 byte[] outBuff = new byte[ len34 ]; int outBuffPosn = 0; 649 650 byte[] b4 = new byte[4]; 651 int b4Posn = 0; 652 int i = 0; 653 byte sbiCrop = 0; 654 byte sbiDecode = 0; 655 for( i = off; i < off+len; i++ ) 656 { 657 sbiCrop = (byte)(source[i] & 0x7f); sbiDecode = DECODABET[ sbiCrop ]; 659 660 if( sbiDecode >= WHITE_SPACE_ENC ) { 662 if( sbiDecode >= EQUALS_SIGN_ENC ) 663 { 664 b4[ b4Posn++ ] = sbiCrop; 665 if( b4Posn > 3 ) 666 { 667 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn ); 668 b4Posn = 0; 669 670 if( sbiCrop == EQUALS_SIGN ) 672 break; 673 } 675 } 677 } else 679 { 680 System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" ); 681 return null; 682 } } 685 byte[] out = new byte[ outBuffPosn ]; 686 System.arraycopy( outBuff, 0, out, 0, outBuffPosn ); 687 return out; 688 } 690 691 692 693 701 public static byte[] decode( String s ) 702 { 703 byte[] bytes; 704 try 705 { 706 bytes = s.getBytes( PREFERRED_ENCODING ); 707 } catch( java.io.UnsupportedEncodingException uee ) 709 { 710 bytes = s.getBytes(); 711 } 714 bytes = decode( bytes, 0, bytes.length ); 716 717 718 if( bytes != null && bytes.length >= 4 ) 721 { 722 723 int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); 724 if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head ) 725 { 726 java.io.ByteArrayInputStream bais = null; 727 java.util.zip.GZIPInputStream gzis = null; 728 java.io.ByteArrayOutputStream baos = null; 729 byte[] buffer = new byte[2048]; 730 int length = 0; 731 732 try 733 { 734 baos = new java.io.ByteArrayOutputStream (); 735 bais = new java.io.ByteArrayInputStream ( bytes ); 736 gzis = new java.util.zip.GZIPInputStream ( bais ); 737 738 while( ( length = gzis.read( buffer ) ) >= 0 ) 739 { 740 baos.write(buffer,0,length); 741 } 743 bytes = baos.toByteArray(); 745 746 } catch( java.io.IOException e ) 748 { 749 } finally 752 { 753 try{ baos.close(); } catch( Exception e ){} 754 try{ gzis.close(); } catch( Exception e ){} 755 try{ bais.close(); } catch( Exception e ){} 756 } 758 } } 761 return bytes; 762 } 764 765 766 767 775 public static Object decodeToObject( String encodedObject ) 776 { 777 byte[] objBytes = decode( encodedObject ); 779 780 java.io.ByteArrayInputStream bais = null; 781 java.io.ObjectInputStream ois = null; 782 Object obj = null; 783 784 try 785 { 786 bais = new java.io.ByteArrayInputStream ( objBytes ); 787 ois = new java.io.ObjectInputStream ( bais ); 788 789 obj = ois.readObject(); 790 } catch( java.io.IOException e ) 792 { 793 e.printStackTrace(); 794 obj = null; 795 } catch( java.lang.ClassNotFoundException e ) 797 { 798 e.printStackTrace(); 799 obj = null; 800 } finally 802 { 803 try{ bais.close(); } catch( Exception e ){} 804 try{ ois.close(); } catch( Exception e ){} 805 } 807 return obj; 808 } 810 811 812 821 public static boolean encodeToFile( byte[] dataToEncode, String filename ) 822 { 823 boolean success = false; 824 Base64.OutputStream bos = null; 825 try 826 { 827 bos = new Base64.OutputStream( 828 new java.io.FileOutputStream ( filename ), Base64.ENCODE ); 829 bos.write( dataToEncode ); 830 success = true; 831 } catch( java.io.IOException e ) 833 { 834 835 success = false; 836 } finally 838 { 839 try{ bos.close(); } catch( Exception e ){} 840 } 842 return success; 843 } 845 846 855 public static boolean decodeToFile( String dataToDecode, String filename ) 856 { 857 boolean success = false; 858 Base64.OutputStream bos = null; 859 try 860 { 861 bos = new Base64.OutputStream( 862 new java.io.FileOutputStream ( filename ), Base64.DECODE ); 863 bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) ); 864 success = true; 865 } catch( java.io.IOException e ) 867 { 868 success = false; 869 } finally 871 { 872 try{ bos.close(); } catch( Exception e ){} 873 } 875 return success; 876 } 878 879 880 881 890 public static byte[] decodeFromFile( String filename ) 891 { 892 byte[] decodedData = null; 893 Base64.InputStream bis = null; 894 try 895 { 896 java.io.File file = new java.io.File ( filename ); 898 byte[] buffer = null; 899 int length = 0; 900 int numBytes = 0; 901 902 if( file.length() > Integer.MAX_VALUE ) 904 { 905 System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." ); 906 return null; 907 } buffer = new byte[ (int)file.length() ]; 909 910 bis = new Base64.InputStream( 912 new java.io.BufferedInputStream ( 913 new java.io.FileInputStream ( file ) ), Base64.DECODE ); 914 915 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 917 length += numBytes; 918 919 decodedData = new byte[ length ]; 921 System.arraycopy( buffer, 0, decodedData, 0, length ); 922 923 } catch( java.io.IOException e ) 925 { 926 System.err.println( "Error decoding from file " + filename ); 927 } finally 929 { 930 try{ bis.close(); } catch( Exception e) {} 931 } 933 return decodedData; 934 } 936 937 938 947 public static String encodeFromFile( String filename ) 948 { 949 String encodedData = null; 950 Base64.InputStream bis = null; 951 try 952 { 953 java.io.File file = new java.io.File ( filename ); 955 byte[] buffer = new byte[ (int)(file.length() * 1.4) ]; 956 int length = 0; 957 int numBytes = 0; 958 959 bis = new Base64.InputStream( 961 new java.io.BufferedInputStream ( 962 new java.io.FileInputStream ( file ) ), Base64.ENCODE ); 963 964 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 966 length += numBytes; 967 968 encodedData = new String ( buffer, 0, length, Base64.PREFERRED_ENCODING ); 970 971 } catch( java.io.IOException e ) 973 { 974 System.err.println( "Error encoding from file " + filename ); 975 } finally 977 { 978 try{ bis.close(); } catch( Exception e) {} 979 } 981 return encodedData; 982 } 984 985 986 994 public static char[] encode(byte[] data) 995 { 996 return encodeBytes(data).toCharArray(); 997 } 998 999 1000 1009 public static byte[] decode(char[] data) 1010 { 1011 return decode(new String (data)); 1012 } 1013 1014 1015 1016 1017 1018 1019 1020 1028 public static class InputStream extends java.io.FilterInputStream 1029 { 1030 private boolean encode; private int position; private byte[] buffer; private int bufferLength; private int numSigBytes; private int lineLength; 1036 private boolean breakLines; 1038 1039 1045 public InputStream( java.io.InputStream in ) 1046 { 1047 this( in, DECODE ); 1048 } 1050 1051 1072 public InputStream( java.io.InputStream in, int options ) 1073 { 1074 super( in ); 1075 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES; 1076 this.encode = (options & ENCODE) == ENCODE; 1077 this.bufferLength = encode ? 4 : 3; 1078 this.buffer = new byte[ bufferLength ]; 1079 this.position = -1; 1080 this.lineLength = 0; 1081 } 1083 1090 public int read() throws java.io.IOException 1091 { 1092 if( position < 0 ) 1094 { 1095 if( encode ) 1096 { 1097 byte[] b3 = new byte[3]; 1098 int numBinaryBytes = 0; 1099 for( int i = 0; i < 3; i++ ) 1100 { 1101 try 1102 { 1103 int b = in.read(); 1104 1105 if( b >= 0 ) 1107 { 1108 b3[i] = (byte)b; 1109 numBinaryBytes++; 1110 } 1112 } catch( java.io.IOException e ) 1114 { 1115 if( i == 0 ) 1117 throw e; 1118 1119 } } 1122 if( numBinaryBytes > 0 ) 1123 { 1124 encode3to4( b3, 0, numBinaryBytes, buffer, 0 ); 1125 position = 0; 1126 numSigBytes = 4; 1127 } else 1129 { 1130 return -1; 1131 } } 1134 else 1136 { 1137 byte[] b4 = new byte[4]; 1138 int i = 0; 1139 for( i = 0; i < 4; i++ ) 1140 { 1141 int b = 0; 1143 do{ b = in.read(); } 1144 while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC ); 1145 1146 if( b < 0 ) 1147 break; 1149 b4[i] = (byte)b; 1150 } 1152 if( i == 4 ) 1153 { 1154 numSigBytes = decode4to3( b4, 0, buffer, 0 ); 1155 position = 0; 1156 } else if( i == 0 ){ 1158 return -1; 1159 } else 1161 { 1162 throw new java.io.IOException ( "Improperly padded Base64 input." ); 1164 } 1166 } } 1169 if( position >= 0 ) 1171 { 1172 if( position >= numSigBytes ) 1174 return -1; 1175 1176 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH ) 1177 { 1178 lineLength = 0; 1179 return '\n'; 1180 } else 1182 { 1183 lineLength++; 1187 int b = buffer[ position++ ]; 1188 1189 if( position >= bufferLength ) 1190 position = -1; 1191 1192 return b & 0xFF; } } 1197 else 1199 { 1200 throw new java.io.IOException ( "Error in Base64 code reading stream." ); 1202 } } 1205 1206 1218 public int read( byte[] dest, int off, int len ) throws java.io.IOException 1219 { 1220 int i; 1221 int b; 1222 for( i = 0; i < len; i++ ) 1223 { 1224 b = read(); 1225 1226 1229 if( b >= 0 ) 1230 dest[off + i] = (byte)b; 1231 else if( i == 0 ) 1232 return -1; 1233 else 1234 break; } return i; 1237 } 1239 } 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1258 public static class OutputStream extends java.io.FilterOutputStream 1259 { 1260 private boolean encode; 1261 private int position; 1262 private byte[] buffer; 1263 private int bufferLength; 1264 private int lineLength; 1265 private boolean breakLines; 1266 private byte[] b4; private boolean suspendEncoding; 1268 1269 1275 public OutputStream( java.io.OutputStream out ) 1276 { 1277 this( out, ENCODE ); 1278 } 1280 1281 1301 public OutputStream( java.io.OutputStream out, int options ) 1302 { 1303 super( out ); 1304 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES; 1305 this.encode = (options & ENCODE) == ENCODE; 1306 this.bufferLength = encode ? 3 : 4; 1307 this.buffer = new byte[ bufferLength ]; 1308 this.position = 0; 1309 this.lineLength = 0; 1310 this.suspendEncoding = false; 1311 this.b4 = new byte[4]; 1312 } 1314 1315 1327 public void write(int theByte) throws java.io.IOException 1328 { 1329 if( suspendEncoding ) 1331 { 1332 super.out.write( theByte ); 1333 return; 1334 } 1336 if( encode ) 1338 { 1339 buffer[ position++ ] = (byte)theByte; 1340 if( position >= bufferLength ) { 1342 out.write( encode3to4( b4, buffer, bufferLength ) ); 1343 1344 lineLength += 4; 1345 if( breakLines && lineLength >= MAX_LINE_LENGTH ) 1346 { 1347 out.write( NEW_LINE ); 1348 lineLength = 0; 1349 } 1351 position = 0; 1352 } } 1355 else 1357 { 1358 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC ) 1360 { 1361 buffer[ position++ ] = (byte)theByte; 1362 if( position >= bufferLength ) { 1364 int len = Base64.decode4to3( buffer, 0, b4, 0 ); 1365 out.write( b4, 0, len ); 1366 position = 0; 1368 } } else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC ) 1371 { 1372 throw new java.io.IOException ( "Invalid character in Base64 data." ); 1373 } } } 1377 1378 1379 1388 public void write( byte[] theBytes, int off, int len ) throws java.io.IOException 1389 { 1390 if( suspendEncoding ) 1392 { 1393 super.out.write( theBytes, off, len ); 1394 return; 1395 } 1397 for( int i = 0; i < len; i++ ) 1398 { 1399 write( theBytes[ off + i ] ); 1400 } 1402 } 1404 1405 1406 1410 public void flushBase64() throws java.io.IOException 1411 { 1412 if( position > 0 ) 1413 { 1414 if( encode ) 1415 { 1416 out.write( encode3to4( b4, buffer, position ) ); 1417 position = 0; 1418 } else 1420 { 1421 throw new java.io.IOException ( "Base64 input not properly padded." ); 1422 } } 1425 } 1427 1428 1433 public void close() throws java.io.IOException 1434 { 1435 flushBase64(); 1437 1438 super.close(); 1441 1442 buffer = null; 1443 out = null; 1444 } 1446 1447 1448 1455 public void suspendEncoding() throws java.io.IOException 1456 { 1457 flushBase64(); 1458 this.suspendEncoding = true; 1459 } 1461 1462 1469 public void resumeEncoding() 1470 { 1471 this.suspendEncoding = false; 1472 } 1474 1475 1476 } 1478 1479} | Popular Tags |