2004-03-04 11:58:35 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
Copyright 2003-2004 Apache Software Foundation
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
package org.apache.poi.util;
|
|
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.Arrays;
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* a utility class for handling little-endian numbers, which the 80x86 world is
|
|
|
|
|
* replete with. The methods are all static, and input/output is from/to byte
|
|
|
|
|
* arrays, or from InputStreams.
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@author Marc Johnson (mjohnson at apache dot org)
|
|
|
|
|
*@author Andrew Oliver (acoliver at apache dot org)
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class LittleEndian
|
2002-05-11 14:51:33 +00:00
|
|
|
implements LittleEndianConsts {
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
// all methods are static, so an accessible constructor makes no
|
|
|
|
|
// sense
|
2002-05-11 14:51:33 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor for the LittleEndian object
|
|
|
|
|
*/
|
|
|
|
|
private LittleEndian() { }
|
|
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a short value from a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the short (16-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static short getShort(final byte[] data, final int offset) {
|
|
|
|
|
return (short) getNumber(data, offset, SHORT_SIZE);
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-05-10 03:01:11 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an unsigned short value from a byte array
|
2002-05-10 03:01:11 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the unsigned short (16-bit) value in an integer
|
2002-05-10 03:01:11 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getUShort(final byte[] data, final int offset) {
|
|
|
|
|
short num = (short) getNumber(data, offset, SHORT_SIZE);
|
2002-05-10 03:01:11 +00:00
|
|
|
int retNum;
|
2002-05-11 14:51:33 +00:00
|
|
|
if (num < 0) {
|
|
|
|
|
retNum = ((int) Short.MAX_VALUE + 1) * 2 + (int) num;
|
|
|
|
|
} else {
|
|
|
|
|
retNum = (int) num;
|
|
|
|
|
}
|
2002-05-10 03:01:11 +00:00
|
|
|
return retNum;
|
|
|
|
|
}
|
2002-05-11 14:51:33 +00:00
|
|
|
|
|
|
|
|
|
2002-03-08 15:07:30 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a short array from a byte array.
|
|
|
|
|
*
|
|
|
|
|
*@param data Description of the Parameter
|
|
|
|
|
*@param offset Description of the Parameter
|
|
|
|
|
*@param size Description of the Parameter
|
|
|
|
|
*@return The simpleShortArray value
|
2002-03-08 15:07:30 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static short[] getSimpleShortArray(final byte[] data, final int offset, final int size) {
|
2002-03-08 15:07:30 +00:00
|
|
|
short[] results = new short[size];
|
2002-05-11 14:51:33 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
2002-03-20 12:35:26 +00:00
|
|
|
results[i] = getShort(data, offset + 2 + (i * 2));
|
2002-03-08 15:07:30 +00:00
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2002-05-11 14:51:33 +00:00
|
|
|
|
|
|
|
|
|
2002-04-19 22:30:50 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a short array from a byte array. The short array is assumed to start
|
|
|
|
|
* with a word describing the length of the array.
|
|
|
|
|
*
|
|
|
|
|
*@param data Description of the Parameter
|
|
|
|
|
*@param offset Description of the Parameter
|
|
|
|
|
*@return The shortArray value
|
2002-04-19 22:30:50 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static short[] getShortArray(final byte[] data, final int offset) {
|
2004-03-04 11:58:35 +00:00
|
|
|
int size = (int) getNumber(data, offset, SHORT_SIZE);
|
2002-04-19 22:30:50 +00:00
|
|
|
short[] results = getSimpleShortArray(data, offset, size);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2002-03-08 15:07:30 +00:00
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a short value from the beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the short (16-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static short getShort(final byte[] data) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getShort(data, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-05-10 03:01:11 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an unsigned short value from the beginning of a byte array
|
2002-05-10 03:01:11 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the unsigned short (16-bit) value in an int
|
2002-05-10 03:01:11 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getUShort(final byte[] data) {
|
2002-05-10 03:01:11 +00:00
|
|
|
return getUShort(data, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an int value from a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the int (32-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getInt(final byte[] data, final int offset) {
|
2002-03-20 12:35:26 +00:00
|
|
|
return (int) getNumber(data, offset, INT_SIZE);
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an int value from the beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the int (32-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getInt(final byte[] data) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getInt(data, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an unsigned int value from a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the unsigned int (32-bit) value in a long
|
|
|
|
|
*/
|
|
|
|
|
public static long getUInt(final byte[] data, final int offset) {
|
|
|
|
|
int num = (int) getNumber(data, offset, INT_SIZE);
|
|
|
|
|
long retNum;
|
|
|
|
|
if (num < 0) {
|
|
|
|
|
retNum = ((long) Integer.MAX_VALUE + 1) * 2 + (long) num;
|
|
|
|
|
} else {
|
|
|
|
|
retNum = (int) num;
|
|
|
|
|
}
|
|
|
|
|
return retNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get an unsigned int value from a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the unsigned int (32-bit) value in a long
|
|
|
|
|
*/
|
|
|
|
|
public static long getUInt(final byte[] data) {
|
|
|
|
|
return getUInt(data,0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get a long value from a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the long (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static long getLong(final byte[] data, final int offset) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getNumber(data, offset, LONG_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a long value from the beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the long (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static long getLong(final byte[] data) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getLong(data, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a double value from a byte array, reads it in little endian format
|
|
|
|
|
* then converts the resulting revolting IEEE 754 (curse them) floating
|
|
|
|
|
* point number to a happy java double
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@return the double (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static double getDouble(final byte[] data, final int offset) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return Double.longBitsToDouble(getNumber(data, offset, DOUBLE_SIZE));
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a double value from the beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the double (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static double getDouble(final byte[] data) {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getDouble(data, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a short value into a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@param value the short (16-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
2002-03-20 12:35:26 +00:00
|
|
|
public static void putShort(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final short value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putNumber(data, offset, value, SHORT_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-03-08 15:07:30 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a array of shorts into a byte array
|
2002-03-08 15:07:30 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@param value the short array
|
2002-03-08 15:07:30 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static void putShortArray(final byte[] data, final int offset, final short[] value) {
|
2002-03-08 15:07:30 +00:00
|
|
|
putNumber(data, offset, value.length, SHORT_SIZE);
|
2002-05-11 14:51:33 +00:00
|
|
|
for (int i = 0; i < value.length; i++) {
|
2002-03-08 15:07:30 +00:00
|
|
|
putNumber(data, offset + 2 + (i * 2), value[i], SHORT_SIZE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-09 12:48:34 +00:00
|
|
|
/**
|
|
|
|
|
* put an unsigned short value into a byte array
|
|
|
|
|
*
|
|
|
|
|
* @param data the byte array
|
|
|
|
|
* @param offset a starting offset into the byte array
|
|
|
|
|
* @param value the short (16-bit) value
|
|
|
|
|
*
|
|
|
|
|
* @exception ArrayIndexOutOfBoundsException may be thrown
|
|
|
|
|
*/
|
|
|
|
|
public static void putUShort(final byte[] data, final int offset,
|
|
|
|
|
final int value)
|
|
|
|
|
{
|
|
|
|
|
putNumber(data, offset, value, SHORT_SIZE);
|
|
|
|
|
}
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a short value into beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param value the short (16-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static void putShort(final byte[] data, final short value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putShort(data, 0, value);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put an int value into a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@param value the int (32-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
public static void putInt(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final int value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putNumber(data, offset, value, INT_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put an int value into beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param value the int (32-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static void putInt(final byte[] data, final int value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putInt(data, 0, value);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a long value into a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@param value the long (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
public static void putLong(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final long value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putNumber(data, offset, value, LONG_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a long value into beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param value the long (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static void putLong(final byte[] data, final long value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putLong(data, 0, value);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a double value into a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param offset a starting offset into the byte array
|
|
|
|
|
*@param value the double (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
public static void putDouble(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final double value) {
|
2002-08-27 10:29:52 +00:00
|
|
|
// Excel likes NaN to be a specific value.
|
|
|
|
|
if (Double.isNaN(value))
|
|
|
|
|
putNumber(data, offset, -276939487313920L, DOUBLE_SIZE);
|
|
|
|
|
else
|
|
|
|
|
putNumber(data, offset, Double.doubleToLongBits(value), DOUBLE_SIZE);
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* put a double value into beginning of a byte array
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@param value the double (64-bit) value
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
public static void putDouble(final byte[] data, final double value) {
|
2002-01-31 02:22:28 +00:00
|
|
|
putDouble(data, 0, value);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* Exception to handle buffer underruns
|
2002-01-31 02:22:28 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@author Marc Johnson (mjohnson at apache dot org)
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static class BufferUnderrunException
|
2002-05-11 14:51:33 +00:00
|
|
|
extends IOException {
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* simple constructor
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
BufferUnderrunException() {
|
2002-01-31 02:22:28 +00:00
|
|
|
super("buffer underrun");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a short value from an InputStream
|
|
|
|
|
*
|
|
|
|
|
*@param stream the InputStream from which the short
|
|
|
|
|
* is to be read
|
|
|
|
|
*@return the short (16-bit) value
|
|
|
|
|
*@exception IOException will be propagated back to the caller
|
|
|
|
|
*@exception BufferUnderrunException if the stream cannot provide enough
|
|
|
|
|
* bytes
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static short readShort(final InputStream stream)
|
2002-05-11 14:51:33 +00:00
|
|
|
throws IOException, BufferUnderrunException {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getShort(readFromStream(stream, SHORT_SIZE));
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get an int value from an InputStream
|
|
|
|
|
*
|
|
|
|
|
*@param stream the InputStream from which the int is
|
|
|
|
|
* to be read
|
|
|
|
|
*@return the int (32-bit) value
|
|
|
|
|
*@exception IOException will be propagated back to the caller
|
|
|
|
|
*@exception BufferUnderrunException if the stream cannot provide enough
|
|
|
|
|
* bytes
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static int readInt(final InputStream stream)
|
2002-05-11 14:51:33 +00:00
|
|
|
throws IOException, BufferUnderrunException {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getInt(readFromStream(stream, INT_SIZE));
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get a long value from an InputStream
|
|
|
|
|
*
|
|
|
|
|
*@param stream the InputStream from which the long
|
|
|
|
|
* is to be read
|
|
|
|
|
*@return the long (64-bit) value
|
|
|
|
|
*@exception IOException will be propagated back to the caller
|
|
|
|
|
*@exception BufferUnderrunException if the stream cannot provide enough
|
|
|
|
|
* bytes
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static long readLong(final InputStream stream)
|
2002-05-11 14:51:33 +00:00
|
|
|
throws IOException, BufferUnderrunException {
|
2002-01-31 02:22:28 +00:00
|
|
|
return getLong(readFromStream(stream, LONG_SIZE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* Read the appropriate number of bytes from the stream and return them to
|
|
|
|
|
* the caller. <p>
|
|
|
|
|
*
|
|
|
|
|
* However, for the purposes of the POI project, this risk is deemed
|
|
|
|
|
* negligible. It is, however, so noted.
|
|
|
|
|
*
|
|
|
|
|
*@param stream the InputStream we're reading from
|
|
|
|
|
*@param size the number of bytes to read; in
|
|
|
|
|
* 99.99% of cases, this will be SHORT_SIZE, INT_SIZE, or LONG_SIZE --
|
|
|
|
|
* but it doesn't have to be.
|
|
|
|
|
*@return the byte array containing the
|
|
|
|
|
* required number of bytes. The array will contain all zero's on end
|
|
|
|
|
* of stream
|
|
|
|
|
*@exception IOException will be propagated back to the caller
|
|
|
|
|
*@exception BufferUnderrunException if the stream cannot provide enough
|
|
|
|
|
* bytes
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
public static byte[] readFromStream(final InputStream stream,
|
2002-05-11 14:51:33 +00:00
|
|
|
final int size)
|
|
|
|
|
throws IOException, BufferUnderrunException {
|
2003-06-26 22:10:54 +00:00
|
|
|
byte[] buffer = new byte[size];
|
|
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
int count = stream.read(buffer);
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
if (count == -1) {
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
// return a zero-filled buffer
|
2002-03-20 12:35:26 +00:00
|
|
|
Arrays.fill(buffer, (byte) 0);
|
2002-05-11 14:51:33 +00:00
|
|
|
} else if (count != size) {
|
2002-01-31 02:22:28 +00:00
|
|
|
throw new BufferUnderrunException();
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the number attribute of the LittleEndian class
|
|
|
|
|
*
|
|
|
|
|
*@param data Description of the Parameter
|
|
|
|
|
*@param offset Description of the Parameter
|
|
|
|
|
*@param size Description of the Parameter
|
|
|
|
|
*@return The number value
|
|
|
|
|
*/
|
2002-03-20 12:35:26 +00:00
|
|
|
private static long getNumber(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final int size) {
|
2002-01-31 02:22:28 +00:00
|
|
|
long result = 0;
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
for (int j = offset + size - 1; j >= offset; j--) {
|
2002-01-31 02:22:28 +00:00
|
|
|
result <<= 8;
|
2002-03-20 12:35:26 +00:00
|
|
|
result |= 0xff & data[j];
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Description of the Method
|
|
|
|
|
*
|
|
|
|
|
*@param data Description of the Parameter
|
|
|
|
|
*@param offset Description of the Parameter
|
|
|
|
|
*@param value Description of the Parameter
|
|
|
|
|
*@param size Description of the Parameter
|
|
|
|
|
*/
|
2002-03-20 12:35:26 +00:00
|
|
|
private static void putNumber(final byte[] data, final int offset,
|
2002-05-11 14:51:33 +00:00
|
|
|
final long value, final int size) {
|
2002-03-20 12:35:26 +00:00
|
|
|
int limit = size + offset;
|
|
|
|
|
long v = value;
|
2002-01-31 02:22:28 +00:00
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
for (int j = offset; j < limit; j++) {
|
2002-03-20 12:35:26 +00:00
|
|
|
data[j] = (byte) (v & 0xFF);
|
|
|
|
|
v >>= 8;
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2002-03-01 13:10:15 +00:00
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-03-01 13:10:15 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* Convert an 'unsigned' byte to an integer. ie, don't carry across the
|
|
|
|
|
* sign.
|
|
|
|
|
*
|
|
|
|
|
*@param b Description of the Parameter
|
|
|
|
|
*@return Description of the Return Value
|
2002-03-01 13:10:15 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int ubyteToInt(byte b) {
|
2002-03-20 12:35:26 +00:00
|
|
|
return ((b & 0x80) == 0 ? (int) b : (int) (b & (byte) 0x7f) + 0x80);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get the unsigned value of a byte.
|
2002-03-20 12:35:26 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array.
|
|
|
|
|
*@param offset a starting offset into the byte array.
|
|
|
|
|
*@return the unsigned value of the byte as a 32 bit integer
|
2002-03-20 12:35:26 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getUnsignedByte(final byte[] data, final int offset) {
|
2002-03-20 12:35:26 +00:00
|
|
|
return (int) getNumber(data, offset, BYTE_SIZE);
|
2002-03-01 13:10:15 +00:00
|
|
|
}
|
2002-03-20 12:35:26 +00:00
|
|
|
|
2002-05-11 14:51:33 +00:00
|
|
|
|
2002-03-20 12:35:26 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* get the unsigned value of a byte.
|
2002-03-20 12:35:26 +00:00
|
|
|
*
|
2002-05-11 14:51:33 +00:00
|
|
|
*@param data the byte array
|
|
|
|
|
*@return the unsigned value of the byte as a 32 bit integer
|
2002-03-20 12:35:26 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static int getUnsignedByte(final byte[] data) {
|
2002-03-20 12:35:26 +00:00
|
|
|
return getUnsignedByte(data, 0);
|
|
|
|
|
}
|
2002-05-11 14:51:33 +00:00
|
|
|
|
|
|
|
|
|
2002-04-19 22:30:50 +00:00
|
|
|
/**
|
2002-05-11 14:51:33 +00:00
|
|
|
* Copy a portion of a byte array
|
|
|
|
|
*
|
|
|
|
|
*@param data the original byte array
|
|
|
|
|
*@param offset Where to start copying from.
|
|
|
|
|
*@param size Number of bytes to copy.
|
|
|
|
|
*@return The byteArray value
|
|
|
|
|
*@throws IndexOutOfBoundsException - if copying would cause access of
|
|
|
|
|
* data outside array bounds.
|
2002-04-19 22:30:50 +00:00
|
|
|
*/
|
2002-05-11 14:51:33 +00:00
|
|
|
public static byte[] getByteArray(final byte[] data, int offset, int size) {
|
2002-04-19 22:30:50 +00:00
|
|
|
byte[] copy = new byte[size];
|
|
|
|
|
System.arraycopy(data, offset, copy, 0, size);
|
|
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
2002-03-20 12:35:26 +00:00
|
|
|
|
2002-06-02 07:30:40 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|