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