1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.util;
25 
26 
27 /**
28  * <p>Helper class providing static methods to convert data to/from
29  *    Network Byte Order (Big Endian).</p>
30  *
31  * <p>With the introduction of <code>java.nio.ByteOrder</code> and
32  *    <code>java.nio.ByteBuffer</code> in Java 1.4, it may no longer be
33  *    necessary to use this class in the future.</p>
34  *
35  * @author  Mark Murnane
36  * @version 1.1
37  */
38 public class EndianConverter {
39 
40     /**
41      * <p>Convert a <code>short</code> to a Little Endian representation.</p>
42      *
43      * @param   value   The <code>short</code> to be converted.
44      *
45      * @return  Two element <code>byte</code> array containing the converted
46      *          value.
47      */
writeShort(short value)48     public static byte[] writeShort (short value) {
49         byte[] leShort = new byte[2];
50 
51         leShort[0] = (byte) value;
52         leShort[1] = (byte) (value >>> 8);
53 
54         return leShort;
55     }
56 
57 
58     /**
59      * <p>Convert an integer to a Little Endian representation.</p>
60      *
61      * @param   value   The <code>int</code> to be converted.
62      *
63      * @return  Four element <code>byte</code> array containing the converted
64      *          value.
65      */
writeInt(int value)66     public static byte[] writeInt (int value) {
67         byte[] leInt = new byte[4];
68 
69         leInt[0] = (byte) value;
70         leInt[1] = (byte) (value >>> 8);
71         leInt[2] = (byte) (value >>> 16);
72         leInt[3] = (byte) (value >>> 24);
73 
74         return leInt;
75     }
76 
77     /**
78      * <p>Converts a <code>double</code> to a Little Endian representation
79 	 * of a float in IEEE-754 format.
80      *
81      * <p>An array with more than eight elements can be used, but only the first
82      * eight elements will be read.</p>
83      *
84      * @param   value <code>double</code> containing the value to be converted
85      *
86      * @return   <code>byte</code> array containing the LE representation of a IEEE-754 float
87      */
writeDouble(double value)88     public static byte[] writeDouble(double value) {
89 
90 		long myDouble = Double.doubleToLongBits(value);
91 		byte[] leDouble = new byte[8];
92 
93 		leDouble[0] = (byte) (myDouble >>> 0);
94 		leDouble[1] = (byte) (myDouble >>> 8);
95 		leDouble[2] = (byte) (myDouble >>> 16);
96 		leDouble[3] = (byte) (myDouble >>> 24);
97 		leDouble[4] = (byte) (myDouble >>> 32);
98 		leDouble[5] = (byte) (myDouble >>> 40);
99 		leDouble[6] = (byte) (myDouble >>> 48);
100 		leDouble[7] = (byte) (myDouble >>> 56);
101 
102 		return leDouble;
103     }
104 
105     /**
106      * <p>Convert a Little Endian representation of a short to a Java
107      *    <code>short</code> (Network Byte Order).</p>
108      *
109      * <p>An array with more than two elements can be used, but only the first
110      *    two elements will be read.</p>
111      *
112      * @param   value   <code>byte</code> array containing the LE representation
113      *                  of the value.
114      *
115      * @return  <code>short</code> containing the converted value.
116      */
readShort(byte[] value)117     public static short readShort (byte[] value) {
118         int high, low;
119 
120         high = value[1] & 0xFF;
121         low  = value[0] & 0xFF;
122 
123         return (short)(high << 8 | low);
124     }
125 
126 
127     /**
128      * <p>Convert a Little Endian representation of an integer to a Java
129      *    <code>int</code> (Network Byte Order).</p>
130      *
131      * <p>An array with more than four elements can be used, but only the first
132      * four elements will be read.</p>
133      *
134      * @param   value   <code>byte</code> array containing the LE representation
135      *                  of the value.
136      *
137      * @return  <code>int</code> containing the converted value.
138      */
readInt(byte[] value)139     public static int readInt(byte[] value) {
140         int number = 0;
141 
142         for (int i = 0; i < 4; i++) {
143             number |= (value[i] & 0xFF) << ( i * 8);
144         }
145 
146         return number;
147     }
148 
149     /**
150      * <p>Convert a Little Endian representation of a float in IEEE-754 Little
151 	 * Endian to a Java <code>double</code> (Network Byte Order).</p>
152      *
153      * <p>An array with more than eight elements can be used, but only the first
154      * eight elements will be read.</p>
155      *
156      * @param   value   <code>byte</code> array containing the LE representation
157      *                  of a IEEE-754 float.
158      *
159      * @return  <code>double</code> containing the converted value.
160      */
readDouble(byte[] value)161     public static double readDouble(byte[] value) {
162 
163 		long lvalue = (	((long)(value[7]) 		<< 56)	+
164 						((long)(value[6]&0xFF)	<< 48)	+
165 						((long)(value[5]&0xFF)	<< 40)	+
166 						((long)(value[4]&0xFF)	<< 32)	+
167 						((long)(value[3]&0xFF)	<< 24)	+
168 						((long)(value[2]&0xFF)	<< 16)	+
169 						((long)(value[1]&0xFF)	<< 8)	+
170 					 	(long)(value[0]&0xFF));
171 
172 		return Double.longBitsToDouble(lvalue);
173     }
174 }
175