1
2
3
4
5
6
7
8 package it.imolinfo.jbi4cics.typemapping.cobol;
9
10 import it.imolinfo.jbi4cics.Logger;
11 import it.imolinfo.jbi4cics.LoggerFactory;
12 import it.imolinfo.jbi4cics.exception.FormatException;
13 import it.imolinfo.jbi4cics.jbi.Messages;
14
15 import java.io.UnsupportedEncodingException;
16
17 public class StringMarshaller {
18
19
20
21
22 private static final Logger LOG
23 = LoggerFactory.getLogger(StringMarshaller.class);
24
25
26
27
28 private static final Messages MESSAGES
29 = Messages.getMessages(StringMarshaller.class);
30
31
32
33
34 public StringMarshaller(){
35 }
36
37 public static void marshallStringIntoBuffer(String value, byte[] buffer, int startingOffset, String codePage, int size, int justification, String padCharStr) throws FormatException {
38 byte[] valueArray = null;
39 try {
40 valueArray = value.getBytes(codePage);
41 } catch (UnsupportedEncodingException e) {
42 LOG.error("CIC002116_Code_page_not_supported", codePage);
43 throw new FormatException(MESSAGES.getString("CIC002116_Code_page_not_supported", codePage));
44 }
45
46 if (valueArray.length > size) {
47 LOG.error("CIC002117_Size_error", value, size);
48 throw new FormatException(MESSAGES.getString("CIC002117_Size_error", value, size));
49 }
50
51 if (valueArray.length < size && padCharStr != null) {
52 byte[] padChar;
53 try {
54 padChar = padCharStr.getBytes(codePage);
55 } catch (UnsupportedEncodingException e) {
56 LOG.error("CIC002116_Code_page_not_supported", codePage);
57 throw new FormatException(MESSAGES.getString("CIC002116_Code_page_not_supported", codePage));
58 }
59 switch (justification) {
60 case CobolTypeDescriptor.STRING_JUSTIFICATION_LEFT:
61 int padlength = size - valueArray.length;
62 byte[] padArray = new byte[padlength];
63 fillArray(padArray, padChar);
64 System.arraycopy(valueArray, 0, buffer, startingOffset, valueArray.length);
65 System.arraycopy(padArray, 0, buffer, startingOffset + valueArray.length, padArray.length);
66 break;
67 case CobolTypeDescriptor.STRING_JUSTIFICATION_CENTER:
68 int padlengthl = (size - valueArray.length) / 2;
69 int padlengthr = (size - valueArray.length) / 2 + (size - valueArray.length) % 2;
70 byte[] padArrayl = new byte[padlengthl];
71 fillArray(padArrayl, padChar);
72 byte[] padArrayr = new byte[padlengthr];
73 fillArray(padArrayr, padChar);
74 System.arraycopy(padArrayl, 0, buffer, startingOffset, padArrayl.length);
75 System.arraycopy(valueArray, 0, buffer, startingOffset + padArrayl.length, valueArray.length);
76 System.arraycopy(padArrayr, 0, buffer, startingOffset + padArrayl.length + valueArray.length, padArrayr.length);
77 break;
78 case CobolTypeDescriptor.STRING_JUSTIFICATION_RIGHT:
79 int padlengthLocal = size - valueArray.length;
80 byte [] padArrayLocal = new byte[padlengthLocal];
81 fillArray(padArrayLocal, padChar);
82 System.arraycopy(padArrayLocal,
83 0,
84 buffer,
85 startingOffset,
86 padArrayLocal.length);
87 System.arraycopy(valueArray,
88 0,
89 buffer,
90 startingOffset + padArrayLocal.length,
91 valueArray.length);
92 break;
93 default :
94 LOG.error("CIC002118_Error_code");
95 throw new FormatException(MESSAGES.getString("CIC002118_Error_code"));
96 }
97 } else {
98 System.arraycopy(valueArray, 0, buffer, startingOffset, valueArray.length);
99 }
100
101
102 }
103
104 public static String unmarshallString(byte[] buffer, int startingOffset, String codePage, int size) throws FormatException {
105 String result = null;
106 try {
107 result = new String(buffer, startingOffset, size, codePage);
108 } catch (UnsupportedEncodingException exc) {
109 LOG.error("CIC002116_Code_page_not_supported", codePage);
110 throw new FormatException(MESSAGES.getString("CIC002116_Code_page_not_supported", codePage));
111 }
112
113 return result;
114
115 }
116
117 private static void fillArray(byte[] toBeFilled, byte[] filler) {
118 for (int i = 0; i < toBeFilled.length; i += filler.length) {
119 System.arraycopy(filler, 0, toBeFilled, i * filler.length, filler.length);
120 }
121 }
122
123 }