?? endianutils.java
字號:
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Utility code for dealing with different endian systems.
* <p>
* Different computer architectures adopt different conventions for
* byte ordering. In so-called "Little Endian" architectures (eg Intel),
* the low-order byte is stored in memory at the lowest address, and
* subsequent bytes at higher addresses. For "Big Endian" architectures
* (eg Motorola), the situation is reversed.
* This class helps you solve this incompatability.
* <p>
* Origin of code: Excalibur
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @version $Id: EndianUtils.java 293039 2005-10-01 23:00:40Z scolebourne $
* @see org.apache.commons.io.input.SwappedDataInputStream
*/
public class EndianUtils {
/**
* Instances should NOT be constructed in standard programming.
*/
public EndianUtils() {
super();
}
// ========================================== Swapping routines
/**
* Converts a "short" value between endian systems.
* @param value value to convert
* @return the converted value
*/
public static short swapShort(short value) {
return (short) ( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
( ( ( value >> 8 ) & 0xff ) << 0 ) );
}
/**
* Converts a "int" value between endian systems.
* @param value value to convert
* @return the converted value
*/
public static int swapInteger(int value) {
return
( ( ( value >> 0 ) & 0xff ) << 24 ) +
( ( ( value >> 8 ) & 0xff ) << 16 ) +
( ( ( value >> 16 ) & 0xff ) << 8 ) +
( ( ( value >> 24 ) & 0xff ) << 0 );
}
/**
* Converts a "long" value between endian systems.
* @param value value to convert
* @return the converted value
*/
public static long swapLong(long value) {
return
( ( ( value >> 0 ) & 0xff ) << 56 ) +
( ( ( value >> 8 ) & 0xff ) << 48 ) +
( ( ( value >> 16 ) & 0xff ) << 40 ) +
( ( ( value >> 24 ) & 0xff ) << 32 ) +
( ( ( value >> 32 ) & 0xff ) << 24 ) +
( ( ( value >> 40 ) & 0xff ) << 16 ) +
( ( ( value >> 48 ) & 0xff ) << 8 ) +
( ( ( value >> 56 ) & 0xff ) << 0 );
}
/**
* Converts a "float" value between endian systems.
* @param value value to convert
* @return the converted value
*/
public static float swapFloat(float value) {
return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
}
/**
* Converts a "double" value between endian systems.
* @param value value to convert
* @return the converted value
*/
public static double swapDouble(double value) {
return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
}
// ========================================== Swapping read/write routines
/**
* Writes a "short" value to a byte array at a given offset. The value is
* converted to the opposed endian system while writing.
* @param data target byte array
* @param offset starting offset in the byte array
* @param value value to write
*/
public static void writeSwappedShort(byte[] data, int offset, short value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
}
/**
* Reads a "short" value from a byte array at a given offset. The value is
* converted to the opposed endian system while reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
public static short readSwappedShort(byte[] data, int offset) {
return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) );
}
/**
* Reads an unsigned short (16-bit) value from a byte array at a given
* offset. The value is converted to the opposed endian system while
* reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
public static int readSwappedUnsignedShort(byte[] data, int offset) {
return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) );
}
/**
* Writes a "int" value to a byte array at a given offset. The value is
* converted to the opposed endian system while writing.
* @param data target byte array
* @param offset starting offset in the byte array
* @param value value to write
*/
public static void writeSwappedInteger(byte[] data, int offset, int value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
}
/**
* Reads a "int" value from a byte array at a given offset. The value is
* converted to the opposed endian system while reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
public static int readSwappedInteger(byte[] data, int offset) {
return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
( ( data[ offset + 3 ] & 0xff ) << 24 ) );
}
/**
* Reads an unsigned integer (32-bit) value from a byte array at a given
* offset. The value is converted to the opposed endian system while
* reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
public static long readSwappedUnsignedInteger(byte[] data, int offset) {
return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
( ( data[ offset + 3 ] & 0xff ) << 24 ) );
}
/**
* Writes a "long" value to a byte array at a given offset. The value is
* converted to the opposed endian system while writing.
* @param data target byte array
* @param offset starting offset in the byte array
* @param value value to write
*/
public static void writeSwappedLong(byte[] data, int offset, long value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
}
/**
* Reads a "long" value from a byte array at a given offset. The value is
* converted to the opposed endian system while reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
public static long readSwappedLong(byte[] data, int offset) {
long low = (long)(
( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
( ( data[ offset + 3 ] & 0xff ) << 24 ) );
long high = (long)(
( ( data[ offset + 4 ] & 0xff ) << 0 ) +
( ( data[ offset + 5 ] & 0xff ) << 8 ) +
( ( data[ offset + 6 ] & 0xff ) << 16 ) +
( ( data[ offset + 7 ] & 0xff ) << 24 ) );
return low + (high << 32);
}
/**
* Writes a "float" value to a byte array at a given offset. The value is
* converted to the opposed endian system while writing.
* @param data target byte array
* @param offset starting offset in the byte array
* @param value value to write
*/
public static void writeSwappedFloat(byte[] data, int offset, float value) {
writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
}
/**
* Reads a "float" value from a byte array at a given offset. The value is
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -