00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 public class Base64
00049 {
00050
00051
00052
00053
00054
00055 public final static int NO_OPTIONS = 0;
00056
00057
00058 public final static int ENCODE = 1;
00059
00060
00061
00062 public final static int DECODE = 0;
00063
00064
00065
00066 public final static int GZIP = 2;
00067
00068
00069
00070 public final static int DONT_BREAK_LINES = 8;
00071
00072
00073
00074
00075
00076
00077 private final static int MAX_LINE_LENGTH = 76;
00078
00079
00080
00081 private final static byte EQUALS_SIGN = (byte)'=';
00082
00083
00084
00085 private final static byte NEW_LINE = (byte)'\n';
00086
00087
00088
00089 private final static String PREFERRED_ENCODING = "UTF-8";
00090
00091
00092
00093 private final static byte[] ALPHABET;
00094 private final static byte[] _NATIVE_ALPHABET =
00095 {
00096 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
00097 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
00098 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
00099 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
00100 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
00101 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
00102 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
00103 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
00104 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
00105 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
00106 };
00107
00108
00109 static
00110 {
00111 byte[] __bytes;
00112 try
00113 {
00114 __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes( PREFERRED_ENCODING );
00115 }
00116 catch (java.io.UnsupportedEncodingException use)
00117 {
00118 __bytes = _NATIVE_ALPHABET;
00119 }
00120 ALPHABET = __bytes;
00121 }
00122
00123
00124
00125
00126
00127
00128 private final static byte[] DECODABET =
00129 {
00130 -9,-9,-9,-9,-9,-9,-9,-9,-9,
00131 -5,-5,
00132 -9,-9,
00133 -5,
00134 -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,
00135 -9,-9,-9,-9,-9,
00136 -5,
00137 -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,
00138 62,
00139 -9,-9,-9,
00140 63,
00141 52,53,54,55,56,57,58,59,60,61,
00142 -9,-9,-9,
00143 -1,
00144 -9,-9,-9,
00145 0,1,2,3,4,5,6,7,8,9,10,11,12,13,
00146 14,15,16,17,18,19,20,21,22,23,24,25,
00147 -9,-9,-9,-9,-9,-9,
00148 26,27,28,29,30,31,32,33,34,35,36,37,38,
00149 39,40,41,42,43,44,45,46,47,48,49,50,51,
00150 -9,-9,-9,-9
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 };
00162
00163
00164
00165 private final static byte WHITE_SPACE_ENC = -5;
00166 private final static byte EQUALS_SIGN_ENC = -1;
00167
00168
00169
00170 private Base64(){}
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes )
00193 {
00194 encode3to4( threeBytes, 0, numSigBytes, b4, 0 );
00195 return b4;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 private static byte[] encode3to4(
00221 byte[] source, int srcOffset, int numSigBytes,
00222 byte[] destination, int destOffset )
00223 {
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
00236 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
00237 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
00238
00239 switch( numSigBytes )
00240 {
00241 case 3:
00242 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
00243 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
00244 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
00245 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
00246 return destination;
00247
00248 case 2:
00249 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
00250 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
00251 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
00252 destination[ destOffset + 3 ] = EQUALS_SIGN;
00253 return destination;
00254
00255 case 1:
00256 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
00257 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
00258 destination[ destOffset + 2 ] = EQUALS_SIGN;
00259 destination[ destOffset + 3 ] = EQUALS_SIGN;
00260 return destination;
00261
00262 default:
00263 return destination;
00264 }
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 public static String encodeObject( java.io.Serializable serializableObject )
00281 {
00282 return encodeObject( serializableObject, NO_OPTIONS );
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 public static String encodeObject( java.io.Serializable serializableObject, int options )
00311 {
00312
00313 java.io.ByteArrayOutputStream baos = null;
00314 java.io.OutputStream b64os = null;
00315 java.io.ObjectOutputStream oos = null;
00316 java.util.zip.GZIPOutputStream gzos = null;
00317
00318
00319 int gzip = (options & GZIP);
00320 int dontBreakLines = (options & DONT_BREAK_LINES);
00321
00322 try
00323 {
00324
00325 baos = new java.io.ByteArrayOutputStream();
00326 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines );
00327
00328
00329 if( gzip == GZIP )
00330 {
00331 gzos = new java.util.zip.GZIPOutputStream( b64os );
00332 oos = new java.io.ObjectOutputStream( gzos );
00333 }
00334 else
00335 oos = new java.io.ObjectOutputStream( b64os );
00336
00337 oos.writeObject( serializableObject );
00338 }
00339 catch( java.io.IOException e )
00340 {
00341 e.printStackTrace();
00342 return null;
00343 }
00344 finally
00345 {
00346 try{ oos.close(); } catch( Exception e ){}
00347 try{ gzos.close(); } catch( Exception e ){}
00348 try{ b64os.close(); } catch( Exception e ){}
00349 try{ baos.close(); } catch( Exception e ){}
00350 }
00351
00352
00353 try
00354 {
00355 return new String( baos.toByteArray(), PREFERRED_ENCODING );
00356 }
00357 catch (java.io.UnsupportedEncodingException uue)
00358 {
00359 return new String( baos.toByteArray() );
00360 }
00361
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 public static String encodeBytes( byte[] source )
00374 {
00375 return encodeBytes( source, 0, source.length, NO_OPTIONS );
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 public static String encodeBytes( byte[] source, int options )
00401 {
00402 return encodeBytes( source, 0, source.length, options );
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 public static String encodeBytes( byte[] source, int off, int len )
00416 {
00417 return encodeBytes( source, off, len, NO_OPTIONS );
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 public static String encodeBytes( byte[] source, int off, int len, int options )
00445 {
00446
00447 int dontBreakLines = ( options & DONT_BREAK_LINES );
00448 int gzip = ( options & GZIP );
00449
00450
00451 if( gzip == GZIP )
00452 {
00453 java.io.ByteArrayOutputStream baos = null;
00454 java.util.zip.GZIPOutputStream gzos = null;
00455 Base64.OutputStream b64os = null;
00456
00457
00458 try
00459 {
00460
00461 baos = new java.io.ByteArrayOutputStream();
00462 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines );
00463 gzos = new java.util.zip.GZIPOutputStream( b64os );
00464
00465 gzos.write( source, off, len );
00466 gzos.close();
00467 }
00468 catch( java.io.IOException e )
00469 {
00470 e.printStackTrace();
00471 return null;
00472 }
00473 finally
00474 {
00475 try{ gzos.close(); } catch( Exception e ){}
00476 try{ b64os.close(); } catch( Exception e ){}
00477 try{ baos.close(); } catch( Exception e ){}
00478 }
00479
00480
00481 try
00482 {
00483 return new String( baos.toByteArray(), PREFERRED_ENCODING );
00484 }
00485 catch (java.io.UnsupportedEncodingException uue)
00486 {
00487 return new String( baos.toByteArray() );
00488 }
00489 }
00490
00491
00492 else
00493 {
00494
00495 boolean breakLines = dontBreakLines == 0;
00496
00497 int len43 = len * 4 / 3;
00498 byte[] outBuff = new byte[ ( len43 )
00499 + ( (len % 3) > 0 ? 4 : 0 )
00500 + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ];
00501 int d = 0;
00502 int e = 0;
00503 int len2 = len - 2;
00504 int lineLength = 0;
00505 for( ; d < len2; d+=3, e+=4 )
00506 {
00507 encode3to4( source, d+off, 3, outBuff, e );
00508
00509 lineLength += 4;
00510 if( breakLines && lineLength == MAX_LINE_LENGTH )
00511 {
00512 outBuff[e+4] = NEW_LINE;
00513 e++;
00514 lineLength = 0;
00515 }
00516 }
00517
00518 if( d < len )
00519 {
00520 encode3to4( source, d+off, len - d, outBuff, e );
00521 e += 4;
00522 }
00523
00524
00525
00526 try
00527 {
00528 return new String( outBuff, 0, e, PREFERRED_ENCODING );
00529 }
00530 catch (java.io.UnsupportedEncodingException uue)
00531 {
00532 return new String( outBuff, 0, e );
00533 }
00534
00535 }
00536
00537 }
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset )
00569 {
00570
00571 if( source[ srcOffset + 2] == EQUALS_SIGN )
00572 {
00573
00574
00575
00576 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
00577 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 );
00578
00579 destination[ destOffset ] = (byte)( outBuff >>> 16 );
00580 return 1;
00581 }
00582
00583
00584 else if( source[ srcOffset + 3 ] == EQUALS_SIGN )
00585 {
00586
00587
00588
00589
00590 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
00591 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
00592 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 );
00593
00594 destination[ destOffset ] = (byte)( outBuff >>> 16 );
00595 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 );
00596 return 2;
00597 }
00598
00599
00600 else
00601 {
00602 try{
00603
00604
00605
00606
00607
00608 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
00609 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
00610 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6)
00611 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) );
00612
00613
00614 destination[ destOffset ] = (byte)( outBuff >> 16 );
00615 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 );
00616 destination[ destOffset + 2 ] = (byte)( outBuff );
00617
00618 return 3;
00619 }catch( Exception e){
00620 System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) );
00621 System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) );
00622 System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) );
00623 System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) );
00624 return -1;
00625 }
00626 }
00627 }
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 public static byte[] decode( byte[] source, int off, int len )
00644 {
00645 int len34 = len * 3 / 4;
00646 byte[] outBuff = new byte[ len34 ];
00647 int outBuffPosn = 0;
00648
00649 byte[] b4 = new byte[4];
00650 int b4Posn = 0;
00651 int i = 0;
00652 byte sbiCrop = 0;
00653 byte sbiDecode = 0;
00654 for( i = off; i < off+len; i++ )
00655 {
00656 sbiCrop = (byte)(source[i] & 0x7f);
00657 sbiDecode = DECODABET[ sbiCrop ];
00658
00659 if( sbiDecode >= WHITE_SPACE_ENC )
00660 {
00661 if( sbiDecode >= EQUALS_SIGN_ENC )
00662 {
00663 b4[ b4Posn++ ] = sbiCrop;
00664 if( b4Posn > 3 )
00665 {
00666 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn );
00667 b4Posn = 0;
00668
00669
00670 if( sbiCrop == EQUALS_SIGN )
00671 break;
00672 }
00673
00674 }
00675
00676 }
00677 else
00678 {
00679 System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" );
00680 return null;
00681 }
00682 }
00683
00684 byte[] out = new byte[ outBuffPosn ];
00685 System.arraycopy( outBuff, 0, out, 0, outBuffPosn );
00686 return out;
00687 }
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 public static byte[] decode( String s )
00701 {
00702 byte[] bytes;
00703 try
00704 {
00705 bytes = s.getBytes( PREFERRED_ENCODING );
00706 }
00707 catch( java.io.UnsupportedEncodingException uee )
00708 {
00709 bytes = s.getBytes();
00710 }
00711
00712
00713
00714 bytes = decode( bytes, 0, bytes.length );
00715
00716
00717
00718
00719 if( bytes != null && bytes.length >= 4 )
00720 {
00721
00722 int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
00723 if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head )
00724 {
00725 java.io.ByteArrayInputStream bais = null;
00726 java.util.zip.GZIPInputStream gzis = null;
00727 java.io.ByteArrayOutputStream baos = null;
00728 byte[] buffer = new byte[2048];
00729 int length = 0;
00730
00731 try
00732 {
00733 baos = new java.io.ByteArrayOutputStream();
00734 bais = new java.io.ByteArrayInputStream( bytes );
00735 gzis = new java.util.zip.GZIPInputStream( bais );
00736
00737 while( ( length = gzis.read( buffer ) ) >= 0 )
00738 {
00739 baos.write(buffer,0,length);
00740 }
00741
00742
00743 bytes = baos.toByteArray();
00744
00745 }
00746 catch( java.io.IOException e )
00747 {
00748
00749 }
00750 finally
00751 {
00752 try{ baos.close(); } catch( Exception e ){}
00753 try{ gzis.close(); } catch( Exception e ){}
00754 try{ bais.close(); } catch( Exception e ){}
00755 }
00756
00757 }
00758 }
00759
00760 return bytes;
00761 }
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774 public static Object decodeToObject( String encodedObject )
00775 {
00776
00777 byte[] objBytes = decode( encodedObject );
00778
00779 java.io.ByteArrayInputStream bais = null;
00780 java.io.ObjectInputStream ois = null;
00781 Object obj = null;
00782
00783 try
00784 {
00785 bais = new java.io.ByteArrayInputStream( objBytes );
00786 ois = new java.io.ObjectInputStream( bais );
00787
00788 obj = ois.readObject();
00789 }
00790 catch( java.io.IOException e )
00791 {
00792 e.printStackTrace();
00793 obj = null;
00794 }
00795 catch( java.lang.ClassNotFoundException e )
00796 {
00797 e.printStackTrace();
00798 obj = null;
00799 }
00800 finally
00801 {
00802 try{ bais.close(); } catch( Exception e ){}
00803 try{ ois.close(); } catch( Exception e ){}
00804 }
00805
00806 return obj;
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 public static boolean encodeToFile( byte[] dataToEncode, String filename )
00821 {
00822 boolean success = false;
00823 Base64.OutputStream bos = null;
00824 try
00825 {
00826 bos = new Base64.OutputStream(
00827 new java.io.FileOutputStream( filename ), Base64.ENCODE );
00828 bos.write( dataToEncode );
00829 success = true;
00830 }
00831 catch( java.io.IOException e )
00832 {
00833
00834 success = false;
00835 }
00836 finally
00837 {
00838 try{ bos.close(); } catch( Exception e ){}
00839 }
00840
00841 return success;
00842 }
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 public static boolean decodeToFile( String dataToDecode, String filename )
00855 {
00856 boolean success = false;
00857 Base64.OutputStream bos = null;
00858 try
00859 {
00860 bos = new Base64.OutputStream(
00861 new java.io.FileOutputStream( filename ), Base64.DECODE );
00862 bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
00863 success = true;
00864 }
00865 catch( java.io.IOException e )
00866 {
00867 success = false;
00868 }
00869 finally
00870 {
00871 try{ bos.close(); } catch( Exception e ){}
00872 }
00873
00874 return success;
00875 }
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889 public static byte[] decodeFromFile( String filename )
00890 {
00891 byte[] decodedData = null;
00892 Base64.InputStream bis = null;
00893 try
00894 {
00895
00896 java.io.File file = new java.io.File( filename );
00897 byte[] buffer = null;
00898 int length = 0;
00899 int numBytes = 0;
00900
00901
00902 if( file.length() > Integer.MAX_VALUE )
00903 {
00904 System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." );
00905 return null;
00906 }
00907 buffer = new byte[ (int)file.length() ];
00908
00909
00910 bis = new Base64.InputStream(
00911 new java.io.BufferedInputStream(
00912 new java.io.FileInputStream( file ) ), Base64.DECODE );
00913
00914
00915 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
00916 length += numBytes;
00917
00918
00919 decodedData = new byte[ length ];
00920 System.arraycopy( buffer, 0, decodedData, 0, length );
00921
00922 }
00923 catch( java.io.IOException e )
00924 {
00925 System.err.println( "Error decoding from file " + filename );
00926 }
00927 finally
00928 {
00929 try{ bis.close(); } catch( Exception e) {}
00930 }
00931
00932 return decodedData;
00933 }
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946 public static String encodeFromFile( String filename )
00947 {
00948 String encodedData = null;
00949 Base64.InputStream bis = null;
00950 try
00951 {
00952
00953 java.io.File file = new java.io.File( filename );
00954 byte[] buffer = new byte[ (int)(file.length() * 1.4) ];
00955 int length = 0;
00956 int numBytes = 0;
00957
00958
00959 bis = new Base64.InputStream(
00960 new java.io.BufferedInputStream(
00961 new java.io.FileInputStream( file ) ), Base64.ENCODE );
00962
00963
00964 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
00965 length += numBytes;
00966
00967
00968 encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
00969
00970 }
00971 catch( java.io.IOException e )
00972 {
00973 System.err.println( "Error encoding from file " + filename );
00974 }
00975 finally
00976 {
00977 try{ bis.close(); } catch( Exception e) {}
00978 }
00979
00980 return encodedData;
00981 }
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 public static class InputStream extends java.io.FilterInputStream
00999 {
01000 private boolean encode;
01001 private int position;
01002 private byte[] buffer;
01003 private int bufferLength;
01004 private int numSigBytes;
01005 private int lineLength;
01006 private boolean breakLines;
01007
01008
01009
01010
01011
01012
01013
01014
01015 public InputStream( java.io.InputStream in )
01016 {
01017 this( in, DECODE );
01018 }
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042 public InputStream( java.io.InputStream in, int options )
01043 {
01044 super( in );
01045 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
01046 this.encode = (options & ENCODE) == ENCODE;
01047 this.bufferLength = encode ? 4 : 3;
01048 this.buffer = new byte[ bufferLength ];
01049 this.position = -1;
01050 this.lineLength = 0;
01051 }
01052
01053
01054
01055
01056
01057
01058
01059
01060 public int read() throws java.io.IOException
01061 {
01062
01063 if( position < 0 )
01064 {
01065 if( encode )
01066 {
01067 byte[] b3 = new byte[3];
01068 int numBinaryBytes = 0;
01069 for( int i = 0; i < 3; i++ )
01070 {
01071 try
01072 {
01073 int b = in.read();
01074
01075
01076 if( b >= 0 )
01077 {
01078 b3[i] = (byte)b;
01079 numBinaryBytes++;
01080 }
01081
01082 }
01083 catch( java.io.IOException e )
01084 {
01085
01086 if( i == 0 )
01087 throw e;
01088
01089 }
01090 }
01091
01092 if( numBinaryBytes > 0 )
01093 {
01094 encode3to4( b3, 0, numBinaryBytes, buffer, 0 );
01095 position = 0;
01096 numSigBytes = 4;
01097 }
01098 else
01099 {
01100 return -1;
01101 }
01102 }
01103
01104
01105 else
01106 {
01107 byte[] b4 = new byte[4];
01108 int i = 0;
01109 for( i = 0; i < 4; i++ )
01110 {
01111
01112 int b = 0;
01113 do{ b = in.read(); }
01114 while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC );
01115
01116 if( b < 0 )
01117 break;
01118
01119 b4[i] = (byte)b;
01120 }
01121
01122 if( i == 4 )
01123 {
01124 numSigBytes = decode4to3( b4, 0, buffer, 0 );
01125 position = 0;
01126 }
01127 else if( i == 0 ){
01128 return -1;
01129 }
01130 else
01131 {
01132
01133 throw new java.io.IOException( "Improperly padded Base64 input." );
01134 }
01135
01136 }
01137 }
01138
01139
01140 if( position >= 0 )
01141 {
01142
01143 if( position >= numSigBytes )
01144 return -1;
01145
01146 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH )
01147 {
01148 lineLength = 0;
01149 return '\n';
01150 }
01151 else
01152 {
01153 lineLength++;
01154
01155
01156
01157 int b = buffer[ position++ ];
01158
01159 if( position >= bufferLength )
01160 position = -1;
01161
01162 return b & 0xFF;
01163
01164 }
01165 }
01166
01167
01168 else
01169 {
01170
01171 throw new java.io.IOException( "Error in Base64 code reading stream." );
01172 }
01173 }
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188 public int read( byte[] dest, int off, int len ) throws java.io.IOException
01189 {
01190 int i;
01191 int b;
01192 for( i = 0; i < len; i++ )
01193 {
01194 b = read();
01195
01196
01197
01198
01199 if( b >= 0 )
01200 dest[off + i] = (byte)b;
01201 else if( i == 0 )
01202 return -1;
01203 else
01204 break;
01205 }
01206 return i;
01207 }
01208
01209 }
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228 public static class OutputStream extends java.io.FilterOutputStream
01229 {
01230 private boolean encode;
01231 private int position;
01232 private byte[] buffer;
01233 private int bufferLength;
01234 private int lineLength;
01235 private boolean breakLines;
01236 private byte[] b4;
01237 private boolean suspendEncoding;
01238
01239
01240
01241
01242
01243
01244
01245 public OutputStream( java.io.OutputStream out )
01246 {
01247 this( out, ENCODE );
01248 }
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271 public OutputStream( java.io.OutputStream out, int options )
01272 {
01273 super( out );
01274 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
01275 this.encode = (options & ENCODE) == ENCODE;
01276 this.bufferLength = encode ? 3 : 4;
01277 this.buffer = new byte[ bufferLength ];
01278 this.position = 0;
01279 this.lineLength = 0;
01280 this.suspendEncoding = false;
01281 this.b4 = new byte[4];
01282 }
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297 public void write(int theByte) throws java.io.IOException
01298 {
01299
01300 if( suspendEncoding )
01301 {
01302 super.out.write( theByte );
01303 return;
01304 }
01305
01306
01307 if( encode )
01308 {
01309 buffer[ position++ ] = (byte)theByte;
01310 if( position >= bufferLength )
01311 {
01312 out.write( encode3to4( b4, buffer, bufferLength ) );
01313
01314 lineLength += 4;
01315 if( breakLines && lineLength >= MAX_LINE_LENGTH )
01316 {
01317 out.write( NEW_LINE );
01318 lineLength = 0;
01319 }
01320
01321 position = 0;
01322 }
01323 }
01324
01325
01326 else
01327 {
01328
01329 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC )
01330 {
01331 buffer[ position++ ] = (byte)theByte;
01332 if( position >= bufferLength )
01333 {
01334 int len = Base64.decode4to3( buffer, 0, b4, 0 );
01335 out.write( b4, 0, len );
01336
01337 position = 0;
01338 }
01339 }
01340 else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC )
01341 {
01342 throw new java.io.IOException( "Invalid character in Base64 data." );
01343 }
01344 }
01345 }
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358 public void write( byte[] theBytes, int off, int len ) throws java.io.IOException
01359 {
01360
01361 if( suspendEncoding )
01362 {
01363 super.out.write( theBytes, off, len );
01364 return;
01365 }
01366
01367 for( int i = 0; i < len; i++ )
01368 {
01369 write( theBytes[ off + i ] );
01370 }
01371
01372 }
01373
01374
01375
01376
01377
01378
01379
01380 public void flushBase64() throws java.io.IOException
01381 {
01382 if( position > 0 )
01383 {
01384 if( encode )
01385 {
01386 out.write( encode3to4( b4, buffer, position ) );
01387 position = 0;
01388 }
01389 else
01390 {
01391 throw new java.io.IOException( "Base64 input not properly padded." );
01392 }
01393 }
01394
01395 }
01396
01397
01398
01399
01400
01401
01402
01403 public void close() throws java.io.IOException
01404 {
01405
01406 flushBase64();
01407
01408
01409
01410 super.close();
01411
01412 buffer = null;
01413 out = null;
01414 }
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425 public void suspendEncoding() throws java.io.IOException
01426 {
01427 flushBase64();
01428 this.suspendEncoding = true;
01429 }
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439 public void resumeEncoding()
01440 {
01441 this.suspendEncoding = false;
01442 }
01443
01444
01445
01446 }
01447
01448
01449 }