-
Notifications
You must be signed in to change notification settings - Fork 3
/
BCDField.java
166 lines (144 loc) · 3.51 KB
/
BCDField.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* $Log: BCDField.java,v $
* Revision 1.4 2007/11/29 08:27:49 wangl
* ISO8583解析错误修改
*
* Revision 1.3 2007/11/29 08:04:08 wangl
* ISO8583解析错误修改
*
* Revision 1.2 2007/11/13 06:36:54 wangl
* 同步代码
*
* Revision 1.5 2007/11/09 07:10:49 wangl
* 加入适当的注释
*
* Revision 1.4 2007/11/09 02:40:14 wangl
* 修改BCDGroupField处理方式
*
* Revision 1.3 2007/11/08 07:13:58 wangl
* 修改错误
*
* Revision 1.2 2007/11/06 09:26:48 wangl
* 修改对齐方式写错的地方
*
* Revision 1.1 2007/11/02 07:16:55 wangl
* 添加新的重构後的代码
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
/**
* @brief
* <p>
* <b>定长BCD类型的数字字段的处理</b>
* </p>
*
* 数字型域(0-9)
*
* @author wangl
* @see
* @since 2007-9-6
*/
public class BCDField extends AField {
/**
* 补齐数字
*/
private static final char STUFF = '0';
/**
*
*
*
* 基本构造方法
*
* @param bit
* @param data_name
* @param length
* @param optional
* @param msgAttributeName
*/
public BCDField(int bit, String data_name, int length, boolean optional,
String msgAttributeName,boolean isMac) {
this(bit, data_name, length, optional, msgAttributeName,
ISO8583Util.PREFIX,isMac);
}
/**
*
*
* 是否压缩式BCD字段构造方法
*
* @param bit
* @param data_name
* @param length
* @param optional
* @param msgAttributeName
* @param isCompressed
*/
public BCDField(int bit, String data_name, int length, boolean optional,
String msgAttributeName, int align, boolean isCompressed,boolean isMac) {
super(bit, data_name, length, optional, msgAttributeName, align, isMac);
this.isCompressed = isCompressed;
}
/**
*
*
* 对齐方式构造方法
*
* @param bit
* @param data_name
* @param length
* @param optional
* @param msgAttributeName
* @param align
*/
public BCDField(int bit, String data_name, int length, boolean optional,
String msgAttributeName, int align,boolean isMac) {
super(bit, data_name, length, optional, msgAttributeName, align,isMac);
}
public boolean pack(EFTMessage msg, OutputStream out,List<String> macList,StringBuffer packLog) {
String valueTemp = (String) getAttributeValue(msg);
String value = ISO8583Util.format(valueTemp, this.length, getAlign(),
STUFF);
if (valueTemp == null) {
if (isRequired()) {
System.out.println("必选域但是EFTMessage数据为空:" + getFieldName());
}
return false;
} else {
byte[] bcdValue = ISO8583Util.createNotCompressedBCDArray(value);
if (isCompressed) {
bcdValue = ISO8583Util.getCompressedBCDArray(bcdValue,ISO8583Util.SUFFIX);
}
try {
out.write(bcdValue);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
public void unpack(InputStream in, EFTMessage msg,StringBuffer retLog) {
byte[] value = null;
if (isCompressed) {
int len = getLength() % 2 == 0 ? getLength() / 2: ((getLength() + 1) / 2);
value = new byte[len];
} else {
value = new byte[getLength()];
}
try {
in.read(value);
String valueStr = null;
if (isCompressed) {
valueStr = ISO8583Util.compressedBCD2String(value);
} else {
valueStr = ISO8583Util.notCompressedBCD2String(value);
}
String val = valueStr.substring(0, getLength());
setAttributeValue(msg, val);
} catch (IOException e) {
e.printStackTrace();
}
}
}