1 /* %%Ignore-License 2 * (C) 2004 - Geotechnical Software Services 3 * 4 * This code is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This code is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this program; if not, write to the Free 16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 17 * MA 02111-1307, USA. 18 */ 19 package info.mikethomas.fahview.v6project.utilities; 20 21 /** 22 * Utility class for doing byte swapping (i.e. conversion between little-endian 23 * and big-endian representations) of different data types. Byte swapping is 24 * typically used when data is read from a stream delivered by a system of 25 * different endian type as the present one. 26 * 27 * @author <a href="mailto:info@geosoft.no">GeoSoft</a> 28 * @version $Id: $Id 29 */ 30 public class ByteSwapper { 31 32 /** 33 * Byte swap a single short value. 34 * 35 * @param value Value to byte swap. 36 * @return Byte swapped representation. 37 */ 38 public static short swap(short value) { 39 int b1 = value & 0xff; 40 int b2 = ( value >> 8 ) & 0xff; 41 42 return (short) ( b1 << 8 | b2 << 0 ); 43 } 44 45 /** 46 * Byte swap a single int value. 47 * 48 * @param value Value to byte swap. 49 * @return Byte swapped representation. 50 */ 51 public static int swap(int value) { 52 int b1 = ( value >> 0 ) & 0xff; 53 int b2 = ( value >> 8 ) & 0xff; 54 int b3 = ( value >> 16 ) & 0xff; 55 int b4 = ( value >> 24 ) & 0xff; 56 57 return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0; 58 } 59 60 /** 61 * Byte swap a single long value. 62 * 63 * @param value Value to byte swap. 64 * @return Byte swapped representation. 65 */ 66 public static long swap(long value) { 67 long b1 = ( value >> 0 ) & 0xff; 68 long b2 = ( value >> 8 ) & 0xff; 69 long b3 = ( value >> 16 ) & 0xff; 70 long b4 = ( value >> 24 ) & 0xff; 71 long b5 = ( value >> 32 ) & 0xff; 72 long b6 = ( value >> 40 ) & 0xff; 73 long b7 = ( value >> 48 ) & 0xff; 74 long b8 = ( value >> 56 ) & 0xff; 75 76 return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 77 | b5 << 24 | b6 << 16 | b7 << 8 | b8 << 0; 78 } 79 80 /** 81 * Byte swap a single float value. 82 * 83 * @param value Value to byte swap. 84 * @return Byte swapped representation. 85 */ 86 public static float swap(float value) { 87 int intValue = Float.floatToIntBits(value); 88 intValue = swap(intValue); 89 return Float.intBitsToFloat(intValue); 90 } 91 92 /** 93 * Byte swap a single double value. 94 * 95 * @param value Value to byte swap. 96 * @return Byte swapped representation. 97 */ 98 public static double swap(double value) { 99 long longValue = Double.doubleToLongBits(value); 100 longValue = swap(longValue); 101 return Double.longBitsToDouble(longValue); 102 } 103 104 /** 105 * Byte swap an array of shorts. The result of the swapping is put back into 106 * the specified array. 107 * 108 * @param array Array of values to swap 109 */ 110 public static void swap(short[] array) { 111 for (int i = 0; i < array.length; i++) { 112 array[i] = swap(array[i]); 113 } 114 } 115 116 /** 117 * Byte swap an array of ints. The result of the swapping is put back into 118 * the specified array. 119 * 120 * @param array Array of values to swap 121 */ 122 public static void swap(int[] array) { 123 for (int i = 0; i < array.length; i++) { 124 array[i] = swap(array[i]); 125 } 126 } 127 128 /** 129 * Byte swap an array of longs. The result of the swapping is put back into 130 * the specified array. 131 * 132 * @param array Array of values to swap 133 */ 134 public static void swap(long[] array) { 135 for (int i = 0; i < array.length; i++) { 136 array[i] = swap(array[i]); 137 } 138 } 139 140 /** 141 * Byte swap an array of floats. The result of the swapping is put back into 142 * the specified array. 143 * 144 * @param array Array of values to swap 145 */ 146 public static void swap(float[] array) { 147 for (int i = 0; i < array.length; i++) { 148 array[i] = swap(array[i]); 149 } 150 } 151 152 /** 153 * Byte swap an array of doubles. The result of the swapping is put back 154 * into the specified array. 155 * 156 * @param array Array of values to swap 157 */ 158 public static void swap(double[] array) { 159 for (int i = 0; i < array.length; i++) { 160 array[i] = swap(array[i]); 161 } 162 } 163 }