⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib.java

📁 nachos操作系统框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    public static int bytesToInt(byte[] array, int offset) {	return (int) ((((int) array[offset+0] & 0xFF) << 0)  |		      (((int) array[offset+1] & 0xFF) << 8)  |		      (((int) array[offset+2] & 0xFF) << 16) |		      (((int) array[offset+3] & 0xFF) << 24));    }        /**     * Convert to an int from a little-endian byte string representation of the     * specified length.     *     * @param	array	the array containing the byte string.     * @param	offset	the offset of the byte string in the array.     * @param	length	the length of the byte string.     * @return	the corresponding value.     */    public static int bytesToInt(byte[] array, int offset, int length) {	assert(length==1 || length==2 || length==4);	switch (length) {	case 1:	    return array[offset];	case 2:	    return bytesToShort(array, offset);	case 4:	    return bytesToInt(array, offset);	default:	    return -1;	}    }    /**     * Convert to a string from a possibly null-terminated array of bytes.     *     * @param	array	the array containing the byte string.     * @param	offset	the offset of the byte string in the array.     * @param	length	the maximum length of the byte string.     * @return	a string containing the specified bytes, up to and not     *		including the null-terminator (if present).     */    public static String bytesToString(byte[] array, int offset, int length) {	int i;	for (i=0; i<length; i++) {	    if (array[offset+i] == 0)		break;	}	return new String(array, offset, i);    }    /** Mask out and shift a bit substring.     *     * @param	bits	the bit string.     * @param	lowest	the first bit of the substring within the string.     * @param	size	the number of bits in the substring.     * @return	the substring.     */    public static int extract(int bits, int lowest, int size) {	if (size == 32)	    return (bits >> lowest);	else	    return ((bits >> lowest) & ((1<<size)-1));    }    /** Mask out and shift a bit substring.     *     * @param	bits	the bit string.     * @param	lowest	the first bit of the substring within the string.     * @param	size	the number of bits in the substring.     * @return	the substring.     */    public static long extract(long bits, int lowest, int size) {	if (size == 64)	    return (bits >> lowest);	else	    return ((bits >> lowest) & ((1L<<size)-1));    }    /** Mask out and shift a bit substring; then sign extend the substring.     *     * @param	bits	the bit string.     * @param	lowest	the first bit of the substring within the string.     * @param	size	the number of bits in the substring.     * @return	the substring, sign-extended.     */    public static int extend(int bits, int lowest, int size) {	int extra = 32 - (lowest+size);	return ((extract(bits, lowest, size) << extra) >> extra);    }    /** Test if a bit is set in a bit string.     *     * @param	flag	the flag to test.     * @param	bits	the bit string.     * @return	<tt>true</tt> if <tt>(bits & flag)</tt> is non-zero.     */    public static boolean test(long flag, long bits) {	return ((bits & flag) != 0);    }    /**     * Creates a padded upper-case string representation of the integer     * argument in base 16.     *     * @param	i	an integer.     * @return	a padded upper-case string representation in base 16.     */    public static String toHexString(int i) {	return toHexString(i, 8);    }        /**     * Creates a padded upper-case string representation of the integer     * argument in base 16, padding to at most the specified number of digits.     *     * @param	i	an integer.     * @param	pad	the minimum number of hex digits to pad to.     * @return	a padded upper-case string representation in base 16.     */    public static String toHexString(int i, int pad) {	String result = Integer.toHexString(i).toUpperCase();	while (result.length() < pad)	    result = "0" + result;	return result;    }    /**     * Divide two non-negative integers, round the quotient up to the nearest     * integer, and return it.     *     * @param	a	the numerator.     * @param	b	the denominator.     * @return	<tt>ceiling(a / b)</tt>.     */    public static int divRoundUp(int a, int b) {	assert(a >= 0 && b > 0);	return ((a + (b-1)) / b);	    }    /**     * Load and return the named class, or return <tt>null</tt> if the class     * could not be loaded.     *     * @param	className	the name of the class to load.     * @return	the loaded class, or <tt>null</tt> if an error occurred.     */    public static Class tryLoadClass(String className) {	try {	    return ClassLoader.getSystemClassLoader().loadClass(className);	}	catch (Throwable e) {	    return null;	}    }	        /**     * Load and return the named class, terminating Nachos on any error.     *     * @param	className	the name of the class to load.     * @return	the loaded class.     */    public static Class loadClass(String className) {	try {	    return ClassLoader.getSystemClassLoader().loadClass(className);	}	catch (Throwable e) {	    Machine.terminate(e);	    return null;	}    }    /**     * Create and return a new instance of the named class, using the     * constructor that takes no arguments.     *     * @param	className	the name of the class to instantiate.     * @return	a new instance of the class.     */    public static Object constructObject(String className) {	try {	    // kamil - workaround for Java 1.4	    // Thanks to Ka-Hing Cheung for the suggestion.	    return loadClass(className).getConstructor(null).newInstance(null);	    //return loadClass(className).newInstance();	}	catch (Throwable e) {	    Machine.terminate(e);	    return null;	}    }    /**     * Verify that the specified class extends or implements the specified     * superclass.     *     * @param	className	the descendant class.     * @param	superName	the ancestor class.     */    public static void checkDerivation(Class cls, Class superCls) {	Lib.assert(superCls.isAssignableFrom(cls));    }    /**     * Verifies that the specified class is public and not abstract, and that a     * constructor with the specified signature exists and is public.     *     * @param	cls	the class containing the constructor.     * @param	parameterTypes	the list of parameters.     */    public static void checkConstructor(Class cls, Class[] parameterTypes) {	try {	    Lib.assert(Modifier.isPublic(cls.getModifiers()) &&		       !Modifier.isAbstract(cls.getModifiers()));	    Constructor constructor = cls.getConstructor(parameterTypes);	    Lib.assert(Modifier.isPublic(constructor.getModifiers()));	}	catch (Exception e) {	    Lib.assertNotReached();	}		           }    /**     * Verifies that the specified class is public, and that a non-static     * method with the specified name and signature exists, is public, and     * returns the specified type.     *     * @param	cls	the class containing the non-static method.     * @param	methodName	the name of the non-static method.     * @param	parameterTypes	the list of parameters.     * @param	returnType	the required return type.     */    public static void checkMethod(Class cls, String methodName,				   Class[] parameterTypes, Class returnType) {	try {	    Lib.assert(Modifier.isPublic(cls.getModifiers()));	    Method method = cls.getMethod(methodName, parameterTypes);	    Lib.assert(Modifier.isPublic(method.getModifiers()) &&		       !Modifier.isStatic(method.getModifiers()));	    Lib.assert(method.getReturnType() == returnType);	}	catch (Exception e) {	    Lib.assertNotReached();	}		           }        /**     * Verifies that the specified class is public, and that a static method     * with the specified name and signature exists, is public, and returns the     * specified type.     *     * @param	cls	the class containing the static method.     * @param	methodName	the name of the static method.     * @param	parameterTypes	the list of parameters.     * @param	returnType	the required return type.     */    public static void checkStaticMethod(Class cls, String methodName,					 Class[] parameterTypes,					 Class returnType) {	try {	    Lib.assert(Modifier.isPublic(cls.getModifiers()));	    Method method = cls.getMethod(methodName, parameterTypes);	    Lib.assert(Modifier.isPublic(method.getModifiers()) &&		       Modifier.isStatic(method.getModifiers()));	    Lib.assert(method.getReturnType() == returnType);	}	catch (Exception e) {	    Lib.assertNotReached();	}		           }    /**     * Verifies that the specified class is public, and that a non-static field     * with the specified name and type exists, is public, and is not final.     *     * @param	cls	the class containing the field.     * @param	fieldName	the name of the field.     * @param	fieldType	the required type.     */    public static void checkField(Class cls, String fieldName,				  Class fieldType) {	try {	    Lib.assert(Modifier.isPublic(cls.getModifiers()));	    Field field = cls.getField(fieldName);	    Lib.assert(field.getType() == fieldType);	    Lib.assert(Modifier.isPublic(field.getModifiers()) &&		       !Modifier.isStatic(field.getModifiers()) &&		       !Modifier.isFinal(field.getModifiers()));	}	catch (Exception e) {	    Lib.assertNotReached();	}    }    /**     * Verifies that the specified class is public, and that a static field     * with the specified name and type exists and is public.     *     * @param	cls	the class containing the static field.     * @param	fieldName	the name of the static field.     * @param	fieldType	the required type.     */    public static void checkStaticField(Class cls, String fieldName,					Class fieldType) {	try {	    Lib.assert(Modifier.isPublic(cls.getModifiers()));	    Field field = cls.getField(fieldName);	    Lib.assert(field.getType() == fieldType);	    Lib.assert(Modifier.isPublic(field.getModifiers()) &&		       Modifier.isStatic(field.getModifiers()));	}	catch (Exception e) {	    Lib.assertNotReached();	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -