MCPcopy Create free account
hub / github.com/aa112901/remusic / Base64Encoder

Class Base64Encoder

app/src/main/java/com/wm/remusic/net/Base64Encoder.java:6–73  ·  view source on GitHub ↗

Created by wm on 2016/4/10.

Source from the content-addressed store, hash-verified

4 * Created by wm on 2016/4/10.
5 */
6public class Base64Encoder {
7 private static final char last2byte = (char) Integer
8 .parseInt("00000011", 2);
9 private static final char last4byte = (char) Integer
10 .parseInt("00001111", 2);
11 private static final char last6byte = (char) Integer
12 .parseInt("00111111", 2);
13 private static final char lead6byte = (char) Integer
14 .parseInt("11111100", 2);
15 private static final char lead4byte = (char) Integer
16 .parseInt("11110000", 2);
17 private static final char lead2byte = (char) Integer
18 .parseInt("11000000", 2);
19 private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D',
20 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
21 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
22 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
23 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
24 '4', '5', '6', '7', '8', '9', '+', '/'};
25
26 /**
27 * Base64 encoding.
28 *
29 * @param from The src data.
30 * @return
31 */
32 public static String encode(byte[] from) {
33 StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);
34 int num = 0;
35 char currentByte = 0;
36 for (int i = 0; i < from.length; i++) {
37 num = num % 8;
38 while (num < 8) {
39 switch (num) {
40 case 0:
41 currentByte = (char) (from[i] & lead6byte);
42 currentByte = (char) (currentByte >>> 2);
43 break;
44 case 2:
45 currentByte = (char) (from[i] & last6byte);
46 break;
47 case 4:
48 currentByte = (char) (from[i] & last4byte);
49 currentByte = (char) (currentByte << 2);
50 if ((i + 1) < from.length) {
51 currentByte |= (from[i + 1] & lead2byte) >>> 6;
52 }
53 break;
54 case 6:
55 currentByte = (char) (from[i] & last2byte);
56 currentByte = (char) (currentByte << 4);
57 if ((i + 1) < from.length) {
58 currentByte |= (from[i + 1] & lead4byte) >>> 4;
59 }
60 break;
61 }
62 to.append(encodeTable[currentByte]);
63 num += 6;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected