1111 * @author admin
1212 */
1313public class AnalysysEncoder {
14- public static final String GZIP_ENCODE_UTF_8 = "UTF-8" ;
14+ public static final String GZIP_ENCODE_UTF_8 = "UTF-8" ;
15+ private static char [] base64EncodeChars = new char []{
16+ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
17+ 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' ,
18+ 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' ,
19+ 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ,
20+ 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
21+ 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' ,
22+ 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' ,
23+ '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' };
24+ private static byte [] base64DecodeChars = new byte []{
25+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
26+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
27+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 ,
28+ 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 ,
29+ -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ,
30+ 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , -1 ,
31+ -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ,
32+ 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 };
1533
16- /**
17- * 编码
18- * @param str 待处理字符串
19- * @return 字节数组
20- */
21- public static byte [] compress (String str ) {
22- return compress (str , GZIP_ENCODE_UTF_8 );
23- }
24-
25- /**
26- * 解码
27- * @param bytes 待处理字节
28- * @return 解码后的字符串
29- * @throws IOException IO异常
30- */
31- public static String uncompress (byte [] bytes ) throws IOException {
32- return uncompress (bytes , GZIP_ENCODE_UTF_8 );
33- }
34+ /**
35+ * 编码
36+ *
37+ * @param str 待处理字符串
38+ * @return 字节数组
39+ */
40+ public static byte [] compress (String str ) {
41+ return compress (str , GZIP_ENCODE_UTF_8 );
42+ }
43+
44+ /**
45+ * 解码
46+ *
47+ * @param bytes 待处理字节
48+ * @return 解码后的字符串
49+ * @throws IOException IO异常
50+ */
51+ public static String uncompress (byte [] bytes ) throws IOException {
52+ return uncompress (bytes , GZIP_ENCODE_UTF_8 );
53+ }
3454
35- /**
36- * 对字符按照指定编码格式进行编码
37- * @param str 待处理字符串
38- * @param encoding 编码格式
39- * @return 编码后的字节数组
40- */
41- public static byte [] compress (String str , String encoding ) {
42- if (str == null || str .length () == 0 ) {
55+ /**
56+ * 对字符按照指定编码格式进行编码
57+ *
58+ * @param str 待处理字符串
59+ * @param encoding 编码格式
60+ * @return 编码后的字节数组
61+ */
62+ public static byte [] compress (String str , String encoding ) {
63+ if (str == null || str .length () == 0 ) {
4364 return null ;
4465 }
4566 ByteArrayOutputStream out = new ByteArrayOutputStream ();
@@ -52,90 +73,71 @@ public static byte[] compress(String str, String encoding) {
5273 e .printStackTrace ();
5374 }
5475 return out .toByteArray ();
55- }
76+ }
77+
78+ /**
79+ * 对字节按照指定编码格式进行解码
80+ *
81+ * @param bytes 待处理字节码
82+ * @param encoding 编码格式
83+ * @return 解码后的字符串
84+ * @throws IOException IO异常
85+ */
86+ public static String uncompress (byte [] bytes , String encoding ) throws IOException {
87+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
88+ ByteArrayInputStream in = new ByteArrayInputStream (bytes );
89+ GZIPInputStream ungzip = new GZIPInputStream (in );
90+ byte [] buffer = new byte [256 ];
91+ int n ;
92+ while ((n = ungzip .read (buffer )) >= 0 ) {
93+ out .write (buffer , 0 , n );
94+ }
95+ return out .toString (encoding );
96+ }
97+
98+ /**
99+ * Base64解码
100+ *
101+ * @param base64str 待处理的Base64编码的字符串
102+ * @return 解码后的字符串
103+ */
104+ public static String ozBase64ToStr (String base64str ) {
105+ String base64Codep ;
106+ try {
107+ base64Codep = new String (AnalysysEncoder .decode (base64str ));
108+ return base64Codep ;
109+ } catch (UnsupportedEncodingException e ) {
110+ e .printStackTrace ();
111+ return null ;
112+ }
113+ }
56114
57- /**
58- * 对字节按照指定编码格式进行解码
59- * @param bytes 待处理字节码
60- * @param encoding 编码格式
61- * @return 解码后的字符串
62- * @throws IOException IO异常
63- */
64- public static String uncompress (byte [] bytes , String encoding ) throws IOException {
65- ByteArrayOutputStream out = new ByteArrayOutputStream ();
66- ByteArrayInputStream in = new ByteArrayInputStream (bytes );
67- GZIPInputStream ungzip = new GZIPInputStream (in );
68- byte [] buffer = new byte [256 ];
69- int n ;
70- while ((n = ungzip .read (buffer )) >= 0 ) {
71- out .write (buffer , 0 , n );
72- }
73- return out .toString (encoding );
74- }
115+ /**
116+ * Base64编码
117+ *
118+ * @param str 待处理字符串
119+ * @return 编码后的字符串
120+ */
121+ public static String ozStrToBase64 (String str ) {
122+ String basestr = AnalysysEncoder .encode (str .getBytes ());
123+ return basestr ;
124+ }
75125
76- /**
77- * Base64解码
78- * @param base64str 待处理的Base64编码的字符串
79- * @return 解码后的字符串
80- */
81- public static String ozBase64ToStr (String base64str ){
82- String base64Codep ;
83- try {
84- base64Codep = new String (AnalysysEncoder .decode (base64str ));
85- return base64Codep ;
86- } catch (UnsupportedEncodingException e ) {
87- e .printStackTrace ();
88- return null ;
89- }
90- }
91-
92- /**
93- * Base64编码
94- * @param str 待处理字符串
95- * @return 编码后的字符串
96- */
97- public static String ozStrToBase64 (String str ){
98- String basestr = AnalysysEncoder .encode (str .getBytes ());
99- return basestr ;
100- }
101-
102- private static char [] base64EncodeChars = new char [] {
103- 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
104- 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' ,
105- 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' ,
106- 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ,
107- 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
108- 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' ,
109- 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' ,
110- '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' };
111-
112- private static byte [] base64DecodeChars = new byte [] {
113- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
114- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
115- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 ,
116- 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 ,
117- -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ,
118- 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , -1 ,
119- -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ,
120- 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 };
121-
122126 public static String encode (byte [] data ) {
123127 StringBuffer sb = new StringBuffer ();
124128 int len = data .length ;
125129 int i = 0 ;
126130 int b1 , b2 , b3 ;
127131 while (i < len ) {
128132 b1 = data [i ++] & 0xff ;
129- if (i == len )
130- {
133+ if (i == len ) {
131134 sb .append (base64EncodeChars [b1 >>> 2 ]);
132135 sb .append (base64EncodeChars [(b1 & 0x3 ) << 4 ]);
133136 sb .append ("==" );
134137 break ;
135138 }
136139 b2 = data [i ++] & 0xff ;
137- if (i == len )
138- {
140+ if (i == len ) {
139141 sb .append (base64EncodeChars [b1 >>> 2 ]);
140142 sb .append (base64EncodeChars [((b1 & 0x03 ) << 4 ) | ((b2 & 0xf0 ) >>> 4 )]);
141143 sb .append (base64EncodeChars [(b2 & 0x0f ) << 2 ]);
@@ -150,7 +152,7 @@ public static String encode(byte[] data) {
150152 }
151153 return sb .toString ();
152154 }
153-
155+
154156 public static byte [] decode (String str ) throws UnsupportedEncodingException {
155157 StringBuffer sb = new StringBuffer ();
156158 byte [] data = str .getBytes ("US-ASCII" );
@@ -161,26 +163,38 @@ public static byte[] decode(String str) throws UnsupportedEncodingException {
161163 do {
162164 b1 = base64DecodeChars [data [i ++]];
163165 } while (i < len && b1 == -1 );
164- if (b1 == -1 ) {break ;}
166+ if (b1 == -1 ) {
167+ break ;
168+ }
165169 do {
166170 b2 = base64DecodeChars [data [i ++]];
167171 } while (i < len && b2 == -1 );
168- if (b2 == -1 ) {break ;}
169- sb .append ((char )((b1 << 2 ) | ((b2 & 0x30 ) >>> 4 )));
172+ if (b2 == -1 ) {
173+ break ;
174+ }
175+ sb .append ((char ) ((b1 << 2 ) | ((b2 & 0x30 ) >>> 4 )));
170176 do {
171177 b3 = data [i ++];
172- if (b3 == 61 ) {return sb .toString ().getBytes ("iso8859-1" );}
178+ if (b3 == 61 ) {
179+ return sb .toString ().getBytes ("iso8859-1" );
180+ }
173181 b3 = base64DecodeChars [b3 ];
174182 } while (i < len && b3 == -1 );
175- if (b3 == -1 ) {break ;}
176- sb .append ((char )(((b2 & 0x0f ) << 4 ) | ((b3 & 0x3c ) >>> 2 )));
183+ if (b3 == -1 ) {
184+ break ;
185+ }
186+ sb .append ((char ) (((b2 & 0x0f ) << 4 ) | ((b3 & 0x3c ) >>> 2 )));
177187 do {
178188 b4 = data [i ++];
179- if (b4 == 61 ) {return sb .toString ().getBytes ("iso8859-1" );}
189+ if (b4 == 61 ) {
190+ return sb .toString ().getBytes ("iso8859-1" );
191+ }
180192 b4 = base64DecodeChars [b4 ];
181193 } while (i < len && b4 == -1 );
182- if (b4 == -1 ) {break ;}
183- sb .append ((char )(((b3 & 0x03 ) << 6 ) | b4 ));
194+ if (b4 == -1 ) {
195+ break ;
196+ }
197+ sb .append ((char ) (((b3 & 0x03 ) << 6 ) | b4 ));
184198 }
185199 return sb .toString ().getBytes ("iso8859-1" );
186200 }
0 commit comments