1 package org.apache.ojb.broker.util; 2 3 17 18 import org.apache.ojb.broker.util.logging.LoggerFactory; 19 20 public class Base64 21 { 22 23 24 public final static boolean ENCODE = true; 25 26 27 28 public final static boolean DECODE = false; 29 30 31 32 private final static int MAX_LINE_LENGTH = 76; 33 34 35 36 private final static byte EQUALS_SIGN = (byte)'='; 37 38 39 40 private final static byte NEW_LINE = (byte)'\n'; 41 42 43 44 private final static byte[] ALPHABET = 45 { 46 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', 47 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', 48 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', 49 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', 50 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', 51 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', 52 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', 53 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', 54 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', 55 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' 56 }; 57 58 62 private final static byte[] DECODABET = 63 { 64 -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 95 }; 96 97 private final static byte BAD_ENCODING = -9; private final static byte WHITE_SPACE_ENC = -5; private final static byte EQUALS_SIGN_ENC = -1; 101 102 103 private Base64(){} 104 105 106 107 111 public static void main( String [] args ) 112 { 113 try 114 { 115 { 117 byte[] bytes1 = { (byte)2,(byte)2,(byte)3,(byte)0,(byte)9 }; byte[] bytes2 = { (byte)99,(byte)2,(byte)2,(byte)3,(byte)0,(byte)9 }; 119 System.out.println( "Bytes 2,2,3,0,9 as Base64: " + encodeBytes( bytes1 ) ); 120 System.out.println( "Bytes 2,2,3,0,9 w/ offset: " + encodeBytes( bytes2, 1, bytes2.length-1 ) ); 121 byte[] dbytes = decode( encodeBytes( bytes1 ) ); 122 System.out.print( encodeBytes( bytes1 ) + " decoded: " ); 123 for( int i = 0; i < dbytes.length; i++ ) 124 System.out.print( dbytes[i] + (i<dbytes.length-1?",":"\n") ); 125 } 127 128 129 130 { 132 java.io.FileInputStream fis = new java.io.FileInputStream ( "test.gif.b64" ); 134 Base64.InputStream b64is = new Base64.InputStream( fis, DECODE ); 135 136 byte[] bytes = new byte[0]; 137 int b = -1; 138 while( (b = b64is.read()) >= 0 ){ 139 byte[] temp = new byte[ bytes.length + 1 ]; 140 System.arraycopy( bytes,0, temp,0,bytes.length ); 141 temp[bytes.length] = (byte)b; 142 bytes = temp; 143 } b64is.close(); 145 javax.swing.ImageIcon iicon = new javax.swing.ImageIcon ( bytes ); 146 javax.swing.JLabel jlabel = new javax.swing.JLabel ( "Read from test.gif.b64", iicon,0 ); 147 javax.swing.JFrame jframe = new javax.swing.JFrame (); 148 jframe.getContentPane().add( jlabel ); 149 jframe.pack(); 150 jframe.show(); 151 152 java.io.FileOutputStream fos = new java.io.FileOutputStream ( "test.gif_out" ); 154 fos.write( bytes ); 155 fos.close(); 156 157 fis = new java.io.FileInputStream ( "test.gif_out" ); 159 b64is = new Base64.InputStream( fis, ENCODE ); 160 byte[] ebytes = new byte[0]; 161 b = -1; 162 while( (b = b64is.read()) >= 0 ){ 163 byte[] temp = new byte[ ebytes.length + 1 ]; 164 System.arraycopy( ebytes,0, temp,0,ebytes.length ); 165 temp[ebytes.length] = (byte)b; 166 ebytes = temp; 167 } b64is.close(); 169 String s = new String ( ebytes ); 170 javax.swing.JTextArea jta = new javax.swing.JTextArea ( s ); 171 javax.swing.JScrollPane jsp = new javax.swing.JScrollPane ( jta ); 172 jframe = new javax.swing.JFrame (); 173 jframe.setTitle( "Read from test.gif_out" ); 174 jframe.getContentPane().add( jsp ); 175 jframe.pack(); 176 jframe.show(); 177 178 fos = new java.io.FileOutputStream ( "test.gif.b64_out" ); 180 fos.write( ebytes ); 181 182 fis = new java.io.FileInputStream ( "test.gif.b64_out" ); 184 b64is = new Base64.InputStream( fis, DECODE ); 185 byte[] edbytes = new byte[0]; 186 b = -1; 187 while( (b = b64is.read()) >= 0 ){ 188 byte[] temp = new byte[ edbytes.length + 1 ]; 189 System.arraycopy( edbytes,0, temp,0,edbytes.length ); 190 temp[edbytes.length] = (byte)b; 191 edbytes = temp; 192 } b64is.close(); 194 iicon = new javax.swing.ImageIcon ( edbytes ); 195 jlabel = new javax.swing.JLabel ( "Read from test.gif.b64_out", iicon,0 ); 196 jframe = new javax.swing.JFrame (); 197 jframe.getContentPane().add( jlabel ); 198 jframe.pack(); 199 jframe.show(); 200 } 202 203 { 205 java.io.FileInputStream fis = new java.io.FileInputStream ( "test.gif_out" ); 207 byte[] rbytes = new byte[0]; 208 int b = -1; 209 while( (b = fis.read()) >= 0 ){ 210 byte[] temp = new byte[ rbytes.length + 1 ]; 211 System.arraycopy( rbytes,0, temp,0,rbytes.length ); 212 temp[rbytes.length] = (byte)b; 213 rbytes = temp; 214 } fis.close(); 216 217 java.io.FileOutputStream fos = new java.io.FileOutputStream ("test.gif.b64_out2"); 219 Base64.OutputStream b64os = new Base64.OutputStream( fos, ENCODE ); 220 b64os.write( rbytes ); 221 b64os.close(); 222 223 224 fis = new java.io.FileInputStream ( "test.gif.b64_out2" ); 226 byte[] rebytes = new byte[0]; 227 b = -1; 228 while( (b = fis.read()) >= 0 ){ 229 byte[] temp = new byte[ rebytes.length + 1 ]; 230 System.arraycopy( rebytes,0, temp,0,rebytes.length ); 231 temp[rebytes.length] = (byte)b; 232 rebytes = temp; 233 } fis.close(); 235 String s = new String ( rebytes ); 236 javax.swing.JTextArea jta = new javax.swing.JTextArea ( s ); 237 javax.swing.JScrollPane jsp = new javax.swing.JScrollPane ( jta ); 238 javax.swing.JFrame jframe = new javax.swing.JFrame (); 239 jframe.setTitle( "Read from test.gif.b64_out2" ); 240 jframe.getContentPane().add( jsp ); 241 jframe.pack(); 242 jframe.show(); 243 244 fos = new java.io.FileOutputStream ("test.gif_out2"); 246 b64os = new Base64.OutputStream( fos, DECODE ); 247 b64os.write( rebytes ); 248 b64os.close(); 249 javax.swing.ImageIcon iicon = new javax.swing.ImageIcon ( "test.gif_out2" ); 250 javax.swing.JLabel jlabel = new javax.swing.JLabel ( "Read from test.gif_out2", iicon,0 ); 251 jframe = new javax.swing.JFrame (); 252 jframe.getContentPane().add( jlabel ); 253 jframe.pack(); 254 jframe.show(); 255 256 } 258 259 { 261 java.io.FileInputStream fis = new java.io.FileInputStream ("D:\\temp\\testencoding.txt"); 262 Base64.InputStream b64is = new Base64.InputStream( fis, DECODE ); 263 java.io.FileOutputStream fos = new java.io.FileOutputStream ("D:\\temp\\file.zip"); 264 int b; 265 while( (b=b64is.read()) >= 0 ) 266 fos.write( b ); 267 fos.close(); 268 b64is.close(); 269 270 } 272 } catch( Exception e) 274 { e.printStackTrace(); 275 } 276 } 278 279 280 281 282 290 private static byte[] encode3to4( byte[] threeBytes ) 291 { return encode3to4( threeBytes, 3 ); 292 } 294 295 296 309 private static byte[] encode3to4( byte[] threeBytes, int numSigBytes ) 310 { byte[] dest = new byte[4]; 311 encode3to4( threeBytes, 0, numSigBytes, dest, 0 ); 312 return dest; 313 } 314 315 316 317 338 private static byte[] encode3to4( 339 byte[] source, int srcOffset, int numSigBytes, 340 byte[] destination, int destOffset ) 341 { 342 349 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 ) 354 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 ) 355 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 ); 356 357 switch( numSigBytes ) 358 { 359 case 3: 360 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 361 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 362 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 363 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ]; 364 return destination; 365 366 case 2: 367 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 368 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 369 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 370 destination[ destOffset + 3 ] = EQUALS_SIGN; 371 return destination; 372 373 case 1: 374 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 375 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 376 destination[ destOffset + 2 ] = EQUALS_SIGN; 377 destination[ destOffset + 3 ] = EQUALS_SIGN; 378 return destination; 379 380 default: 381 return destination; 382 } } 385 395 public static String encodeObject( java.io.Serializable serializableObject ) 396 { 397 return encodeObject( serializableObject, true ); 398 } 400 411 public static String encodeObject(java.io.Serializable serializableObject, boolean breakLines) 412 { 413 java.io.ByteArrayOutputStream baos = null; 414 java.io.OutputStream b64os = null; 415 java.io.ObjectOutputStream oos = null; 416 417 try 418 { 419 baos = new java.io.ByteArrayOutputStream (); 420 b64os = new Base64.OutputStream(baos, Base64.ENCODE, breakLines); 421 oos = new java.io.ObjectOutputStream (b64os); 422 423 oos.writeObject(serializableObject); 424 } catch (java.io.IOException e) 426 { 427 e.printStackTrace(); 428 return null; 429 } finally 431 { 432 try 433 { 434 oos.close(); 435 } 436 catch (Exception e) 437 { 438 } 440 try 441 { 442 b64os.close(); 443 } 444 catch (Exception e) 445 { 446 } 448 try 449 { 450 baos.close(); 451 } 452 catch (Exception e) 453 { 454 } 456 } 458 return new String (baos.toByteArray()); 459 } 461 462 470 public static String encodeBytes( byte[] source ) 471 { 472 return encodeBytes( source, true ); 473 } 475 484 public static String encodeBytes( byte[] source, boolean breakLines ) 485 { 486 return encodeBytes( source, 0, source.length, breakLines ); 487 } 489 490 498 public static String encodeBytes( byte[] source, int off, int len ) 499 { 500 return encodeBytes( source, off, len, true ); 501 } 503 504 513 public static String encodeBytes( byte[] source, int off, int len, boolean breakLines ) 514 { 515 int len43 = len * 4 / 3; 516 byte[] outBuff = new byte[ ( len43 ) + ( (len % 3) > 0 ? 4 : 0 ) + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; int d = 0; 520 int e = 0; 521 int len2 = len - 2; 522 int lineLength = 0; 523 for( ; d < len2; d+=3, e+=4 ) 524 { 525 encode3to4( source, d+off, 3, outBuff, e ); 526 527 lineLength += 4; 528 if( breakLines && lineLength == MAX_LINE_LENGTH ) 529 { 530 outBuff[e+4] = NEW_LINE; 531 e++; 532 lineLength = 0; 533 } } 536 if( d < len ) 537 { 538 encode3to4( source, d+off, len - d, outBuff, e ); 539 e += 4; 540 } 542 return new String ( outBuff, 0, e ); 543 } 545 546 554 public static String encodeString( String s ) 555 { 556 return encodeString( s, true ); 557 } 559 568 public static String encodeString( String s, boolean breakLines ) 569 { 570 return encodeBytes( s.getBytes(), breakLines ); 571 } 573 574 575 576 577 578 579 588 private static byte[] decode4to3( byte[] fourBytes ) 589 { 590 byte[] outBuff1 = new byte[3]; 591 int count = decode4to3( fourBytes, 0, outBuff1, 0 ); 592 byte[] outBuff2 = new byte[ count ]; 593 594 System.arraycopy( outBuff1, 0, outBuff2, 0, count ); 595 return outBuff2; 596 } 597 598 599 600 601 623 private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset ) 624 { 625 if( source[ srcOffset + 2] == EQUALS_SIGN ) 627 { 628 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 632 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 ); 633 634 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 635 return 1; 636 } 637 638 else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) 640 { 641 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 646 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 647 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 ); 648 649 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 650 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 ); 651 return 2; 652 } 653 654 else 656 { 657 try{ 658 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 664 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 665 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6) 666 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) ); 667 668 669 destination[ destOffset ] = (byte)( outBuff >> 16 ); 670 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 ); 671 destination[ destOffset + 2 ] = (byte)( outBuff ); 672 673 return 3; 674 }catch( Exception e){ 675 LoggerFactory.getDefaultLogger().error(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) ); 676 LoggerFactory.getDefaultLogger().error(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) ); 677 LoggerFactory.getDefaultLogger().error(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) ); 678 LoggerFactory.getDefaultLogger().error(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) ); 679 return -1; 680 } } 682 } 684 685 686 693 public static byte[] decode( String s ) 694 { 695 byte[] bytes = s.getBytes(); 696 return decode( bytes, 0, bytes.length ); 697 } 699 700 710 public static String decodeToString( String s ) 711 { return new String ( decode( s ) ); 712 } 714 715 723 public static Object decodeToObject(String encodedObject) 724 { 725 byte[] objBytes = decode(encodedObject); 726 727 java.io.ByteArrayInputStream bais = null; 728 java.io.ObjectInputStream ois = null; 729 730 try 731 { 732 bais = new java.io.ByteArrayInputStream (objBytes); 733 ois = new java.io.ObjectInputStream (bais); 734 735 return ois.readObject(); 736 } catch (java.io.IOException e) 738 { 739 e.printStackTrace(); 740 return null; 741 } catch (java.lang.ClassNotFoundException e) 743 { 744 e.printStackTrace(); 745 return null; 746 } finally 748 { 749 try 750 { 751 bais.close(); 752 } 753 catch (Exception e) 754 { 755 } 757 try 758 { 759 ois.close(); 760 } 761 catch (Exception e) 762 { 763 } 765 } } 768 769 779 public static byte[] decode( byte[] source, int off, int len ) 780 { 781 int len34 = len * 3 / 4; 782 byte[] outBuff = new byte[ len34 ]; int outBuffPosn = 0; 784 785 byte[] b4 = new byte[4]; 786 int b4Posn = 0; 787 int i = 0; 788 byte sbiCrop = 0; 789 byte sbiDecode = 0; 790 for( i = 0; i < len; i++ ) 791 { 792 sbiCrop = (byte)(source[i] & 0x7f); sbiDecode = DECODABET[ sbiCrop ]; 794 795 if( sbiDecode >= WHITE_SPACE_ENC ) { 797 if( sbiDecode >= EQUALS_SIGN_ENC ) 798 { 799 b4[ b4Posn++ ] = sbiCrop; 800 if( b4Posn > 3 ) 801 { 802 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn ); 803 b4Posn = 0; 804 805 if( sbiCrop == EQUALS_SIGN ) 807 break; 808 } 810 } 812 } else 814 { 815 LoggerFactory.getDefaultLogger().error( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" ); 816 return null; 817 } } 820 byte[] out = new byte[ outBuffPosn ]; 821 System.arraycopy( outBuff, 0, out, 0, outBuffPosn ); 822 return out; 823 } 825 826 827 828 829 830 831 832 841 public static class InputStream extends java.io.FilterInputStream 842 { 843 private boolean encode; private int position; private byte[] buffer; private int bufferLength; private int numSigBytes; private int lineLength; 849 private boolean breakLines; 851 852 858 public InputStream( java.io.InputStream in ) 859 { 860 this( in, Base64.DECODE ); 861 } 863 864 874 public InputStream( java.io.InputStream in, boolean encode ) 875 { 876 this( in, encode, true ); 877 } 879 880 891 public InputStream( java.io.InputStream in, boolean encode, boolean breakLines ) 892 { 893 super( in ); 894 this.breakLines = breakLines; 895 this.encode = encode; 896 this.bufferLength = encode ? 4 : 3; 897 this.buffer = new byte[ bufferLength ]; 898 this.position = -1; 899 this.lineLength = 0; 900 } 902 909 public int read() throws java.io.IOException 910 { 911 if( position < 0 ) 913 { 914 if( encode ) 915 { 916 byte[] b3 = new byte[3]; 917 int numBinaryBytes = 0; 918 for( int i = 0; i < 3; i++ ) 919 { 920 try 921 { 922 int b = in.read(); 923 924 if( b >= 0 ) 926 { 927 b3[i] = (byte)b; 928 numBinaryBytes++; 929 } 931 } catch( java.io.IOException e ) 933 { 934 if( i == 0 ) 936 throw e; 937 938 } } 941 if( numBinaryBytes > 0 ) 942 { 943 encode3to4( b3, 0, numBinaryBytes, buffer, 0 ); 944 position = 0; 945 numSigBytes = 4; 946 } else 948 { 949 return -1; 950 } } 953 else 955 { 956 byte[] b4 = new byte[4]; 957 int i = 0; 958 for( i = 0; i < 4; i++ ) 959 { 960 int b = 0; 962 do{ b = in.read(); } 963 while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC ); 964 965 if( b < 0 ) 966 break; 968 b4[i] = (byte)b; 969 } 971 if( i == 4 ) 972 { 973 numSigBytes = decode4to3( b4, 0, buffer, 0 ); 974 position = 0; 975 } else if( i == 0 ){ 977 return -1; 978 } else 980 { 981 throw new java.io.IOException ( "Improperly padded Base64 input." ); 983 } 985 } } 988 if( position >= 0 ) 990 { 991 if( position >= numSigBytes ) 993 return -1; 994 995 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH ) 996 { 997 lineLength = 0; 998 return '\n'; 999 } else 1001 { 1002 lineLength++; 1006 int b = buffer[ position++ ]; 1007 1008 if( position >= bufferLength ) 1009 position = -1; 1010 1011 return b & 0xFF; } } 1016 else 1018 { 1019 throw new java.io.IOException ( "Error in Base64 code reading stream." ); 1021 } } 1024 1025 1037 public int read( byte[] dest, int off, int len ) throws java.io.IOException 1038 { 1039 int i; 1040 int b; 1041 for( i = 0; i < len; i++ ) 1042 { 1043 b = read(); 1044 1045 1048 if( b >= 0 ) 1049 dest[off + i] = (byte)b; 1050 else if( i == 0 ) 1051 return -1; 1052 else 1053 break; } return i; 1056 } 1058 } 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1078 public static class OutputStream extends java.io.FilterOutputStream 1079 { 1080 private boolean encode; 1081 private int position; 1082 private byte[] buffer; 1083 private int bufferLength; 1084 private int lineLength; 1085 private boolean breakLines; 1086 1087 1088 1094 public OutputStream( java.io.OutputStream out ) 1095 { 1096 this( out, Base64.ENCODE ); 1097 } 1099 1100 1110 public OutputStream( java.io.OutputStream out, boolean encode ) 1111 { 1112 this( out, encode, true ); 1113 } 1115 1116 1127 public OutputStream( java.io.OutputStream out, boolean encode, boolean breakLines ) 1128 { 1129 super( out ); 1130 this.breakLines = breakLines; 1131 this.encode = encode; 1132 this.bufferLength = encode ? 3 : 4; 1133 this.buffer = new byte[ bufferLength ]; 1134 this.position = 0; 1135 this.lineLength = 0; 1136 } 1138 1139 1151 public void write(int theByte) throws java.io.IOException 1152 { 1153 if( encode ) 1154 { 1155 buffer[ position++ ] = (byte)theByte; 1156 if( position >= bufferLength ) { 1158 out.write( Base64.encode3to4( buffer, bufferLength ) ); 1159 1160 lineLength += 4; 1161 if( breakLines && lineLength >= MAX_LINE_LENGTH ) 1162 { 1163 out.write( NEW_LINE ); 1164 lineLength = 0; 1165 } 1167 position = 0; 1168 } } 1171 else 1173 { 1174 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC ) 1176 { 1177 buffer[ position++ ] = (byte)theByte; 1178 if( position >= bufferLength ) { 1180 out.write( Base64.decode4to3( buffer ) ); 1181 position = 0; 1182 } } else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC ) 1185 { 1186 throw new java.io.IOException ( "Invalid character in Base64 data." ); 1187 } } } 1191 1192 1193 1202 public void write( byte[] theBytes, int off, int len ) throws java.io.IOException 1203 { 1204 for( int i = 0; i < len; i++ ) 1205 { 1206 write( theBytes[ off + i ] ); 1207 } 1209 } 1211 1212 1219 public void flush() throws java.io.IOException 1220 { 1221 super.flush(); 1222 1223 if( position > 0 ) 1224 { 1225 if( encode ) 1226 { 1227 out.write( Base64.encode3to4( buffer, position ) ); 1228 } else 1230 { 1231 throw new java.io.IOException ( "Base64 input not properly padded." ); 1232 } } 1235 out.flush(); 1236 } 1238 1239 1244 public void close() throws java.io.IOException 1245 { 1246 super.close(); 1247 1249 out.close(); 1250 1251 buffer = null; 1252 out = null; 1253 } 1255 } 1257 1258} | Popular Tags |