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