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
26 package org.jclouds.encryption.internal;
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class Base64
77 {
78
79
80
81
82
83 public final static int NO_OPTIONS = 0;
84
85
86 public final static int ENCODE = 1;
87
88
89
90 public final static int DECODE = 0;
91
92
93
94 public final static int GZIP = 2;
95
96
97
98 public final static int DONT_BREAK_LINES = 8;
99
100
101
102
103
104
105 private final static int MAX_LINE_LENGTH = 76;
106
107
108
109 private final static byte EQUALS_SIGN = (byte)'=';
110
111
112
113 private final static byte NEW_LINE = (byte)'\n';
114
115
116
117 private final static String PREFERRED_ENCODING = "UTF-8";
118
119
120
121 private final static byte[] ALPHABET;
122 private final static byte[] _NATIVE_ALPHABET =
123 {
124 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
125 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
126 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
127 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
128 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
129 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
130 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
131 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
132 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
133 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
134 };
135
136
137 static
138 {
139 byte[] __bytes;
140 try
141 {
142 __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes( PREFERRED_ENCODING );
143 }
144 catch (java.io.UnsupportedEncodingException use)
145 {
146 __bytes = _NATIVE_ALPHABET;
147 }
148 ALPHABET = __bytes;
149 }
150
151
152
153
154
155
156 private final static byte[] DECODABET =
157 {
158 -9,-9,-9,-9,-9,-9,-9,-9,-9,
159 -5,-5,
160 -9,-9,
161 -5,
162 -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,
163 -9,-9,-9,-9,-9,
164 -5,
165 -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,
166 62,
167 -9,-9,-9,
168 63,
169 52,53,54,55,56,57,58,59,60,61,
170 -9,-9,-9,
171 -1,
172 -9,-9,-9,
173 0,1,2,3,4,5,6,7,8,9,10,11,12,13,
174 14,15,16,17,18,19,20,21,22,23,24,25,
175 -9,-9,-9,-9,-9,-9,
176 26,27,28,29,30,31,32,33,34,35,36,37,38,
177 39,40,41,42,43,44,45,46,47,48,49,50,51,
178 -9,-9,-9,-9
179
180
181
182
183
184
185
186
187
188
189 };
190
191
192
193 private final static byte WHITE_SPACE_ENC = -5;
194 private final static byte EQUALS_SIGN_ENC = -1;
195
196
197
198 private Base64(){}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes )
221 {
222 encode3to4( threeBytes, 0, numSigBytes, b4, 0 );
223 return b4;
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 private static byte[] encode3to4(
249 byte[] source, int srcOffset, int numSigBytes,
250 byte[] destination, int destOffset )
251 {
252
253
254
255
256
257
258
259
260
261
262
263 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
264 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
265 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
266
267 switch( numSigBytes )
268 {
269 case 3:
270 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
271 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
272 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
273 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
274 return destination;
275
276 case 2:
277 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
278 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
279 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
280 destination[ destOffset + 3 ] = EQUALS_SIGN;
281 return destination;
282
283 case 1:
284 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
285 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
286 destination[ destOffset + 2 ] = EQUALS_SIGN;
287 destination[ destOffset + 3 ] = EQUALS_SIGN;
288 return destination;
289
290 default:
291 return destination;
292 }
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 public static String encodeObject( java.io.Serializable serializableObject )
309 {
310 return encodeObject( serializableObject, NO_OPTIONS );
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static String encodeObject( java.io.Serializable serializableObject, int options )
339 {
340
341 java.io.ByteArrayOutputStream baos = null;
342 java.io.OutputStream b64os = null;
343 java.io.ObjectOutputStream oos = null;
344 java.util.zip.GZIPOutputStream gzos = null;
345
346
347 int gzip = (options & GZIP);
348 int dontBreakLines = (options & DONT_BREAK_LINES);
349
350 try
351 {
352
353 baos = new java.io.ByteArrayOutputStream();
354 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines );
355
356
357 if( gzip == GZIP )
358 {
359 gzos = new java.util.zip.GZIPOutputStream( b64os );
360 oos = new java.io.ObjectOutputStream( gzos );
361 }
362 else
363 oos = new java.io.ObjectOutputStream( b64os );
364
365 oos.writeObject( serializableObject );
366 }
367 catch( java.io.IOException e )
368 {
369 e.printStackTrace();
370 return null;
371 }
372 finally
373 {
374 try{ oos.close(); } catch( Exception e ){}
375 try{ gzos.close(); } catch( Exception e ){}
376 try{ b64os.close(); } catch( Exception e ){}
377 try{ baos.close(); } catch( Exception e ){}
378 }
379
380
381 try
382 {
383 return new String( baos.toByteArray(), PREFERRED_ENCODING );
384 }
385 catch (java.io.UnsupportedEncodingException uue)
386 {
387 return new String( baos.toByteArray() );
388 }
389
390 }
391
392
393
394
395
396
397
398
399
400
401 public static String encodeBytes( byte[] source )
402 {
403 return encodeBytes( source, 0, source.length, NO_OPTIONS );
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428 public static String encodeBytes( byte[] source, int options )
429 {
430 return encodeBytes( source, 0, source.length, options );
431 }
432
433
434
435
436
437
438
439
440
441
442
443 public static String encodeBytes( byte[] source, int off, int len )
444 {
445 return encodeBytes( source, off, len, NO_OPTIONS );
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 public static String encodeBytes( byte[] source, int off, int len, int options )
473 {
474
475 int dontBreakLines = ( options & DONT_BREAK_LINES );
476 int gzip = ( options & GZIP );
477
478
479 if( gzip == GZIP )
480 {
481 java.io.ByteArrayOutputStream baos = null;
482 java.util.zip.GZIPOutputStream gzos = null;
483 Base64.OutputStream b64os = null;
484
485
486 try
487 {
488
489 baos = new java.io.ByteArrayOutputStream();
490 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines );
491 gzos = new java.util.zip.GZIPOutputStream( b64os );
492
493 gzos.write( source, off, len );
494 gzos.close();
495 }
496 catch( java.io.IOException e )
497 {
498 e.printStackTrace();
499 return null;
500 }
501 finally
502 {
503 try{ gzos.close(); } catch( Exception e ){}
504 try{ b64os.close(); } catch( Exception e ){}
505 try{ baos.close(); } catch( Exception e ){}
506 }
507
508
509 try
510 {
511 return new String( baos.toByteArray(), PREFERRED_ENCODING );
512 }
513 catch (java.io.UnsupportedEncodingException uue)
514 {
515 return new String( baos.toByteArray() );
516 }
517 }
518
519
520 else
521 {
522
523 boolean breakLines = dontBreakLines == 0;
524
525 int len43 = len * 4 / 3;
526 byte[] outBuff = new byte[ ( len43 )
527 + ( (len % 3) > 0 ? 4 : 0 )
528 + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ];
529 int d = 0;
530 int e = 0;
531 int len2 = len - 2;
532 int lineLength = 0;
533 for( ; d < len2; d+=3, e+=4 )
534 {
535 encode3to4( source, d+off, 3, outBuff, e );
536
537 lineLength += 4;
538 if( breakLines && lineLength == MAX_LINE_LENGTH )
539 {
540 outBuff[e+4] = NEW_LINE;
541 e++;
542 lineLength = 0;
543 }
544 }
545
546 if( d < len )
547 {
548 encode3to4( source, d+off, len - d, outBuff, e );
549 e += 4;
550 }
551
552
553
554 try
555 {
556 return new String( outBuff, 0, e, PREFERRED_ENCODING );
557 }
558 catch (java.io.UnsupportedEncodingException uue)
559 {
560 return new String( outBuff, 0, e );
561 }
562
563 }
564
565 }
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596 private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset )
597 {
598
599 if( source[ srcOffset + 2] == EQUALS_SIGN )
600 {
601
602
603
604 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
605 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 );
606
607 destination[ destOffset ] = (byte)( outBuff >>> 16 );
608 return 1;
609 }
610
611
612 else if( source[ srcOffset + 3 ] == EQUALS_SIGN )
613 {
614
615
616
617
618 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
619 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
620 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 );
621
622 destination[ destOffset ] = (byte)( outBuff >>> 16 );
623 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 );
624 return 2;
625 }
626
627
628 else
629 {
630 try{
631
632
633
634
635
636 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
637 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
638 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6)
639 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) );
640
641
642 destination[ destOffset ] = (byte)( outBuff >> 16 );
643 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 );
644 destination[ destOffset + 2 ] = (byte)( outBuff );
645
646 return 3;
647 }catch( Exception e){
648 System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) );
649 System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) );
650 System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) );
651 System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) );
652 return -1;
653 }
654 }
655 }
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671 public static byte[] decode( byte[] source, int off, int len )
672 {
673 int len34 = len * 3 / 4;
674 byte[] outBuff = new byte[ len34 ];
675 int outBuffPosn = 0;
676
677 byte[] b4 = new byte[4];
678 int b4Posn = 0;
679 int i = 0;
680 byte sbiCrop = 0;
681 byte sbiDecode = 0;
682 for( i = off; i < off+len; i++ )
683 {
684 sbiCrop = (byte)(source[i] & 0x7f);
685 sbiDecode = DECODABET[ sbiCrop ];
686
687 if( sbiDecode >= WHITE_SPACE_ENC )
688 {
689 if( sbiDecode >= EQUALS_SIGN_ENC )
690 {
691 b4[ b4Posn++ ] = sbiCrop;
692 if( b4Posn > 3 )
693 {
694 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn );
695 b4Posn = 0;
696
697
698 if( sbiCrop == EQUALS_SIGN )
699 break;
700 }
701
702 }
703
704 }
705 else
706 {
707 System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" );
708 return null;
709 }
710 }
711
712 byte[] out = new byte[ outBuffPosn ];
713 System.arraycopy( outBuff, 0, out, 0, outBuffPosn );
714 return out;
715 }
716
717
718
719
720
721
722
723
724
725
726
727
728 public static byte[] decode( String s )
729 {
730 byte[] bytes;
731 try
732 {
733 bytes = s.getBytes( PREFERRED_ENCODING );
734 }
735 catch( java.io.UnsupportedEncodingException uee )
736 {
737 bytes = s.getBytes();
738 }
739
740
741
742 bytes = decode( bytes, 0, bytes.length );
743
744
745
746
747 if( bytes != null && bytes.length >= 4 )
748 {
749
750 int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
751 if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head )
752 {
753 java.io.ByteArrayInputStream bais = null;
754 java.util.zip.GZIPInputStream gzis = null;
755 java.io.ByteArrayOutputStream baos = null;
756 byte[] buffer = new byte[2048];
757 int length = 0;
758
759 try
760 {
761 baos = new java.io.ByteArrayOutputStream();
762 bais = new java.io.ByteArrayInputStream( bytes );
763 gzis = new java.util.zip.GZIPInputStream( bais );
764
765 while( ( length = gzis.read( buffer ) ) >= 0 )
766 {
767 baos.write(buffer,0,length);
768 }
769
770
771 bytes = baos.toByteArray();
772
773 }
774 catch( java.io.IOException e )
775 {
776
777 }
778 finally
779 {
780 try{ baos.close(); } catch( Exception e ){}
781 try{ gzis.close(); } catch( Exception e ){}
782 try{ bais.close(); } catch( Exception e ){}
783 }
784
785 }
786 }
787
788 return bytes;
789 }
790
791
792
793
794
795
796
797
798
799
800
801
802 public static Object decodeToObject( String encodedObject )
803 {
804
805 byte[] objBytes = decode( encodedObject );
806
807 java.io.ByteArrayInputStream bais = null;
808 java.io.ObjectInputStream ois = null;
809 Object obj = null;
810
811 try
812 {
813 bais = new java.io.ByteArrayInputStream( objBytes );
814 ois = new java.io.ObjectInputStream( bais );
815
816 obj = ois.readObject();
817 }
818 catch( java.io.IOException e )
819 {
820 e.printStackTrace();
821 obj = null;
822 }
823 catch( java.lang.ClassNotFoundException e )
824 {
825 e.printStackTrace();
826 obj = null;
827 }
828 finally
829 {
830 try{ bais.close(); } catch( Exception e ){}
831 try{ ois.close(); } catch( Exception e ){}
832 }
833
834 return obj;
835 }
836
837
838
839
840
841
842
843
844
845
846
847
848 public static boolean encodeToFile( byte[] dataToEncode, String filename )
849 {
850 boolean success = false;
851 Base64.OutputStream bos = null;
852 try
853 {
854 bos = new Base64.OutputStream(
855 new java.io.FileOutputStream( filename ), Base64.ENCODE );
856 bos.write( dataToEncode );
857 success = true;
858 }
859 catch( java.io.IOException e )
860 {
861
862 success = false;
863 }
864 finally
865 {
866 try{ bos.close(); } catch( Exception e ){}
867 }
868
869 return success;
870 }
871
872
873
874
875
876
877
878
879
880
881
882 public static boolean decodeToFile( String dataToDecode, String filename )
883 {
884 boolean success = false;
885 Base64.OutputStream bos = null;
886 try
887 {
888 bos = new Base64.OutputStream(
889 new java.io.FileOutputStream( filename ), Base64.DECODE );
890 bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
891 success = true;
892 }
893 catch( java.io.IOException e )
894 {
895 success = false;
896 }
897 finally
898 {
899 try{ bos.close(); } catch( Exception e ){}
900 }
901
902 return success;
903 }
904
905
906
907
908
909
910
911
912
913
914
915
916
917 public static byte[] decodeFromFile( String filename )
918 {
919 byte[] decodedData = null;
920 Base64.InputStream bis = null;
921 try
922 {
923
924 java.io.File file = new java.io.File( filename );
925 byte[] buffer = null;
926 int length = 0;
927 int numBytes = 0;
928
929
930 if( file.length() > Integer.MAX_VALUE )
931 {
932 System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." );
933 return null;
934 }
935 buffer = new byte[ (int)file.length() ];
936
937
938 bis = new Base64.InputStream(
939 new java.io.BufferedInputStream(
940 new java.io.FileInputStream( file ) ), Base64.DECODE );
941
942
943 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
944 length += numBytes;
945
946
947 decodedData = new byte[ length ];
948 System.arraycopy( buffer, 0, decodedData, 0, length );
949
950 }
951 catch( java.io.IOException e )
952 {
953 System.err.println( "Error decoding from file " + filename );
954 }
955 finally
956 {
957 try{ bis.close(); } catch( Exception e) {}
958 }
959
960 return decodedData;
961 }
962
963
964
965
966
967
968
969
970
971
972
973
974 public static String encodeFromFile( String filename )
975 {
976 String encodedData = null;
977 Base64.InputStream bis = null;
978 try
979 {
980
981 java.io.File file = new java.io.File( filename );
982 byte[] buffer = new byte[ (int)(file.length() * 1.4) ];
983 int length = 0;
984 int numBytes = 0;
985
986
987 bis = new Base64.InputStream(
988 new java.io.BufferedInputStream(
989 new java.io.FileInputStream( file ) ), Base64.ENCODE );
990
991
992 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
993 length += numBytes;
994
995
996 encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
997
998 }
999 catch( java.io.IOException e )
1000 {
1001 System.err.println( "Error encoding from file " + filename );
1002 }
1003 finally
1004 {
1005 try{ bis.close(); } catch( Exception e) {}
1006 }
1007
1008 return encodedData;
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 public static class InputStream extends java.io.FilterInputStream
1027 {
1028 private boolean encode;
1029 private int position;
1030 private byte[] buffer;
1031 private int bufferLength;
1032 private int numSigBytes;
1033 private int lineLength;
1034 private boolean breakLines;
1035
1036
1037
1038
1039
1040
1041
1042
1043 public InputStream( java.io.InputStream in )
1044 {
1045 this( in, DECODE );
1046 }
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 public InputStream( java.io.InputStream in, int options )
1071 {
1072 super( in );
1073 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
1074 this.encode = (options & ENCODE) == ENCODE;
1075 this.bufferLength = encode ? 4 : 3;
1076 this.buffer = new byte[ bufferLength ];
1077 this.position = -1;
1078 this.lineLength = 0;
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088 public int read() throws java.io.IOException
1089 {
1090
1091 if( position < 0 )
1092 {
1093 if( encode )
1094 {
1095 byte[] b3 = new byte[3];
1096 int numBinaryBytes = 0;
1097 for( int i = 0; i < 3; i++ )
1098 {
1099 try
1100 {
1101 int b = in.read();
1102
1103
1104 if( b >= 0 )
1105 {
1106 b3[i] = (byte)b;
1107 numBinaryBytes++;
1108 }
1109
1110 }
1111 catch( java.io.IOException e )
1112 {
1113
1114 if( i == 0 )
1115 throw e;
1116
1117 }
1118 }
1119
1120 if( numBinaryBytes > 0 )
1121 {
1122 encode3to4( b3, 0, numBinaryBytes, buffer, 0 );
1123 position = 0;
1124 numSigBytes = 4;
1125 }
1126 else
1127 {
1128 return -1;
1129 }
1130 }
1131
1132
1133 else
1134 {
1135 byte[] b4 = new byte[4];
1136 int i = 0;
1137 for( i = 0; i < 4; i++ )
1138 {
1139
1140 int b = 0;
1141 do{ b = in.read(); }
1142 while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC );
1143
1144 if( b < 0 )
1145 break;
1146
1147 b4[i] = (byte)b;
1148 }
1149
1150 if( i == 4 )
1151 {
1152 numSigBytes = decode4to3( b4, 0, buffer, 0 );
1153 position = 0;
1154 }
1155 else if( i == 0 ){
1156 return -1;
1157 }
1158 else
1159 {
1160
1161 throw new java.io.IOException( "Improperly padded Base64 input." );
1162 }
1163
1164 }
1165 }
1166
1167
1168 if( position >= 0 )
1169 {
1170
1171 if(
1172 return -1;
1173
1174 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH )
1175 {
1176 lineLength = 0;
1177 return '\n';
1178 }
1179 else
1180 {
1181 lineLength++;
1182
1183
1184
1185 int b = buffer[ position++ ];
1186
1187 if( position >= bufferLength )
1188 position = -1;
1189
1190 return b & 0xFF;
1191
1192 }
1193 }
1194
1195
1196 else
1197 {
1198
1199 throw new java.io.IOException( "Error in Base64 code reading stream." );
1200 }
1201 }
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216 public int read( byte[] dest, int off, int len ) throws java.io.IOException
1217 {
1218 int i;
1219 int b;
1220 for( i = 0; i < len; i++ )
1221 {
1222 b = read();
1223
1224
1225
1226
1227 if( b >= 0 )
1228 dest[off + i] = (byte)b;
1229 else if( i == 0 )
1230 return -1;
1231 else
1232 break;
1233 }
1234 return i;
1235 }
1236
1237 }
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 public static class OutputStream extends java.io.FilterOutputStream
1257 {
1258 private boolean encode;
1259 private int position;
1260 private byte[] buffer;
1261 private int bufferLength;
1262 private int lineLength;
1263 private boolean breakLines;
1264 private byte[] b4;
1265 private boolean suspendEncoding;
1266
1267
1268
1269
1270
1271
1272
1273 public OutputStream( java.io.OutputStream out )
1274 {
1275 this( out, ENCODE );
1276 }
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299 public OutputStream( java.io.OutputStream out, int options )
1300 {
1301 super( out );
1302 this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
1303 this.encode = (options & ENCODE) == ENCODE;
1304 this.bufferLength = encode ? 3 : 4;
1305 this.buffer = new byte[ bufferLength ];
1306 this.position = 0;
1307 this.lineLength = 0;
1308 this.suspendEncoding = false;
1309 this.b4 = new byte[4];
1310 }
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325 public void write(int theByte) throws java.io.IOException
1326 {
1327
1328 if( suspendEncoding )
1329 {
1330 super.out.write( theByte );
1331 return;
1332 }
1333
1334
1335 if( encode )
1336 {
1337 buffer[ position++ ] = (byte)theByte;
1338 if( position >= bufferLength )
1339 {
1340 out.write( encode3to4( b4, buffer, bufferLength ) );
1341
1342 lineLength += 4;
1343 if( breakLines && lineLength >= MAX_LINE_LENGTH )
1344 {
1345 out.write( NEW_LINE );
1346 lineLength = 0;
1347 }
1348
1349 position = 0;
1350 }
1351 }
1352
1353
1354 else
1355 {
1356
1357 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC )
1358 {
1359 buffer[ position++ ] = (byte)theByte;
1360 if( position >= bufferLength )
1361 {
1362 int len = Base64.decode4to3( buffer, 0, b4, 0 );
1363 out.write( b4, 0, len );
1364
1365 position = 0;
1366 }
1367 }
1368 else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC )
1369 {
1370 throw new java.io.IOException( "Invalid character in Base64 data." );
1371 }
1372 }
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386 public void write( byte[] theBytes, int off, int len ) throws java.io.IOException
1387 {
1388
1389 if( suspendEncoding )
1390 {
1391 super.out.write( theBytes, off, len );
1392 return;
1393 }
1394
1395 for( int i = 0; i < len; i++ )
1396 {
1397 write( theBytes[ off + i ] );
1398 }
1399
1400 }
1401
1402
1403
1404
1405
1406
1407
1408 public void flushBase64() throws java.io.IOException
1409 {
1410 if( position > 0 )
1411 {
1412 if( encode )
1413 {
1414 out.write( encode3to4( b4, buffer, position ) );
1415 position = 0;
1416 }
1417 else
1418 {
1419 throw new java.io.IOException( "Base64 input not properly padded." );
1420 }
1421 }
1422
1423 }
1424
1425
1426
1427
1428
1429
1430
1431 public void close() throws java.io.IOException
1432 {
1433
1434 flushBase64();
1435
1436
1437
1438 super.close();
1439
1440 buffer = null;
1441 out = null;
1442 }
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453 public void suspendEncoding() throws java.io.IOException
1454 {
1455 flushBase64();
1456 this.suspendEncoding = true;
1457 }
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467 public void resumeEncoding()
1468 {
1469 this.suspendEncoding = false;
1470 }
1471
1472
1473
1474 }
1475
1476
1477 }