midrange.com code scratchpad
Name:
Trim a unicode String, that might contain double byte data so it will fit into the specified data le
Scriptlanguage:
Java
Tabwidth:
4
Date:
07/02/2008 03:00:10 pm
IP:
Logged
Description:
The user was to lazy to give a description
Code:
  1.     private static final byte SHIFT_OUT = 0x0E;
  2.     private static final byte SHIFT_IN = 0x0F;
  3.     private static final byte BLANK = 0x40;
  4.  
  5.     /**
  6.      * Trim a unicode String, that might contain double byte data so it will
  7.      * fit into the specified data length, assuming it might contain double
  8.      * byte data. 
  9.      * 
  10.      * @param value unicode value to be trimmed
  11.      * @param field host field that data will ultimately be stored in
  12.      * @return unicode 
  13.      */
  14.     public static String trimToLength(String value, AS400Text field) {
  15.     return trimToLength(value, field.getByteLength(), field.getCcsid());
  16.     }
  17.     
  18.     /**
  19.      * Trim a unicode String, that might contain double byte data so it will
  20.      * fit into the specified data length, assuming it might contain double
  21.      * byte data. 
  22.      * 
  23.      * @param value unicode value to be trimmed
  24.      * @param length length value should be trimmed to
  25.      * @param ccsid ccsid of data being trimmed
  26.      * @return unicode 
  27.      */
  28.     public static String trimToLength(String value, int length, int ccsid)  {
  29.  
  30.     String newValue;
  31.     if (value.length() > length) {
  32.         newValue = value.substring(0, length);
  33.     } else {
  34.         newValue = value;
  35.     }
  36.  
  37.     try {
  38.         CharConverter converter = new CharConverter(ccsid);
  39.  
  40.         value = " " + value;
  41.         
  42.         byte[] originalData = converter.stringToByteArray(value);
  43.  
  44.         if (ArrayUtils.contains(originalData, SHIFT_OUT)) {
  45.  
  46.         byte[] workingData = ArrayUtils.subarray(originalData, 0, length);
  47.  
  48.         int lastShiftIn = ArrayUtils.lastIndexOf(workingData, SHIFT_IN);
  49.         int lastShiftOut = ArrayUtils.lastIndexOf(workingData, SHIFT_OUT);
  50.  
  51.         // if the last shift out is further in the string than the
  52.         // last shift in, then we have an unbalanced so/si pair.
  53.         // Add a shift in to the end and blank the last character
  54.         // as needed.
  55.         if (lastShiftOut > lastShiftIn) {
  56.             boolean isEven = ((length - lastShiftOut+1)%2 == 0);
  57.             if (!isEven) {
  58.             workingData[length-1] = SHIFT_IN;
  59.             } else {
  60.             workingData[length-2] = SHIFT_IN;
  61.             workingData[length-1] = BLANK;
  62.             }
  63.         }
  64.         newValue  = converter.byteArrayToString(workingData);
  65.         }
  66.     } catch (UnsupportedEncodingException e) {
  67.         Logger.exception("Error calculating double byte data", e);
  68.     }
  69.  
  70.  
  71.     return newValue;
  72.     }
  73.  
© 2004-2019 by midrange.com generated in 0.013s valid xhtml & css