Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java Sun May 1 23:25:59 2005 @@ -22,618 +22,595 @@ /** * High performance converters from date/time byte encodings to JDBC Date, Time and Timestamp objects. - * <p> - * Using this class for direct date/time conversions from bytes offers - * superior performance over the alternative method of first constructing - * a Java String from the encoded bytes, and then using - * [EMAIL PROTECTED] java.sql.Date#valueOf java.sql.Date.valueOf()}, - * [EMAIL PROTECTED] java.sql.Time#valueOf java.sql.Time.valueOf()} or - * [EMAIL PROTECTED] java.sql.Timestamp#valueOf java.sql.Timestamp.valueOf()}. - * <p> + * <p/> + * Using this class for direct date/time conversions from bytes offers superior performance over the alternative method + * of first constructing a Java String from the encoded bytes, and then using [EMAIL PROTECTED] java.sql.Date#valueOf + * java.sql.Date.valueOf()}, [EMAIL PROTECTED] java.sql.Time#valueOf java.sql.Time.valueOf()} or [EMAIL PROTECTED] java.sql.Timestamp#valueOf + * java.sql.Timestamp.valueOf()}. + * <p/> */ -public class DateTime -{ +public class DateTime { - // Hide the default constructor - private DateTime () {} + // Hide the default constructor + private DateTime() { + } + + private static final int dateRepresentationLength = 10; + private static final int timeRepresentationLength = 8; + private static final int timestampRepresentationLength = 26; + + // ********************************************************* + // ********** Output converters (byte[] -> class) ********** + // ********************************************************* + + /** + * Expected character representation is DERBY string representation of a date, which is in one of the following + * format. + */ + public static final java.sql.Date dateBytesToDate(byte[] buffer, + int offset, + java.sql.Date recyclableDate) { + int year, month, day; + + String date = new String(buffer, offset, DateTime.dateRepresentationLength); + int yearIndx, monthIndx, dayIndx; + if (date.charAt(4) == '-') { + // JIS format: yyyy-mm-dd. + yearIndx = 0; + monthIndx = 5; + dayIndx = 8; + } else { + throw new java.lang.IllegalArgumentException("Unsupported date format!"); + } + + int zeroBase = ((int) '0'); + // Character arithmetic is used rather than + // the less efficient Integer.parseInt (date.substring()). + year = + 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + + 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) + + 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) + + (((int) date.charAt(yearIndx + 3)) - zeroBase) - + 1900; + month = + 10 * (((int) date.charAt(monthIndx)) - zeroBase) + + (((int) date.charAt(monthIndx + 1)) - zeroBase) - + 1; + day = + 10 * (((int) date.charAt(dayIndx)) - zeroBase) + + (((int) date.charAt(dayIndx + 1)) - zeroBase); + + if (recyclableDate == null) { + return new java.sql.Date(year, month, day); + } else { + recyclableDate.setYear(year); + recyclableDate.setMonth(month); + recyclableDate.setDate(day); + return recyclableDate; + } + } + + /** + * Expected character representation is DERBY string representation of a time, which is in one of the following + * format: hh.mm.ss. + */ + public static final java.sql.Time timeBytesToTime(byte[] buffer, + int offset, + java.sql.Time recyclableTime) { + int hour, minute, second; + + String time = new String(buffer, offset, DateTime.timeRepresentationLength); + int zeroBase = ((int) '0'); + + // compute hour. + hour = + 10 * (((int) time.charAt(0)) - zeroBase) + + (((int) time.charAt(1)) - zeroBase); + // compute minute. + minute = + 10 * (((int) time.charAt(3)) - zeroBase) + + (((int) time.charAt(4)) - zeroBase); + // compute second. + second = + 10 * (((int) time.charAt(6)) - zeroBase) + + (((int) time.charAt(7)) - zeroBase); + + if (recyclableTime == null) { + return new java.sql.Time(hour, minute, second); + } else { + recyclableTime.setHours(hour); + recyclableTime.setMinutes(minute); + recyclableTime.setSeconds(second); + return recyclableTime; + } + } + + /** + * Expected character representation is DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final java.sql.Timestamp timestampBytesToTimestamp(byte[] buffer, + int offset, + java.sql.Timestamp recyclableTimestamp) { + int year, month, day, hour, minute, second, fraction; + + String timestamp = new String(buffer, offset, DateTime.timestampRepresentationLength); + int zeroBase = ((int) '0'); + + year = + 1000 * (((int) timestamp.charAt(0)) - zeroBase) + + 100 * (((int) timestamp.charAt(1)) - zeroBase) + + 10 * (((int) timestamp.charAt(2)) - zeroBase) + + (((int) timestamp.charAt(3)) - zeroBase) - + 1900; + month = + 10 * (((int) timestamp.charAt(5)) - zeroBase) + + (((int) timestamp.charAt(6)) - zeroBase) - + 1; + day = + 10 * (((int) timestamp.charAt(8)) - zeroBase) + + (((int) timestamp.charAt(9)) - zeroBase); + hour = + 10 * (((int) timestamp.charAt(11)) - zeroBase) + + (((int) timestamp.charAt(12)) - zeroBase); + minute = + 10 * (((int) timestamp.charAt(14)) - zeroBase) + + (((int) timestamp.charAt(15)) - zeroBase); + second = + 10 * (((int) timestamp.charAt(17)) - zeroBase) + + (((int) timestamp.charAt(18)) - zeroBase); + fraction = + 100000 * (((int) timestamp.charAt(20)) - zeroBase) + + 10000 * (((int) timestamp.charAt(21)) - zeroBase) + + 1000 * (((int) timestamp.charAt(22)) - zeroBase) + + 100 * (((int) timestamp.charAt(23)) - zeroBase) + + 10 * (((int) timestamp.charAt(24)) - zeroBase) + + (((int) timestamp.charAt(25)) - zeroBase); + + if (recyclableTimestamp == null) { + return new java.sql.Timestamp(year, month, day, hour, minute, second, fraction * 1000); + } else { + recyclableTimestamp.setYear(year); + recyclableTimestamp.setMonth(month); + recyclableTimestamp.setDate(day); + recyclableTimestamp.setHours(hour); + recyclableTimestamp.setMinutes(minute); + recyclableTimestamp.setSeconds(second); + recyclableTimestamp.setNanos(fraction * 1000); + return recyclableTimestamp; + } + } + + // ******************************************************** + // ********** Input converters (class -> byte[]) ********** + // ******************************************************** + + /** + * The returned character representation is in JDBC date format: <code>yyyy-mm-dd</code> date format in DERBY string + * representation of a date. + */ + public static final int dateToDateBytes(byte[] buffer, + int offset, + java.sql.Date date) throws ConversionException { + int year = date.getYear() + 1900; + if (year > 9999) { + throw new ConversionException("Year exceeds the maximum \"9999\"."); + } + int month = date.getMonth() + 1; + int day = date.getDate(); + + char[] dateChars = new char[DateTime.dateRepresentationLength]; + int zeroBase = (int) '0'; + dateChars[0] = (char) (year / 1000 + zeroBase); + dateChars[1] = (char) ((year % 1000) / 100 + zeroBase); + dateChars[2] = (char) ((year % 100) / 10 + zeroBase); + dateChars[3] = (char) (year % 10 + +zeroBase); + dateChars[4] = '-'; + dateChars[5] = (char) (month / 10 + zeroBase); + dateChars[6] = (char) (month % 10 + zeroBase); + dateChars[7] = '-'; + dateChars[8] = (char) (day / 10 + zeroBase); + dateChars[9] = (char) (day % 10 + zeroBase); + byte[] dateBytes = (new String(dateChars)).getBytes(); + System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); + + return DateTime.dateRepresentationLength; + } + + /** + * The returned character representation is in JDBC time escape format: <code>hh:mm:ss</code>, which is the same as + * JIS time format in DERBY string representation of a time. + */ + public static final int timeToTimeBytes(byte[] buffer, + int offset, + java.sql.Time time) { + int hour = time.getHours(); + int minute = time.getMinutes(); + int second = time.getSeconds(); + + char[] timeChars = new char[DateTime.timeRepresentationLength]; + int zeroBase = (int) '0'; + timeChars[0] = (char) (hour / 10 + zeroBase); + timeChars[1] = (char) (hour % 10 + +zeroBase); + timeChars[2] = ':'; + timeChars[3] = (char) (minute / 10 + zeroBase); + timeChars[4] = (char) (minute % 10 + zeroBase); + timeChars[5] = ':'; + timeChars[6] = (char) (second / 10 + zeroBase); + timeChars[7] = (char) (second % 10 + zeroBase); + byte[] timeBytes = (new String(timeChars)).getBytes(); + System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); + + return DateTime.timeRepresentationLength; + } + + /** + * The returned character representation is in DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final int timestampToTimestampBytes(byte[] buffer, + int offset, + java.sql.Timestamp timestamp) throws ConversionException { + int year = timestamp.getYear() + 1900; + if (year > 9999) { + throw new ConversionException("Year exceeds the maximum \"9999\"."); + } + int month = timestamp.getMonth() + 1; + int day = timestamp.getDate(); + int hour = timestamp.getHours(); + int minute = timestamp.getMinutes(); + int second = timestamp.getSeconds(); + int microsecond = timestamp.getNanos() / 1000; + + char[] timestampChars = new char[DateTime.timestampRepresentationLength]; + int zeroBase = (int) '0'; + timestampChars[0] = (char) (year / 1000 + zeroBase); + timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase); + timestampChars[2] = (char) ((year % 100) / 10 + zeroBase); + timestampChars[3] = (char) (year % 10 + +zeroBase); + timestampChars[4] = '-'; + timestampChars[5] = (char) (month / 10 + zeroBase); + timestampChars[6] = (char) (month % 10 + zeroBase); + timestampChars[7] = '-'; + timestampChars[8] = (char) (day / 10 + zeroBase); + timestampChars[9] = (char) (day % 10 + zeroBase); + timestampChars[10] = '-'; + timestampChars[11] = (char) (hour / 10 + zeroBase); + timestampChars[12] = (char) (hour % 10 + zeroBase); + timestampChars[13] = '.'; + timestampChars[14] = (char) (minute / 10 + zeroBase); + timestampChars[15] = (char) (minute % 10 + zeroBase); + timestampChars[16] = '.'; + timestampChars[17] = (char) (second / 10 + zeroBase); + timestampChars[18] = (char) (second % 10 + zeroBase); + timestampChars[19] = '.'; + timestampChars[20] = (char) (microsecond / 100000 + zeroBase); + timestampChars[21] = (char) ((microsecond % 100000) / 10000 + zeroBase); + timestampChars[22] = (char) ((microsecond % 10000) / 1000 + zeroBase); + timestampChars[23] = (char) ((microsecond % 1000) / 100 + zeroBase); + timestampChars[24] = (char) ((microsecond % 100) / 10 + zeroBase); + timestampChars[25] = (char) (microsecond % 10 + zeroBase); + + byte[] timestampBytes = (new String(timestampChars)).getBytes(); + System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); + + return DateTime.timestampRepresentationLength; + } + + // ********************************************************* + // ******* CROSS output converters (byte[] -> class) ******* + // ********************************************************* + + /** + * Expected character representation is DERBY string representation of a date, which is in one of the following + * format. + */ + public static final java.sql.Timestamp dateBytesToTimestamp(byte[] buffer, + int offset, + java.sql.Timestamp recyclableTimestamp) { + int year, month, day; + + String date = new String(buffer, offset, DateTime.dateRepresentationLength); + int yearIndx, monthIndx, dayIndx; + + yearIndx = 0; + monthIndx = 5; + dayIndx = 8; + + int zeroBase = ((int) '0'); + // Character arithmetic is used rather than + // the less efficient Integer.parseInt (date.substring()). + year = + 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + + 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) + + 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) + + (((int) date.charAt(yearIndx + 3)) - zeroBase) - + 1900; + month = + 10 * (((int) date.charAt(monthIndx)) - zeroBase) + + (((int) date.charAt(monthIndx + 1)) - zeroBase) - + 1; + day = + 10 * (((int) date.charAt(dayIndx)) - zeroBase) + + (((int) date.charAt(dayIndx + 1)) - zeroBase); + + if (recyclableTimestamp == null) { + return new java.sql.Timestamp(year, month, day, 0, 0, 0, 0); + } else { + recyclableTimestamp.setYear(year); + recyclableTimestamp.setMonth(month); + recyclableTimestamp.setDate(day); + recyclableTimestamp.setHours(0); + recyclableTimestamp.setMinutes(0); + recyclableTimestamp.setSeconds(0); + recyclableTimestamp.setNanos(0); + return recyclableTimestamp; + } + } + + /** + * Expected character representation is DERBY string representation of a time, which is in one of the following + * format. + */ + public static final java.sql.Timestamp timeBytesToTimestamp(byte[] buffer, + int offset, + java.sql.Timestamp recyclableTimestamp) { + int hour, minute, second; + + String time = new String(buffer, offset, DateTime.timeRepresentationLength); + int zeroBase = ((int) '0'); + + // compute hour. + hour = + 10 * (((int) time.charAt(0)) - zeroBase) + + (((int) time.charAt(1)) - zeroBase); + // compute minute. + minute = + 10 * (((int) time.charAt(3)) - zeroBase) + + (((int) time.charAt(4)) - zeroBase); + // compute second JIS format: hh:mm:ss. + second = + 10 * (((int) time.charAt(6)) - zeroBase) + + (((int) time.charAt(7)) - zeroBase); + + if (recyclableTimestamp == null) { + return new java.sql.Timestamp(0, 0, 1, hour, minute, second, 0); + } else { + recyclableTimestamp.setYear(0); + recyclableTimestamp.setMonth(0); + recyclableTimestamp.setDate(1); + recyclableTimestamp.setHours(hour); + recyclableTimestamp.setMinutes(minute); + recyclableTimestamp.setSeconds(second); + recyclableTimestamp.setNanos(0); + return recyclableTimestamp; + } + } + + /** + * Expected character representation is DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final java.sql.Date timestampBytesToDate(byte[] buffer, + int offset, + java.sql.Date recyclableDate) { + int year, month, day; + + String timestamp = new String(buffer, offset, DateTime.timestampRepresentationLength); + int zeroBase = ((int) '0'); + + year = + 1000 * (((int) timestamp.charAt(0)) - zeroBase) + + 100 * (((int) timestamp.charAt(1)) - zeroBase) + + 10 * (((int) timestamp.charAt(2)) - zeroBase) + + (((int) timestamp.charAt(3)) - zeroBase) - + 1900; + month = + 10 * (((int) timestamp.charAt(5)) - zeroBase) + + (((int) timestamp.charAt(6)) - zeroBase) - + 1; + day = + 10 * (((int) timestamp.charAt(8)) - zeroBase) + + (((int) timestamp.charAt(9)) - zeroBase); + + if (recyclableDate == null) { + return new java.sql.Date(year, month, day); + } else { + recyclableDate.setYear(year); + recyclableDate.setMonth(month); + recyclableDate.setDate(day); + return recyclableDate; + } + } + + /** + * Expected character representation is DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final java.sql.Time timestampBytesToTime(byte[] buffer, + int offset, + java.sql.Time recyclableTime) { + int hour, minute, second; + + String timestamp = new String(buffer, offset, DateTime.timestampRepresentationLength); + int zeroBase = ((int) '0'); + + hour = + 10 * (((int) timestamp.charAt(11)) - zeroBase) + + (((int) timestamp.charAt(12)) - zeroBase); + minute = + 10 * (((int) timestamp.charAt(14)) - zeroBase) + + (((int) timestamp.charAt(15)) - zeroBase); + second = + 10 * (((int) timestamp.charAt(17)) - zeroBase) + + (((int) timestamp.charAt(18)) - zeroBase); + + if (recyclableTime == null) { + return new java.sql.Time(hour, minute, second); + } else { + recyclableTime.setYear(hour); + recyclableTime.setMonth(minute); + recyclableTime.setDate(second); + return recyclableTime; + } + } + + // ********************************************************* + // ******* CROSS input converters (class -> byte[]) ******** + // ********************************************************* + + /** + * The returned character representation is in JDBC date escape format: <code>yyyy-mm-dd</code>, which is the same + * as JIS date format in DERBY string representation of a date. + */ + public static final int timestampToDateBytes(byte[] buffer, + int offset, + java.sql.Timestamp timestamp) throws ConversionException { + int year = timestamp.getYear() + 1900; + if (year > 9999) { + throw new ConversionException("Year exceeds the maximum \"9999\"."); + } + int month = timestamp.getMonth() + 1; + int day = timestamp.getDate(); + + char[] dateChars = new char[DateTime.dateRepresentationLength]; + int zeroBase = (int) '0'; + dateChars[0] = (char) (year / 1000 + zeroBase); + dateChars[1] = (char) ((year % 1000) / 100 + zeroBase); + dateChars[2] = (char) ((year % 100) / 10 + zeroBase); + dateChars[3] = (char) (year % 10 + +zeroBase); + dateChars[4] = '-'; + dateChars[5] = (char) (month / 10 + zeroBase); + dateChars[6] = (char) (month % 10 + zeroBase); + dateChars[7] = '-'; + dateChars[8] = (char) (day / 10 + zeroBase); + dateChars[9] = (char) (day % 10 + zeroBase); + byte[] dateBytes = (new String(dateChars)).getBytes(); + System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); + + return DateTime.dateRepresentationLength; + } + + /** + * The returned character representation is in JDBC time escape format: <code>hh:mm:ss</code>, which is the same as + * JIS time format in DERBY string representation of a time. + */ + public static final int timestampToTimeBytes(byte[] buffer, + int offset, + java.sql.Timestamp timestamp) { + int hour = timestamp.getHours(); + int minute = timestamp.getMinutes(); + int second = timestamp.getSeconds(); + + char[] timeChars = new char[DateTime.timeRepresentationLength]; + int zeroBase = (int) '0'; + timeChars[0] = (char) (hour / 10 + zeroBase); + timeChars[1] = (char) (hour % 10 + +zeroBase); + timeChars[2] = ':'; + timeChars[3] = (char) (minute / 10 + zeroBase); + timeChars[4] = (char) (minute % 10 + zeroBase); + timeChars[5] = ':'; + timeChars[6] = (char) (second / 10 + zeroBase); + timeChars[7] = (char) (second % 10 + zeroBase); + byte[] timeBytes = (new String(timeChars)).getBytes(); + System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); - private static final int dateRepresentationLength = 10; - private static final int timeRepresentationLength = 8; - private static final int timestampRepresentationLength = 26; - - // ********************************************************* - // ********** Output converters (byte[] -> class) ********** - // ********************************************************* - - /** - * Expected character representation is DERBY string representation - * of a date, which is in one of the following format. - */ - public static final java.sql.Date dateBytesToDate (byte[] buffer, - int offset, - java.sql.Date recyclableDate) - { - int year, month, day; - - String date = new String (buffer, offset, DateTime.dateRepresentationLength); - int yearIndx, monthIndx, dayIndx; - if (date.charAt (4) == '-') { - // JIS format: yyyy-mm-dd. - yearIndx = 0; - monthIndx = 5; - dayIndx = 8; - } - else throw new java.lang.IllegalArgumentException ("Unsupported date format!"); - - int zeroBase = ((int) '0'); - // Character arithmetic is used rather than - // the less efficient Integer.parseInt (date.substring()). - year = - 1000*(((int) date.charAt (yearIndx)) - zeroBase) + - 100*(((int) date.charAt (yearIndx+1)) - zeroBase) + - 10*(((int) date.charAt (yearIndx+2)) - zeroBase) + - (((int) date.charAt (yearIndx+3)) - zeroBase) - - 1900; - month = - 10*(((int) date.charAt (monthIndx)) - zeroBase) + - (((int) date.charAt (monthIndx+1)) - zeroBase) - - 1; - day = - 10*(((int) date.charAt (dayIndx)) - zeroBase) + - (((int) date.charAt (dayIndx+1)) - zeroBase); - - if (recyclableDate == null) - return new java.sql.Date (year, month, day); - else { - recyclableDate.setYear (year); - recyclableDate.setMonth (month); - recyclableDate.setDate (day); - return recyclableDate; - } - } - - /** - * Expected character representation is DERBY string representation - * of a time, which is in one of the following format: hh.mm.ss. - */ - public static final java.sql.Time timeBytesToTime (byte[] buffer, - int offset, - java.sql.Time recyclableTime) - { - int hour, minute, second; - - String time = new String (buffer, offset, DateTime.timeRepresentationLength); - int zeroBase = ((int) '0'); - - // compute hour. - hour = - 10*(((int) time.charAt (0)) - zeroBase) + - (((int) time.charAt (1)) - zeroBase); - // compute minute. - minute = - 10*(((int) time.charAt (3)) - zeroBase) + - (((int) time.charAt (4)) - zeroBase); - // compute second. - second = - 10*(((int) time.charAt (6)) - zeroBase) + - (((int) time.charAt (7)) - zeroBase); - - if (recyclableTime == null) - return new java.sql.Time (hour, minute, second); - else { - recyclableTime.setHours (hour); - recyclableTime.setMinutes (minute); - recyclableTime.setSeconds (second); - return recyclableTime; - } - } - - /** - * Expected character representation is DERBY string representation - * of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - * - */ - public static final java.sql.Timestamp timestampBytesToTimestamp (byte[] buffer, - int offset, - java.sql.Timestamp recyclableTimestamp) - { - int year, month, day, hour, minute, second, fraction; - - String timestamp = new String (buffer, offset, DateTime.timestampRepresentationLength); - int zeroBase = ((int) '0'); - - year = - 1000*(((int) timestamp.charAt (0)) - zeroBase) + - 100*(((int) timestamp.charAt (1)) - zeroBase) + - 10*(((int) timestamp.charAt (2)) - zeroBase) + - (((int) timestamp.charAt (3)) - zeroBase) - - 1900; - month = - 10*(((int) timestamp.charAt (5)) - zeroBase) + - (((int) timestamp.charAt (6)) - zeroBase) - - 1; - day = - 10*(((int) timestamp.charAt (8)) - zeroBase) + - (((int) timestamp.charAt (9)) - zeroBase); - hour = - 10*(((int) timestamp.charAt (11)) - zeroBase) + - (((int) timestamp.charAt (12)) - zeroBase); - minute = - 10*(((int) timestamp.charAt (14)) - zeroBase) + - (((int) timestamp.charAt (15)) - zeroBase); - second = - 10*(((int) timestamp.charAt (17)) - zeroBase) + - (((int) timestamp.charAt (18)) - zeroBase); - fraction = - 100000*(((int) timestamp.charAt (20)) - zeroBase) + - 10000*(((int) timestamp.charAt (21)) - zeroBase) + - 1000*(((int) timestamp.charAt (22)) - zeroBase) + - 100*(((int) timestamp.charAt (23)) - zeroBase) + - 10*(((int) timestamp.charAt (24)) - zeroBase) + - (((int) timestamp.charAt (25)) - zeroBase); - - if (recyclableTimestamp == null) - return new java.sql.Timestamp (year, month, day, hour, minute, second, fraction*1000); - else { - recyclableTimestamp.setYear (year); - recyclableTimestamp.setMonth (month); - recyclableTimestamp.setDate (day); - recyclableTimestamp.setHours (hour); - recyclableTimestamp.setMinutes (minute); - recyclableTimestamp.setSeconds (second); - recyclableTimestamp.setNanos (fraction*1000); - return recyclableTimestamp; - } - } - - // ******************************************************** - // ********** Input converters (class -> byte[]) ********** - // ******************************************************** - - /** - * The returned character representation is in JDBC date format: - * <code>yyyy-mm-dd</code> date format in - * DERBY string representation of a date. - * - */ - public static final int dateToDateBytes (byte[] buffer, - int offset, - java.sql.Date date - ) throws ConversionException - { - int year = date.getYear () + 1900; - if (year > 9999) throw new ConversionException ("Year exceeds the maximum \"9999\"."); - int month = date.getMonth () + 1; - int day = date.getDate (); - - char[] dateChars = new char[DateTime.dateRepresentationLength]; - int zeroBase = (int) '0'; - dateChars[0] = (char) (year/1000 + zeroBase); - dateChars[1] = (char) ((year%1000)/100 + zeroBase); - dateChars[2] = (char) ((year%100)/10 + zeroBase); - dateChars[3] = (char) (year%10 + + zeroBase); - dateChars[4] = '-'; - dateChars[5] = (char) (month/10 + zeroBase); - dateChars[6] = (char) (month%10 + zeroBase); - dateChars[7] = '-'; - dateChars[8] = (char) (day/10 + zeroBase); - dateChars[9] = (char) (day%10 + zeroBase); - byte[] dateBytes = (new String(dateChars)).getBytes (); - System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); - - return DateTime.dateRepresentationLength; - } - - /** - * The returned character representation is in JDBC time escape format: - * <code>hh:mm:ss</code>, which is the same as JIS time format in - * DERBY string representation of a time. - * - */ - public static final int timeToTimeBytes (byte[] buffer, - int offset, - java.sql.Time time) - { - int hour = time.getHours(); - int minute = time.getMinutes(); - int second = time.getSeconds(); - - char[] timeChars = new char[DateTime.timeRepresentationLength]; - int zeroBase = (int) '0'; - timeChars[0] = (char) (hour/10 + zeroBase); - timeChars[1] = (char) (hour%10 + + zeroBase); - timeChars[2] = ':'; - timeChars[3] = (char) (minute/10 + zeroBase); - timeChars[4] = (char) (minute%10 + zeroBase); - timeChars[5] = ':'; - timeChars[6] = (char) (second/10 + zeroBase); - timeChars[7] = (char) (second%10 + zeroBase); - byte[] timeBytes = (new String(timeChars)).getBytes (); - System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); - - return DateTime.timeRepresentationLength; - } - - /** - * The returned character representation is in DERBY string representation of - * a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - */ - public static final int timestampToTimestampBytes (byte[] buffer, - int offset, - java.sql.Timestamp timestamp - ) throws ConversionException - { - int year = timestamp.getYear () + 1900; - if (year > 9999) throw new ConversionException ("Year exceeds the maximum \"9999\"."); - int month = timestamp.getMonth () + 1; - int day = timestamp.getDate (); - int hour = timestamp.getHours(); - int minute = timestamp.getMinutes(); - int second = timestamp.getSeconds(); - int microsecond = timestamp.getNanos()/1000; - - char[] timestampChars = new char[DateTime.timestampRepresentationLength]; - int zeroBase = (int) '0'; - timestampChars[0] = (char) (year/1000 + zeroBase); - timestampChars[1] = (char) ((year%1000)/100 + zeroBase); - timestampChars[2] = (char) ((year%100)/10 + zeroBase); - timestampChars[3] = (char) (year%10 + + zeroBase); - timestampChars[4] = '-'; - timestampChars[5] = (char) (month/10 + zeroBase); - timestampChars[6] = (char) (month%10 + zeroBase); - timestampChars[7] = '-'; - timestampChars[8] = (char) (day/10 + zeroBase); - timestampChars[9] = (char) (day%10 + zeroBase); - timestampChars[10] = '-'; - timestampChars[11] = (char) (hour/10 + zeroBase); - timestampChars[12] = (char) (hour%10 + zeroBase); - timestampChars[13] = '.'; - timestampChars[14] = (char) (minute/10 + zeroBase); - timestampChars[15] = (char) (minute%10 + zeroBase); - timestampChars[16] = '.'; - timestampChars[17] = (char) (second/10 + zeroBase); - timestampChars[18] = (char) (second%10 + zeroBase); - timestampChars[19] = '.'; - timestampChars[20] = (char) (microsecond/100000 + zeroBase); - timestampChars[21] = (char) ((microsecond%100000)/10000 + zeroBase); - timestampChars[22] = (char) ((microsecond%10000)/1000 + zeroBase); - timestampChars[23] = (char) ((microsecond%1000)/100 + zeroBase); - timestampChars[24] = (char) ((microsecond%100)/10 + zeroBase); - timestampChars[25] = (char) (microsecond%10 + zeroBase); - - byte[] timestampBytes = (new String(timestampChars)).getBytes (); - System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); - - return DateTime.timestampRepresentationLength; - } - - // ********************************************************* - // ******* CROSS output converters (byte[] -> class) ******* - // ********************************************************* - - /** - * Expected character representation is DERBY string representation - * of a date, which is in one of the following format. - */ - public static final java.sql.Timestamp dateBytesToTimestamp (byte[] buffer, - int offset, - java.sql.Timestamp recyclableTimestamp) - { - int year, month, day; - - String date = new String (buffer, offset, DateTime.dateRepresentationLength); - int yearIndx, monthIndx, dayIndx; - - yearIndx = 0; - monthIndx = 5; - dayIndx = 8; - - int zeroBase = ((int) '0'); - // Character arithmetic is used rather than - // the less efficient Integer.parseInt (date.substring()). - year = - 1000*(((int) date.charAt (yearIndx)) - zeroBase) + - 100*(((int) date.charAt (yearIndx+1)) - zeroBase) + - 10*(((int) date.charAt (yearIndx+2)) - zeroBase) + - (((int) date.charAt (yearIndx+3)) - zeroBase) - - 1900; - month = - 10*(((int) date.charAt (monthIndx)) - zeroBase) + - (((int) date.charAt (monthIndx+1)) - zeroBase) - - 1; - day = - 10*(((int) date.charAt (dayIndx)) - zeroBase) + - (((int) date.charAt (dayIndx+1)) - zeroBase); - - if (recyclableTimestamp == null) - return new java.sql.Timestamp (year, month, day, 0, 0, 0, 0); - else { - recyclableTimestamp.setYear (year); - recyclableTimestamp.setMonth (month); - recyclableTimestamp.setDate (day); - recyclableTimestamp.setHours (0); - recyclableTimestamp.setMinutes (0); - recyclableTimestamp.setSeconds (0); - recyclableTimestamp.setNanos (0); - return recyclableTimestamp; - } - } - - /** - * Expected character representation is DERBY string representation - * of a time, which is in one of the following format. - */ - public static final java.sql.Timestamp timeBytesToTimestamp (byte[] buffer, - int offset, - java.sql.Timestamp recyclableTimestamp) - { - int hour, minute, second; - - String time = new String (buffer, offset, DateTime.timeRepresentationLength); - int zeroBase = ((int) '0'); - - // compute hour. - hour = - 10*(((int) time.charAt (0)) - zeroBase) + - (((int) time.charAt (1)) - zeroBase); - // compute minute. - minute = - 10*(((int) time.charAt (3)) - zeroBase) + - (((int) time.charAt (4)) - zeroBase); - // compute second JIS format: hh:mm:ss. - second = - 10*(((int) time.charAt (6)) - zeroBase) + - (((int) time.charAt (7)) - zeroBase); - - if (recyclableTimestamp == null) - return new java.sql.Timestamp (0, 0, 1, hour, minute, second, 0); - else { - recyclableTimestamp.setYear (0); - recyclableTimestamp.setMonth (0); - recyclableTimestamp.setDate (1); - recyclableTimestamp.setHours (hour); - recyclableTimestamp.setMinutes (minute); - recyclableTimestamp.setSeconds (second); - recyclableTimestamp.setNanos (0); - return recyclableTimestamp; - } - } - - /** - * Expected character representation is DERBY string representation - * of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - */ - public static final java.sql.Date timestampBytesToDate (byte[] buffer, - int offset, - java.sql.Date recyclableDate) - { - int year, month, day; - - String timestamp = new String (buffer, offset, DateTime.timestampRepresentationLength); - int zeroBase = ((int) '0'); - - year = - 1000*(((int) timestamp.charAt (0)) - zeroBase) + - 100*(((int) timestamp.charAt (1)) - zeroBase) + - 10*(((int) timestamp.charAt (2)) - zeroBase) + - (((int) timestamp.charAt (3)) - zeroBase) - - 1900; - month = - 10*(((int) timestamp.charAt (5)) - zeroBase) + - (((int) timestamp.charAt (6)) - zeroBase) - - 1; - day = - 10*(((int) timestamp.charAt (8)) - zeroBase) + - (((int) timestamp.charAt (9)) - zeroBase); - - if (recyclableDate == null) - return new java.sql.Date (year, month, day); - else { - recyclableDate.setYear (year); - recyclableDate.setMonth (month); - recyclableDate.setDate (day); - return recyclableDate; - } - } - - /** - * Expected character representation is DERBY string representation - * of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - */ - public static final java.sql.Time timestampBytesToTime (byte[] buffer, - int offset, - java.sql.Time recyclableTime) - { - int hour, minute, second; - - String timestamp = new String (buffer, offset, DateTime.timestampRepresentationLength); - int zeroBase = ((int) '0'); - - hour = - 10*(((int) timestamp.charAt (11)) - zeroBase) + - (((int) timestamp.charAt (12)) - zeroBase); - minute = - 10*(((int) timestamp.charAt (14)) - zeroBase) + - (((int) timestamp.charAt (15)) - zeroBase); - second = - 10*(((int) timestamp.charAt (17)) - zeroBase) + - (((int) timestamp.charAt (18)) - zeroBase); - - if (recyclableTime == null) - return new java.sql.Time (hour, minute, second); - else { - recyclableTime.setYear (hour); - recyclableTime.setMonth (minute); - recyclableTime.setDate (second); - return recyclableTime; - } - } - - // ********************************************************* - // ******* CROSS input converters (class -> byte[]) ******** - // ********************************************************* - - /** - * The returned character representation is in JDBC date escape format: - * <code>yyyy-mm-dd</code>, which is the same as JIS date format in - * DERBY string representation of a date. - * - */ - public static final int timestampToDateBytes (byte[] buffer, - int offset, - java.sql.Timestamp timestamp - ) throws ConversionException - { - int year = timestamp.getYear () + 1900; - if (year > 9999) throw new ConversionException ("Year exceeds the maximum \"9999\"."); - int month = timestamp.getMonth () + 1; - int day = timestamp.getDate (); - - char[] dateChars = new char[DateTime.dateRepresentationLength]; - int zeroBase = (int) '0'; - dateChars[0] = (char) (year/1000 + zeroBase); - dateChars[1] = (char) ((year%1000)/100 + zeroBase); - dateChars[2] = (char) ((year%100)/10 + zeroBase); - dateChars[3] = (char) (year%10 + + zeroBase); - dateChars[4] = '-'; - dateChars[5] = (char) (month/10 + zeroBase); - dateChars[6] = (char) (month%10 + zeroBase); - dateChars[7] = '-'; - dateChars[8] = (char) (day/10 + zeroBase); - dateChars[9] = (char) (day%10 + zeroBase); - byte[] dateBytes = (new String(dateChars)).getBytes (); - System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); - - return DateTime.dateRepresentationLength; - } - - /** - * The returned character representation is in JDBC time escape format: - * <code>hh:mm:ss</code>, which is the same as JIS time format in - * DERBY string representation of a time. - * - */ - public static final int timestampToTimeBytes (byte[] buffer, - int offset, - java.sql.Timestamp timestamp) - { - int hour = timestamp.getHours(); - int minute = timestamp.getMinutes(); - int second = timestamp.getSeconds(); - - char[] timeChars = new char[DateTime.timeRepresentationLength]; - int zeroBase = (int) '0'; - timeChars[0] = (char) (hour/10 + zeroBase); - timeChars[1] = (char) (hour%10 + + zeroBase); - timeChars[2] = ':'; - timeChars[3] = (char) (minute/10 + zeroBase); - timeChars[4] = (char) (minute%10 + zeroBase); - timeChars[5] = ':'; - timeChars[6] = (char) (second/10 + zeroBase); - timeChars[7] = (char) (second%10 + zeroBase); - byte[] timeBytes = (new String(timeChars)).getBytes (); - System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); - - return DateTime.timeRepresentationLength; - } - - /** - * The returned character representation is in DERBY string representation of - * a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - * - */ - public static final int dateToTimestampBytes (byte[] buffer, - int offset, - java.sql.Date date - ) throws ConversionException - { - int year = date.getYear () + 1900; - if (year > 9999) throw new ConversionException ("Year exceeds the maximum \"9999\"."); - int month = date.getMonth () + 1; - int day = date.getDate (); - - char[] timestampChars = new char[DateTime.timestampRepresentationLength]; - int zeroBase = (int) '0'; - timestampChars[0] = (char) (year/1000 + zeroBase); - timestampChars[1] = (char) ((year%1000)/100 + zeroBase); - timestampChars[2] = (char) ((year%100)/10 + zeroBase); - timestampChars[3] = (char) (year%10 + + zeroBase); - timestampChars[4] = '-'; - timestampChars[5] = (char) (month/10 + zeroBase); - timestampChars[6] = (char) (month%10 + zeroBase); - timestampChars[7] = '-'; - timestampChars[8] = (char) (day/10 + zeroBase); - timestampChars[9] = (char) (day%10 + zeroBase); - timestampChars[10] = '-'; - timestampChars[11] = '0'; - timestampChars[12] = '0'; - timestampChars[13] = '.'; - timestampChars[14] = '0'; - timestampChars[15] = '0'; - timestampChars[16] = '.'; - timestampChars[17] = '0'; - timestampChars[18] = '0'; - timestampChars[19] = '.'; - timestampChars[20] = '0'; - timestampChars[21] = '0'; - timestampChars[22] = '0'; - timestampChars[23] = '0'; - timestampChars[24] = '0'; - timestampChars[25] = '0'; - - byte[] timestampBytes = (new String(timestampChars)).getBytes (); - System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); - - return DateTime.timestampRepresentationLength; - } - - /** - * The returned character representation is in DERBY string representation of - * a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. - * - */ - public static final int timeToTimestampBytes (byte[] buffer, - int offset, - java.sql.Time time - ) - { - int hour = time.getHours(); - int minute = time.getMinutes(); - int second = time.getSeconds(); - - char[] timestampChars = new char[DateTime.timestampRepresentationLength]; - int zeroBase = (int) '0'; - timestampChars[0] = '1'; - timestampChars[1] = '9'; - timestampChars[2] = '0'; - timestampChars[3] = '0'; - timestampChars[4] = '-'; - timestampChars[5] = '0'; - timestampChars[6] = '1'; - timestampChars[7] = '-'; - timestampChars[8] = '0'; - timestampChars[9] = '1'; - timestampChars[10] = '-'; - timestampChars[11] = (char) (hour/10 + zeroBase); - timestampChars[12] = (char) (hour%10 + zeroBase); - timestampChars[13] = '.'; - timestampChars[14] = (char) (minute/10 + zeroBase); - timestampChars[15] = (char) (minute%10 + zeroBase); - timestampChars[16] = '.'; - timestampChars[17] = (char) (second/10 + zeroBase); - timestampChars[18] = (char) (second%10 + zeroBase); - timestampChars[19] = '.'; - timestampChars[20] = '0'; - timestampChars[21] = '0'; - timestampChars[22] = '0'; - timestampChars[23] = '0'; - timestampChars[24] = '0'; - timestampChars[25] = '0'; + return DateTime.timeRepresentationLength; + } - byte[] timestampBytes = (new String(timestampChars)).getBytes (); - System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); + /** + * The returned character representation is in DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final int dateToTimestampBytes(byte[] buffer, + int offset, + java.sql.Date date) throws ConversionException { + int year = date.getYear() + 1900; + if (year > 9999) { + throw new ConversionException("Year exceeds the maximum \"9999\"."); + } + int month = date.getMonth() + 1; + int day = date.getDate(); + + char[] timestampChars = new char[DateTime.timestampRepresentationLength]; + int zeroBase = (int) '0'; + timestampChars[0] = (char) (year / 1000 + zeroBase); + timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase); + timestampChars[2] = (char) ((year % 100) / 10 + zeroBase); + timestampChars[3] = (char) (year % 10 + +zeroBase); + timestampChars[4] = '-'; + timestampChars[5] = (char) (month / 10 + zeroBase); + timestampChars[6] = (char) (month % 10 + zeroBase); + timestampChars[7] = '-'; + timestampChars[8] = (char) (day / 10 + zeroBase); + timestampChars[9] = (char) (day % 10 + zeroBase); + timestampChars[10] = '-'; + timestampChars[11] = '0'; + timestampChars[12] = '0'; + timestampChars[13] = '.'; + timestampChars[14] = '0'; + timestampChars[15] = '0'; + timestampChars[16] = '.'; + timestampChars[17] = '0'; + timestampChars[18] = '0'; + timestampChars[19] = '.'; + timestampChars[20] = '0'; + timestampChars[21] = '0'; + timestampChars[22] = '0'; + timestampChars[23] = '0'; + timestampChars[24] = '0'; + timestampChars[25] = '0'; - return DateTime.timestampRepresentationLength; - } + byte[] timestampBytes = (new String(timestampChars)).getBytes(); + System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); + + return DateTime.timestampRepresentationLength; + } + + /** + * The returned character representation is in DERBY string representation of a timestamp: + * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. + */ + public static final int timeToTimestampBytes(byte[] buffer, + int offset, + java.sql.Time time) { + int hour = time.getHours(); + int minute = time.getMinutes(); + int second = time.getSeconds(); + + char[] timestampChars = new char[DateTime.timestampRepresentationLength]; + int zeroBase = (int) '0'; + timestampChars[0] = '1'; + timestampChars[1] = '9'; + timestampChars[2] = '0'; + timestampChars[3] = '0'; + timestampChars[4] = '-'; + timestampChars[5] = '0'; + timestampChars[6] = '1'; + timestampChars[7] = '-'; + timestampChars[8] = '0'; + timestampChars[9] = '1'; + timestampChars[10] = '-'; + timestampChars[11] = (char) (hour / 10 + zeroBase); + timestampChars[12] = (char) (hour % 10 + zeroBase); + timestampChars[13] = '.'; + timestampChars[14] = (char) (minute / 10 + zeroBase); + timestampChars[15] = (char) (minute % 10 + zeroBase); + timestampChars[16] = '.'; + timestampChars[17] = (char) (second / 10 + zeroBase); + timestampChars[18] = (char) (second % 10 + zeroBase); + timestampChars[19] = '.'; + timestampChars[20] = '0'; + timestampChars[21] = '0'; + timestampChars[22] = '0'; + timestampChars[23] = '0'; + timestampChars[24] = '0'; + timestampChars[25] = '0'; + + byte[] timestampBytes = (new String(timestampChars)).getBytes(); + System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); + + return DateTime.timestampRepresentationLength; + } }
Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java Sun May 1 23:25:59 2005 @@ -20,470 +20,457 @@ package org.apache.derby.client.am; /** - * Converters from fixed point decimal bytes to <code>java.math.BigDecimal</code>, - * <code>double</code>, or <code>long</code>. + * Converters from fixed point decimal bytes to <code>java.math.BigDecimal</code>, <code>double</code>, or + * <code>long</code>. */ -public class Decimal -{ - /** - * Packed Decimal representation - */ - public final static int PACKED_DECIMAL = 0x30; - - //--------------------------private constants--------------------------------- - - private static final int[][] tenRadixMagnitude = { - { 0x3b9aca00 }, // 10^9 - { 0x0de0b6b3, 0xa7640000 }, // 10^18 - { 0x033b2e3c, 0x9fd0803c, 0xe8000000 }, // 10^27 - }; - - //--------------------------constructors-------------------------------------- - - // Hide the default constructor, this is a static class. - private Decimal() {} - - //--------------------------private helper methods---------------------------- - - /** - * Convert a range of packed nybbles (up to 9 digits without overflow) to an int. - * Note that for performance purpose, it does not do array-out-of-bound checking. - */ - private static final int packedNybblesToInt (byte[] buffer, - int offset, - int startNybble, - int numberOfNybbles) - { - int value = 0; - - int i = startNybble / 2; - if ((startNybble % 2) != 0) { - // process low nybble of the first byte if necessary. - value += buffer[offset+i] & 0x0F; - i++; - } +public class Decimal { + /** + * Packed Decimal representation + */ + public final static int PACKED_DECIMAL = 0x30; + + //--------------------------private constants--------------------------------- + + private static final int[][] tenRadixMagnitude = { + {0x3b9aca00}, // 10^9 + {0x0de0b6b3, 0xa7640000}, // 10^18 + {0x033b2e3c, 0x9fd0803c, 0xe8000000}, // 10^27 + }; + + //--------------------------constructors-------------------------------------- + + // Hide the default constructor, this is a static class. + private Decimal() { + } + + //--------------------------private helper methods---------------------------- + + /** + * Convert a range of packed nybbles (up to 9 digits without overflow) to an int. Note that for performance purpose, + * it does not do array-out-of-bound checking. + */ + private static final int packedNybblesToInt(byte[] buffer, + int offset, + int startNybble, + int numberOfNybbles) { + int value = 0; + + int i = startNybble / 2; + if ((startNybble % 2) != 0) { + // process low nybble of the first byte if necessary. + value += buffer[offset + i] & 0x0F; + i++; + } - int endNybble = startNybble + numberOfNybbles -1; - for (; i<(endNybble+1)/2; i++) { - value = value*10 + ((buffer[offset+i] & 0xF0) >>> 4); // high nybble. - value = value*10 + (buffer[offset+i] & 0x0F); // low nybble. - } + int endNybble = startNybble + numberOfNybbles - 1; + for (; i < (endNybble + 1) / 2; i++) { + value = value * 10 + ((buffer[offset + i] & 0xF0) >>> 4); // high nybble. + value = value * 10 + (buffer[offset + i] & 0x0F); // low nybble. + } - if ((endNybble % 2) == 0) { - // process high nybble of the last byte if necessary. - value = value*10 + ((buffer[offset+i] & 0xF0) >>> 4); + if ((endNybble % 2) == 0) { + // process high nybble of the last byte if necessary. + value = value * 10 + ((buffer[offset + i] & 0xF0) >>> 4); + } + + return value; } - return value; - } + /** + * Convert a range of packed nybbles (up to 18 digits without overflow) to a long. Note that for performance + * purpose, it does not do array-out-of-bound checking. + */ + private static final long packedNybblesToLong(byte[] buffer, + int offset, + int startNybble, + int numberOfNybbles) { + long value = 0; + + int i = startNybble / 2; + if ((startNybble % 2) != 0) { + // process low nybble of the first byte if necessary. + value += buffer[offset + i] & 0x0F; + i++; + } - /** - * Convert a range of packed nybbles (up to 18 digits without overflow) to a long. - * Note that for performance purpose, it does not do array-out-of-bound checking. - */ - private static final long packedNybblesToLong (byte[] buffer, - int offset, - int startNybble, - int numberOfNybbles) - { - long value = 0; - - int i = startNybble / 2; - if ((startNybble % 2) != 0) { - // process low nybble of the first byte if necessary. - value += buffer[offset+i] & 0x0F; - i++; - } + int endNybble = startNybble + numberOfNybbles - 1; + for (; i < (endNybble + 1) / 2; i++) { + value = value * 10 + ((buffer[offset + i] & 0xF0) >>> 4); // high nybble. + value = value * 10 + (buffer[offset + i] & 0x0F); // low nybble. + } + + if ((endNybble % 2) == 0) { + // process high nybble of the last byte if necessary. + value = value * 10 + ((buffer[offset + i] & 0xF0) >>> 4); + } - int endNybble = startNybble + numberOfNybbles -1; - for (; i<(endNybble+1)/2; i++) { - value = value*10 + ((buffer[offset+i] & 0xF0) >>> 4); // high nybble. - value = value*10 + (buffer[offset+i] & 0x0F); // low nybble. + return value; } - if ((endNybble % 2) == 0) { - // process high nybble of the last byte if necessary. - value = value*10 + ((buffer[offset+i] & 0xF0) >>> 4); + /** + * Compute the int array of magnitude from input value segments. + */ + private static final int[] computeMagnitude(int[] input) { + int length = input.length; + int[] mag = new int[length]; + + mag[length - 1] = input[length - 1]; + for (int i = 0; i < length - 1; i++) { + int carry = 0; + int j = tenRadixMagnitude[i].length - 1; + int k = length - 1; + for (; j >= 0; j--, k--) { + long product = (input[length - 2 - i] & 0xFFFFFFFFL) * (tenRadixMagnitude[i][j] & 0xFFFFFFFFL) + + (mag[k] & 0xFFFFFFFFL) // add previous value + + (carry & 0xFFFFFFFFL); // add carry + carry = (int) (product >>> 32); + mag[k] = (int) (product & 0xFFFFFFFFL); + } + mag[k] = (int) carry; + } + return mag; } - return value; - } + //--------------entry points for runtime representation----------------------- - /** - * Compute the int array of magnitude from input value segments. - */ - private static final int[] computeMagnitude(int[] input) - { - int length = input.length; - int[] mag = new int[length]; - - mag[length-1] = input[length-1]; - for (int i=0; i<length-1; i++) { - int carry = 0; - int j = tenRadixMagnitude[i].length-1; - int k = length-1; - for (; j>=0; j--, k--) { - long product = (input[length-2-i] & 0xFFFFFFFFL) * (tenRadixMagnitude[i][j] & 0xFFFFFFFFL) - + (mag[k] & 0xFFFFFFFFL) // add previous value - + (carry & 0xFFFFFFFFL); // add carry - carry = (int) (product >>> 32); - mag[k] = (int) (product & 0xFFFFFFFFL); - } - mag[k] = (int) carry; - } - return mag; - } - - //--------------entry points for runtime representation----------------------- - - /** - * Build a <code>java.math.BigDecimal</code> from a fixed point decimal byte representation. - * - * @exception IllegalArgumentException if the specified representation is not recognized. - */ - public static final java.math.BigDecimal getBigDecimal (byte[] buffer, - int offset, - int precision, - int scale - ) throws java.io.UnsupportedEncodingException - { - // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> - int length = precision / 2 + 1; - - // check for sign. - int signum; - if ((buffer[offset+length-1] & 0x0F) == 0x0D) - signum = -1; - else - signum = 1; - - if (precision <= 9) { - // can be handled by int without overflow. - int value = packedNybblesToInt(buffer, offset, 0, length*2-1); - - // convert value to a byte array of magnitude. - byte[] magnitude = new byte[4]; - magnitude[0] = (byte)(value >>> 24); - magnitude[1] = (byte)(value >>> 16); - magnitude[2] = (byte)(value >>> 8); - magnitude[3] = (byte)(value); + /** + * Build a <code>java.math.BigDecimal</code> from a fixed point decimal byte representation. + * + * @throws IllegalArgumentException if the specified representation is not recognized. + */ + public static final java.math.BigDecimal getBigDecimal(byte[] buffer, + int offset, + int precision, + int scale) throws java.io.UnsupportedEncodingException { + // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> + int length = precision / 2 + 1; + + // check for sign. + int signum; + if ((buffer[offset + length - 1] & 0x0F) == 0x0D) { + signum = -1; + } else { + signum = 1; + } - return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale); + if (precision <= 9) { + // can be handled by int without overflow. + int value = packedNybblesToInt(buffer, offset, 0, length * 2 - 1); + + // convert value to a byte array of magnitude. + byte[] magnitude = new byte[4]; + magnitude[0] = (byte) (value >>> 24); + magnitude[1] = (byte) (value >>> 16); + magnitude[2] = (byte) (value >>> 8); + magnitude[3] = (byte) (value); + + return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude), scale); + } else if (precision <= 18) { + // can be handled by long without overflow. + long value = packedNybblesToLong(buffer, offset, 0, length * 2 - 1); + + // convert value to a byte array of magnitude. + byte[] magnitude = new byte[8]; + magnitude[0] = (byte) (value >>> 56); + magnitude[1] = (byte) (value >>> 48); + magnitude[2] = (byte) (value >>> 40); + magnitude[3] = (byte) (value >>> 32); + magnitude[4] = (byte) (value >>> 24); + magnitude[5] = (byte) (value >>> 16); + magnitude[6] = (byte) (value >>> 8); + magnitude[7] = (byte) (value); + + return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude), scale); + } else if (precision <= 27) { + // get the value of last 9 digits (5 bytes). + int lo = packedNybblesToInt(buffer, offset, (length - 5) * 2, 9); + // get the value of another 9 digits (5 bytes). + int me = packedNybblesToInt(buffer, offset, (length - 10) * 2 + 1, 9); + // get the value of the rest digits. + int hi = packedNybblesToInt(buffer, offset, 0, (length - 10) * 2 + 1); + + // compute the int array of magnitude. + int[] value = computeMagnitude(new int[]{hi, me, lo}); + + // convert value to a byte array of magnitude. + byte[] magnitude = new byte[12]; + magnitude[0] = (byte) (value[0] >>> 24); + magnitude[1] = (byte) (value[0] >>> 16); + magnitude[2] = (byte) (value[0] >>> 8); + magnitude[3] = (byte) (value[0]); + magnitude[4] = (byte) (value[1] >>> 24); + magnitude[5] = (byte) (value[1] >>> 16); + magnitude[6] = (byte) (value[1] >>> 8); + magnitude[7] = (byte) (value[1]); + magnitude[8] = (byte) (value[2] >>> 24); + magnitude[9] = (byte) (value[2] >>> 16); + magnitude[10] = (byte) (value[2] >>> 8); + magnitude[11] = (byte) (value[2]); + + return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude), scale); + } else if (precision <= 31) { + // get the value of last 9 digits (5 bytes). + int lo = packedNybblesToInt(buffer, offset, (length - 5) * 2, 9); + // get the value of another 9 digits (5 bytes). + int meLo = packedNybblesToInt(buffer, offset, (length - 10) * 2 + 1, 9); + // get the value of another 9 digits (5 bytes). + int meHi = packedNybblesToInt(buffer, offset, (length - 14) * 2, 9); + // get the value of the rest digits. + int hi = packedNybblesToInt(buffer, offset, 0, (length - 14) * 2); + + // compute the int array of magnitude. + int[] value = computeMagnitude(new int[]{hi, meHi, meLo, lo}); + + // convert value to a byte array of magnitude. + byte[] magnitude = new byte[16]; + magnitude[0] = (byte) (value[0] >>> 24); + magnitude[1] = (byte) (value[0] >>> 16); + magnitude[2] = (byte) (value[0] >>> 8); + magnitude[3] = (byte) (value[0]); + magnitude[4] = (byte) (value[1] >>> 24); + magnitude[5] = (byte) (value[1] >>> 16); + magnitude[6] = (byte) (value[1] >>> 8); + magnitude[7] = (byte) (value[1]); + magnitude[8] = (byte) (value[2] >>> 24); + magnitude[9] = (byte) (value[2] >>> 16); + magnitude[10] = (byte) (value[2] >>> 8); + magnitude[11] = (byte) (value[2]); + magnitude[12] = (byte) (value[3] >>> 24); + magnitude[13] = (byte) (value[3] >>> 16); + magnitude[14] = (byte) (value[3] >>> 8); + magnitude[15] = (byte) (value[3]); + + return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude), scale); + } else { + // throw an exception here if nibbles is greater than 31 + throw new java.lang.IllegalArgumentException("Decimal may only be up to 31 digits!"); + } } - else if (precision <= 18) { - // can be handled by long without overflow. - long value = packedNybblesToLong(buffer, offset, 0, length*2-1); - - // convert value to a byte array of magnitude. - byte[] magnitude = new byte[8]; - magnitude[0] = (byte)(value >>> 56); - magnitude[1] = (byte)(value >>> 48); - magnitude[2] = (byte)(value >>> 40); - magnitude[3] = (byte)(value >>> 32); - magnitude[4] = (byte)(value >>> 24); - magnitude[5] = (byte)(value >>> 16); - magnitude[6] = (byte)(value >>> 8); - magnitude[7] = (byte)(value); - return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale); - } - else if (precision <= 27) { - // get the value of last 9 digits (5 bytes). - int lo = packedNybblesToInt(buffer, offset, (length-5)*2, 9); - // get the value of another 9 digits (5 bytes). - int me = packedNybblesToInt(buffer, offset, (length-10)*2+1, 9); - // get the value of the rest digits. - int hi = packedNybblesToInt(buffer, offset, 0, (length-10)*2+1); - - // compute the int array of magnitude. - int[] value = computeMagnitude(new int[] {hi, me, lo}); - - // convert value to a byte array of magnitude. - byte[] magnitude = new byte[12]; - magnitude[0] = (byte)(value[0] >>> 24); - magnitude[1] = (byte)(value[0] >>> 16); - magnitude[2] = (byte)(value[0] >>> 8); - magnitude[3] = (byte)(value[0]); - magnitude[4] = (byte)(value[1] >>> 24); - magnitude[5] = (byte)(value[1] >>> 16); - magnitude[6] = (byte)(value[1] >>> 8); - magnitude[7] = (byte)(value[1]); - magnitude[8] = (byte)(value[2] >>> 24); - magnitude[9] = (byte)(value[2] >>> 16); - magnitude[10] = (byte)(value[2] >>> 8); - magnitude[11] = (byte)(value[2]); + /** + * Build a Java <code>double</code> from a fixed point decimal byte representation. + * + * @throws IllegalArgumentException if the specified representation is not recognized. + */ + public static final double getDouble(byte[] buffer, + int offset, + int precision, + int scale) throws java.io.UnsupportedEncodingException { + // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> + int length = precision / 2 + 1; + + // check for sign. + int signum; + if ((buffer[offset + length - 1] & 0x0F) == 0x0D) { + signum = -1; + } else { + signum = 1; + } - return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale); + if (precision <= 9) { + // can be handled by int without overflow. + int value = packedNybblesToInt(buffer, offset, 0, length * 2 - 1); + + return signum * value / Math.pow(10, scale); + } else if (precision <= 18) { + // can be handled by long without overflow. + long value = packedNybblesToLong(buffer, offset, 0, length * 2 - 1); + + return signum * value / Math.pow(10, scale); + } else if (precision <= 27) { + // get the value of last 9 digits (5 bytes). + int lo = packedNybblesToInt(buffer, offset, (length - 5) * 2, 9); + // get the value of another 9 digits (5 bytes). + int me = packedNybblesToInt(buffer, offset, (length - 10) * 2 + 1, 9); + // get the value of the rest digits. + int hi = packedNybblesToInt(buffer, offset, 0, (length - 10) * 2 + 1); + + return signum * (lo / Math.pow(10, scale) + + me * Math.pow(10, 9 - scale) + + hi * Math.pow(10, 18 - scale)); + } else if (precision <= 31) { + // get the value of last 9 digits (5 bytes). + int lo = packedNybblesToInt(buffer, offset, (length - 5) * 2, 9); + // get the value of another 9 digits (5 bytes). + int meLo = packedNybblesToInt(buffer, offset, (length - 10) * 2 + 1, 9); + // get the value of another 9 digits (5 bytes). + int meHi = packedNybblesToInt(buffer, offset, (length - 14) * 2, 9); + // get the value of the rest digits. + int hi = packedNybblesToInt(buffer, offset, 0, (length - 14) * 2); + + return signum * (lo / Math.pow(10, scale) + + meLo * Math.pow(10, 9 - scale) + + meHi * Math.pow(10, 18 - scale) + + hi * Math.pow(10, 27 - scale)); + } else { + // throw an exception here if nibbles is greater than 31 + throw new java.lang.IllegalArgumentException("Decimal may only be up to 31 digits!"); + } } - else if (precision <= 31) { - // get the value of last 9 digits (5 bytes). - int lo = packedNybblesToInt(buffer, offset, (length-5)*2, 9); - // get the value of another 9 digits (5 bytes). - int meLo = packedNybblesToInt(buffer, offset, (length-10)*2+1, 9); - // get the value of another 9 digits (5 bytes). - int meHi = packedNybblesToInt(buffer, offset, (length-14)*2, 9); - // get the value of the rest digits. - int hi = packedNybblesToInt(buffer, offset, 0, (length-14)*2); - - // compute the int array of magnitude. - int[] value = computeMagnitude(new int[] {hi, meHi, meLo, lo}); - - // convert value to a byte array of magnitude. - byte[] magnitude = new byte[16]; - magnitude[0] = (byte)(value[0] >>> 24); - magnitude[1] = (byte)(value[0] >>> 16); - magnitude[2] = (byte)(value[0] >>> 8); - magnitude[3] = (byte)(value[0]); - magnitude[4] = (byte)(value[1] >>> 24); - magnitude[5] = (byte)(value[1] >>> 16); - magnitude[6] = (byte)(value[1] >>> 8); - magnitude[7] = (byte)(value[1]); - magnitude[8] = (byte)(value[2] >>> 24); - magnitude[9] = (byte)(value[2] >>> 16); - magnitude[10] = (byte)(value[2] >>> 8); - magnitude[11] = (byte)(value[2]); - magnitude[12] = (byte)(value[3] >>> 24); - magnitude[13] = (byte)(value[3] >>> 16); - magnitude[14] = (byte)(value[3] >>> 8); - magnitude[15] = (byte)(value[3]); - return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale); - } - else { - // throw an exception here if nibbles is greater than 31 - throw new java.lang.IllegalArgumentException("Decimal may only be up to 31 digits!"); - } - } + /** + * Build a Java <code>long</code> from a fixed point decimal byte representation. + * + * @throws IllegalArgumentException if the specified representation is not recognized. + */ + public static final long getLong(byte[] buffer, + int offset, + int precision, + int scale) throws java.io.UnsupportedEncodingException { + if (precision > 31) { + // throw an exception here if nibbles is greater than 31 + throw new java.lang.IllegalArgumentException("Decimal may only be up to 31 digits!"); + } - /** - * Build a Java <code>double</code> from a fixed point decimal byte representation. - * - * @exception IllegalArgumentException if the specified representation is not recognized. - */ - public static final double getDouble (byte[] buffer, - int offset, - int precision, - int scale - ) throws java.io.UnsupportedEncodingException - { - // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> - int length = precision / 2 + 1; - - // check for sign. - int signum; - if ((buffer[offset+length-1] & 0x0F) == 0x0D) - signum = -1; - else - signum = 1; - - if (precision <= 9) { - // can be handled by int without overflow. - int value = packedNybblesToInt(buffer, offset, 0, length*2-1); + // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> + int length = precision / 2 + 1; - return signum * value / Math.pow(10, scale); - } - else if (precision <= 18) { - // can be handled by long without overflow. - long value = packedNybblesToLong(buffer, offset, 0, length*2-1); + // check for sign. + int signum; + if ((buffer[offset + length - 1] & 0x0F) == 0x0D) { + signum = -1; + } else { + signum = 1; + } - return signum * value / Math.pow(10, scale); - } - else if (precision <= 27) { - // get the value of last 9 digits (5 bytes). - int lo = packedNybblesToInt(buffer, offset, (length-5)*2, 9); - // get the value of another 9 digits (5 bytes). - int me = packedNybblesToInt(buffer, offset, (length-10)*2+1, 9); - // get the value of the rest digits. - int hi = packedNybblesToInt(buffer, offset, 0, (length-10)*2+1); - - return signum * (lo / Math.pow(10, scale) + - me * Math.pow(10, 9-scale) + - hi * Math.pow(10, 18-scale)); - } - else if (precision <= 31) { - // get the value of last 9 digits (5 bytes). - int lo = packedNybblesToInt(buffer, offset, (length-5)*2, 9); - // get the value of another 9 digits (5 bytes). - int meLo = packedNybblesToInt(buffer, offset, (length-10)*2+1, 9); - // get the value of another 9 digits (5 bytes). - int meHi = packedNybblesToInt(buffer, offset, (length-14)*2, 9); - // get the value of the rest digits. - int hi = packedNybblesToInt(buffer, offset, 0, (length-14)*2); - - return signum * (lo / Math.pow(10, scale) + - meLo * Math.pow(10, 9-scale) + - meHi * Math.pow(10, 18-scale) + - hi * Math.pow(10, 27-scale)); - } - else { - // throw an exception here if nibbles is greater than 31 - throw new java.lang.IllegalArgumentException ("Decimal may only be up to 31 digits!"); - } - } + // compute the integer part only. + int leftOfDecimalPoint = length * 2 - 1 - scale; + long integer = 0; + if (leftOfDecimalPoint > 0) { + int i = 0; + for (; i < leftOfDecimalPoint / 2; i++) { + integer = integer * 10 + signum * ((buffer[offset + i] & 0xF0) >>> 4); // high nybble. + integer = integer * 10 + signum * (buffer[offset + i] & 0x0F); // low nybble. + } + if ((leftOfDecimalPoint % 2) == 1) { + // process high nybble of the last byte if necessary. + integer = integer * 10 + signum * ((buffer[offset + i] & 0xF0) >>> 4); + } + } - /** - * Build a Java <code>long</code> from a fixed point decimal byte representation. - * - * @exception IllegalArgumentException if the specified representation is not recognized. - */ - public static final long getLong (byte[] buffer, - int offset, - int precision, - int scale - ) throws java.io.UnsupportedEncodingException - { - if (precision > 31) { - // throw an exception here if nibbles is greater than 31 - throw new java.lang.IllegalArgumentException ("Decimal may only be up to 31 digits!"); + return integer; } - // The byte-length of a packed decimal with precision <code>p</code> is always <code>p/2 + 1</code> - int length = precision / 2 + 1; + //--------------entry points for runtime representation----------------------- - // check for sign. - int signum; - if ((buffer[offset+length-1] & 0x0F) == 0x0D) - signum = -1; - else - signum = 1; - - // compute the integer part only. - int leftOfDecimalPoint = length*2-1-scale; - long integer = 0; - if (leftOfDecimalPoint > 0) { - int i = 0; - for (; i<leftOfDecimalPoint/2; i++) { - integer = integer*10 + signum*((buffer[offset+i] & 0xF0) >>> 4); // high nybble. - integer = integer*10 + signum* (buffer[offset+i] & 0x0F); // low nybble. - } - if ((leftOfDecimalPoint % 2) == 1) { - // process high nybble of the last byte if necessary. - integer = integer*10 + signum*((buffer[offset+i] & 0xF0) >>> 4); - } - } + /** + * Write a Java <code>java.math.BigDecimal</code> to packed decimal bytes. + */ + public static final int bigDecimalToPackedDecimalBytes(byte[] buffer, + int offset, + java.math.BigDecimal b, + int declaredPrecision, + int declaredScale) + throws ConversionException { + // packed decimal may only be up to 31 digits. + if (declaredPrecision > 31) { + throw new ConversionException("Packed decimal may only be up to 31 digits!"); + } - return integer; - } + // get absolute unscaled value of the BigDecimal as a String. + String unscaledStr = b.unscaledValue().abs().toString(); - //--------------entry points for runtime representation----------------------- + // get precision of the BigDecimal. + int bigPrecision = unscaledStr.length(); - /** - Write a Java <code>java.math.BigDecimal</code> to packed decimal bytes. - */ - public static final int bigDecimalToPackedDecimalBytes (byte[] buffer, - int offset, - java.math.BigDecimal b, - int declaredPrecision, - int declaredScale) - throws ConversionException - { - // packed decimal may only be up to 31 digits. - if (declaredPrecision > 31) - throw new ConversionException ("Packed decimal may only be up to 31 digits!"); - - // get absolute unscaled value of the BigDecimal as a String. - String unscaledStr = b.unscaledValue().abs().toString(); - - // get precision of the BigDecimal. - int bigPrecision = unscaledStr.length(); - - if (bigPrecision > 31) - throw new ConversionException ("The numeric literal \"" + - b.toString() + - "\" is not valid because its value is out of range.", - "42820", - -405); - - int bigScale = b.scale(); - int bigWholeIntegerLength = bigPrecision - bigScale; - if ( (bigWholeIntegerLength > 0) && (!unscaledStr.equals ("0")) ) { - // if whole integer part exists, check if overflow. - int declaredWholeIntegerLength = declaredPrecision - declaredScale; - if (bigWholeIntegerLength > declaredWholeIntegerLength) - throw new ConversionException ("Overflow occurred during numeric data type conversion of \"" + - b.toString() + - "\".", - "22003", - -413); - } + if (bigPrecision > 31) { + throw new ConversionException("The numeric literal \"" + + b.toString() + + "\" is not valid because its value is out of range.", + "42820", + -405); + } - // convert the unscaled value to a packed decimal bytes. + int bigScale = b.scale(); + int bigWholeIntegerLength = bigPrecision - bigScale; + if ((bigWholeIntegerLength > 0) && (!unscaledStr.equals("0"))) { + // if whole integer part exists, check if overflow. + int declaredWholeIntegerLength = declaredPrecision - declaredScale; + if (bigWholeIntegerLength > declaredWholeIntegerLength) { + throw new ConversionException("Overflow occurred during numeric data type conversion of \"" + + b.toString() + + "\".", + "22003", + -413); + } + } - // get unicode '0' value. - int zeroBase = '0'; + // convert the unscaled value to a packed decimal bytes. - // start index in target packed decimal. - int packedIndex = declaredPrecision-1; + // get unicode '0' value. + int zeroBase = '0'; - // start index in source big decimal. - int bigIndex; - - if (bigScale >= declaredScale) { - // If target scale is less than source scale, - // discard excessive fraction. - - // set start index in source big decimal to ignore excessive fraction. - bigIndex = bigPrecision-1-(bigScale-declaredScale); - - if (bigIndex < 0) { - // all digits are discarded, so only process the sign nybble. - buffer[offset+(packedIndex+1)/2] = - (byte) ( (b.signum()>=0)?12:13 ); // sign nybble - } - else { - // process the last nybble together with the sign nybble. - buffer[offset+(packedIndex+1)/2] = - (byte) ( ( (unscaledStr.charAt(bigIndex)-zeroBase) << 4 ) + // last nybble - ( (b.signum()>=0)?12:13 ) ); // sign nybble - } - packedIndex-=2; - bigIndex-=2; - } - else { - // If target scale is greater than source scale, - // pad the fraction with zero. - - // set start index in source big decimal to pad fraction with zero. - bigIndex = declaredScale-bigScale-1; - - // process the sign nybble. - buffer[offset+(packedIndex+1)/2] = - (byte) ( (b.signum()>=0)?12:13 ); // sign nybble - - for (packedIndex-=2, bigIndex-=2; bigIndex>=0; packedIndex-=2, bigIndex-=2) - buffer[offset+(packedIndex+1)/2] = (byte) 0; - - if (bigIndex == -1) { - buffer[offset+(packedIndex+1)/2] = - (byte) ( (unscaledStr.charAt(bigPrecision-1)-zeroBase) << 4 ); // high nybble - - packedIndex-=2; - bigIndex = bigPrecision-3; - } - else { - bigIndex = bigPrecision-2; - } - } + // start index in target packed decimal. + int packedIndex = declaredPrecision - 1; - // process the rest. - for (; bigIndex>=0; packedIndex-=2, bigIndex-=2) { - buffer[offset+(packedIndex+1)/2] = - (byte) ( ( (unscaledStr.charAt(bigIndex)-zeroBase) << 4 ) + // high nybble - ( unscaledStr.charAt(bigIndex+1)-zeroBase ) ); // low nybble - } + // start index in source big decimal. + int bigIndex; + + if (bigScale >= declaredScale) { + // If target scale is less than source scale, + // discard excessive fraction. + + // set start index in source big decimal to ignore excessive fraction. + bigIndex = bigPrecision - 1 - (bigScale - declaredScale); + + if (bigIndex < 0) { + // all digits are discarded, so only process the sign nybble. + buffer[offset + (packedIndex + 1) / 2] = + (byte) ((b.signum() >= 0) ? 12 : 13); // sign nybble + } else { + // process the last nybble together with the sign nybble. + buffer[offset + (packedIndex + 1) / 2] = + (byte) (((unscaledStr.charAt(bigIndex) - zeroBase) << 4) + // last nybble + ((b.signum() >= 0) ? 12 : 13)); // sign nybble + } + packedIndex -= 2; + bigIndex -= 2; + } else { + // If target scale is greater than source scale, + // pad the fraction with zero. + + // set start index in source big decimal to pad fraction with zero. + bigIndex = declaredScale - bigScale - 1; + + // process the sign nybble. + buffer[offset + (packedIndex + 1) / 2] = + (byte) ((b.signum() >= 0) ? 12 : 13); // sign nybble + + for (packedIndex -= 2, bigIndex -= 2; bigIndex >= 0; packedIndex -= 2, bigIndex -= 2) { + buffer[offset + (packedIndex + 1) / 2] = (byte) 0; + } + + if (bigIndex == -1) { + buffer[offset + (packedIndex + 1) / 2] = + (byte) ((unscaledStr.charAt(bigPrecision - 1) - zeroBase) << 4); // high nybble + + packedIndex -= 2; + bigIndex = bigPrecision - 3; + } else { + bigIndex = bigPrecision - 2; + } + } - // process the first nybble when there is one left. - if (bigIndex == -1) { - buffer[offset+(packedIndex+1)/2] = - (byte) (unscaledStr.charAt(0) - zeroBase); + // process the rest. + for (; bigIndex >= 0; packedIndex -= 2, bigIndex -= 2) { + buffer[offset + (packedIndex + 1) / 2] = + (byte) (((unscaledStr.charAt(bigIndex) - zeroBase) << 4) + // high nybble + (unscaledStr.charAt(bigIndex + 1) - zeroBase)); // low nybble + } - packedIndex-=2; - } + // process the first nybble when there is one left. + if (bigIndex == -1) { + buffer[offset + (packedIndex + 1) / 2] = + (byte) (unscaledStr.charAt(0) - zeroBase); - // pad zero in front of the big decimal if necessary. - for (; packedIndex>=-1; packedIndex-=2) - buffer[offset+(packedIndex+1)/2] = (byte) 0; + packedIndex -= 2; + } - return declaredPrecision/2 + 1; - } + // pad zero in front of the big decimal if necessary. + for (; packedIndex >= -1; packedIndex -= 2) { + buffer[offset + (packedIndex + 1) / 2] = (byte) 0; + } + + return declaredPrecision / 2 + 1; + } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Diagnosable.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Diagnosable.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Diagnosable.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Diagnosable.java Sun May 1 23:25:59 2005 @@ -20,9 +20,10 @@ package org.apache.derby.client.am; -public interface Diagnosable -{ - public Sqlca getSqlca(); - public java.lang.Throwable getThrowable (); - public void printTrace (java.io.PrintWriter printWriter, String messageHeader); +public interface Diagnosable { + public Sqlca getSqlca(); + + public java.lang.Throwable getThrowable(); + + public void printTrace(java.io.PrintWriter printWriter, String messageHeader); }