KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > Bits


1 /*
2  * @(#)Bits.java 1.4 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * Utility methods for packing/unpacking primitive values in/out of byte arrays
12  * using big-endian byte ordering.
13  */

14 class Bits {
15     
16     /*
17      * Methods for unpacking primitive values from byte arrays starting at
18      * given offsets.
19      */

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

77
78     static void putBoolean(byte[] b, int off, boolean val) {
79     b[off] = (byte) (val ? 1 : 0);
80     }
81
82     static void putChar(byte[] b, int off, char val) {
83     b[off + 1] = (byte) (val >>> 0);
84     b[off + 0] = (byte) (val >>> 8);
85     }
86
87     static void putShort(byte[] b, int off, short val) {
88     b[off + 1] = (byte) (val >>> 0);
89     b[off + 0] = (byte) (val >>> 8);
90     }
91
92     static void putInt(byte[] b, int off, int val) {
93     b[off + 3] = (byte) (val >>> 0);
94     b[off + 2] = (byte) (val >>> 8);
95     b[off + 1] = (byte) (val >>> 16);
96     b[off + 0] = (byte) (val >>> 24);
97     }
98
99     static void putFloat(byte[] b, int off, float val) {
100     int i = Float.floatToIntBits(val);
101     b[off + 3] = (byte) (i >>> 0);
102     b[off + 2] = (byte) (i >>> 8);
103     b[off + 1] = (byte) (i >>> 16);
104     b[off + 0] = (byte) (i >>> 24);
105     }
106
107     static void putLong(byte[] b, int off, long val) {
108     b[off + 7] = (byte) (val >>> 0);
109     b[off + 6] = (byte) (val >>> 8);
110     b[off + 5] = (byte) (val >>> 16);
111     b[off + 4] = (byte) (val >>> 24);
112     b[off + 3] = (byte) (val >>> 32);
113     b[off + 2] = (byte) (val >>> 40);
114     b[off + 1] = (byte) (val >>> 48);
115     b[off + 0] = (byte) (val >>> 56);
116     }
117
118     static void putDouble(byte[] b, int off, double val) {
119     long j = Double.doubleToLongBits(val);
120     b[off + 7] = (byte) (j >>> 0);
121     b[off + 6] = (byte) (j >>> 8);
122     b[off + 5] = (byte) (j >>> 16);
123     b[off + 4] = (byte) (j >>> 24);
124     b[off + 3] = (byte) (j >>> 32);
125     b[off + 2] = (byte) (j >>> 40);
126     b[off + 1] = (byte) (j >>> 48);
127     b[off + 0] = (byte) (j >>> 56);
128     }
129 }
130
Popular Tags