KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > FloatingPoint


1 /*
2
3    Derby - Class org.apache.derby.client.am.FloatingPoint
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client.am;
22
23 /**
24  * Converters from floating point bytes to Java <code>float</code>, <code>double</code>, or
25  * <code>java.math.BigDecimal</code>.
26  */

27 public class FloatingPoint {
28     // Hide the default constructor, this is a static class.
29
private FloatingPoint() {
30     }
31
32     /**
33      * Supported Unix Big Endian IEEE 754 floating point representation.
34      */

35     public final static int IEEE_754_FLOATING_POINT = 0x48;
36
37     //--------------------------private helper methods----------------------------
38

39     /**
40      * Convert the byte array to an int.
41      */

42     private static final int convertFromByteToInt(byte[] buffer, int offset) {
43         return (buffer[offset] << 24) |
44                 ((buffer[offset + 1] & 0xFF) << 16) |
45                 ((buffer[offset + 2] & 0xFF) << 8) |
46                 (buffer[offset + 3] & 0xFF);
47     }
48
49     /**
50      * Convert the byte array to a long.
51      */

52     private static final long convertFromByteToLong(byte[] buffer, int offset) {
53         return ((buffer[offset] & 0xFFL) << 56) |
54                 ((buffer[offset + 1] & 0xFFL) << 48) |
55                 ((buffer[offset + 2] & 0xFFL) << 40) |
56                 ((buffer[offset + 3] & 0xFFL) << 32) |
57                 ((buffer[offset + 4] & 0xFFL) << 24) |
58                 ((buffer[offset + 5] & 0xFFL) << 16) |
59                 ((buffer[offset + 6] & 0xFFL) << 8) |
60                 (buffer[offset + 7] & 0xFFL);
61     }
62
63
64     //--------------entry points for runtime representation-----------------------
65

66     /**
67      * Build a Java float from a 4-byte floating point representation.
68      * <p/>
69      * This includes DERBY types: <ul> <li> REAL <li> FLOAT(1<=n<=24) </ul>
70      *
71      * @throws IllegalArgumentException if the specified representation is not recognized.
72      */

73     public static final float getFloat(byte[] buffer, int offset) {
74         return Float.intBitsToFloat(convertFromByteToInt(buffer, offset));
75     }
76
77     /**
78      * Build a Java double from an 8-byte floating point representation.
79      * <p/>
80      * <p/>
81      * This includes DERBY types: <ul> <li> FLOAT <li> DOUBLE [PRECISION] </ul>
82      *
83      * @throws IllegalArgumentException if the specified representation is not recognized.
84      */

85     public static final double getDouble(byte[] buffer, int offset) {
86         return Double.longBitsToDouble(convertFromByteToLong(buffer, offset));
87     }
88
89     //--------------entry points for runtime representation-----------------------
90

91     /**
92      * Write a Java <code>float</code> to a 4-byte floating point representation.
93      */

94     public static final void floatToIeee754Bytes(byte[] buffer, int offset, float f) {
95         int intBits = Float.floatToIntBits(f);
96         buffer[offset] = (byte) ((intBits >>> 24) & 0xFF);
97         buffer[offset + 1] = (byte) ((intBits >>> 16) & 0xFF);
98         buffer[offset + 2] = (byte) ((intBits >>> 8) & 0xFF);
99         buffer[offset + 3] = (byte) (intBits & 0xFF);
100     }
101
102     /**
103      * Write a Java <code>double</code> to an 8-byte double precision floating point representation.
104      */

105     public static final void doubleToIeee754Bytes(byte[] buffer, int offset, double d) {
106         long longBits = Double.doubleToLongBits(d);
107         buffer[offset] = (byte) ((longBits >>> 56) & 0xFF);
108         buffer[offset + 1] = (byte) ((longBits >>> 48) & 0xFF);
109         buffer[offset + 2] = (byte) ((longBits >>> 40) & 0xFF);
110         buffer[offset + 3] = (byte) ((longBits >>> 32) & 0xFF);
111         buffer[offset + 4] = (byte) ((longBits >>> 24) & 0xFF);
112         buffer[offset + 5] = (byte) ((longBits >>> 16) & 0xFF);
113         buffer[offset + 6] = (byte) ((longBits >>> 8) & 0xFF);
114         buffer[offset + 7] = (byte) (longBits & 0xFF);
115     }
116 }
117
Popular Tags