1+ package com .github .jsbxyyx .xbook .common ;
2+
3+ public class Ba {
4+
5+ public static String atob (byte [] a ) {
6+ return atob (a , false );
7+ }
8+
9+ public static String atoab (byte [] a ) {
10+ return atob (a , true );
11+ }
12+
13+ private static String atob (byte [] a , boolean alternate ) {
14+ int aLen = a .length ;
15+ int numFullGroups = aLen / 3 ;
16+ int numBytesInPartialGroup = aLen - 3 * numFullGroups ;
17+ int resultLen = 4 * ((aLen + 2 ) / 3 );
18+ StringBuilder result = new StringBuilder (resultLen );
19+ char [] intToAlpha = (alternate ? itoab : itob );
20+
21+ int inCursor = 0 ;
22+ for (int i = 0 ; i < numFullGroups ; i ++) {
23+ int byte0 = a [inCursor ++] & 0xff ;
24+ int byte1 = a [inCursor ++] & 0xff ;
25+ int byte2 = a [inCursor ++] & 0xff ;
26+ result .append (intToAlpha [byte0 >> 2 ]);
27+ result .append (intToAlpha [(byte0 << 4 ) & 0x3f | (byte1 >> 4 )]);
28+ result .append (intToAlpha [(byte1 << 2 ) & 0x3f | (byte2 >> 6 )]);
29+ result .append (intToAlpha [byte2 & 0x3f ]);
30+ }
31+
32+ if (numBytesInPartialGroup != 0 ) {
33+ int byte0 = a [inCursor ++] & 0xff ;
34+ result .append (intToAlpha [byte0 >> 2 ]);
35+ if (numBytesInPartialGroup == 1 ) {
36+ result .append (intToAlpha [(byte0 << 4 ) & 0x3f ]);
37+ result .append ("==" );
38+ } else {
39+ int byte1 = a [inCursor ++] & 0xff ;
40+ result .append (intToAlpha [(byte0 << 4 ) & 0x3f | (byte1 >> 4 )]);
41+ result .append (intToAlpha [(byte1 << 2 ) & 0x3f ]);
42+ result .append ('=' );
43+ }
44+ }
45+ return result .toString ();
46+ }
47+
48+ private static final char [] itob = {'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
49+ 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
50+ 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' ,
51+ '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' };
52+
53+ private static final char [] itoab = {'!' , '"' , '#' , '$' , '%' , '&' , '\'' , '(' , ')' , ',' , '-' , '.' , ':' ,
54+ ';' , '<' , '>' , '@' , '[' , ']' , '^' , '`' , '_' , '{' , '|' , '}' , '~' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
55+ 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' ,
56+ '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '?' };
57+
58+ public static byte [] btoa (String s ) {
59+ return btoa (s , false );
60+ }
61+
62+ public static byte [] abtoa (String s ) {
63+ return btoa (s , true );
64+ }
65+
66+ private static byte [] btoa (String s , boolean alternate ) {
67+ byte [] alphaToInt = (alternate ? abtoi : btoi );
68+ int sLen = s .length ();
69+ int numGroups = sLen / 4 ;
70+ if (4 * numGroups != sLen ) {
71+ throw new IllegalArgumentException ("String length must be a multiple of four." );
72+ }
73+ int missingBytesInLastGroup = 0 ;
74+ int numFullGroups = numGroups ;
75+ if (sLen != 0 ) {
76+ if (s .charAt (sLen - 1 ) == '=' ) {
77+ missingBytesInLastGroup ++;
78+ numFullGroups --;
79+ }
80+ if (s .charAt (sLen - 2 ) == '=' ) {
81+ missingBytesInLastGroup ++;
82+ }
83+ }
84+ byte [] result = new byte [3 * numGroups - missingBytesInLastGroup ];
85+
86+ int inCursor = 0 , outCursor = 0 ;
87+ for (int i = 0 ; i < numFullGroups ; i ++) {
88+ int ch0 = btoi (s .charAt (inCursor ++), alphaToInt );
89+ int ch1 = btoi (s .charAt (inCursor ++), alphaToInt );
90+ int ch2 = btoi (s .charAt (inCursor ++), alphaToInt );
91+ int ch3 = btoi (s .charAt (inCursor ++), alphaToInt );
92+ result [outCursor ++] = (byte ) ((ch0 << 2 ) | (ch1 >> 4 ));
93+ result [outCursor ++] = (byte ) ((ch1 << 4 ) | (ch2 >> 2 ));
94+ result [outCursor ++] = (byte ) ((ch2 << 6 ) | ch3 );
95+ }
96+
97+ if (missingBytesInLastGroup != 0 ) {
98+ int ch0 = btoi (s .charAt (inCursor ++), alphaToInt );
99+ int ch1 = btoi (s .charAt (inCursor ++), alphaToInt );
100+ result [outCursor ++] = (byte ) ((ch0 << 2 ) | (ch1 >> 4 ));
101+
102+ if (missingBytesInLastGroup == 1 ) {
103+ int ch2 = btoi (s .charAt (inCursor ++), alphaToInt );
104+ result [outCursor ++] = (byte ) ((ch1 << 4 ) | (ch2 >> 2 ));
105+ }
106+ }
107+ return result ;
108+ }
109+
110+ private static int btoi (char c , byte [] alphaToInt ) {
111+ int result = alphaToInt [c ];
112+ if (result < 0 ) {
113+ throw new IllegalArgumentException ("Illegal character " + c );
114+ }
115+ return result ;
116+ }
117+
118+ private static final byte [] btoi = {-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
119+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 ,
120+ -1 , -1 , -1 , 63 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
121+ 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , -1 , -1 , 26 , 27 , 28 ,
122+ 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 };
123+
124+ private static final byte [] abtoi = {-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
125+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , -1 , 62 , 9 , 10 ,
126+ 11 , -1 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 12 , 13 , 14 , -1 , 15 , 63 , 16 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
127+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 17 , -1 , 18 , 19 , 21 , 20 , 26 , 27 , 28 ,
128+ 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 22 , 23 , 24 , 25 };
129+
130+ }
0 commit comments