diff --git a/src/java/org/apache/poi/util/HexDump.java b/src/java/org/apache/poi/util/HexDump.java index 4eac10f37..4e0cfd35a 100644 --- a/src/java/org/apache/poi/util/HexDump.java +++ b/src/java/org/apache/poi/util/HexDump.java @@ -183,6 +183,24 @@ public class HexDump return _cbuffer; } + /** + * Converts the parameter to a hex value. + * + * @param value The value to convert + * @return A String representing the array of bytes + */ + public static String toHex(final byte[] value) + { + StringBuffer retVal = new StringBuffer(); + retVal.append('['); + for(int x = 0; x < value.length; x++) + { + retVal.append(toHex(value[x])); + retVal.append(", "); + } + retVal.append(']'); + return retVal.toString(); + } /** * Converts the parameter to a hex value. * diff --git a/src/java/org/apache/poi/util/LittleEndian.java b/src/java/org/apache/poi/util/LittleEndian.java index 3621423a9..6c7a4f4d2 100644 --- a/src/java/org/apache/poi/util/LittleEndian.java +++ b/src/java/org/apache/poi/util/LittleEndian.java @@ -94,6 +94,18 @@ public class LittleEndian return (short) getNumber(data, offset, SHORT_SIZE); } + /** + * get a short array from a byte array. + */ + public static short[] getSimpleShortArray(final byte[] data, final int offset, final int size) + { + short[] results = new short[size]; + for (int i = 0; i < size; i++) + { + results[i] = getShort(data, offset + 2 + (i * 2)); + } + return results; + } /** * get a short array from a byte array. The short array is assumed * to start with a word describing the length of the array. @@ -101,11 +113,7 @@ public class LittleEndian public static short[] getShortArray(final byte[] data, final int offset) { int size = (short) getNumber(data, offset, SHORT_SIZE); - short[] results = new short[size]; - for (int i = 0; i < size; i++) - { - results[i] = getShort(data, offset + 2 + (i * 2)); - } + short[] results = getSimpleShortArray(data, offset, size); return results; } @@ -566,5 +574,22 @@ public class LittleEndian { return getUnsignedByte(data, 0); } + /** + * 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. + * + * @throws IndexOutOfBoundsException - if copying would cause access of data + * outside array bounds. + */ + public static byte[] getByteArray(final byte[] data, int offset, int size) + { + byte[] copy = new byte[size]; + System.arraycopy(data, offset, copy, 0, size); + + return copy; + } }