001: /*
002:
003: Derby - Class org.apache.derby.client.am.FloatingPoint
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: package org.apache.derby.client.am;
022:
023: /**
024: * Converters from floating point bytes to Java <code>float</code>, <code>double</code>, or
025: * <code>java.math.BigDecimal</code>.
026: */
027: public class FloatingPoint {
028: // Hide the default constructor, this is a static class.
029: private FloatingPoint() {
030: }
031:
032: /**
033: * Supported Unix Big Endian IEEE 754 floating point representation.
034: */
035: public final static int IEEE_754_FLOATING_POINT = 0x48;
036:
037: //--------------------------private helper methods----------------------------
038:
039: /**
040: * Convert the byte array to an int.
041: */
042: private static final int convertFromByteToInt(byte[] buffer,
043: int offset) {
044: return (buffer[offset] << 24)
045: | ((buffer[offset + 1] & 0xFF) << 16)
046: | ((buffer[offset + 2] & 0xFF) << 8)
047: | (buffer[offset + 3] & 0xFF);
048: }
049:
050: /**
051: * Convert the byte array to a long.
052: */
053: private static final long convertFromByteToLong(byte[] buffer,
054: int offset) {
055: return ((buffer[offset] & 0xFFL) << 56)
056: | ((buffer[offset + 1] & 0xFFL) << 48)
057: | ((buffer[offset + 2] & 0xFFL) << 40)
058: | ((buffer[offset + 3] & 0xFFL) << 32)
059: | ((buffer[offset + 4] & 0xFFL) << 24)
060: | ((buffer[offset + 5] & 0xFFL) << 16)
061: | ((buffer[offset + 6] & 0xFFL) << 8)
062: | (buffer[offset + 7] & 0xFFL);
063: }
064:
065: //--------------entry points for runtime representation-----------------------
066:
067: /**
068: * Build a Java float from a 4-byte floating point representation.
069: * <p/>
070: * This includes DERBY types: <ul> <li> REAL <li> FLOAT(1<=n<=24) </ul>
071: *
072: * @throws IllegalArgumentException if the specified representation is not recognized.
073: */
074: public static final float getFloat(byte[] buffer, int offset) {
075: return Float
076: .intBitsToFloat(convertFromByteToInt(buffer, offset));
077: }
078:
079: /**
080: * Build a Java double from an 8-byte floating point representation.
081: * <p/>
082: * <p/>
083: * This includes DERBY types: <ul> <li> FLOAT <li> DOUBLE [PRECISION] </ul>
084: *
085: * @throws IllegalArgumentException if the specified representation is not recognized.
086: */
087: public static final double getDouble(byte[] buffer, int offset) {
088: return Double.longBitsToDouble(convertFromByteToLong(buffer,
089: offset));
090: }
091:
092: //--------------entry points for runtime representation-----------------------
093:
094: /**
095: * Write a Java <code>float</code> to a 4-byte floating point representation.
096: */
097: public static final void floatToIeee754Bytes(byte[] buffer,
098: int offset, float f) {
099: int intBits = Float.floatToIntBits(f);
100: buffer[offset] = (byte) ((intBits >>> 24) & 0xFF);
101: buffer[offset + 1] = (byte) ((intBits >>> 16) & 0xFF);
102: buffer[offset + 2] = (byte) ((intBits >>> 8) & 0xFF);
103: buffer[offset + 3] = (byte) (intBits & 0xFF);
104: }
105:
106: /**
107: * Write a Java <code>double</code> to an 8-byte double precision floating point representation.
108: */
109: public static final void doubleToIeee754Bytes(byte[] buffer,
110: int offset, double d) {
111: long longBits = Double.doubleToLongBits(d);
112: buffer[offset] = (byte) ((longBits >>> 56) & 0xFF);
113: buffer[offset + 1] = (byte) ((longBits >>> 48) & 0xFF);
114: buffer[offset + 2] = (byte) ((longBits >>> 40) & 0xFF);
115: buffer[offset + 3] = (byte) ((longBits >>> 32) & 0xFF);
116: buffer[offset + 4] = (byte) ((longBits >>> 24) & 0xFF);
117: buffer[offset + 5] = (byte) ((longBits >>> 16) & 0xFF);
118: buffer[offset + 6] = (byte) ((longBits >>> 8) & 0xFF);
119: buffer[offset + 7] = (byte) (longBits & 0xFF);
120: }
121: }
|