1 50 package org.apache.avalon.excalibur.io; 51 52 import java.io.EOFException ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.io.OutputStream ; 56 57 64 public final class EndianUtil 65 { 66 public static final int SIZEOF_BYTE = 1; 67 public static final int SIZEOF_SHORT = 2; 68 public static final int SIZEOF_INT = 4; 69 public static final int SIZEOF_FLOAT = 4; 70 public static final int SIZEOF_LONG = 8; 71 72 public static short swapShort( final short value ) 73 { 74 return (short)( ( ( ( value >> 0 ) & 0xff ) << 8 ) + 75 ( ( ( value >> 8 ) & 0xff ) << 0 ) ); 76 } 77 78 public static int swapInteger( final int value ) 79 { 80 return 81 ( ( ( value >> 0 ) & 0xff ) << 24 ) + 82 ( ( ( value >> 8 ) & 0xff ) << 16 ) + 83 ( ( ( value >> 16 ) & 0xff ) << 8 ) + 84 ( ( ( value >> 24 ) & 0xff ) << 0 ); 85 } 86 87 public static long swapLong( final long value ) 88 { 89 return 90 ( ( ( value >> 0 ) & 0xff ) << 56 ) + 91 ( ( ( value >> 8 ) & 0xff ) << 48 ) + 92 ( ( ( value >> 16 ) & 0xff ) << 40 ) + 93 ( ( ( value >> 24 ) & 0xff ) << 32 ) + 94 ( ( ( value >> 32 ) & 0xff ) << 24 ) + 95 ( ( ( value >> 40 ) & 0xff ) << 16 ) + 96 ( ( ( value >> 48 ) & 0xff ) << 8 ) + 97 ( ( ( value >> 56 ) & 0xff ) << 0 ); 98 } 99 100 public static float swapFloat( final float value ) 101 { 102 return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) ); 103 } 104 105 public static double swapDouble( final double value ) 106 { 107 return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) ); 108 } 109 110 public static void writeSwappedShort( final byte[] data, final int offset, final int value ) 111 { 112 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff ); 113 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff ); 114 } 115 116 public static short readSwappedShort( final byte[] data, final int offset ) 117 { 118 return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + 119 ( ( data[ offset + 1 ] & 0xff ) << 8 ) ); 120 } 121 122 public static int readSwappedUnsignedShort( final byte[] data, final int offset ) 123 { 124 return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + 125 ( ( data[ offset + 1 ] & 0xff ) << 8 ) ); 126 } 127 128 public static void writeSwappedInteger( final byte[] data, final int offset, final int value ) 129 { 130 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff ); 131 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff ); 132 data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff ); 133 data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff ); 134 } 135 136 public static int readSwappedInteger( final byte[] data, final int offset ) 137 { 138 return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + 139 ( ( data[ offset + 1 ] & 0xff ) << 8 ) + 140 ( ( data[ offset + 2 ] & 0xff ) << 16 ) + 141 ( ( data[ offset + 3 ] & 0xff ) << 24 ) ); 142 } 143 144 public static long readSwappedUnsignedInteger( final byte[] data, final int offset ) 145 { 146 return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + 147 ( ( data[ offset + 1 ] & 0xff ) << 8 ) + 148 ( ( data[ offset + 2 ] & 0xff ) << 16 ) + 149 ( ( data[ offset + 3 ] & 0xff ) << 24 ) ); 150 } 151 152 public static void writeSwappedLong( final byte[] data, final int offset, final long value ) 153 { 154 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff ); 155 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff ); 156 data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff ); 157 data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff ); 158 data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff ); 159 data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff ); 160 data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff ); 161 data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff ); 162 } 163 164 public static long readSwappedLong( final byte[] data, final int offset ) 165 { 166 return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + 167 ( ( data[ offset + 1 ] & 0xff ) << 8 ) + 168 ( ( data[ offset + 2 ] & 0xff ) << 16 ) + 169 ( ( data[ offset + 3 ] & 0xff ) << 24 ) + 170 ( ( data[ offset + 4 ] & 0xff ) << 32 ) + 171 ( ( data[ offset + 5 ] & 0xff ) << 40 ) + 172 ( ( data[ offset + 6 ] & 0xff ) << 48 ) + 173 ( ( data[ offset + 7 ] & 0xff ) << 56 ) ); 174 } 175 176 public static void writeSwappedFloat( final byte[] data, final int offset, final float value ) 177 { 178 writeSwappedInteger( data, offset, Float.floatToIntBits( value ) ); 179 } 180 181 public static float readSwappedFloat( final byte[] data, final int offset ) 182 { 183 return Float.intBitsToFloat( readSwappedInteger( data, offset ) ); 184 } 185 186 public static void writeSwappedDouble( final byte[] data, final int offset, final double value ) 187 { 188 writeSwappedLong( data, offset, Double.doubleToLongBits( value ) ); 189 } 190 191 public static double readSwappedDouble( final byte[] data, final int offset ) 192 { 193 return Double.longBitsToDouble( readSwappedLong( data, offset ) ); 194 } 195 196 public static void writeSwappedShort( final OutputStream output, final int value ) 202 throws IOException 203 { 204 output.write( (byte)( ( value >> 0 ) & 0xff ) ); 205 output.write( (byte)( ( value >> 8 ) & 0xff ) ); 206 } 207 208 public static short readSwappedShort( final InputStream input ) 209 throws IOException 210 { 211 return (short)( ( ( read( input ) & 0xff ) << 0 ) + 212 ( ( read( input ) & 0xff ) << 8 ) ); 213 } 214 215 public static int readSwappedUnsignedShort( final InputStream input ) 216 throws IOException 217 { 218 final int value1 = read( input ); 219 final int value2 = read( input ); 220 221 return (int)( ( ( value1 & 0xff ) << 0 ) + 222 ( ( value2 & 0xff ) << 8 ) ); 223 } 224 225 public static void writeSwappedInteger( final OutputStream output, final int value ) 226 throws IOException 227 { 228 output.write( (byte)( ( value >> 0 ) & 0xff ) ); 229 output.write( (byte)( ( value >> 8 ) & 0xff ) ); 230 output.write( (byte)( ( value >> 16 ) & 0xff ) ); 231 output.write( (byte)( ( value >> 24 ) & 0xff ) ); 232 } 233 234 public static int readSwappedInteger( final InputStream input ) 235 throws IOException 236 { 237 final int value1 = read( input ); 238 final int value2 = read( input ); 239 final int value3 = read( input ); 240 final int value4 = read( input ); 241 242 return (int)( ( ( value1 & 0xff ) << 0 ) + 243 ( ( value2 & 0xff ) << 8 ) + 244 ( ( value3 & 0xff ) << 16 ) + 245 ( ( value4 & 0xff ) << 24 ) ); 246 } 247 248 public static long readSwappedUnsignedInteger( final InputStream input ) 249 throws IOException 250 { 251 final int value1 = read( input ); 252 final int value2 = read( input ); 253 final int value3 = read( input ); 254 final int value4 = read( input ); 255 256 return (long)( ( ( value1 & 0xff ) << 0 ) + 257 ( ( value2 & 0xff ) << 8 ) + 258 ( ( value3 & 0xff ) << 16 ) + 259 ( ( value4 & 0xff ) << 24 ) ); 260 } 261 262 public static void writeSwappedLong( final OutputStream output, final long value ) 263 throws IOException 264 { 265 output.write( (byte)( ( value >> 0 ) & 0xff ) ); 266 output.write( (byte)( ( value >> 8 ) & 0xff ) ); 267 output.write( (byte)( ( value >> 16 ) & 0xff ) ); 268 output.write( (byte)( ( value >> 24 ) & 0xff ) ); 269 output.write( (byte)( ( value >> 32 ) & 0xff ) ); 270 output.write( (byte)( ( value >> 40 ) & 0xff ) ); 271 output.write( (byte)( ( value >> 48 ) & 0xff ) ); 272 output.write( (byte)( ( value >> 56 ) & 0xff ) ); 273 } 274 275 public static long readSwappedLong( final InputStream input ) 276 throws IOException 277 { 278 final int value1 = read( input ); 279 final int value2 = read( input ); 280 final int value3 = read( input ); 281 final int value4 = read( input ); 282 final int value5 = read( input ); 283 final int value6 = read( input ); 284 final int value7 = read( input ); 285 final int value8 = read( input ); 286 287 return (long)( ( ( value1 & 0xff ) << 0 ) + 288 ( ( value2 & 0xff ) << 8 ) + 289 ( ( value3 & 0xff ) << 16 ) + 290 ( ( value4 & 0xff ) << 24 ) + 291 ( ( value5 & 0xff ) << 32 ) + 292 ( ( value6 & 0xff ) << 40 ) + 293 ( ( value7 & 0xff ) << 48 ) + 294 ( ( value8 & 0xff ) << 56 ) ); 295 } 296 297 public static void writeSwappedFloat( final OutputStream output, final float value ) 298 throws IOException 299 { 300 writeSwappedInteger( output, Float.floatToIntBits( value ) ); 301 } 302 303 public static float readSwappedFloat( final InputStream input ) 304 throws IOException 305 { 306 return Float.intBitsToFloat( readSwappedInteger( input ) ); 307 } 308 309 public static void writeSwappedDouble( final OutputStream output, final double value ) 310 throws IOException 311 { 312 writeSwappedLong( output, Double.doubleToLongBits( value ) ); 313 } 314 315 public static double readSwappedDouble( final InputStream input ) 316 throws IOException 317 { 318 return Double.longBitsToDouble( readSwappedLong( input ) ); 319 } 320 321 private static int read( final InputStream input ) 322 throws IOException 323 { 324 final int value = input.read(); 325 326 if( -1 == value ) 327 { 328 throw new EOFException ( "Unexpected EOF reached" ); 329 } 330 331 return value; 332 } 333 } 334 | Popular Tags |