Code:
- private static final byte SHIFT_OUT = 0x0E;
- private static final byte SHIFT_IN = 0x0F;
- private static final byte BLANK = 0x40;
-
-
- * Trim a unicode String, that might contain double byte data so it will
- * fit into the specified data length, assuming it might contain double
- * byte data.
- *
- * @param value unicode value to be trimmed
- * @param field host field that data will ultimately be stored in
- * @return unicode
- */
- public static String trimToLength(String value, AS400Text field) {
- return trimToLength(value, field.getByteLength(), field.getCcsid());
- }
-
-
- * Trim a unicode String, that might contain double byte data so it will
- * fit into the specified data length, assuming it might contain double
- * byte data.
- *
- * @param value unicode value to be trimmed
- * @param length length value should be trimmed to
- * @param ccsid ccsid of data being trimmed
- * @return unicode
- */
- public static String trimToLength(String value, int length, int ccsid) {
-
- String newValue;
- if (value.length() > length) {
- newValue = value.substring(0, length);
- } else {
- newValue = value;
- }
-
- try {
- CharConverter converter = new CharConverter(ccsid);
-
- value = " " + value;
-
- byte[] originalData = converter.stringToByteArray(value);
-
- if (ArrayUtils.contains(originalData, SHIFT_OUT)) {
-
- byte[] workingData = ArrayUtils.subarray(originalData, 0, length);
-
- int lastShiftIn = ArrayUtils.lastIndexOf(workingData, SHIFT_IN);
- int lastShiftOut = ArrayUtils.lastIndexOf(workingData, SHIFT_OUT);
-
-
-
-
-
- if (lastShiftOut > lastShiftIn) {
- boolean isEven = ((length - lastShiftOut+1)%2 == 0);
- if (!isEven) {
- workingData[length-1] = SHIFT_IN;
- } else {
- workingData[length-2] = SHIFT_IN;
- workingData[length-1] = BLANK;
- }
- }
- newValue = converter.byteArrayToString(workingData);
- }
- } catch (UnsupportedEncodingException e) {
- Logger.exception("Error calculating double byte data", e);
- }
-
-
- return newValue;
- }
-
|
|