FileDocCategorySizeDatePackage
Bits.javaAPI DocJava SE 5 API17158Fri Aug 26 14:57:08 BST 2005java.nio

Bits

public class Bits extends Object
Access to bits, native and otherwise.

Fields Summary
private static final Unsafe
unsafe
private static ByteOrder
byteOrder
private static int
pageSize
private static boolean
unaligned
private static boolean
unalignedKnown
private static volatile long
maxMemory
private static volatile long
reservedMemory
private static boolean
memoryLimitSet
static final int
JNI_COPY_TO_ARRAY_THRESHOLD
static final int
JNI_COPY_FROM_ARRAY_THRESHOLD
Constructors Summary
private Bits()

 
Methods Summary
private static byte_get(long a)


         
	return unsafe.getByte(a);
    
private static void_put(long a, byte b)

	unsafe.putByte(a, b);
    
static java.nio.ByteOrderbyteOrder()


       
	if (byteOrder != null)
	    return byteOrder;
	long a = unsafe.allocateMemory(8);
	try {
	    unsafe.putLong(a, 0x0102030405060708L);
	    byte b = unsafe.getByte(a);
	    switch (b) {
	    case 0x01: byteOrder = ByteOrder.BIG_ENDIAN;     break;
	    case 0x08: byteOrder = ByteOrder.LITTLE_ENDIAN;  break;
	    default:
		throw new Error("Unknown byte order");
	    }
	} finally {
	    unsafe.freeMemory(a);
	}
	return byteOrder;
    
private static bytechar0(char x)

 return (byte)(x >> 0); 
private static bytechar1(char x)

 return (byte)(x >> 8); 
static native voidcopyFromByteArray(java.lang.Object src, long srcPos, long dstAddr, long length)

static voidcopyFromCharArray(java.lang.Object src, long srcPos, long dstAddr, long length)


    // These methods do no bounds checking.  Verification that the copy will not
    // result in memory corruption should be done prior to invocation.
    // All positions and lengths are specified in bytes.

            
					  
            
				        

           
				   
    
	copyFromShortArray(src, srcPos, dstAddr, length);
    
static native voidcopyFromIntArray(java.lang.Object src, long srcPos, long dstAddr, long length)

static native voidcopyFromLongArray(java.lang.Object src, long srcPos, long dstAddr, long length)

static native voidcopyFromShortArray(java.lang.Object src, long srcPos, long dstAddr, long length)

static native voidcopyToByteArray(long srcAddr, java.lang.Object dst, long dstPos, long length)

static voidcopyToCharArray(long srcAddr, java.lang.Object dst, long dstPos, long length)

 	copyToShortArray(srcAddr, dst, dstPos, length);
    
static native voidcopyToIntArray(long srcAddr, java.lang.Object dst, long dstPos, long length)

static native voidcopyToLongArray(long srcAddr, java.lang.Object dst, long dstPos, long length)

static native voidcopyToShortArray(long srcAddr, java.lang.Object dst, long dstPos, long length)

static chargetChar(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getCharB(bb, bi) : getCharL(bb, bi));
    
static chargetChar(long a, boolean bigEndian)

	return (bigEndian ? getCharB(a) : getCharL(a));
    
static chargetCharB(long a)

	return makeChar(_get(a + 0),
			_get(a + 1));
    
static chargetCharB(java.nio.ByteBuffer bb, int bi)

	return makeChar(bb._get(bi + 0),
			bb._get(bi + 1));
    
static chargetCharL(java.nio.ByteBuffer bb, int bi)

	return makeChar(bb._get(bi + 1),
			bb._get(bi + 0));
    
static chargetCharL(long a)

	return makeChar(_get(a + 1),
			_get(a + 0));
    
static doublegetDouble(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi));
    
static doublegetDouble(long a, boolean bigEndian)

	return (bigEndian ? getDoubleB(a) : getDoubleL(a));
    
static doublegetDoubleB(java.nio.ByteBuffer bb, int bi)

	return Double.longBitsToDouble(getLongB(bb, bi));
    
static doublegetDoubleB(long a)

	return Double.longBitsToDouble(getLongB(a));
    
static doublegetDoubleL(java.nio.ByteBuffer bb, int bi)

	return Double.longBitsToDouble(getLongL(bb, bi));
    
static doublegetDoubleL(long a)

	return Double.longBitsToDouble(getLongL(a));
    
static floatgetFloat(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi));
    
static floatgetFloat(long a, boolean bigEndian)

	return (bigEndian ? getFloatB(a) : getFloatL(a));
    
static floatgetFloatB(java.nio.ByteBuffer bb, int bi)

	return Float.intBitsToFloat(getIntB(bb, bi));
    
static floatgetFloatB(long a)

	return Float.intBitsToFloat(getIntB(a));
    
static floatgetFloatL(java.nio.ByteBuffer bb, int bi)

	return Float.intBitsToFloat(getIntL(bb, bi));
    
static floatgetFloatL(long a)

	return Float.intBitsToFloat(getIntL(a));
    
static intgetInt(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getIntB(bb, bi) : getIntL(bb, bi));
    
static intgetInt(long a, boolean bigEndian)

	return (bigEndian ? getIntB(a) : getIntL(a));
    
static intgetIntB(java.nio.ByteBuffer bb, int bi)

	return makeInt(bb._get(bi + 0),
		       bb._get(bi + 1),
		       bb._get(bi + 2),
		       bb._get(bi + 3));
    
static intgetIntB(long a)

	return makeInt(_get(a + 0),
		       _get(a + 1),
		       _get(a + 2),
		       _get(a + 3));
    
static intgetIntL(java.nio.ByteBuffer bb, int bi)

	return makeInt(bb._get(bi + 3),
		       bb._get(bi + 2),
		       bb._get(bi + 1),
		       bb._get(bi + 0));
    
static intgetIntL(long a)

	return makeInt(_get(a + 3),
		       _get(a + 2),
		       _get(a + 1),
		       _get(a + 0));
    
static longgetLong(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getLongB(bb, bi) : getLongL(bb, bi));
    
static longgetLong(long a, boolean bigEndian)

	return (bigEndian ? getLongB(a) : getLongL(a));
    
static longgetLongB(java.nio.ByteBuffer bb, int bi)

	return makeLong(bb._get(bi + 0),
			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));
    
static longgetLongB(long a)

	return makeLong(_get(a + 0),
			_get(a + 1),
			_get(a + 2),
			_get(a + 3),
			_get(a + 4),
			_get(a + 5),
			_get(a + 6),
			_get(a + 7));
    
static longgetLongL(java.nio.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 + 0));
    
static longgetLongL(long a)

	return makeLong(_get(a + 7),
			_get(a + 6),
			_get(a + 5),
			_get(a + 4),
			_get(a + 3),
			_get(a + 2),
			_get(a + 1),
			_get(a + 0));
    
static shortgetShort(java.nio.ByteBuffer bb, int bi, boolean bigEndian)

	return (bigEndian ? getShortB(bb, bi) : getShortL(bb, bi));
    
static shortgetShort(long a, boolean bigEndian)

	return (bigEndian ? getShortB(a) : getShortL(a));
    
static shortgetShortB(java.nio.ByteBuffer bb, int bi)

	return makeShort(bb._get(bi + 0),
			 bb._get(bi + 1));
    
static shortgetShortB(long a)

	return makeShort(_get(a),
			 _get(a + 1));
    
static shortgetShortL(java.nio.ByteBuffer bb, int bi)

	return makeShort(bb._get(bi + 1),
			 bb._get(bi + 0));
    
static shortgetShortL(long a)

	return makeShort(_get(a + 1),
			 _get(a));
    
private static byteint0(int x)

 return (byte)(x >>  0); 
private static byteint1(int x)

 return (byte)(x >>  8); 
private static byteint2(int x)

 return (byte)(x >> 16); 
private static byteint3(int x)

 return (byte)(x >> 24); 
private static bytelong0(long x)

 return (byte)(x >>  0); 
private static bytelong1(long x)

 return (byte)(x >>  8); 
private static bytelong2(long x)

 return (byte)(x >> 16); 
private static bytelong3(long x)

 return (byte)(x >> 24); 
private static bytelong4(long x)

 return (byte)(x >> 32); 
private static bytelong5(long x)

 return (byte)(x >> 40); 
private static bytelong6(long x)

 return (byte)(x >> 48); 
private static bytelong7(long x)

 return (byte)(x >> 56); 
private static charmakeChar(byte b1, byte b0)

	return (char)((b1 << 8) | (b0 & 0xff));
    
private static intmakeInt(byte b3, byte b2, byte b1, byte b0)

	return (int)((((b3 & 0xff) << 24) |
		      ((b2 & 0xff) << 16) |
		      ((b1 & 0xff) <<  8) |
		      ((b0 & 0xff) <<  0)));
    
private static longmakeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0)

	return ((((long)b7 & 0xff) << 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) <<  0));
    
private static shortmakeShort(byte b1, byte b0)

	return (short)((b1 << 8) | (b0 & 0xff));
    
static intpageSize()


       
	if (pageSize == -1)
	    pageSize = unsafe().pageSize();
	return pageSize;
    
static voidputChar(java.nio.ByteBuffer bb, int bi, char x, boolean bigEndian)

	if (bigEndian)
	    putCharB(bb, bi, x);
	else
	    putCharL(bb, bi, x);
    
static voidputChar(long a, char x, boolean bigEndian)

	if (bigEndian)
	    putCharB(a, x);
	else
	    putCharL(a, x);
    
static voidputCharB(java.nio.ByteBuffer bb, int bi, char x)

	bb._put(bi + 0, char1(x));
	bb._put(bi + 1, char0(x));
    
static voidputCharB(long a, char x)

	_put(a + 0, char1(x));
	_put(a + 1, char0(x));
    
static voidputCharL(java.nio.ByteBuffer bb, int bi, char x)

	bb._put(bi + 0, char0(x));
	bb._put(bi + 1, char1(x));
    
static voidputCharL(long a, char x)

	_put(a + 0, char0(x));
	_put(a + 1, char1(x));
    
static voidputDouble(java.nio.ByteBuffer bb, int bi, double x, boolean bigEndian)

	if (bigEndian)
	    putDoubleB(bb, bi, x);
	else
	    putDoubleL(bb, bi, x);
    
static voidputDouble(long a, double x, boolean bigEndian)

	if (bigEndian)
	    putDoubleB(a, x);
	else
	    putDoubleL(a, x);
    
static voidputDoubleB(java.nio.ByteBuffer bb, int bi, double x)

	putLongB(bb, bi, Double.doubleToRawLongBits(x));
    
static voidputDoubleB(long a, double x)

	putLongB(a, Double.doubleToRawLongBits(x));
    
static voidputDoubleL(java.nio.ByteBuffer bb, int bi, double x)

	putLongL(bb, bi, Double.doubleToRawLongBits(x));
    
static voidputDoubleL(long a, double x)

	putLongL(a, Double.doubleToRawLongBits(x));
    
static voidputFloat(java.nio.ByteBuffer bb, int bi, float x, boolean bigEndian)

	if (bigEndian)
	    putFloatB(bb, bi, x);
	else
	    putFloatL(bb, bi, x);
    
static voidputFloat(long a, float x, boolean bigEndian)

	if (bigEndian)
	    putFloatB(a, x);
	else
	    putFloatL(a, x);
    
static voidputFloatB(java.nio.ByteBuffer bb, int bi, float x)

	putIntB(bb, bi, Float.floatToRawIntBits(x));
    
static voidputFloatB(long a, float x)

	putIntB(a, Float.floatToRawIntBits(x));
    
static voidputFloatL(java.nio.ByteBuffer bb, int bi, float x)

	putIntL(bb, bi, Float.floatToRawIntBits(x));
    
static voidputFloatL(long a, float x)

	putIntL(a, Float.floatToRawIntBits(x));
    
static voidputInt(java.nio.ByteBuffer bb, int bi, int x, boolean bigEndian)

	if (bigEndian)
	    putIntB(bb, bi, x);
	else
	    putIntL(bb, bi, x);
    
static voidputInt(long a, int x, boolean bigEndian)

	if (bigEndian)
	    putIntB(a, x);
	else
	    putIntL(a, x);
    
static voidputIntB(java.nio.ByteBuffer bb, int bi, int x)

	bb._put(bi + 0, int3(x));
	bb._put(bi + 1, int2(x));
	bb._put(bi + 2, int1(x));
	bb._put(bi + 3, int0(x));
    
static voidputIntB(long a, int x)

	_put(a + 0, int3(x));
	_put(a + 1, int2(x));
	_put(a + 2, int1(x));
	_put(a + 3, int0(x));
    
static voidputIntL(java.nio.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 + 0, int0(x));
    
static voidputIntL(long a, int x)

	_put(a + 3, int3(x));
	_put(a + 2, int2(x));
	_put(a + 1, int1(x));
	_put(a + 0, int0(x));
    
static voidputLong(java.nio.ByteBuffer bb, int bi, long x, boolean bigEndian)

	if (bigEndian)
	    putLongB(bb, bi, x);
	else
	    putLongL(bb, bi, x);
    
static voidputLong(long a, long x, boolean bigEndian)

	if (bigEndian)
	    putLongB(a, x);
	else
	    putLongL(a, x);
    
static voidputLongB(java.nio.ByteBuffer bb, int bi, long x)

	bb._put(bi + 0, 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));
    
static voidputLongB(long a, long x)

	_put(a + 0, long7(x));
	_put(a + 1, long6(x));
	_put(a + 2, long5(x));
	_put(a + 3, long4(x));
	_put(a + 4, long3(x));
	_put(a + 5, long2(x));
	_put(a + 6, long1(x));
	_put(a + 7, long0(x));
    
static voidputLongL(java.nio.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 + 0, long0(x));
    
static voidputLongL(long a, long x)

	_put(a + 7, long7(x));
	_put(a + 6, long6(x));
	_put(a + 5, long5(x));
	_put(a + 4, long4(x));
	_put(a + 3, long3(x));
	_put(a + 2, long2(x));
	_put(a + 1, long1(x));
	_put(a + 0, long0(x));
    
static voidputShort(java.nio.ByteBuffer bb, int bi, short x, boolean bigEndian)

	if (bigEndian)
	    putShortB(bb, bi, x);
	else
	    putShortL(bb, bi, x);
    
static voidputShort(long a, short x, boolean bigEndian)

	if (bigEndian)
	    putShortB(a, x);
	else
	    putShortL(a, x);
    
static voidputShortB(java.nio.ByteBuffer bb, int bi, short x)

	bb._put(bi + 0, short1(x));
	bb._put(bi + 1, short0(x));
    
static voidputShortB(long a, short x)

	_put(a, short1(x));
	_put(a + 1, short0(x));
    
static voidputShortL(java.nio.ByteBuffer bb, int bi, short x)

	bb._put(bi + 0, short0(x));
	bb._put(bi + 1, short1(x));
    
static voidputShortL(long a, short x)

	_put(a, short0(x));
	_put(a + 1, short1(x));
    
static voidreserveMemory(long size)


    // These methods should be called whenever direct memory is allocated or
    // freed.  They allow the user to control the amount of direct memory
    // which a process may access.  All sizes are specified in bytes.
        

	synchronized (Bits.class) {
	    if (!memoryLimitSet && VM.isBooted()) {
		maxMemory = VM.maxDirectMemory();
		memoryLimitSet = true;
	    }
	    if (size <= maxMemory - reservedMemory) {
		reservedMemory += size;
		return;
	    }
	}

	System.gc();
	try {
	    Thread.sleep(100);
	} catch (InterruptedException x) {
	    // Restore interrupt status
	    Thread.currentThread().interrupt();
	}
	synchronized (Bits.class) {
	    if (reservedMemory + size > maxMemory)
		throw new OutOfMemoryError("Direct buffer memory");
	    reservedMemory += size;
	}

    
private static byteshort0(short x)

 return (byte)(x >> 0); 
private static byteshort1(short x)

 return (byte)(x >> 8); 
static shortswap(short x)

	return (short)((x << 8) |
		       ((x >> 8) & 0xff));
    
static charswap(char x)

	return (char)((x << 8) |
		      ((x >> 8) & 0xff));
    
static intswap(int x)

	return (int)((swap((short)x) << 16) |
		     (swap((short)(x >> 16)) & 0xffff));
    
static longswap(long x)

	return (long)(((long)swap((int)(x)) << 32) |
		      ((long)swap((int)(x >> 32)) & 0xffffffffL));
    
static booleanunaligned()


       
	if (unalignedKnown)
	    return unaligned;
        PrivilegedAction pa
	    = new sun.security.action.GetPropertyAction("os.arch");
        String arch = (String)AccessController.doPrivileged(pa);
	unaligned = arch.equals("i386") || arch.equals("x86");
	unalignedKnown = true;
	return unaligned;
    
static synchronized voidunreserveMemory(long size)

	if (reservedMemory > 0) {
	    reservedMemory -= size;
	    assert (reservedMemory > -1);
	}
    
static sun.misc.Unsafeunsafe()

	return unsafe;