KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > Bits


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 /**
21  * Utility methods for packing/unpacking primitive values in/out of byte arrays
22  * using big-endian byte ordering.
23  */

24 class Bits
25 {
26     /*
27      * Methods for unpacking primitive values from byte arrays starting at
28      * given offsets.
29      */

30
31     static boolean getBoolean(byte[] b, int off)
32     {
33         return b[off] != 0;
34     }
35     
36     static char getChar(byte[] b, int off)
37     {
38         return (char) (((b[off + 1] & 0xFF) << 0) +
39             ((b[off + 0] & 0xFF) << 8));
40     }
41     
42     static short getShort(byte[] b, int off)
43     {
44         return (short) (((b[off + 1] & 0xFF) << 0) +
45             ((b[off + 0] & 0xFF) << 8));
46     }
47     
48     static int getInt(byte[] b, int off)
49     {
50         return ((b[off + 3] & 0xFF) << 0) +
51             ((b[off + 2] & 0xFF) << 8) +
52             ((b[off + 1] & 0xFF) << 16) +
53             ((b[off + 0] & 0xFF) << 24);
54     }
55     
56     static float getFloat(byte[] b, int off)
57     {
58         int i = ((b[off + 3] & 0xFF) << 0) +
59             ((b[off + 2] & 0xFF) << 8) +
60             ((b[off + 1] & 0xFF) << 16) +
61             ((b[off + 0] & 0xFF) << 24);
62         return Float.intBitsToFloat(i);
63     }
64     
65     static long getLong(byte[] b, int off)
66     {
67         return ((b[off + 7] & 0xFFL) << 0) +
68             ((b[off + 6] & 0xFFL) << 8) +
69             ((b[off + 5] & 0xFFL) << 16) +
70             ((b[off + 4] & 0xFFL) << 24) +
71             ((b[off + 3] & 0xFFL) << 32) +
72             ((b[off + 2] & 0xFFL) << 40) +
73             ((b[off + 1] & 0xFFL) << 48) +
74             ((b[off + 0] & 0xFFL) << 56);
75     }
76
77     static double getDouble(byte[] b, int off)
78     {
79         long j = ((b[off + 7] & 0xFFL) << 0) +
80             ((b[off + 6] & 0xFFL) << 8) +
81             ((b[off + 5] & 0xFFL) << 16) +
82             ((b[off + 4] & 0xFFL) << 24) +
83             ((b[off + 3] & 0xFFL) << 32) +
84             ((b[off + 2] & 0xFFL) << 40) +
85             ((b[off + 1] & 0xFFL) << 48) +
86             ((b[off + 0] & 0xFFL) << 56);
87         return Double.longBitsToDouble(j);
88     }
89     
90     /*
91      * Methods for packing primitive values into byte arrays starting at given
92      * offsets.
93      */

94
95     static void putBoolean(byte[] b, int off, boolean val)
96     {
97         b[off] = (byte) (val ? 1 : 0);
98     }
99
100     static void putChar(byte[] b, int off, char val)
101     {
102         b[off + 1] = (byte) (val >>> 0);
103         b[off + 0] = (byte) (val >>> 8);
104     }
105
106     static void putShort(byte[] b, int off, short val)
107     {
108         b[off + 1] = (byte) (val >>> 0);
109         b[off + 0] = (byte) (val >>> 8);
110     }
111
112     static void putInt(byte[] b, int off, int val)
113     {
114         b[off + 3] = (byte) (val >>> 0);
115         b[off + 2] = (byte) (val >>> 8);
116         b[off + 1] = (byte) (val >>> 16);
117         b[off + 0] = (byte) (val >>> 24);
118     }
119
120     static void putFloat(byte[] b, int off, float val)
121     {
122         int i = Float.floatToIntBits(val);
123         b[off + 3] = (byte) (i >>> 0);
124         b[off + 2] = (byte) (i >>> 8);
125         b[off + 1] = (byte) (i >>> 16);
126         b[off + 0] = (byte) (i >>> 24);
127     }
128
129     static void putLong(byte[] b, int off, long val)
130     {
131         b[off + 7] = (byte) (val >>> 0);
132         b[off + 6] = (byte) (val >>> 8);
133         b[off + 5] = (byte) (val >>> 16);
134         b[off + 4] = (byte) (val >>> 24);
135         b[off + 3] = (byte) (val >>> 32);
136         b[off + 2] = (byte) (val >>> 40);
137         b[off + 1] = (byte) (val >>> 48);
138         b[off + 0] = (byte) (val >>> 56);
139     }
140
141     static void putDouble(byte[] b, int off, double val)
142     {
143         long j = Double.doubleToLongBits(val);
144         b[off + 7] = (byte) (j >>> 0);
145         b[off + 6] = (byte) (j >>> 8);
146         b[off + 5] = (byte) (j >>> 16);
147         b[off + 4] = (byte) (j >>> 24);
148         b[off + 3] = (byte) (j >>> 32);
149         b[off + 2] = (byte) (j >>> 40);
150         b[off + 1] = (byte) (j >>> 48);
151         b[off + 0] = (byte) (j >>> 56);
152     }
153 }
154
Popular Tags