KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > ByteSwapper


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

16  
17 package org.python.core;
18
19 import java.lang.reflect.Array JavaDoc;
20
21 /**
22  * Simple class that provides the capability to swap or reverse the byte order
23  * of all elements of an <code>Array</code>. Used to convert from one endian
24  * type to another. The class swaps the following types:
25  * <ul>
26  * <li>short</li>
27  * <li>integer</li>
28  * <li>long</li>
29  * <li>float</li>
30  * <li>double</li>
31  * </ul><p />
32  * Note this functionality is provided in the base types since 1.5.
33  *
34  * @author Andrew Howard
35  */

36 public class ByteSwapper {
37     
38     /**
39      * Reverses the byte order of all elements in the supplied array,
40      * converting between little and big endian byte order.
41      *
42      * @param array the input array for type sensitive byte swapping.
43      */

44     public static void swap(Object JavaDoc array) {
45         Class JavaDoc arrayType = array.getClass().getComponentType();
46
47         if (arrayType.isPrimitive()) {
48             if (arrayType == Boolean.TYPE)
49                 return;
50             else if (arrayType == Byte.TYPE)
51                 return;
52             else if (arrayType == Character.TYPE)
53                 return;
54             else if (arrayType == Short.TYPE)
55                 swapShortArray(array);
56             else if (arrayType == Integer.TYPE)
57                 swapIntegerArray(array);
58             else if (arrayType == Long.TYPE)
59                 swapLongArray(array);
60             else if (arrayType == Float.TYPE)
61                 swapFloatArray(array);
62             else if (arrayType == Double.TYPE)
63                 swapDoubleArray(array);
64         }
65         
66     }
67     
68     /**
69      * Byte order reverses an <code>Array</code> of <code>doubles</code>
70      *
71      * @param array input array
72      */

73     private static void swapDoubleArray(Object JavaDoc array) {
74         int len = Array.getLength(array);
75         double dtmp;
76         long tmp;
77         long b1, b2, b3, b4, b5, b6, b7, b8;
78         
79         for(int i = 0; i < len; i++) {
80             dtmp = Array.getDouble(array, i);
81             tmp = Double.doubleToLongBits(dtmp);
82             
83             b1 = (tmp >> 0) & 0xff;
84             b2 = (tmp >> 8) & 0xff;
85             b3 = (tmp >> 16) & 0xff;
86             b4 = (tmp >> 24) & 0xff;
87             b5 = (tmp >> 32) & 0xff;
88             b6 = (tmp >> 40) & 0xff;
89             b7 = (tmp >> 48) & 0xff;
90             b8 = (tmp >> 56) & 0xff;
91             tmp = (long)(b1<<56 | b2<<48 | b3<<40 | b4<<32 |
92                          b5<<24 | b6<<16 | b7<<8 | b8<<0);
93             
94             dtmp = Double.longBitsToDouble(tmp);
95             Array.setDouble(array, i, dtmp);
96         }
97     }
98
99     /**
100      * Byte order reverses an <code>Array</code> of <code>floats</code>
101      *
102      * @param array input array
103      */

104     private static void swapFloatArray(Object JavaDoc array) {
105         int len = Array.getLength(array);
106         float ftmp;
107         int tmp;
108         int b1, b2, b3, b4;
109         
110         for(int i = 0; i < len; i++) {
111             ftmp = Array.getFloat(array, i);
112             tmp = Float.floatToIntBits(ftmp);
113             
114             b1 = (tmp >> 0) & 0xff;
115             b2 = (tmp >> 8) & 0xff;
116             b3 = (tmp >> 16) & 0xff;
117             b4 = (tmp >> 24) & 0xff;
118             tmp = (int)(b1<<24 | b2<<16 | b3<<8 | b4<<0);
119             
120             ftmp = Float.intBitsToFloat(tmp);
121             Array.setFloat(array, i, ftmp);
122         }
123     }
124
125     /**
126      * Byte order reverses an <code>Array</code> of <code>ints</code>
127      *
128      * @param array input array
129      */

130     private static void swapIntegerArray(Object JavaDoc array) {
131         int len = Array.getLength(array);
132         int tmp;
133         int b1, b2, b3, b4;
134         
135         for(int i = 0; i < len; i++) {
136             tmp = Array.getInt(array, i);
137             
138             b1 = (tmp >> 0) & 0xff;
139             b2 = (tmp >> 8) & 0xff;
140             b3 = (tmp >> 16) & 0xff;
141             b4 = (tmp >> 24) & 0xff;
142             tmp = (int)(b1<<24 | b2<<16 | b3<<8 | b4<<0);
143             
144             Array.setInt(array, i, tmp);
145         }
146     }
147
148     /**
149      * Byte order reverses an <code>Array</code> of <code>longs</code>
150      *
151      * @param array input array
152      */

153     private static void swapLongArray(Object JavaDoc array) {
154         int len = Array.getLength(array);
155         long tmp;
156         long b1, b2, b3, b4, b5, b6, b7, b8;
157         
158         for(int i = 0; i < len; i++) {
159             tmp = Array.getLong(array, i);
160             
161             b1 = (tmp >> 0) & 0xff;
162             b2 = (tmp >> 8) & 0xff;
163             b3 = (tmp >> 16) & 0xff;
164             b4 = (tmp >> 24) & 0xff;
165             b5 = (tmp >> 32) & 0xff;
166             b6 = (tmp >> 40) & 0xff;
167             b7 = (tmp >> 48) & 0xff;
168             b8 = (tmp >> 56) & 0xff;
169             tmp = (long)(b1<<56 | b2<<48 | b3<<40 | b4<<32 |
170                          b5<<24 | b6<<16 | b7<<8 | b8<<0);
171             
172             Array.setLong(array, i, tmp);
173         }
174     }
175
176     /**
177      * Byte order reverses an <code>Array</code> of <code>shorts</code>
178      *
179      * @param array input array
180      */

181     private static void swapShortArray(Object JavaDoc array) {
182         int len = Array.getLength(array);
183         short tmp;
184         int b1, b2;
185         
186         for(int i = 0; i < len; i++) {
187             tmp = Array.getShort(array, i);
188             
189             b1 = (tmp >> 0) & 0xff;
190             b2 = (tmp >> 8) & 0xff;
191             tmp = (short)(b1<<8 | b2<<0);
192             
193             Array.setShort(array, i, tmp);
194         }
195     }
196 }
197
Popular Tags