Base64.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.happy.Unitil_elc;
  2. import java.io.ByteArrayOutputStream;
  3. public class Base64 {
  4. // map 6-bit int to char
  5. private static final char[] chars64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
  6. 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
  7. '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
  8. // map char to 6-bit int
  9. private static final int[] ints64 = new int[128];
  10. static {
  11. for (int i = 0; i < 64; i++) {
  12. ints64[chars64[i]] = i;
  13. }
  14. }
  15. /**
  16. * 转换byte数组成Base64 string
  17. *
  18. * @param unencoded
  19. * @return
  20. */
  21. public static final String encode(byte[] unencoded) {
  22. // Take 24-bits from three octets, translate into four encoded chars.
  23. // If necessary, pad with 0 bits on the right at the end
  24. // Use = signs as padding at the end to ensure encodedLength % 4 == 0
  25. if (unencoded == null || unencoded.length == 0) return null;
  26. ByteArrayOutputStream out = new ByteArrayOutputStream((int) (unencoded.length * 1.37));
  27. int byteCount = 0;
  28. int carryOver = 0;
  29. for (int i = 0; i < unencoded.length; i++) {
  30. int bc = (byteCount % 3);
  31. byte b = unencoded[i];
  32. int lookup = 0;
  33. // First byte use first six bits, save last two bits
  34. if (bc == 0) {
  35. lookup = (b >> 2) & 0x3F;
  36. carryOver = b & 0x03; // last two bits
  37. out.write(chars64[lookup]);
  38. } else if (bc == 1) {
  39. // Second byte use previous two bits and first four new bits,
  40. // save last four bits
  41. lookup = ((carryOver << 4) | ((b >> 4) & 0x0F));
  42. carryOver = b & 0x0F; // last four bits
  43. out.write(chars64[lookup]);
  44. } else if (bc == 2) {
  45. // Third byte use previous four bits and first two new bits,
  46. // then use last six new bits
  47. lookup = ((carryOver << 2) | ((b >> 6) & 0x03));
  48. out.write(chars64[lookup]);
  49. lookup = b & 0x3F; // last six bits
  50. out.write(chars64[lookup]);
  51. carryOver = 0;
  52. }
  53. byteCount++;
  54. }
  55. if (byteCount % 3 == 1) { // one leftover
  56. int lookup = (carryOver << 4) & 0xF0;
  57. out.write(chars64[lookup]);
  58. out.write('=');
  59. out.write('=');
  60. } else if (byteCount % 3 == 2) { // two leftovers
  61. int lookup = (carryOver << 2) & 0x3C;
  62. out.write(chars64[lookup]);
  63. out.write('=');
  64. }
  65. return out.toString();
  66. }
  67. /**
  68. * Decode Base64 string back to byte array
  69. *
  70. * @param encoded
  71. * @return
  72. */
  73. public static final byte[] decode(String encoded) {
  74. if (encoded == null || encoded.length() == 0) return null;
  75. byte[] bytes = encoded.getBytes();
  76. ByteArrayOutputStream out = new ByteArrayOutputStream((int) (bytes.length * 0.67));
  77. int byteCount = 0;
  78. int carryOver = 0;
  79. DECODE_LOOP: for (int i = 0; i < bytes.length; i++) {
  80. int ch = bytes[i];
  81. // Read the next non-whitespace character
  82. // if (Character.isWhitespace((char)ch))
  83. // continue;
  84. // The '=' sign is just padding; geffective end of stream
  85. if (ch == '=') break DECODE_LOOP;
  86. // Convert from raw form to 6-bit form
  87. int newbits = ints64[ch];
  88. int bc = (byteCount % 4);
  89. if (bc == 0) {
  90. // First char save all six bits, go for another
  91. carryOver = newbits & 0x3F;
  92. } else if (bc == 1) {
  93. // second char use 6 previous bits and first 2 new bits
  94. int data = ((carryOver << 2) + ((newbits >> 4) & 0x03));
  95. out.write(data);
  96. carryOver = newbits & 0x0F; // save 4 bits
  97. } else if (bc == 2) {
  98. // Third char use previous four bits and first four new bits,
  99. // save last two bits
  100. int data = ((carryOver << 4) + ((newbits >> 2) & 0x0F));
  101. out.write(data);
  102. carryOver = newbits & 0x03; // save 2 bits
  103. } else if (bc == 3) {
  104. // Fourth char use previous two bits and all six new bits
  105. int data = ((carryOver << 6) + (newbits & 0x3F));
  106. out.write(data);
  107. carryOver = 0;
  108. }
  109. byteCount++;
  110. }
  111. return out.toByteArray();
  112. }
  113. }