1 16 17 package org.python.core; 18 19 import java.lang.reflect.Array ; 20 21 36 public class ByteSwapper { 37 38 44 public static void swap(Object array) { 45 Class arrayType = array.getClass().getComponentType(); 46 47 if (arrayType.isPrimitive()) { 48 if (arrayType == Boolean.TYPE) 49 return; 50 else if (arrayType == Byte.TYPE) 51 return; 52 else if (arrayType == Character.TYPE) 53 return; 54 else if (arrayType == Short.TYPE) 55 swapShortArray(array); 56 else if (arrayType == Integer.TYPE) 57 swapIntegerArray(array); 58 else if (arrayType == Long.TYPE) 59 swapLongArray(array); 60 else if (arrayType == Float.TYPE) 61 swapFloatArray(array); 62 else if (arrayType == Double.TYPE) 63 swapDoubleArray(array); 64 } 65 66 } 67 68 73 private static void swapDoubleArray(Object array) { 74 int len = Array.getLength(array); 75 double dtmp; 76 long tmp; 77 long b1, b2, b3, b4, b5, b6, b7, b8; 78 79 for(int i = 0; i < len; i++) { 80 dtmp = Array.getDouble(array, i); 81 tmp = Double.doubleToLongBits(dtmp); 82 83 b1 = (tmp >> 0) & 0xff; 84 b2 = (tmp >> 8) & 0xff; 85 b3 = (tmp >> 16) & 0xff; 86 b4 = (tmp >> 24) & 0xff; 87 b5 = (tmp >> 32) & 0xff; 88 b6 = (tmp >> 40) & 0xff; 89 b7 = (tmp >> 48) & 0xff; 90 b8 = (tmp >> 56) & 0xff; 91 tmp = (long)(b1<<56 | b2<<48 | b3<<40 | b4<<32 | 92 b5<<24 | b6<<16 | b7<<8 | b8<<0); 93 94 dtmp = Double.longBitsToDouble(tmp); 95 Array.setDouble(array, i, dtmp); 96 } 97 } 98 99 104 private static void swapFloatArray(Object array) { 105 int len = Array.getLength(array); 106 float ftmp; 107 int tmp; 108 int b1, b2, b3, b4; 109 110 for(int i = 0; i < len; i++) { 111 ftmp = Array.getFloat(array, i); 112 tmp = Float.floatToIntBits(ftmp); 113 114 b1 = (tmp >> 0) & 0xff; 115 b2 = (tmp >> 8) & 0xff; 116 b3 = (tmp >> 16) & 0xff; 117 b4 = (tmp >> 24) & 0xff; 118 tmp = (int)(b1<<24 | b2<<16 | b3<<8 | b4<<0); 119 120 ftmp = Float.intBitsToFloat(tmp); 121 Array.setFloat(array, i, ftmp); 122 } 123 } 124 125 130 private static void swapIntegerArray(Object array) { 131 int len = Array.getLength(array); 132 int tmp; 133 int b1, b2, b3, b4; 134 135 for(int i = 0; i < len; i++) { 136 tmp = Array.getInt(array, i); 137 138 b1 = (tmp >> 0) & 0xff; 139 b2 = (tmp >> 8) & 0xff; 140 b3 = (tmp >> 16) & 0xff; 141 b4 = (tmp >> 24) & 0xff; 142 tmp = (int)(b1<<24 | b2<<16 | b3<<8 | b4<<0); 143 144 Array.setInt(array, i, tmp); 145 } 146 } 147 148 153 private static void swapLongArray(Object array) { 154 int len = Array.getLength(array); 155 long tmp; 156 long b1, b2, b3, b4, b5, b6, b7, b8; 157 158 for(int i = 0; i < len; i++) { 159 tmp = Array.getLong(array, i); 160 161 b1 = (tmp >> 0) & 0xff; 162 b2 = (tmp >> 8) & 0xff; 163 b3 = (tmp >> 16) & 0xff; 164 b4 = (tmp >> 24) & 0xff; 165 b5 = (tmp >> 32) & 0xff; 166 b6 = (tmp >> 40) & 0xff; 167 b7 = (tmp >> 48) & 0xff; 168 b8 = (tmp >> 56) & 0xff; 169 tmp = (long)(b1<<56 | b2<<48 | b3<<40 | b4<<32 | 170 b5<<24 | b6<<16 | b7<<8 | b8<<0); 171 172 Array.setLong(array, i, tmp); 173 } 174 } 175 176 181 private static void swapShortArray(Object array) { 182 int len = Array.getLength(array); 183 short tmp; 184 int b1, b2; 185 186 for(int i = 0; i < len; i++) { 187 tmp = Array.getShort(array, i); 188 189 b1 = (tmp >> 0) & 0xff; 190 b2 = (tmp >> 8) & 0xff; 191 tmp = (short)(b1<<8 | b2<<0); 192 193 Array.setShort(array, i, tmp); 194 } 195 } 196 } 197 | Popular Tags |