Commit 8445fe9afb2b7d2e02ac38ace628cb01c37fe796
1 parent
f71045df
Exists in
master
1. optimize resultinfo 2. add some tool
Showing
5 changed files
with
345 additions
and
7 deletions
Show diff stats
build.gradle
... | ... | @@ -0,0 +1,176 @@ |
1 | +package com.taover.util; | |
2 | + | |
3 | +import java.io.ByteArrayOutputStream; | |
4 | +import java.io.File; | |
5 | +import java.io.FileInputStream; | |
6 | +import java.io.IOException; | |
7 | + | |
8 | +public class ToolsCharset { | |
9 | + /** | |
10 | + * 检查是否utf编码文件 | |
11 | + * @param dataFile | |
12 | + * @return | |
13 | + * @throws Exception | |
14 | + */ | |
15 | + public static boolean isUtf8File(File dataFile) throws Exception{ | |
16 | + if(!dataFile.exists()) { | |
17 | + throw new Exception("file not found"); | |
18 | + } | |
19 | + byte[] dataBuffer = readFileToByteArr(dataFile); | |
20 | + return isUtf8(dataBuffer, dataBuffer.length); | |
21 | + } | |
22 | + | |
23 | + /** | |
24 | + * 检查是否gbk编码文件 | |
25 | + * @param dataFile | |
26 | + * @return | |
27 | + * @throws Exception | |
28 | + */ | |
29 | + public static boolean isGBKFile(File dataFile) throws Exception{ | |
30 | + if(!dataFile.exists()) { | |
31 | + throw new Exception("file not found"); | |
32 | + } | |
33 | + byte[] dataBuffer = readFileToByteArr(dataFile); | |
34 | + return isGBK(dataBuffer, dataBuffer.length); | |
35 | + } | |
36 | + | |
37 | + private static byte[] readFileToByteArr(File dataFile) throws IOException { | |
38 | + FileInputStream fileIS = new FileInputStream(dataFile); | |
39 | + int estimateLen = (int)(fileIS.available() * 1.2); | |
40 | + ByteArrayOutputStream arrStream = new ByteArrayOutputStream(estimateLen); | |
41 | + byte[] buffer = null; | |
42 | + try { | |
43 | + int readLen = -1; | |
44 | + buffer = new byte[estimateLen]; | |
45 | + while((readLen = fileIS.read(buffer)) != -1) { | |
46 | + arrStream.write(buffer, 0, readLen); | |
47 | + } | |
48 | + buffer = arrStream.toByteArray(); | |
49 | + }catch (Exception e) { | |
50 | + throw e; | |
51 | + }finally { | |
52 | + arrStream.close(); | |
53 | + fileIS.close(); | |
54 | + } | |
55 | + return buffer; | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * 字符集格式,参见: | |
60 | + * http://jszx.cuit.edu.cn/NewsCont.asp?bm=00&type=1009&id=20575 | |
61 | + * 参考实现: | |
62 | + * https://www.cnblogs.com/Toney-01-22/p/9935297.html | |
63 | + * @param contentByte | |
64 | + * @return | |
65 | + */ | |
66 | + public static boolean isUtf8(byte[] contentByte, int length) { | |
67 | + //System.out.println("DATA>>>\n"+Hex.encodeHexString(contentByte)); | |
68 | + | |
69 | + int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节 | |
70 | + boolean bAllAscii = true; | |
71 | + | |
72 | + for (int i=0; i<contentByte.length && i<length; ++i) { | |
73 | + int chr = contentByte[i] & 0x0FF; | |
74 | + | |
75 | + //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx | |
76 | + if (nBytes == 0 && (chr & 0x80) != 0) { | |
77 | + bAllAscii = false; | |
78 | + } | |
79 | + | |
80 | + if (nBytes == 0) { | |
81 | + //如果不是ASCII码,应该是多字节符,计算字节数 | |
82 | + if (chr >= 0x80) { | |
83 | + if (chr >= 0xFC && chr <= 0xFD) { | |
84 | + nBytes = 6; | |
85 | + } else if (chr >= 0xF8) { | |
86 | + nBytes = 5; | |
87 | + } else if (chr >= 0xF0) { | |
88 | + nBytes = 4; | |
89 | + } else if (chr >= 0xE0) { | |
90 | + nBytes = 3; | |
91 | + } else if (chr >= 0xC0) { | |
92 | + nBytes = 2; | |
93 | + } else { | |
94 | + return false; | |
95 | + } | |
96 | + nBytes--; | |
97 | + } | |
98 | + } else { | |
99 | + //多字节符的非首字节,应为 10xxxxxx | |
100 | + if ((chr & 0xC0) != 0x80) { | |
101 | + return false; | |
102 | + } | |
103 | + //减到为零为止 | |
104 | + nBytes--; | |
105 | + } | |
106 | + } | |
107 | + | |
108 | + //违返UTF8编码规则 | |
109 | + if (nBytes != 0) { | |
110 | + return false; | |
111 | + } | |
112 | + | |
113 | + if (bAllAscii) { //如果全部都是ASCII, 也是UTF8 | |
114 | + return true; | |
115 | + } | |
116 | + | |
117 | + return true; | |
118 | + } | |
119 | + | |
120 | + /** | |
121 | + * 字符集格式,参见: | |
122 | + * https://www.qqxiuzi.cn/zh/hanzi-gbk-bianma.php | |
123 | + * 参考实现: | |
124 | + * https://www.cnblogs.com/Toney-01-22/p/9935297.html | |
125 | + * @param contentByte | |
126 | + * @return | |
127 | + */ | |
128 | + public static boolean isGBK(byte[] contentByte, int length) { | |
129 | + int nBytes = 0;//GBK可用1-2个字节编码,中文两个 ,英文一个 | |
130 | + boolean bAllAscii = true; //如果全部都是ASCII, | |
131 | + | |
132 | + for (int i=0; i<contentByte.length && i<length; ++i) { | |
133 | + int chr = contentByte[i] & 0x0FF; | |
134 | + if ((chr & 0x80) != 0 && nBytes == 0) | |
135 | + {// 判断是否ASCII编码,如果不是,说明有可能是GBK | |
136 | + bAllAscii = false; | |
137 | + } | |
138 | + | |
139 | + if (nBytes == 0) | |
140 | + { | |
141 | + if (chr >= 0x80) | |
142 | + { | |
143 | + if (chr >= 0x81 && chr <= 0xFE) | |
144 | + { | |
145 | + nBytes = +2; | |
146 | + } | |
147 | + else | |
148 | + { | |
149 | + return false; | |
150 | + } | |
151 | + nBytes--; | |
152 | + } | |
153 | + } | |
154 | + else | |
155 | + { | |
156 | + if (chr < 0x40 || chr>0xFE) | |
157 | + { | |
158 | + return false; | |
159 | + } | |
160 | + nBytes--; | |
161 | + }//else end | |
162 | + } | |
163 | + | |
164 | + if (nBytes != 0) | |
165 | + { //违返规则 | |
166 | + return false; | |
167 | + } | |
168 | + | |
169 | + if (bAllAscii) | |
170 | + { //如果全部都是ASCII, 也是GBK | |
171 | + return true; | |
172 | + } | |
173 | + | |
174 | + return true; | |
175 | + } | |
176 | +} | ... | ... |
... | ... | @@ -0,0 +1,162 @@ |
1 | +package com.taover.util; | |
2 | + | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.HashMap; | |
5 | +import java.util.Iterator; | |
6 | +import java.util.List; | |
7 | +import java.util.Map; | |
8 | + | |
9 | +import com.alibaba.fastjson.JSONArray; | |
10 | +import com.alibaba.fastjson.JSONObject; | |
11 | + | |
12 | +public class ToolsFastJson { | |
13 | + | |
14 | + public static JSONObject getPreviewObjArr(JSONObject originExcelData, int arrMaxSize) { | |
15 | + JSONObject previewObj = new JSONObject(); | |
16 | + if(originExcelData != null && !originExcelData.isEmpty()) { | |
17 | + for(String keyItem: originExcelData.keySet()) { | |
18 | + JSONArray dataItem = originExcelData.getJSONArray(keyItem); | |
19 | + int maxSize = dataItem.size() > arrMaxSize ? arrMaxSize : dataItem.size(); | |
20 | + JSONArray dataItemShunk = new JSONArray(); | |
21 | + for(int i=0; i<maxSize; ++i) { | |
22 | + dataItemShunk.add(dataItem.getJSONArray(i)); | |
23 | + } | |
24 | + previewObj.put(keyItem, dataItemShunk); | |
25 | + } | |
26 | + } | |
27 | + return previewObj; | |
28 | + } | |
29 | + | |
30 | + public static Map<String, List<List<Object>>> translateObjArrArr(JSONObject previewOriginExcelDatasJson) { | |
31 | + Map<String, List<List<Object>>> originDataMap = new HashMap<String, List<List<Object>>>(); | |
32 | + if(previewOriginExcelDatasJson != null && !previewOriginExcelDatasJson.isEmpty()) { | |
33 | + for(String keyItem: previewOriginExcelDatasJson.keySet()) { | |
34 | + originDataMap.put(keyItem, translateArrArr(previewOriginExcelDatasJson.getJSONArray(keyItem))); | |
35 | + } | |
36 | + } | |
37 | + return originDataMap; | |
38 | + } | |
39 | + | |
40 | + public static List<List<Object>> translateArrArr(JSONArray jsonArray){ | |
41 | + if(jsonArray == null) { | |
42 | + return null; | |
43 | + } | |
44 | + List<List<Object>> resultList = new ArrayList<List<Object>>(); | |
45 | + for (int i = 0; i < jsonArray.size(); i++) { | |
46 | + ArrayList<Object> columList = new ArrayList<Object>(); | |
47 | + JSONArray columArray = jsonArray.getJSONArray(i); | |
48 | + for (int j = 0; j < columArray.size(); j++) { | |
49 | + columList.add(columArray.getString(j)); | |
50 | + } | |
51 | + resultList.add(columList); | |
52 | + } | |
53 | + return resultList; | |
54 | + } | |
55 | + | |
56 | + public static boolean isJsonNull(Object data){ | |
57 | + if(data == null){ | |
58 | + return true; | |
59 | + } | |
60 | + if(!(data instanceof String) && data.toString().equals("null")){ | |
61 | + return true; | |
62 | + } | |
63 | + return false; | |
64 | + } | |
65 | + | |
66 | + public static void removeJsonNull(JSONObject data){ | |
67 | + if(data == null){ | |
68 | + return; | |
69 | + } | |
70 | + Iterator<String> keyIter = data.keySet().iterator(); | |
71 | + List<String> removeKeys = new ArrayList<String>(data.size()); | |
72 | + while(keyIter.hasNext()){ | |
73 | + String keyItem = keyIter.next(); | |
74 | + Object value = data.get(keyItem); | |
75 | + | |
76 | + if(isJsonNull(value)) { | |
77 | + removeKeys.add(keyItem); | |
78 | + continue; | |
79 | + } | |
80 | + | |
81 | + if(value instanceof JSONObject){ | |
82 | + removeJsonNull((JSONObject)value); | |
83 | + }else if(value instanceof JSONArray){ | |
84 | + removeJsonNull((JSONArray)value); | |
85 | + } | |
86 | + } | |
87 | + | |
88 | + for(String rKey: removeKeys) { | |
89 | + data.remove(rKey); | |
90 | + } | |
91 | + } | |
92 | + | |
93 | + public static void removeJsonNull(JSONArray dataArr){ | |
94 | + if(dataArr == null){ | |
95 | + return; | |
96 | + } | |
97 | + for(int i=0; i<dataArr.size(); ++i){ | |
98 | + Object dataItem = dataArr.get(i); | |
99 | + if(isJsonNull(dataItem)){ | |
100 | + dataArr.remove(i); | |
101 | + --i; | |
102 | + continue; | |
103 | + } | |
104 | + | |
105 | + if(dataItem instanceof JSONObject){ | |
106 | + removeJsonNull((JSONObject)dataItem); | |
107 | + }else if(dataItem instanceof JSONArray){ | |
108 | + removeJsonNull((JSONArray)dataItem); | |
109 | + } | |
110 | + } | |
111 | + } | |
112 | + | |
113 | + public static void replaceJsonNull(JSONObject data, Object target){ | |
114 | + if(data == null){ | |
115 | + return; | |
116 | + } | |
117 | + Iterator<String> keyIter = data.keySet().iterator(); | |
118 | + while(keyIter.hasNext()){ | |
119 | + String keyItem = keyIter.next(); | |
120 | + Object value = data.get(keyItem); | |
121 | + | |
122 | + if(isJsonNull(value)) { | |
123 | + data.put(keyItem, target); | |
124 | + continue; | |
125 | + } | |
126 | + | |
127 | + if(value instanceof JSONObject){ | |
128 | + replaceJsonNull((JSONObject)value, target); | |
129 | + }else if(value instanceof JSONArray){ | |
130 | + replaceJsonNull((JSONArray)value, target); | |
131 | + } | |
132 | + } | |
133 | + } | |
134 | + | |
135 | + public static void replaceJsonNull(JSONArray dataArr, Object target){ | |
136 | + if(dataArr == null){ | |
137 | + return; | |
138 | + } | |
139 | + for(int i=0; i<dataArr.size(); ++i){ | |
140 | + Object dataItem = dataArr.get(i); | |
141 | + if(isJsonNull(dataItem)){ | |
142 | + dataArr.set(i, target); | |
143 | + continue; | |
144 | + } | |
145 | + | |
146 | + if(dataItem instanceof JSONObject){ | |
147 | + replaceJsonNull((JSONObject)dataItem, target); | |
148 | + }else if(dataItem instanceof JSONArray){ | |
149 | + replaceJsonNull((JSONArray)dataItem, target); | |
150 | + } | |
151 | + } | |
152 | + } | |
153 | + | |
154 | + public static void main(String[] args){ | |
155 | + //JSONObject data = JSONObject.parseObject("{\"message\":\"鏂囧瓧瑙f瀽鎴愬姛\",\"status\":true,\"statusCode\":\"null\",\"result\":{\"items\":[{\"district\":\"闅嗗寲鍘縗\",\"city\":\"鎵垮痉甯俓\",\"province\":\"娌冲寳鐪乗\",\"phone\":\"13621051230\",\"name\":{\"first_name\":\"wang\",\"second_name\":\"bin\",\"full_name\":null},\"address\":\"涓滈樋瓒呮潙\"}]}}"); | |
156 | + //JSONObject data = JSONObject.parseObject("{\"full_name\":null,\"statusCode\":\"null\"}"); | |
157 | + JSONArray data = JSONArray.parseArray("[null,\"null\"]"); | |
158 | + //removeJsonNull(data); | |
159 | + replaceJsonNull(data, ""); | |
160 | + System.out.println(data.toString()); | |
161 | + } | |
162 | +} | ... | ... |
src/main/java/com/taover/util/bean/ResultInfoException.java
1 | 1 | package com.taover.util.bean; |
2 | 2 | |
3 | -public class ResultInfoException extends Exception { | |
3 | +public class ResultInfoException extends RuntimeException { | |
4 | 4 | private static final long serialVersionUID = 1L; |
5 | 5 | |
6 | 6 | private String code; |
... | ... | @@ -36,4 +36,8 @@ public class ResultInfoException extends Exception { |
36 | 36 | public void setData(Object data) { |
37 | 37 | this.data = data; |
38 | 38 | } |
39 | + | |
40 | + public ResultInfo toResultInfo() { | |
41 | + return new ResultInfo(code, error, data); | |
42 | + } | |
39 | 43 | } | ... | ... |
src/main/java/com/taover/util/bean/UtilResultInfo.java
... | ... | @@ -9,7 +9,7 @@ public class UtilResultInfo { |
9 | 9 | return getResultInfo(UtilResultInfo.FAIL, error, null); |
10 | 10 | } |
11 | 11 | |
12 | - public static ResultInfo getResultFailure(String error, Object data){ | |
12 | + public static ResultInfo getFailure(String error, Object data){ | |
13 | 13 | return getResultInfo(UtilResultInfo.FAIL, error, data); |
14 | 14 | } |
15 | 15 | |
... | ... | @@ -64,8 +64,4 @@ public class UtilResultInfo { |
64 | 64 | public static ResultInfoException getExceptionFromResultInfo(ResultInfo result){ |
65 | 65 | return getResultInfoException(result.getCode(), result.getCode(), result.getData()); |
66 | 66 | } |
67 | - | |
68 | - public static ResultInfo getResultInfoFromException(ResultInfoException exception){ | |
69 | - return getResultInfo(exception.getCode(), exception.getError(), exception.getData()); | |
70 | - } | |
71 | 67 | } | ... | ... |