Base64.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package cn.com.lzt.message.send.util;
  2. import javax.xml.bind.DatatypeConverter;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.io.UnsupportedEncodingException;
  6. /**
  7. * Base64编码工具类
  8. */
  9. public class Base64 {
  10. private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  11. .toCharArray();
  12. public static String encode(String s) {
  13. if(s==null)return null;
  14. try
  15. {
  16. return encode(s.getBytes("UTF-8"));
  17. }
  18. catch (UnsupportedEncodingException e)
  19. {
  20. e.printStackTrace();
  21. }
  22. return null;
  23. }
  24. public static String encode(byte[] data) {
  25. return DatatypeConverter.printBase64Binary( data );
  26. /*int start = 0;
  27. int len = data.length;
  28. StringBuffer buf = new StringBuffer(data.length * 3 / 2);
  29. int end = len - 3;
  30. int i = start;
  31. int n = 0;
  32. while (i <= end) {
  33. int d = ((((int) data[i]) & 0x0ff) << 16)
  34. | ((((int) data[i + 1]) & 0x0ff) << 8)
  35. | (((int) data[i + 2]) & 0x0ff);
  36. buf.append(legalChars[(d >> 18) & 63]);
  37. buf.append(legalChars[(d >> 12) & 63]);
  38. buf.append(legalChars[(d >> 6) & 63]);
  39. buf.append(legalChars[d & 63]);
  40. i += 3;
  41. if (n++ >= 14) {
  42. n = 0;
  43. buf.append(" ");
  44. }
  45. }
  46. if (i == start + len - 2) {
  47. int d = ((((int) data[i]) & 0x0ff) << 16)
  48. | ((((int) data[i + 1]) & 255) << 8);
  49. buf.append(legalChars[(d >> 18) & 63]);
  50. buf.append(legalChars[(d >> 12) & 63]);
  51. buf.append(legalChars[(d >> 6) & 63]);
  52. buf.append("=");
  53. } else if (i == start + len - 1) {
  54. int d = (((int) data[i]) & 0x0ff) << 16;
  55. buf.append(legalChars[(d >> 18) & 63]);
  56. buf.append(legalChars[(d >> 12) & 63]);
  57. buf.append("==");
  58. }
  59. return buf.toString();*/
  60. }
  61. private static int decode(char c) {
  62. if (c >= 'A' && c <= 'Z')
  63. return ((int) c) - 65;
  64. else if (c >= 'a' && c <= 'z')
  65. return ((int) c) - 97 + 26;
  66. else if (c >= '0' && c <= '9')
  67. return ((int) c) - 48 + 26 + 26;
  68. else
  69. switch (c) {
  70. case '+':
  71. return 62;
  72. case '/':
  73. return 63;
  74. case '=':
  75. return 0;
  76. default:
  77. throw new RuntimeException("unexpected code: " + c);
  78. }
  79. }
  80. /**
  81. * Decodes the given Base64 encoded String to a new byte array. The byte
  82. * array holding the decoded data is returned.
  83. */
  84. public static byte[] decode(String s) {
  85. return DatatypeConverter.parseBase64Binary( s );
  86. /*ByteArrayOutputStream bos = new ByteArrayOutputStream();
  87. try {
  88. decode(s, bos);
  89. } catch (IOException e) {
  90. throw new RuntimeException();
  91. }
  92. byte[] decodedBytes = bos.toByteArray();
  93. try {
  94. bos.close();
  95. bos = null;
  96. } catch (IOException ex) {
  97. System.err.println("Error while decoding BASE64: " + ex.toString());
  98. }
  99. return decodedBytes;*/
  100. }
  101. private static void decode(String s, OutputStream os) throws IOException {
  102. int i = 0;
  103. int len = s.length();
  104. while (true) {
  105. while (i < len && s.charAt(i) <= ' ')
  106. i++;
  107. if (i == len)
  108. break;
  109. int tri = (decode(s.charAt(i)) << 18)
  110. + (decode(s.charAt(i + 1)) << 12)
  111. + (decode(s.charAt(i + 2)) << 6)
  112. + (decode(s.charAt(i + 3)));
  113. os.write((tri >> 16) & 255);
  114. if (s.charAt(i + 2) == '=')
  115. break;
  116. os.write((tri >> 8) & 255);
  117. if (s.charAt(i + 3) == '=')
  118. break;
  119. os.write(tri & 255);
  120. i += 4;
  121. }
  122. }
  123. }