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