KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > BigEndian


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.util;
19
20 public abstract class BigEndian {
21     public static byte[] getShortBytes(short value) {
22         byte[] bytes = new byte[2];
23         setShort(bytes, value);
24         return bytes;
25     }
26
27     public static byte[] getIntBytes(int value) {
28         byte[] bytes = new byte[4];
29         setInt(bytes, value);
30         return bytes;
31     }
32
33     public static byte[] getLongBytes(long value) {
34         byte[] bytes = new byte[8];
35         setLong(bytes, value);
36         return bytes;
37     }
38
39     public static byte[] get48BitLongBytes(long value) {
40         byte[] bytes = new byte[6];
41         set48BitLong(bytes, value);
42         return bytes;
43     }
44
45     public static short getShort(byte[] bytes) {
46         return getShort(bytes, 0);
47     }
48
49     public static short getShort(byte[] bytes, int offset) {
50         int b1 = (bytes[offset] << 8) & 0x0000ff00;
51         int b0 = bytes[offset + 1] & 0x000000ff;
52         return (short) (b1 | b0);
53     }
54
55     public static int getInt(byte[] bytes) {
56         return getInt(bytes, 0);
57     }
58
59     public static int getInt(byte[] bytes, int offset) {
60         int b3 = (bytes[offset] << 24) & 0xff000000;
61         int b2 = (bytes[offset + 1] << 16) & 0x00ff0000;
62         int b1 = (bytes[offset + 2] << 8) & 0x0000ff00;
63         int b0 = bytes[offset + 3] & 0x000000ff;
64         return b3 | b2 | b1 | b0;
65     }
66
67     public static long getLong(byte[] bytes) {
68         return getLong(bytes, 0);
69     }
70
71     public static long getLong(byte[] bytes, int offset) {
72         long hi = getInt(bytes, offset) & 0xffffffffL;
73         long lo = getInt(bytes, offset + 4) & 0xffffffffL;
74         return (hi << 32) | lo;
75     }
76
77     public static long get48BitLong(byte[] bytes) {
78         return get48BitLong(bytes, 0);
79     }
80
81     public static long get48BitLong(byte[] bytes, int offset) {
82         long hi = getShort(bytes, offset) & 0xffffL;
83         long lo = getInt(bytes, offset + 2) & 0xffffffffL;
84         return (hi << 32) | lo;
85     }
86
87     public static void setShort(byte[] bytes, short value) {
88         setShort(bytes, 0, value);
89     }
90
91     public static void setShort(byte[] bytes, int offset, short value) {
92         bytes[offset] = (byte) ((value >>> 8) & 0xff);
93         bytes[offset + 1] = (byte) (value & 0xff);
94     }
95
96     public static void setInt(byte[] bytes, int value) {
97         setInt(bytes, 0, value);
98     }
99
100     public static void setInt(byte[] bytes, int offset, int value) {
101         bytes[offset] = (byte) ((value >>> 24) & 0xff);
102         bytes[offset + 1] = (byte) ((value >>> 16) & 0xff);
103         bytes[offset + 2] = (byte) ((value >>> 8) & 0xff);
104         bytes[offset + 3] = (byte) (value & 0xff);
105     }
106
107     public static void setLong(byte[] bytes, long value) {
108         setLong(bytes, 0, value);
109     }
110
111     public static void setLong(byte[] bytes, int offset, long value) {
112         int hi = (int) (value >>> 32);
113         int lo = (int) value;
114         setInt(bytes, offset, hi);
115         setInt(bytes, offset + 4, lo);
116     }
117
118     public static void set48BitLong(byte[] bytes, long value) {
119         set48BitLong(bytes, 0, value);
120     }
121
122     public static void set48BitLong(byte[] bytes, int offset, long value) {
123         int hi = (int) (value >>> 32);
124         int lo = (int) value;
125         setShort(bytes, offset, (short) hi);
126         setInt(bytes, offset + 2, lo);
127     }
128 }
129
Popular Tags