KickJava   Java API By Example, From Geeks To Geeks.

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


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 LittleEndian {
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 short getShort(byte[] bytes) {
40         return getShort(bytes, 0);
41     }
42
43     public static short getShort(byte[] bytes, int offset) {
44         int b1 = (bytes[offset + 1] << 8) & 0x0000ff00;
45         int b0 = bytes[offset] & 0x000000ff;
46         return (short) (b1 | b0);
47     }
48
49     public static int getInt(byte[] bytes) {
50         return getInt(bytes, 0);
51     }
52
53     public static int getInt(byte[] bytes, int offset) {
54         int b3 = (bytes[offset + 3] << 24) & 0xff000000;
55         int b2 = (bytes[offset + 2] << 16) & 0x00ff0000;
56         int b1 = (bytes[offset + 1] << 8) & 0x0000ff00;
57         int b0 = bytes[offset] & 0x000000ff;
58         return b3 | b2 | b1 | b0;
59     }
60
61     public static long getLong(byte[] bytes) {
62         return getLong(bytes, 0);
63     }
64
65     public static long getLong(byte[] bytes, int offset) {
66         long hi = getInt(bytes, offset + 4) & 0xffffffffL;
67         long lo = getInt(bytes, offset) & 0xffffffffL;
68         return (lo << 32) | hi;
69     }
70
71     public static void setShort(byte[] bytes, short value) {
72         setShort(bytes, 0, value);
73     }
74
75     public static void setShort(byte[] bytes, int offset, short value) {
76         bytes[offset] = (byte) (value & 0xff);
77         bytes[offset + 1] = (byte) ((value >>> 8) & 0xff);
78     }
79
80     public static void setInt(byte[] bytes, int value) {
81         setInt(bytes, 0, value);
82     }
83
84     public static void setInt(byte[] bytes, int offset, int value) {
85         bytes[offset] = (byte) (value & 0xff);
86         bytes[offset + 1] = (byte) ((value >>> 8) & 0xff);
87         bytes[offset + 2] = (byte) ((value >>> 16) & 0xff);
88         bytes[offset + 3] = (byte) ((value >>> 24) & 0xff);
89     }
90
91     public static void setLong(byte[] bytes, long value) {
92         setLong(bytes, 0, value);
93     }
94
95     public static void setLong(byte[] bytes, int offset, long value) {
96         int hi = (int) (value >>> 32);
97         int lo = (int) value;
98         setInt(bytes, offset + 4, hi);
99         setInt(bytes, offset, lo);
100     }
101 }
102
Popular Tags