* Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact:
- * Jaewon Lim <jaewon81.lim@samsung.com>
- * Jooyoul Lee <jy.exe.lee@samsung.com>
* Juyoung Kim <j0.kim@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* ARM: default LITTLE-ENDIAN, can be BIG-ENDIAN
*/
+
+import java.lang.reflect.Array;
+
+
public class ByteSwapper {
+
+ /**
+ * Reverses the byte order of all elements in the supplied array, converting
+ * between little and big endian byte order.
+ *
+ * @param array the input array for type sensitive byte swapping.
+ */
+ public static void swap(Object array) {
+ Class<?> arrayType = array.getClass().getComponentType();
+
+ if (arrayType.isPrimitive()) {
+ if (arrayType == Boolean.TYPE) {
+ return;
+ } else if (arrayType == Byte.TYPE) {
+ return;
+ } else if (arrayType == Character.TYPE) {
+ return;
+ } else if (arrayType == Short.TYPE) {
+ swapShortArray(array);
+ } else if (arrayType == Integer.TYPE) {
+ swapIntegerArray(array);
+ } else if (arrayType == Long.TYPE) {
+ swapLongArray(array);
+ } else if (arrayType == Float.TYPE) {
+ swapFloatArray(array);
+ } else if (arrayType == Double.TYPE) {
+ swapDoubleArray(array);
+ }
+ }
+
+ }
+
+ /**
+ * Byte order reverses an <code>Array</code> of <code>doubles</code>
+ *
+ * @param array input array
+ */
+ private static void swapDoubleArray(Object array) {
+ int len = Array.getLength(array);
+ double dtmp;
+ long tmp;
+ long b1, b2, b3, b4, b5, b6, b7, b8;
+
+ for (int i = 0; i < len; i++) {
+ dtmp = Array.getDouble(array, i);
+ tmp = Double.doubleToRawLongBits(dtmp);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ b5 = (tmp >> 32) & 0xff;
+ b6 = (tmp >> 40) & 0xff;
+ b7 = (tmp >> 48) & 0xff;
+ b8 = (tmp >> 56) & 0xff;
+ tmp = b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8
+ | b8 << 0;
+
+ dtmp = Double.longBitsToDouble(tmp);
+ Array.setDouble(array, i, dtmp);
+ }
+ }
+
+ public static double swap(double value) {
+
+ double dtmp;
+ long tmp;
+ long b1, b2, b3, b4, b5, b6, b7, b8;
+
+ tmp = Double.doubleToRawLongBits(value);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ b5 = (tmp >> 32) & 0xff;
+ b6 = (tmp >> 40) & 0xff;
+ b7 = (tmp >> 48) & 0xff;
+ b8 = (tmp >> 56) & 0xff;
+ tmp = b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8
+ | b8 << 0;
+
+ dtmp = Double.longBitsToDouble(tmp);
+ return dtmp;
+ }
+ /**
+ * Byte order reverses an <code>Array</code> of <code>floats</code>
+ *
+ * @param array input array
+ */
+ private static void swapFloatArray(Object array) {
+ int len = Array.getLength(array);
+ float ftmp;
+ int tmp;
+ int b1, b2, b3, b4;
+
+ for (int i = 0; i < len; i++) {
+ ftmp = Array.getFloat(array, i);
+ tmp = Float.floatToRawIntBits(ftmp);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ tmp = b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
+
+ ftmp = Float.intBitsToFloat(tmp);
+ Array.setFloat(array, i, ftmp);
+ }
+ }
+
+ public static float swap(float value) {
+
+ float ftmp;
+ int tmp;
+ int b1, b2, b3, b4;
+
+ tmp = Float.floatToRawIntBits(value);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ tmp = b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
+
+ ftmp = Float.intBitsToFloat(tmp);
+
+ return ftmp;
+ }
+ /**
+ * Byte order reverses an <code>Array</code> of <code>ints</code>
+ *
+ * @param array input array
+ */
+ private static void swapIntegerArray(Object array) {
+ int len = Array.getLength(array);
+ int tmp;
+ int b1, b2, b3, b4;
+
+ for (int i = 0; i < len; i++) {
+ tmp = Array.getInt(array, i);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ tmp = b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
+
+ Array.setInt(array, i, tmp);
+ }
+ }
+
+ public static int swap(int value) {
+ int tmp;
+ int b1, b2, b3, b4;
+
+ b1 = (value >> 0) & 0xff;
+ b2 = (value >> 8) & 0xff;
+ b3 = (value >> 16) & 0xff;
+ b4 = (value >> 24) & 0xff;
+ tmp = b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
+
+ return tmp;
+ }
- /**
- * Byte swap a single short value.
- *
- * @param value
- * Value to byte swap.
- * @return Byte swapped representation.
- */
+ /**
+ * Byte order reverses an <code>Array</code> of <code>longs</code>
+ *
+ * @param array input array
+ */
+ private static void swapLongArray(Object array) {
+ int len = Array.getLength(array);
+ long tmp;
+ long b1, b2, b3, b4, b5, b6, b7, b8;
+
+ for (int i = 0; i < len; i++) {
+ tmp = Array.getLong(array, i);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ b3 = (tmp >> 16) & 0xff;
+ b4 = (tmp >> 24) & 0xff;
+ b5 = (tmp >> 32) & 0xff;
+ b6 = (tmp >> 40) & 0xff;
+ b7 = (tmp >> 48) & 0xff;
+ b8 = (tmp >> 56) & 0xff;
+ tmp = b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8
+ | b8 << 0;
+
+ Array.setLong(array, i, tmp);
+ }
+ }
+
+ public static long swap(long value) {
+ long tmp;
+ long b1, b2, b3, b4, b5, b6, b7, b8;
+
+ b1 = (value >> 0) & 0xff;
+ b2 = (value >> 8) & 0xff;
+ b3 = (value >> 16) & 0xff;
+ b4 = (value >> 24) & 0xff;
+ b5 = (value >> 32) & 0xff;
+ b6 = (value >> 40) & 0xff;
+ b7 = (value >> 48) & 0xff;
+ b8 = (value >> 56) & 0xff;
+ tmp = b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8
+ | b8 << 0;
+
+ return tmp;
+ }
+ /**
+ * Byte order reverses an <code>Array</code> of <code>shorts</code>
+ *
+ * @param array input array
+ */
+ private static void swapShortArray(Object array) {
+ int len = Array.getLength(array);
+ short tmp;
+ int b1, b2;
+
+ for (int i = 0; i < len; i++) {
+ tmp = Array.getShort(array, i);
+
+ b1 = (tmp >> 0) & 0xff;
+ b2 = (tmp >> 8) & 0xff;
+ tmp = (short) (b1 << 8 | b2 << 0);
+
+ Array.setShort(array, i, tmp);
+ }
+ }
+
public static short swap(short value) {
- int b1 = value & 0xff;
- int b2 = (value >> 8) & 0xff;
-
- return (short) (b1 << 8 | b2 << 0);
- }
-
- /**
- * Byte swap a single int value.
- *
- * @param value
- * Value to byte swap.
- * @return Byte swapped representation.
- */
- public static int swap(int value) {
- int b1 = (value >> 0) & 0xff;
- int b2 = (value >> 8) & 0xff;
- int b3 = (value >> 16) & 0xff;
- int b4 = (value >> 24) & 0xff;
-
- return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
- }
-
- /**
- * Byte swap a single long value.
- *
- * @param value
- * Value to byte swap.
- * @return Byte swapped representation.
- */
- public static long swap(long value) {
- long b1 = (value >> 0) & 0xff;
- long b2 = (value >> 8) & 0xff;
- long b3 = (value >> 16) & 0xff;
- long b4 = (value >> 24) & 0xff;
- long b5 = (value >> 32) & 0xff;
- long b6 = (value >> 40) & 0xff;
- long b7 = (value >> 48) & 0xff;
- long b8 = (value >> 56) & 0xff;
-
- return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16
- | b7 << 8 | b8 << 0;
- }
-
- /**
- * Byte swap a single float value.
- *
- * @param value
- * Value to byte swap.
- * @return Byte swapped representation.
- */
- public static float swap(float value) {
- int intValue = Float.floatToRawIntBits(value);
- intValue = swap(intValue);
- return Float.intBitsToFloat(intValue);
- }
-
- /**
- * Byte swap a single double value.
- *
- * @param value
- * Value to byte swap.
- * @return Byte swapped representation.
- */
- public static double swap(double value) {
- long longValue = Double.doubleToRawLongBits(value);
- longValue = swap(longValue);
- return Double.longBitsToDouble(longValue);
- }
-
- /**
- * Byte swap an array of shorts. The result of the swapping is put back into
- * the specified array.
- *
- * @param array
- * Array of values to swap
- */
- public static void swap(short[] array) {
- for (int i = 0; i < array.length; i++)
- array[i] = swap(array[i]);
- }
-
- /**
- * Byte swap an array of ints. The result of the swapping is put back into
- * the specified array.
- *
- * @param array
- * Array of values to swap
- */
- public static void swap(int[] array) {
- for (int i = 0; i < array.length; i++)
- array[i] = swap(array[i]);
- }
-
- /**
- * Byte swap an array of longs. The result of the swapping is put back into
- * the specified array.
- *
- * @param array
- * Array of values to swap
- */
- public static void swap(long[] array) {
- for (int i = 0; i < array.length; i++)
- array[i] = swap(array[i]);
- }
-
- /**
- * Byte swap an array of floats. The result of the swapping is put back into
- * the specified array.
- *
- * @param array
- * Array of values to swap
- */
- public static void swap(float[] array) {
- for (int i = 0; i < array.length; i++)
- array[i] = swap(array[i]);
- }
-
- /**
- * Byte swap an array of doubles. The result of the swapping is put back
- * into the specified array.
- *
- * @param array
- * Array of values to swap
- */
- public static void swap(double[] array) {
- for (int i = 0; i < array.length; i++)
- array[i] = swap(array[i]);
- }
-}
+ short tmp;
+ int b1, b2;
+
+ b1 = (value >> 0) & 0xff;
+ b2 = (value >> 8) & 0xff;
+ tmp = (short) (b1 << 8 | b2 << 0);
+
+ return tmp;
+ }
+}
\ No newline at end of file