lizhanwei
2020-03-02 7a621d952fb83005ce7b6db82f9b2206746562be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.safeluck.aykj.decoder;
 
import com.safeluck.aykj.utils.BytesUtils;
 
import java.io.UnsupportedEncodingException;
 
/**
 * Created by zhouwei on 2016/12/3.
 */
 
public class GBKStringDecoder extends BaseDecoder<String> {
 
    public String stripEnd(String str, String stripChars) {
        int end;
        if(str != null && (end = str.length()) != 0) {
            if(stripChars == null) {
                while(end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
                    --end;
                }
            } else {
                if(stripChars.length() == 0) {
                    return str;
                }
 
                while(end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) {
                    --end;
                }
            }
 
            return str.substring(0, end);
        } else {
            return str;
        }
    }
 
 
    @Override
    public String decode(String str) {
 
        if(str==null)
            return null;
        byte[] bytes = BytesUtils.hexStringToBytes(str);
 
        try {
 
            String ret = new String(bytes,"gbk").trim();
            return ret;// this.stripEnd(ret,"\0");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
 
    @Override
    public String encode(String str) {
        try {
            if(str==null)
                return null;
            return BytesUtils.bytesToHexString(str.getBytes("gbk"));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}