|
| 1 | +package cn.com.analysys.javasdk; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.UnsupportedEncodingException; |
| 7 | +import java.util.zip.GZIPInputStream; |
| 8 | +import java.util.zip.GZIPOutputStream; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author admin |
| 12 | + */ |
| 13 | +public class AnalysysEncoder { |
| 14 | + public static final String GZIP_ENCODE_UTF_8 = "UTF-8"; |
| 15 | + |
| 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 | + * @param encoding 编码格式 |
| 39 | + * @return 编码后的字节数组 |
| 40 | + */ |
| 41 | + public static byte[] compress(String str, String encoding) { |
| 42 | + if (str == null || str.length() == 0) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 46 | + GZIPOutputStream gzip; |
| 47 | + try { |
| 48 | + gzip = new GZIPOutputStream(out); |
| 49 | + gzip.write(str.getBytes(encoding)); |
| 50 | + gzip.close(); |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + return out.toByteArray(); |
| 55 | + } |
| 56 | + |
| 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 | + } |
| 75 | + |
| 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 | + |
| 122 | + public static String encode(byte[] data) { |
| 123 | + StringBuffer sb = new StringBuffer(); |
| 124 | + int len = data.length; |
| 125 | + int i = 0; |
| 126 | + int b1, b2, b3; |
| 127 | + while (i < len) { |
| 128 | + b1 = data[i++] & 0xff; |
| 129 | + if (i == len) |
| 130 | + { |
| 131 | + sb.append(base64EncodeChars[b1 >>> 2]); |
| 132 | + sb.append(base64EncodeChars[(b1 & 0x3) << 4]); |
| 133 | + sb.append("=="); |
| 134 | + break; |
| 135 | + } |
| 136 | + b2 = data[i++] & 0xff; |
| 137 | + if (i == len) |
| 138 | + { |
| 139 | + sb.append(base64EncodeChars[b1 >>> 2]); |
| 140 | + sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); |
| 141 | + sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); |
| 142 | + sb.append("="); |
| 143 | + break; |
| 144 | + } |
| 145 | + b3 = data[i++] & 0xff; |
| 146 | + sb.append(base64EncodeChars[b1 >>> 2]); |
| 147 | + sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); |
| 148 | + sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); |
| 149 | + sb.append(base64EncodeChars[b3 & 0x3f]); |
| 150 | + } |
| 151 | + return sb.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + public static byte[] decode(String str) throws UnsupportedEncodingException { |
| 155 | + StringBuffer sb = new StringBuffer(); |
| 156 | + byte[] data = str.getBytes("US-ASCII"); |
| 157 | + int len = data.length; |
| 158 | + int i = 0; |
| 159 | + int b1, b2, b3, b4; |
| 160 | + while (i < len) { |
| 161 | + do { |
| 162 | + b1 = base64DecodeChars[data[i++]]; |
| 163 | + } while (i < len && b1 == -1); |
| 164 | + if (b1 == -1) {break;} |
| 165 | + do { |
| 166 | + b2 = base64DecodeChars[data[i++]]; |
| 167 | + } while (i < len && b2 == -1); |
| 168 | + if (b2 == -1) {break;} |
| 169 | + sb.append((char)((b1 << 2) | ((b2 & 0x30) >>> 4))); |
| 170 | + do { |
| 171 | + b3 = data[i++]; |
| 172 | + if (b3 == 61) {return sb.toString().getBytes("iso8859-1");} |
| 173 | + b3 = base64DecodeChars[b3]; |
| 174 | + } while (i < len && b3 == -1); |
| 175 | + if (b3 == -1) {break;} |
| 176 | + sb.append((char)(((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2))); |
| 177 | + do { |
| 178 | + b4 = data[i++]; |
| 179 | + if (b4 == 61) {return sb.toString().getBytes("iso8859-1");} |
| 180 | + b4 = base64DecodeChars[b4]; |
| 181 | + } while (i < len && b4 == -1); |
| 182 | + if (b4 == -1) {break;} |
| 183 | + sb.append((char)(((b3 & 0x03) << 6) | b4)); |
| 184 | + } |
| 185 | + return sb.toString().getBytes("iso8859-1"); |
| 186 | + } |
| 187 | +} |
0 commit comments