source: osm/applications/editors/josm/plugins/opendata/includes/org/apache/poi/util/LittleEndian.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 6.4 KB
Line 
1/* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. 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 See the License for the specific language governing permissions and
15 limitations under the License.
16==================================================================== */
17
18package org.apache.poi.util;
19
20
21/**
22 * a utility class for handling little-endian numbers, which the 80x86 world is
23 * replete with. The methods are all static, and input/output is from/to byte
24 * arrays, or from InputStreams.
25 *
26 *@author Marc Johnson (mjohnson at apache dot org)
27 *@author Andrew Oliver (acoliver at apache dot org)
28 */
29public class LittleEndian implements LittleEndianConsts {
30
31 private LittleEndian() {
32 // no instances of this class
33 }
34
35 /**
36 * get a short value from a byte array
37 *
38 *@param data the byte array
39 *@param offset a starting offset into the byte array
40 *@return the short (16-bit) value
41 */
42 public static short getShort(byte[] data, int offset) {
43 int b0 = data[offset] & 0xFF;
44 int b1 = data[offset+1] & 0xFF;
45 return (short) ((b1 << 8) + (b0 << 0));
46 }
47
48
49 /**
50 * get an unsigned short value from a byte array
51 *
52 *@param data the byte array
53 *@param offset a starting offset into the byte array
54 *@return the unsigned short (16-bit) value in an integer
55 */
56 public static int getUShort(byte[] data, int offset) {
57 int b0 = data[offset] & 0xFF;
58 int b1 = data[offset+1] & 0xFF;
59 return (b1 << 8) + (b0 << 0);
60 }
61
62
63 /**
64 * get an unsigned short value from the beginning of a byte array
65 *
66 *@param data the byte array
67 *@return the unsigned short (16-bit) value in an int
68 */
69 public static int getUShort(byte[] data) {
70 return getUShort(data, 0);
71 }
72
73 /**
74 * get an int value from a byte array
75 *
76 *@param data the byte array
77 *@param offset a starting offset into the byte array
78 *@return the int (32-bit) value
79 */
80 public static int getInt(byte[] data, int offset) {
81 int i=offset;
82 int b0 = data[i++] & 0xFF;
83 int b1 = data[i++] & 0xFF;
84 int b2 = data[i++] & 0xFF;
85 int b3 = data[i++] & 0xFF;
86 return (b3 << 24) + (b2 << 16) + (b1 << 8) + (b0 << 0);
87 }
88
89
90
91
92 /**
93 * get an unsigned int value from a byte array
94 *
95 *@param data the byte array
96 *@param offset a starting offset into the byte array
97 *@return the unsigned int (32-bit) value in a long
98 */
99 public static long getUInt(byte[] data, int offset) {
100 long retNum = getInt(data, offset);
101 return retNum & 0x00FFFFFFFF;
102 }
103
104
105 /**
106 * get a long value from a byte array
107 *
108 *@param data the byte array
109 *@param offset a starting offset into the byte array
110 *@return the long (64-bit) value
111 */
112 public static long getLong(byte[] data, int offset) {
113 long result = 0;
114
115 for (int j = offset + LONG_SIZE - 1; j >= offset; j--) {
116 result <<= 8;
117 result |= 0xff & data[j];
118 }
119 return result;
120 }
121
122 /**
123 * get a double value from a byte array, reads it in little endian format
124 * then converts the resulting revolting IEEE 754 (curse them) floating
125 * point number to a happy java double
126 *
127 *@param data the byte array
128 *@param offset a starting offset into the byte array
129 *@return the double (64-bit) value
130 */
131 public static double getDouble(byte[] data, int offset) {
132 return Double.longBitsToDouble(getLong(data, offset));
133 }
134
135
136 /**
137 * put a short value into a byte array
138 *
139 *@param data the byte array
140 *@param offset a starting offset into the byte array
141 *@param value the short (16-bit) value
142 */
143 public static void putShort(byte[] data, int offset, short value) {
144 int i = offset;
145 data[i++] = (byte)((value >>> 0) & 0xFF);
146 data[i++] = (byte)((value >>> 8) & 0xFF);
147 }
148
149 /**
150 * put a short value into beginning of a byte array
151 *
152 *@param data the byte array
153 *@param value the short (16-bit) value
154 */
155 public static void putShort(byte[] data, short value) {
156 putShort(data, 0, value);
157 }
158
159
160 /**
161 * put an int value into a byte array
162 *
163 *@param data the byte array
164 *@param offset a starting offset into the byte array
165 *@param value the int (32-bit) value
166 */
167 public static void putInt(byte[] data, int offset, int value) {
168 int i = offset;
169 data[i++] = (byte)((value >>> 0) & 0xFF);
170 data[i++] = (byte)((value >>> 8) & 0xFF);
171 data[i++] = (byte)((value >>> 16) & 0xFF);
172 data[i++] = (byte)((value >>> 24) & 0xFF);
173 }
174
175
176
177 /**
178 * put a long value into a byte array
179 *
180 *@param data the byte array
181 *@param offset a starting offset into the byte array
182 *@param value the long (64-bit) value
183 */
184 public static void putLong(byte[] data, int offset, long value) {
185 int limit = LONG_SIZE + offset;
186 long v = value;
187
188 for (int j = offset; j < limit; j++) {
189 data[j] = (byte) (v & 0xFF);
190 v >>= 8;
191 }
192 }
193
194
195 /**
196 * put a double value into a byte array
197 *
198 *@param data the byte array
199 *@param offset a starting offset into the byte array
200 *@param value the double (64-bit) value
201 */
202 public static void putDouble(byte[] data, int offset, double value) {
203 putLong(data, offset, Double.doubleToLongBits(value));
204 }
205}
Note: See TracBrowser for help on using the repository browser.