diff --git a/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java b/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java index 751f7f025c218feb731b46d7b881dd0239421b7e..423b911b7f94015f4ce9a6a907bc591ac8e3d539 100644 --- a/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java +++ b/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java @@ -70,7 +70,7 @@ public class NativeData * Returns <code>true</code> if this platform is a little-endian platform and <code>false</code> * , if it is a big-endian platform. */ - static native boolean isLittleEndian(); + private static native boolean isLittleEndian(); /** * Copies a range from an array of <code>int</code> into an array of <code>byte</code>. @@ -85,7 +85,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyIntToByte(int[] inData, int inStart, byte[] outData, int outStart, + private static native void copyIntToByte(int[] inData, int inStart, byte[] outData, int outStart, int len, int byteOrder); /** @@ -101,7 +101,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyByteToInt(byte[] inData, int inStart, int[] outData, int outStart, + private static native void copyByteToInt(byte[] inData, int inStart, int[] outData, int outStart, int len, int byteOrder); /** @@ -117,7 +117,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyLongToByte(long[] inData, int inStart, byte[] outData, int outStart, + private static native void copyLongToByte(long[] inData, int inStart, byte[] outData, int outStart, int len, int byteOrder); /** @@ -133,7 +133,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyByteToLong(byte[] inData, int inStart, long[] outData, int outStart, + private static native void copyByteToLong(byte[] inData, int inStart, long[] outData, int outStart, int len, int byteOrder); /** @@ -149,7 +149,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyShortToByte(short[] inData, int inStart, byte[] outData, int outStart, + private static native void copyShortToByte(short[] inData, int inStart, byte[] outData, int outStart, int len, int byteOrder); /** @@ -165,7 +165,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyByteToShort(byte[] inData, int inStart, short[] outData, int outStart, + private static native void copyByteToShort(byte[] inData, int inStart, short[] outData, int outStart, int len, int byteOrder); /** @@ -181,7 +181,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyFloatToByte(float[] inData, int inStart, byte[] outData, int outStart, + private static native void copyFloatToByte(float[] inData, int inStart, byte[] outData, int outStart, int len, int byteOrder); /** @@ -197,7 +197,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyByteToFloat(byte[] inData, int inStart, float[] outData, int outStart, + private static native void copyByteToFloat(byte[] inData, int inStart, float[] outData, int outStart, int len, int byteOrder); /** @@ -213,7 +213,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyDoubleToByte(double[] inData, int inStart, byte[] outData, int outStart, + private static native void copyDoubleToByte(double[] inData, int inStart, byte[] outData, int outStart, int len, int byteOrder); /** @@ -229,7 +229,7 @@ public class NativeData * @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the * <var>outData</var> should be in. */ - static native void copyByteToDouble(byte[] inData, int inStart, double[] outData, int outStart, + private static native void copyByteToDouble(byte[] inData, int inStart, double[] outData, int outStart, int len, int byteOrder); // @@ -240,7 +240,7 @@ public class NativeData public static void ensureNativeLibIsLoaded() { } - + /** * Returns the native byte order of the host running this JRE. */ @@ -249,6 +249,196 @@ public class NativeData return isLittleEndian() ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN; } + /** + * Copies a range from an array of <code>int</code> into an array of <code>byte</code>. + * + * @param inData The input array of <code>int</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>int</code> to + * start + * @param outData The output array of <code>byte</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to + * start + * @param len The number of <code>int</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyIntToByte(int[] inData, int inStart, byte[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyIntToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>byte</code> into an array of <code>int</code>. + * + * @param inData The input array of <code>byte</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>byte</code> to + * start + * @param outData The output array of <code>int</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>int</code> to + * start + * @param len The number of <code>int</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyByteToInt(byte[] inData, int inStart, int[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyByteToInt(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>long</code> into an array of <code>byte</code>. + * + * @param inData The input array of <code>long</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>long</code> to + * start + * @param outData The output array of <code>byte</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to + * start + * @param len The number of <code>long</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyLongToByte(long[] inData, int inStart, byte[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyLongToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>byte</code> into an array of <code>long</code>. + * + * @param inData The input array of <code>byte</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>byte</code> to + * start + * @param outData The output array of <code>long</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>long</code> to + * start + * @param len The number of <code>long</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyByteToLong(byte[] inData, int inStart, long[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyByteToLong(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>short</code> into an array of <code>byte</code>. + * + * @param inData The input array of <code>short</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>short</code> to + * start + * @param outData The output array of <code>byte</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to + * start + * @param len The number of <code>short</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyShortToByte(short[] inData, int inStart, byte[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyShortToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>byte</code> into an array of <code>short</code>. + * + * @param inData The input array of <code>byte</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>byte</code> to + * start + * @param outData The output array of <code>short</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>short</code> to + * start + * @param len The number of <code>short</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyByteToShort(byte[] inData, int inStart, short[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyByteToShort(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>float</code> into an array of <code>byte</code>. + * + * @param inData The input array of <code>float</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>float</code> to + * start + * @param outData The output array of <code>byte</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to + * start + * @param len The number of <code>float</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyFloatToByte(float[] inData, int inStart, byte[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyFloatToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>byte</code> into an array of <code>float</code>. + * + * @param inData The input array of <code>byte</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>byte</code> to + * start + * @param outData The output array of <code>float</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>float</code> to + * start + * @param len The number of <code>float</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyByteToFloat(byte[] inData, int inStart, float[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyByteToFloat(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>double</code> into an array of <code>byte</code>. + * + * @param inData The input array of <code>double</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>double</code> to + * start + * @param outData The output array of <code>byte</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to + * start + * @param len The number of <code>double</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyDoubleToByte(double[] inData, int inStart, byte[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyDoubleToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + + /** + * Copies a range from an array of <code>byte</code> into an array of <code>double</code>. + * + * @param inData The input array of <code>byte</code> values. + * @param inStart The position in the input array <code>inData</code> of <code>byte</code> to + * start + * @param outData The output array of <code>double</code> values. + * @param outStart The start in the output array <code>byteData</code> of <code>double</code> to + * start + * @param len The number of <code>double</code> to copy + * @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var> + * should be in. + */ + public static void copyByteToDouble(byte[] inData, int inStart, double[] outData, int outStart, + int len, ByteOrder byteOrder) + { + copyByteToDouble(inData, inStart, outData, outStart, len, byteOrder.ordinal()); + } + /** * Converts a <code>byte[]</code> array into a <code>short[]</code> array. * diff --git a/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java b/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java index 3bee4651bba03e23e0cbbb5a013604ab107bd7e9..9a303599a9b79d347b935037edc4fa01248696fe 100644 --- a/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java +++ b/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java @@ -16,6 +16,7 @@ package ch.systemsx.cisd.base.convert; +import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertTrue; @@ -71,10 +72,10 @@ public class NativeDataTests System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; NativeData.copyIntToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); final int[] iarr2 = new int[(barr.length - targetOfs) / sizeOfTarget]; NativeData.copyByteToInt(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); assertTrue(Arrays.equals(iarr, iarr2)); } @@ -88,10 +89,10 @@ public class NativeDataTests System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; NativeData.copyLongToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); final long[] iarr2 = new long[(barr.length - targetOfs) / sizeOfTarget]; NativeData.copyByteToLong(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); assertTrue(Arrays.equals(iarr, iarr2)); } @@ -105,10 +106,10 @@ public class NativeDataTests System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; NativeData.copyShortToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); final short[] iarr2 = new short[(barr.length - targetOfs) / sizeOfTarget]; NativeData.copyByteToShort(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); assertTrue(Arrays.equals(iarr, iarr2)); } @@ -122,10 +123,10 @@ public class NativeDataTests System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; NativeData.copyFloatToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); final float[] iarr2 = new float[(barr.length - targetOfs) / sizeOfTarget]; NativeData.copyByteToFloat(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); assertTrue(Arrays.equals(iarr, iarr2)); } @@ -139,10 +140,10 @@ public class NativeDataTests System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; NativeData.copyDoubleToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); final double[] iarr2 = new double[(barr.length - targetOfs) / sizeOfTarget]; NativeData.copyByteToDouble(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, - NativeData.ByteOrder.NATIVE.ordinal()); + NativeData.ByteOrder.NATIVE); assertTrue(Arrays.equals(iarr, iarr2)); } @@ -239,13 +240,13 @@ public class NativeDataTests @Test(expectedExceptions = NullPointerException.class) public void testNPE() { - NativeData.copyByteToLong(null, 0, null, 0, 0, 0); + NativeData.copyByteToLong(null, 0, null, 0, 0, ByteOrder.NATIVE); } @Test(expectedExceptions = IllegalArgumentException.class) public void testIAE() { - NativeData.copyByteToLong(new byte[] {}, -1, new long[] {}, 0, 0, 0); + NativeData.copyByteToLong(new byte[] {}, -1, new long[] {}, 0, 0, ByteOrder.NATIVE); } @Test @@ -261,12 +262,12 @@ public class NativeDataTests ByteOrder.NATIVE); if (Arrays.equals(values, valuesLE)) { - System.out.println("Platform is little endian."); + assertEquals(NativeData.ByteOrder.LITTLE_ENDIAN, NativeData.getNativeByteOrder()); assertFalse(Arrays.equals(values, valuesBE)); } if (Arrays.equals(values, valuesBE)) { - System.out.println("Platform is big endian."); + assertEquals(NativeData.ByteOrder.BIG_ENDIAN, NativeData.getNativeByteOrder()); assertFalse(Arrays.equals(values, valuesLE)); } }