Guest User

Untitled

a guest
Aug 5th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package com.example.jartest;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6.  
  7.  
  8.  
  9. public class Base64 {
  10. private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
  11.  
  12. public static String encode(byte[] data) {
  13. int start = 0;
  14. int len = data.length;
  15. StringBuffer buf = new StringBuffer(data.length * 3 / 2);
  16.  
  17. int end = len - 3;
  18. int i = start;
  19. int n = 0;
  20.  
  21. while (i <= end) {
  22. int d = ((((int) data[i]) & 0x0ff) << 16)
  23. | ((((int) data[i + 1]) & 0x0ff) << 8)
  24. | (((int) data[i + 2]) & 0x0ff);
  25.  
  26. buf.append(legalChars[(d >> 18) & 63]);
  27. buf.append(legalChars[(d >> 12) & 63]);
  28. buf.append(legalChars[(d >> 6) & 63]);
  29. buf.append(legalChars[d & 63]);
  30.  
  31. i += 3;
  32.  
  33. if (n++ >= 14) {
  34. n = 0;
  35. buf.append(" ");
  36. }
  37. }
  38.  
  39. if (i == start + len - 2) {
  40. int d = ((((int) data[i]) & 0x0ff) << 16)
  41. | ((((int) data[i + 1]) & 255) << 8);
  42.  
  43. buf.append(legalChars[(d >> 18) & 63]);
  44. buf.append(legalChars[(d >> 12) & 63]);
  45. buf.append(legalChars[(d >> 6) & 63]);
  46. buf.append("=");
  47. } else if (i == start + len - 1) {
  48. int d = (((int) data[i]) & 0x0ff) << 16;
  49.  
  50. buf.append(legalChars[(d >> 18) & 63]);
  51. buf.append(legalChars[(d >> 12) & 63]);
  52. buf.append("==");
  53. }
  54.  
  55. return buf.toString();
  56. }
  57.  
  58. private static int decode(char c) {
  59. if (c >= 'A' && c <= 'Z')
  60. return ((int) c) - 65;
  61. else if (c >= 'a' && c <= 'z')
  62. return ((int) c) - 97 + 26;
  63. else if (c >= '0' && c <= '9')
  64. return ((int) c) - 48 + 26 + 26;
  65. else
  66. switch (c) {
  67. case '+':
  68. return 62;
  69. case '/':
  70. return 63;
  71. case '=':
  72. return 0;
  73. default:
  74. throw new RuntimeException("unexpected code: " + c);
  75. }
  76. }
  77.  
  78. /**
  79. * Decodes the given Base64 encoded String to a new byte array. The byte
  80. * array holding the decoded data is returned.
  81. */
  82.  
  83. public static byte[] decode(String s) {
  84.  
  85. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  86. try {
  87. decode(s, bos);
  88. } catch (IOException e) {
  89. throw new RuntimeException();
  90. }
  91. byte[] decodedBytes = bos.toByteArray();
  92. try {
  93. bos.close();
  94. bos = null;
  95. } catch (IOException ex) {
  96. System.err.println("Error while decoding BASE64: " + ex.toString());
  97. }
  98. return decodedBytes;
  99. }
  100.  
  101. private static void decode(String s, OutputStream os) throws IOException {
  102. int i = 0;
  103.  
  104. int len = s.length();
  105.  
  106. while (true) {
  107. while (i < len && s.charAt(i) <= ' ')
  108. i++;
  109.  
  110. if (i == len)
  111. break;
  112.  
  113. int tri = (decode(s.charAt(i)) << 18)
  114. + (decode(s.charAt(i + 1)) << 12)
  115. + (decode(s.charAt(i + 2)) << 6)
  116. + (decode(s.charAt(i + 3)));
  117.  
  118. os.write((tri >> 16) & 255);
  119. if (s.charAt(i + 2) == '=')
  120. break;
  121. os.write((tri >> 8) & 255);
  122. if (s.charAt(i + 3) == '=')
  123. break;
  124. os.write(tri & 255);
  125.  
  126. i += 4;
  127. }
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment