All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.tom.kit.Bits Maven / Gradle / Ivy

The newest version!
package cn.tom.kit;

import java.nio.ByteBuffer;
/**
 * copy of java.nio.Bits
 * 
 * @author tomsun
 *
 */
public class Bits {

	// -- Swapping --

	public static short swap(short x) {
		return Short.reverseBytes(x);
	}

	public static char swap(char x) {
		return Character.reverseBytes(x);
	}

	public static int swap(int x) {
		return Integer.reverseBytes(x);
	}

	public static long swap(long x) {
		return Long.reverseBytes(x);
	}

	// -- get/put char --

	public static  char makeChar(byte b1, byte b0) {
		return (char) ((b1 << 8) | (b0 & 0xff));
	}

	public static char getCharL(ByteBuffer bb, int bi) {
		return makeChar(bb.get(bi + 1), bb.get(bi));
	}

	public static char getCharB(ByteBuffer bb, int bi) {
		return makeChar(bb.get(bi), bb.get(bi + 1));
	}

	public static char getChar(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getCharB(bb, bi) : getCharL(bb, bi);
	}

	public static byte char1(char x) {
		return (byte) (x >> 8);
	}

	public static byte char0(char x) {
		return (byte) (x);
	}

	public static void putCharL(ByteBuffer bb, int bi, char x) {
		bb.put(bi, char0(x));
		bb.put(bi + 1, char1(x));
	}

	public static void putCharB(ByteBuffer bb, int bi, char x) {
		bb.put(bi, char1(x));
		bb.put(bi + 1, char0(x));
	}

	public static void putChar(ByteBuffer bb, int bi, char x, boolean bigEndian) {
		if (bigEndian)
			putCharB(bb, bi, x);
		else
			putCharL(bb, bi, x);
	}


	// -- get/put short --

	public static  short makeShort(byte b1, byte b0) {
		return (short) ((b1 << 8) | (b0 & 0xff));
	}

	public static short getShortL(ByteBuffer bb, int bi) {
		return makeShort(bb.get(bi + 1), bb.get(bi));
	}

	public static short getShortB(ByteBuffer bb, int bi) {
		return makeShort(bb.get(bi), bb.get(bi + 1));
	}

	public static short getShort(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getShortB(bb, bi) : getShortL(bb, bi);
	}


	public static byte short1(short x) {
		return (byte) (x >> 8);
	}

	public static byte short0(short x) {
		return (byte) (x);
	}

	public static void putShortL(ByteBuffer bb, int bi, short x) {
		bb.put(bi, short0(x));
		bb.put(bi + 1, short1(x));
	}

	public static void putShortB(ByteBuffer bb, int bi, short x) {
		bb.put(bi, short1(x));
		bb.put(bi + 1, short0(x));
	}

	public static void putShort(ByteBuffer bb, int bi, short x, boolean bigEndian) {
		if (bigEndian)
			putShortB(bb, bi, x);
		else
			putShortL(bb, bi, x);
	}

	// -- get/put int --

	public static  int makeInt(byte b3, byte b2, byte b1, byte b0) {
		return (((b3) << 24) 
				| ((b2 & 0xff) << 16) 
				| ((b1 & 0xff) << 8) 
				| ((b0 & 0xff)));
	}

	public static int getIntL(ByteBuffer bb, int bi) {
		return makeInt(bb.get(bi + 3),
				bb.get(bi + 2), 
				bb.get(bi + 1),
				bb.get(bi));
	}

	public static int getIntB(ByteBuffer bb, int bi) {
		return makeInt(bb.get(bi), 
				bb.get(bi + 1), 
				bb.get(bi + 2), 
				bb.get(bi + 3));
	}

	public static int getInt(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getIntB(bb, bi) : getIntL(bb, bi);
	}


	public static byte int3(int x) {
		return (byte) (x >> 24);
	}

	public static byte int2(int x) {
		return (byte) (x >> 16);
	}

	public static byte int1(int x) {
		return (byte) (x >> 8);
	}

	public static byte int0(int x) {
		return (byte) (x);
	}

	public static void putIntL(ByteBuffer bb, int bi, int x) {
		bb.put(bi + 3, int3(x));
		bb.put(bi + 2, int2(x));
		bb.put(bi + 1, int1(x));
		bb.put(bi, int0(x));
	}

	public static void putIntB(ByteBuffer bb, int bi, int x) {
		bb.put(bi, int3(x));
		bb.put(bi + 1, int2(x));
		bb.put(bi + 2, int1(x));
		bb.put(bi + 3, int0(x));
	}

	public static void putInt(ByteBuffer bb, int bi, int x, boolean bigEndian) {
		if (bigEndian)
			putIntB(bb, bi, x);
		else
			putIntL(bb, bi, x);
	}

	// -- get/put long --

	public static  long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) {
		return ((((long) b7) << 56) 
				| (((long) b6 & 0xff) << 48) 
				| (((long) b5 & 0xff) << 40) 
				| (((long) b4 & 0xff) << 32) 
				| (((long) b3 & 0xff) << 24) 
				| (((long) b2 & 0xff) << 16) 
				| (((long) b1 & 0xff) << 8) 
				| (((long) b0 & 0xff)));
	}

	public static long getLongL(ByteBuffer bb, int bi) {
		return makeLong(bb.get(bi + 7),
				bb.get(bi + 6), 
				bb.get(bi + 5), 
				bb.get(bi + 4), 
				bb.get(bi + 3), 
				bb.get(bi + 2), 
				bb.get(bi + 1), 
				bb.get(bi));
	}

	public static long getLongB(ByteBuffer bb, int bi) {
		return makeLong(bb.get(bi), 
				bb.get(bi + 1),
				bb.get(bi + 2), 
				bb.get(bi + 3), 
				bb.get(bi + 4), 
				bb.get(bi + 5), 
				bb.get(bi + 6), 
				bb.get(bi + 7));
	}

	public static long getLong(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getLongB(bb, bi) : getLongL(bb, bi);
	}


	public static byte long7(long x) {
		return (byte) (x >> 56);
	}

	public static byte long6(long x) {
		return (byte) (x >> 48);
	}

	public static byte long5(long x) {
		return (byte) (x >> 40);
	}

	public static byte long4(long x) {
		return (byte) (x >> 32);
	}

	public static byte long3(long x) {
		return (byte) (x >> 24);
	}

	public static byte long2(long x) {
		return (byte) (x >> 16);
	}

	public static byte long1(long x) {
		return (byte) (x >> 8);
	}

	public static byte long0(long x) {
		return (byte) (x);
	}

	public static void putLongL(ByteBuffer bb, int bi, long x) {
		bb.put(bi + 7, long7(x));
		bb.put(bi + 6, long6(x));
		bb.put(bi + 5, long5(x));
		bb.put(bi + 4, long4(x));
		bb.put(bi + 3, long3(x));
		bb.put(bi + 2, long2(x));
		bb.put(bi + 1, long1(x));
		bb.put(bi, long0(x));
	}

	public static void putLongB(ByteBuffer bb, int bi, long x) {
		bb.put(bi, long7(x));
		bb.put(bi + 1, long6(x));
		bb.put(bi + 2, long5(x));
		bb.put(bi + 3, long4(x));
		bb.put(bi + 4, long3(x));
		bb.put(bi + 5, long2(x));
		bb.put(bi + 6, long1(x));
		bb.put(bi + 7, long0(x));
	}

	public static void putLong(ByteBuffer bb, int bi, long x, boolean bigEndian) {
		if (bigEndian)
			putLongB(bb, bi, x);
		else
			putLongL(bb, bi, x);
	}


	// -- get/put float --

	public static float getFloatL(ByteBuffer bb, int bi) {
		return Float.intBitsToFloat(getIntL(bb, bi));
	}


	public static float getFloatB(ByteBuffer bb, int bi) {
		return Float.intBitsToFloat(getIntB(bb, bi));
	}


	public static float getFloat(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi);
	}


	public static void putFloatL(ByteBuffer bb, int bi, float x) {
		putIntL(bb, bi, Float.floatToRawIntBits(x));
	}


	public static void putFloatB(ByteBuffer bb, int bi, float x) {
		putIntB(bb, bi, Float.floatToRawIntBits(x));
	}


	public static void putFloat(ByteBuffer bb, int bi, float x, boolean bigEndian) {
		if (bigEndian)
			putFloatB(bb, bi, x);
		else
			putFloatL(bb, bi, x);
	}


	// -- get/put double --

	public static double getDoubleL(ByteBuffer bb, int bi) {
		return Double.longBitsToDouble(getLongL(bb, bi));
	}


	public static double getDoubleB(ByteBuffer bb, int bi) {
		return Double.longBitsToDouble(getLongB(bb, bi));
	}


	public static double getDouble(ByteBuffer bb, int bi, boolean bigEndian) {
		return bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi);
	}


	public static void putDoubleL(ByteBuffer bb, int bi, double x) {
		putLongL(bb, bi, Double.doubleToRawLongBits(x));
	}


	public static void putDoubleB(ByteBuffer bb, int bi, double x) {
		putLongB(bb, bi, Double.doubleToRawLongBits(x));
	}

	public static void putDouble(ByteBuffer bb, int bi, double x, boolean bigEndian) {
		if (bigEndian)
			putDoubleB(bb, bi, x);
		else
			putDoubleL(bb, bi, x);
	}
	
	public static boolean getBoolean(byte[] b, int off) {
		return b[off] != 0;
	}

	public static char getChar(byte[] b, int off) {
		return (char) (((b[off + 1] & 0xFF)) +
				((b[off]) << 8));
	}

	public static short getShort(byte[] b, int off) {
		return (short) (((b[off + 1] & 0xFF)) +
				((b[off]) << 8));
	}

	public static int getInt(byte[] b, int off) {
		return ((b[off + 3] & 0xFF)) |
				((b[off + 2] & 0xFF) << 8) |
				((b[off + 1] & 0xFF) << 16) |
				((b[off]) << 24);
	}

	public static float getFloat(byte[] b, int off) {
		int i = getInt(b, off);
		return Float.intBitsToFloat(i);
	}

	public static long getLong(byte[] b, int off) {
		return ((b[off + 7] & 0xFFL)) +
				((b[off + 6] & 0xFFL) << 8) +
				((b[off + 5] & 0xFFL) << 16) +
				((b[off + 4] & 0xFFL) << 24) +
				((b[off + 3] & 0xFFL) << 32) +
				((b[off + 2] & 0xFFL) << 40) +
				((b[off + 1] & 0xFFL) << 48) +
				(((long) b[off]) << 56);
	}

	public static double getDouble(byte[] b, int off) {
		long j = getLong(b, off);
		return Double.longBitsToDouble(j);
	}

	public static void putBoolean(byte[] b, int off, boolean val) {
		b[off] = (byte) (val ? 1 : 0);
	}

	public static void putChar(byte[] b, int off, char val) {
		b[off + 1] = (byte) (val);
		b[off] = (byte) (val >>> 8);
	}

	public static void putShort(byte[] b, int off, short val) {
		b[off + 1] = (byte) (val);
		b[off] = (byte) (val >>> 8);
	}

	public static void putInt(byte[] b, int off, int val) {
		b[off + 3] = (byte) (val);
		b[off + 2] = (byte) (val >>> 8);
		b[off + 1] = (byte) (val >>> 16);
		b[off] = (byte) (val >>> 24);
	}

	public static void putFloat(byte[] b, int off, float val) {
		int i = Float.floatToIntBits(val);
		putInt(b, off, i);
	}

	public static void putLong(byte[] b, int off, long val) {
		b[off + 7] = (byte) (val);
		b[off + 6] = (byte) (val >>> 8);
		b[off + 5] = (byte) (val >>> 16);
		b[off + 4] = (byte) (val >>> 24);
		b[off + 3] = (byte) (val >>> 32);
		b[off + 2] = (byte) (val >>> 40);
		b[off + 1] = (byte) (val >>> 48);
		b[off] = (byte) (val >>> 56);
	}

	public static void putDouble(byte[] b, int off, double val) {
		long j = Double.doubleToLongBits(val);
		putLong(b, off, j);
	}
	
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy