From: JuYoung Kim Date: Thu, 25 Sep 2014 14:24:35 +0000 (+0900) Subject: Get ByteSwapper from Apache licenced X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a1c80ea63396e1c692d299f321057f8315c0b3bf;p=sdk%2Ftools%2Fdynamic-analyzer.git Get ByteSwapper from Apache licenced Change-Id: Ie5b527e5156f56d6867399aa420cbd5596f4b1f3 Signed-off-by: JuYoung Kim --- diff --git a/org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteSwapper.java b/org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteSwapper.java index c9cae28..0f03414 100644 --- a/org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteSwapper.java +++ b/org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteSwapper.java @@ -4,8 +4,6 @@ * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: - * Jaewon Lim - * Jooyoul Lee * Juyoung Kim * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,142 +32,250 @@ package org.tizen.dynamicanalyzer.util; * 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 Array of doubles + * + * @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 Array of floats + * + * @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 Array of ints + * + * @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 Array of longs + * + * @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 Array of shorts + * + * @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