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