Пример #1
0
  /**
   * Set the width of the to the desired value. Used when CASTing. Ideally we'd recycle normalize(),
   * but the behavior is different (we issue a warning instead of an error, and we aren't interested
   * in nullability).
   *
   * @param desiredWidth the desired length
   * @param desiredScale the desired scale (ignored)
   * @param errorOnTrunc throw error on truncation
   * @exception StandardException Thrown on non-zero truncation if errorOnTrunc is true
   */
  public void setWidth(
      int desiredWidth,
      int desiredScale, // Ignored
      boolean errorOnTrunc)
      throws StandardException {
    /*
     ** If the input is NULL, nothing to do.
     */
    if (getValue() == null) {
      return;
    }

    int sourceWidth = dataValue.length;

    /*
     ** If the input is shorter than the desired type,
     ** then pad with blanks to the right length.
     */
    if (sourceWidth < desiredWidth) {
      byte[] actualData = new byte[desiredWidth];
      System.arraycopy(dataValue, 0, actualData, 0, dataValue.length);
      java.util.Arrays.fill(actualData, dataValue.length, actualData.length, SQLBinary.PAD);
      dataValue = actualData;
    }
    /*
     ** Truncation?
     */
    else if (sourceWidth > desiredWidth) {
      if (errorOnTrunc) {
        // error if truncating non pad characters.
        for (int i = desiredWidth; i < dataValue.length; i++) {

          if (dataValue[i] != SQLBinary.PAD)
            throw StandardException.newException(
                SQLState.LANG_STRING_TRUNCATION,
                getTypeName(),
                StringUtil.formatForPrint(this.toString()),
                String.valueOf(desiredWidth));
        }
      }

      /*
       ** Truncate to the desired width.
       */
      truncate(sourceWidth, desiredWidth, !errorOnTrunc);
    }
  }