1
2
3
4
5
6
7
8
9
10
11 package com.legstar.coxb.convert.simple;
12
13 import java.io.UnsupportedEncodingException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import com.legstar.coxb.CobolContext;
18 import com.legstar.coxb.ICobolArrayDbcsBinding;
19 import com.legstar.coxb.ICobolDbcsBinding;
20 import com.legstar.coxb.convert.ICobolDbcsConverter;
21 import com.legstar.coxb.convert.CobolConversionException;
22 import com.legstar.coxb.host.HostData;
23 import com.legstar.coxb.host.HostException;
24
25
26
27
28
29
30
31
32 public class CobolDbcsSimpleConverter extends CobolSimpleConverter
33 implements ICobolDbcsConverter {
34
35
36 public static final char SHIFT_IN = 0x0e;
37
38
39 public static final char SHIFT_OUT = 0x0f;
40
41
42
43
44 public CobolDbcsSimpleConverter(
45 final CobolContext cobolContext) {
46 super(cobolContext);
47 }
48
49
50 public int toHost(
51 final ICobolDbcsBinding ce,
52 final byte[] hostTarget,
53 final int offset)
54 throws HostException {
55 int newOffset = 0;
56 try {
57 newOffset = toHostSingle(ce.getStringValue(),
58 getCobolContext().getHostCharsetName(),
59 ce.getByteLength(),
60 ce.isJustifiedRight(),
61 hostTarget,
62 offset);
63 } catch (CobolConversionException e) {
64 throwHostException(ce, e);
65 }
66 return newOffset;
67 }
68
69
70 public int toHost(
71 final ICobolArrayDbcsBinding ce,
72 final byte[] hostTarget,
73 final int offset,
74 final int currentOccurs)
75 throws HostException {
76 int newOffset = offset;
77 try {
78 for (String javaSource : ce.getStringList()) {
79 newOffset = toHostSingle(javaSource,
80 getCobolContext().getHostCharsetName(),
81 ce.getItemByteLength(),
82 ce.isJustifiedRight(),
83 hostTarget,
84 newOffset);
85 }
86
87 for (int i = ce.getStringList().size(); i < currentOccurs; i++) {
88 newOffset = toHostSingle("",
89 getCobolContext().getHostCharsetName(),
90 ce.getItemByteLength(),
91 ce.isJustifiedRight(),
92 hostTarget,
93 newOffset);
94 }
95 } catch (CobolConversionException e) {
96 throwHostException(ce, e);
97 }
98 return newOffset;
99 }
100
101
102 public int fromHost(
103 final ICobolDbcsBinding ce,
104 final byte[] hostSource,
105 final int offset)
106 throws HostException {
107 int newOffset = offset;
108 try {
109 String javaString = fromHostSingle(
110 getCobolContext().getHostCharsetName(),
111 ce.getByteLength(),
112 hostSource,
113 newOffset);
114 ce.setStringValue(javaString);
115 newOffset += ce.getByteLength();
116 } catch (CobolConversionException e) {
117 throwHostException(ce, e);
118 }
119 return newOffset;
120 }
121
122
123 public int fromHost(
124 final ICobolArrayDbcsBinding ce,
125 final byte[] hostSource,
126 final int offset,
127 final int currentOccurs)
128 throws HostException {
129 List < String > lArray = new ArrayList < String >();
130 int newOffset = offset;
131 try {
132 for (int i = 0; i < currentOccurs; i++) {
133 String javaString = fromHostSingle(
134 getCobolContext().getHostCharsetName(),
135 ce.getItemByteLength(),
136 hostSource,
137 newOffset);
138 lArray.add(javaString);
139 newOffset += ce.getItemByteLength();
140 }
141 ce.setStringList(lArray);
142 } catch (CobolConversionException e) {
143 throwHostException(ce, e);
144 }
145 return newOffset;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public static final int toHostSingle(
162 final String javaString,
163 final String hostCharsetName,
164 final int cobolByteLength,
165 final boolean isJustifiedRight,
166 final byte[] hostTarget,
167 final int offset)
168 throws CobolConversionException {
169
170
171 int lastOffset = offset + cobolByteLength;
172 if (lastOffset > hostTarget.length) {
173 throw (new CobolConversionException(
174 "Attempt to write past end of host source buffer",
175 new HostData(hostTarget), offset, cobolByteLength));
176 }
177
178
179
180
181
182 byte[] hostSource;
183
184
185
186
187
188 try {
189 if (javaString == null) {
190 hostSource =
191 "".getBytes(hostCharsetName);
192 } else {
193 hostSource =
194 javaString.getBytes(hostCharsetName);
195 }
196
197
198
199
200
201
202 int iSource = 0;
203 int iTarget = offset;
204 int iLength = hostSource.length;
205
206 if (iLength > 0) {
207
208
209
210
211
212 if (hostSource[0] == SHIFT_IN) {
213 iSource++;
214 }
215 if (hostSource[hostSource.length - 1] == SHIFT_OUT) {
216 iLength--;
217 }
218 }
219
220
221 if (isJustifiedRight) {
222 iTarget += pad(hostTarget,
223 iTarget, (lastOffset - iLength),
224 hostCharsetName);
225 }
226
227
228 while (iSource < iLength && iTarget < lastOffset) {
229 hostTarget[iTarget] = hostSource[iSource];
230 iSource++;
231 iTarget++;
232 }
233
234
235 if (!isJustifiedRight) {
236 pad(hostTarget,
237 iTarget, lastOffset,
238 hostCharsetName);
239 }
240 return lastOffset;
241 } catch (UnsupportedEncodingException uee) {
242 throw new CobolConversionException(
243 "UnsupportedEncodingException:" + uee.getMessage());
244 }
245
246 }
247
248
249
250
251
252
253
254
255
256
257
258 public static final String fromHostSingle(
259 final String hostCharsetName,
260 final int cobolByteLength,
261 final byte[] hostSource,
262 final int offset)
263 throws CobolConversionException {
264
265 String javaString = null;
266 int javaStringLength = cobolByteLength;
267
268
269
270
271
272
273
274 int lastOffset = offset + cobolByteLength;
275 if (lastOffset > hostSource.length) {
276 if (offset >= hostSource.length) {
277 return javaString;
278 } else {
279 javaStringLength = hostSource.length - offset;
280 }
281 }
282
283
284
285
286
287 byte[] shiftHostSource = new byte[javaStringLength + 2];
288 shiftHostSource[0] = SHIFT_IN;
289 System.arraycopy(hostSource, offset, shiftHostSource, 1,
290 javaStringLength);
291 shiftHostSource[shiftHostSource.length - 1] = SHIFT_OUT;
292 javaStringLength += 2;
293
294
295
296
297
298 try {
299 javaString = new String(
300 shiftHostSource, 0, javaStringLength,
301 hostCharsetName);
302
303
304
305
306 if (javaString.indexOf("\0") != -1) {
307 javaString = javaString.replace('\0', ' ');
308 }
309 } catch (UnsupportedEncodingException uee) {
310 throw new CobolConversionException(
311 "UnsupportedEncodingException:" + uee.getMessage());
312 }
313
314 return javaString.trim();
315 }
316
317
318
319
320
321
322
323
324
325 private static byte getPadChar(
326 final String hostCharsetName) throws UnsupportedEncodingException {
327 return " ".getBytes(hostCharsetName)[0];
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 public static int pad(
346 final byte[] bytes,
347 final int beginIndex,
348 final int endIndex,
349 final String hostCharsetName) throws UnsupportedEncodingException {
350 byte padChar = getPadChar(hostCharsetName);
351 int j = 0;
352 for (int i = beginIndex; i < endIndex; i++) {
353 bytes[i] = padChar;
354 j++;
355 }
356 return j;
357 }
358 }