001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Arrays;
023:
024: /**
025: * a utility class for handling little-endian numbers, which the 80x86 world is
026: * replete with. The methods are all static, and input/output is from/to byte
027: * arrays, or from InputStreams.
028: *
029: *@author Marc Johnson (mjohnson at apache dot org)
030: *@author Andrew Oliver (acoliver at apache dot org)
031: */
032:
033: public class LittleEndian implements LittleEndianConsts {
034:
035: // all methods are static, so an accessible constructor makes no
036: // sense
037: /**
038: * Constructor for the LittleEndian object
039: */
040: private LittleEndian() {
041: }
042:
043: /**
044: * get a short value from a byte array
045: *
046: *@param data the byte array
047: *@param offset a starting offset into the byte array
048: *@return the short (16-bit) value
049: */
050:
051: public static short getShort(final byte[] data, final int offset) {
052: return (short) getNumber(data, offset, SHORT_SIZE);
053: }
054:
055: /**
056: * get an unsigned short value from a byte array
057: *
058: *@param data the byte array
059: *@param offset a starting offset into the byte array
060: *@return the unsigned short (16-bit) value in an integer
061: */
062: public static int getUShort(final byte[] data, final int offset) {
063: short num = (short) getNumber(data, offset, SHORT_SIZE);
064: int retNum;
065: if (num < 0) {
066: retNum = (Short.MAX_VALUE + 1) * 2 + num;
067: } else {
068: retNum = num;
069: }
070: return retNum;
071: }
072:
073: /**
074: * get a short array from a byte array.
075: *
076: *@param data Description of the Parameter
077: *@param offset Description of the Parameter
078: *@param size Description of the Parameter
079: *@return The simpleShortArray value
080: */
081: public static short[] getSimpleShortArray(final byte[] data,
082: final int offset, final int size) {
083: short[] results = new short[size];
084: for (int i = 0; i < size; i++) {
085: results[i] = getShort(data, offset + 2 + (i * 2));
086: }
087: return results;
088: }
089:
090: /**
091: * get a short array from a byte array. The short array is assumed to start
092: * with a word describing the length of the array.
093: *
094: *@param data Description of the Parameter
095: *@param offset Description of the Parameter
096: *@return The shortArray value
097: */
098: public static short[] getShortArray(final byte[] data,
099: final int offset) {
100: int size = (int) getNumber(data, offset, SHORT_SIZE);
101: short[] results = getSimpleShortArray(data, offset, size);
102: return results;
103: }
104:
105: /**
106: * get a short value from the beginning of a byte array
107: *
108: *@param data the byte array
109: *@return the short (16-bit) value
110: */
111:
112: public static short getShort(final byte[] data) {
113: return getShort(data, 0);
114: }
115:
116: /**
117: * get an unsigned short value from the beginning of a byte array
118: *
119: *@param data the byte array
120: *@return the unsigned short (16-bit) value in an int
121: */
122: public static int getUShort(final byte[] data) {
123: return getUShort(data, 0);
124: }
125:
126: /**
127: * get an int value from a byte array
128: *
129: *@param data the byte array
130: *@param offset a starting offset into the byte array
131: *@return the int (32-bit) value
132: */
133:
134: public static int getInt(final byte[] data, final int offset) {
135: return (int) getNumber(data, offset, INT_SIZE);
136: }
137:
138: /**
139: * get an int value from the beginning of a byte array
140: *
141: *@param data the byte array
142: *@return the int (32-bit) value
143: */
144:
145: public static int getInt(final byte[] data) {
146: return getInt(data, 0);
147: }
148:
149: /**
150: * get an unsigned int value from a byte array
151: *
152: *@param data the byte array
153: *@param offset a starting offset into the byte array
154: *@return the unsigned int (32-bit) value in a long
155: */
156: public static long getUInt(final byte[] data, final int offset) {
157: int num = (int) getNumber(data, offset, INT_SIZE);
158: long retNum;
159: if (num < 0) {
160: retNum = ((long) Integer.MAX_VALUE + 1) * 2 + num;
161: } else {
162: retNum = num;
163: }
164: return retNum;
165: }
166:
167: /**
168: * get an unsigned int value from a byte array
169: *
170: *@param data the byte array
171: *@return the unsigned int (32-bit) value in a long
172: */
173: public static long getUInt(final byte[] data) {
174: return getUInt(data, 0);
175: }
176:
177: /**
178: * get a long value from a byte array
179: *
180: *@param data the byte array
181: *@param offset a starting offset into the byte array
182: *@return the long (64-bit) value
183: */
184:
185: public static long getLong(final byte[] data, final int offset) {
186: return getNumber(data, offset, LONG_SIZE);
187: }
188:
189: /**
190: * get a long value from the beginning of a byte array
191: *
192: *@param data the byte array
193: *@return the long (64-bit) value
194: */
195:
196: public static long getLong(final byte[] data) {
197: return getLong(data, 0);
198: }
199:
200: /**
201: * get a double value from a byte array, reads it in little endian format
202: * then converts the resulting revolting IEEE 754 (curse them) floating
203: * point number to a happy java double
204: *
205: *@param data the byte array
206: *@param offset a starting offset into the byte array
207: *@return the double (64-bit) value
208: */
209:
210: public static double getDouble(final byte[] data, final int offset) {
211: return Double.longBitsToDouble(getNumber(data, offset,
212: DOUBLE_SIZE));
213: }
214:
215: /**
216: * get a double value from the beginning of a byte array
217: *
218: *@param data the byte array
219: *@return the double (64-bit) value
220: */
221:
222: public static double getDouble(final byte[] data) {
223: return getDouble(data, 0);
224: }
225:
226: /**
227: * put a short value into a byte array
228: *
229: *@param data the byte array
230: *@param offset a starting offset into the byte array
231: *@param value the short (16-bit) value
232: */
233: public static void putShort(final byte[] data, final int offset,
234: final short value) {
235: putNumber(data, offset, value, SHORT_SIZE);
236: }
237:
238: /**
239: * put a array of shorts into a byte array
240: *
241: *@param data the byte array
242: *@param offset a starting offset into the byte array
243: *@param value the short array
244: */
245: public static void putShortArray(final byte[] data,
246: final int offset, final short[] value) {
247: putNumber(data, offset, value.length, SHORT_SIZE);
248: for (int i = 0; i < value.length; i++) {
249: putNumber(data, offset + 2 + (i * 2), value[i], SHORT_SIZE);
250: }
251: }
252:
253: /**
254: * put an unsigned short value into a byte array
255: *
256: * @param data the byte array
257: * @param offset a starting offset into the byte array
258: * @param value the short (16-bit) value
259: *
260: * @exception ArrayIndexOutOfBoundsException may be thrown
261: */
262: public static void putUShort(final byte[] data, final int offset,
263: final int value) {
264: putNumber(data, offset, value, SHORT_SIZE);
265: }
266:
267: /**
268: * put a short value into beginning of a byte array
269: *
270: *@param data the byte array
271: *@param value the short (16-bit) value
272: */
273:
274: public static void putShort(final byte[] data, final short value) {
275: putShort(data, 0, value);
276: }
277:
278: /**
279: * put an int value into a byte array
280: *
281: *@param data the byte array
282: *@param offset a starting offset into the byte array
283: *@param value the int (32-bit) value
284: */
285:
286: public static void putInt(final byte[] data, final int offset,
287: final int value) {
288: putNumber(data, offset, value, INT_SIZE);
289: }
290:
291: /**
292: * put an int value into beginning of a byte array
293: *
294: *@param data the byte array
295: *@param value the int (32-bit) value
296: */
297:
298: public static void putInt(final byte[] data, final int value) {
299: putInt(data, 0, value);
300: }
301:
302: /**
303: * put a long value into a byte array
304: *
305: *@param data the byte array
306: *@param offset a starting offset into the byte array
307: *@param value the long (64-bit) value
308: */
309:
310: public static void putLong(final byte[] data, final int offset,
311: final long value) {
312: putNumber(data, offset, value, LONG_SIZE);
313: }
314:
315: /**
316: * put a long value into beginning of a byte array
317: *
318: *@param data the byte array
319: *@param value the long (64-bit) value
320: */
321:
322: public static void putLong(final byte[] data, final long value) {
323: putLong(data, 0, value);
324: }
325:
326: /**
327: * put a double value into a byte array
328: *
329: *@param data the byte array
330: *@param offset a starting offset into the byte array
331: *@param value the double (64-bit) value
332: */
333:
334: public static void putDouble(final byte[] data, final int offset,
335: final double value) {
336: // Excel likes NaN to be a specific value.
337: if (Double.isNaN(value))
338: putNumber(data, offset, -276939487313920L, DOUBLE_SIZE);
339: else
340: putNumber(data, offset, Double.doubleToLongBits(value),
341: DOUBLE_SIZE);
342: }
343:
344: /**
345: * put a double value into beginning of a byte array
346: *
347: *@param data the byte array
348: *@param value the double (64-bit) value
349: */
350:
351: public static void putDouble(final byte[] data, final double value) {
352: putDouble(data, 0, value);
353: }
354:
355: /**
356: * Exception to handle buffer underruns
357: *
358: *@author Marc Johnson (mjohnson at apache dot org)
359: */
360:
361: public static class BufferUnderrunException extends IOException {
362:
363: /**
364: * simple constructor
365: */
366:
367: BufferUnderrunException() {
368: super ("buffer underrun");
369: }
370: }
371:
372: /**
373: * get a short value from an InputStream
374: *
375: *@param stream the InputStream from which the short
376: * is to be read
377: *@return the short (16-bit) value
378: *@exception IOException will be propagated back to the caller
379: *@exception BufferUnderrunException if the stream cannot provide enough
380: * bytes
381: */
382:
383: public static short readShort(final InputStream stream)
384: throws IOException, BufferUnderrunException {
385: return getShort(readFromStream(stream, SHORT_SIZE));
386: }
387:
388: /**
389: * get an int value from an InputStream
390: *
391: *@param stream the InputStream from which the int is
392: * to be read
393: *@return the int (32-bit) value
394: *@exception IOException will be propagated back to the caller
395: *@exception BufferUnderrunException if the stream cannot provide enough
396: * bytes
397: */
398:
399: public static int readInt(final InputStream stream)
400: throws IOException, BufferUnderrunException {
401: return getInt(readFromStream(stream, INT_SIZE));
402: }
403:
404: /**
405: * get a long value from an InputStream
406: *
407: *@param stream the InputStream from which the long
408: * is to be read
409: *@return the long (64-bit) value
410: *@exception IOException will be propagated back to the caller
411: *@exception BufferUnderrunException if the stream cannot provide enough
412: * bytes
413: */
414:
415: public static long readLong(final InputStream stream)
416: throws IOException, BufferUnderrunException {
417: return getLong(readFromStream(stream, LONG_SIZE));
418: }
419:
420: /**
421: * Read the appropriate number of bytes from the stream and return them to
422: * the caller. <p>
423: *
424: * However, for the purposes of the POI project, this risk is deemed
425: * negligible. It is, however, so noted.
426: *
427: *@param stream the InputStream we're reading from
428: *@param size the number of bytes to read; in
429: * 99.99% of cases, this will be SHORT_SIZE, INT_SIZE, or LONG_SIZE --
430: * but it doesn't have to be.
431: *@return the byte array containing the
432: * required number of bytes. The array will contain all zero's on end
433: * of stream
434: *@exception IOException will be propagated back to the caller
435: *@exception BufferUnderrunException if the stream cannot provide enough
436: * bytes
437: */
438:
439: public static byte[] readFromStream(final InputStream stream,
440: final int size) throws IOException, BufferUnderrunException {
441: byte[] buffer = new byte[size];
442:
443: int count = stream.read(buffer);
444:
445: if (count == -1) {
446:
447: // return a zero-filled buffer
448: Arrays.fill(buffer, (byte) 0);
449: } else if (count != size) {
450: throw new BufferUnderrunException();
451: }
452: return buffer;
453: }
454:
455: /**
456: * Gets the number attribute of the LittleEndian class
457: *
458: *@param data Description of the Parameter
459: *@param offset Description of the Parameter
460: *@param size Description of the Parameter
461: *@return The number value
462: */
463: private static long getNumber(final byte[] data, final int offset,
464: final int size) {
465: long result = 0;
466:
467: for (int j = offset + size - 1; j >= offset; j--) {
468: result <<= 8;
469: result |= 0xff & data[j];
470: }
471: return result;
472: }
473:
474: /**
475: * Description of the Method
476: *
477: *@param data Description of the Parameter
478: *@param offset Description of the Parameter
479: *@param value Description of the Parameter
480: *@param size Description of the Parameter
481: */
482: private static void putNumber(final byte[] data, final int offset,
483: final long value, final int size) {
484: int limit = size + offset;
485: long v = value;
486:
487: for (int j = offset; j < limit; j++) {
488: data[j] = (byte) (v & 0xFF);
489: v >>= 8;
490: }
491: }
492:
493: /**
494: * Convert an 'unsigned' byte to an integer. ie, don't carry across the
495: * sign.
496: *
497: *@param b Description of the Parameter
498: *@return Description of the Return Value
499: */
500: public static int ubyteToInt(byte b) {
501: return ((b & 0x80) == 0 ? (int) b : (b & (byte) 0x7f) + 0x80);
502: }
503:
504: /**
505: * get the unsigned value of a byte.
506: *
507: *@param data the byte array.
508: *@param offset a starting offset into the byte array.
509: *@return the unsigned value of the byte as a 32 bit integer
510: */
511: public static int getUnsignedByte(final byte[] data,
512: final int offset) {
513: return (int) getNumber(data, offset, BYTE_SIZE);
514: }
515:
516: /**
517: * get the unsigned value of a byte.
518: *
519: *@param data the byte array
520: *@return the unsigned value of the byte as a 32 bit integer
521: */
522: public static int getUnsignedByte(final byte[] data) {
523: return getUnsignedByte(data, 0);
524: }
525:
526: /**
527: * Copy a portion of a byte array
528: *
529: *@param data the original byte array
530: *@param offset Where to start copying from.
531: *@param size Number of bytes to copy.
532: *@return The byteArray value
533: *@throws IndexOutOfBoundsException - if copying would cause access of
534: * data outside array bounds.
535: */
536: public static byte[] getByteArray(final byte[] data, int offset,
537: int size) {
538: byte[] copy = new byte[size];
539: System.arraycopy(data, offset, copy, 0, size);
540:
541: return copy;
542: }
543:
544: /**
545: * <p>Gets an unsigned int value (8 bytes) from a byte array.</p>
546: *
547: * @param data the byte array
548: * @param offset a starting offset into the byte array
549: * @return the unsigned int (32-bit) value in a long
550: */
551: public static long getULong(final byte[] data, final int offset) {
552: int num = (int) getNumber(data, offset, LONG_SIZE);
553: long retNum;
554: if (num < 0)
555: retNum = ((long) Integer.MAX_VALUE + 1) * 2 + num;
556: else
557: retNum = num;
558: return retNum;
559: }
560:
561: }
|