java.lang.System

package java.lang;

import java.io.*;
import java.util.Properties;
import java.util.PropertyPermission;
import java.util.StringTokenizer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.AllPermission;
import sun.net.InetAddressCachePolicy;
import sun.reflect.Reflection;
import sun.security.util.SecurityConstants;

/**
 *
 * System类中提供了标准输入,标准输出,标准错误输出,提供了获取系统属性的能力
 * 提供了装载文件和链接库的能力,还提供了快速拷贝数组的本地方法,他的所有方法都为静态的
 *
 * comment by liqiang
 *
 * @author  Arthur van Hoff
 *
 */
public final class System {
    private static native void registerNatives();
    static {
        registerNatives();
    }

    //私有的构造函数,此类没有实例
    private System() {
    }

    //用null做初始化,如果currentTimeMillis不大于0抛出异常
    //表示标准输入
    public final static InputStream in = nullInputStream();

    //用null做初始化,如果currentTimeMillis不大于0抛出异常
    //表示标准输出
    public final static PrintStream out = nullPrintStream();

    //用null做初始化,如果currentTimeMillis不大于0抛出异常
    //表示标准错误输出
    public final static PrintStream err = nullPrintStream();

    //安全管理器
    private static SecurityManager security = null;

    /**
     *
     * 重置标准输入
     *
     */
    public static void setIn(InputStream in) {
    //安全检查
 checkIO();
 
 //重置标准输入的本地方法
 setIn0(in);
    }

    /**
     *
     * 重置标准输出
     *
     */
    public static void setOut(PrintStream out) {
    //安全检查
 checkIO();
 
 //重置标准输出的本地方法
 setOut0(out);
    }

    /**
     *
     * 重置标准输出
     *
     */
    public static void setErr(PrintStream err) {
    //安全检查
 checkIO();
 
 //重置标准输出的本地方法
 setErr0(err);
    }

    private static void checkIO() {
     //安全检查
        if (security != null)
     security.checkPermission(new RuntimePermission("setIO"));
    }

    //重置标准输入的本地方法
    private static native void setIn0(InputStream in);
    //重置标准输出的本地方法
    private static native void setOut0(PrintStream out);
    //重置标准错误输出的本地方法
    private static native void setErr0(PrintStream err);

    /**
     *
     * 设置安全管理器
     *
     */
    public static
    void setSecurityManager(final SecurityManager s) {
        try {
         //是否可以处理"java.lang"包
            s.checkPackageAccess("java.lang");
        } catch (Exception e) {
            //空操作
        }
       
        //设置安全管理器
        setSecurityManager0(s);
    }

    private static synchronized
    void setSecurityManager0(final SecurityManager s) {
 if (security != null) {
  //询问当前的安全管理器,是否有改变安全管理器的能力
      security.checkPermission(new RuntimePermission
         ("setSecurityManager"));
 }

 if ((s != null) && (s.getClass().getClassLoader() != null)) {
     AccessController.doPrivileged(new PrivilegedAction() {
  public Object run() {
      s.getClass().getProtectionDomain().implies
   (SecurityConstants.ALL_PERMISSION);
      return null;
  }
     });
 }

 security = s;
 InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.FOREVER);
    }

    /**
     *
     * 获得安全管理器
     *
     */
    public static SecurityManager getSecurityManager() {
 return security;
    }

    /**
     *
     * 从January 1, 1970 UTC开始到现在时刻的毫秒数
     *
     */
    public static native long currentTimeMillis();

    /**
     *
     * 将源数组种的元素拷贝到目标数组中
     *
     * @param      src      源数组
     * @param      srcPos   源数组拷贝的起始位置
     * @param      dest     目标数组
     * @param      destPos  目标数组的起始位置
     * @param      length   拷贝的个数
     *
     */
    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

    /**
     *
     * 返回对象x的hashCode值,无论x对应的实际类是否重写了hashCode方法
     * 都返回原始的Object的hashCode方法的返回值,如果对象为null则返回0
     *
     */
    public static native int identityHashCode(Object x);

    /**
     *
     * 系统属性,下列对象必须被设置
     *
     * 下列属性应保证被设置
     * java.version: Java的版本号 例如: 1.4.2_05
     * java.vendor: Java的卖主 例如: Sun Microsystems Inc.
     * java.vendor.url: Java卖主的URL 例如: http://java.sun.com/
     * java.home: Java运行环境的位置 例如:C:/j2sdk1.4.2_05/jre
     * java.class.version: Java Class文件的版本 例如: 48.0
     * java.class.path: 系统设置的ClassPath环境变量
     * os.name: 操作系统名 例如: Windows 2000
     * os.arch: 操作系统架构 例如: x86
     * os.version: 操作系统版本 例如: 5.0
     * file.separator: 文件分隔符 例如: /
     * path.separator: 路径分隔符 例如: ;
     * line.separator: 换行符 例如: "/n"
     * user.name: 用户名 例如: Administrator
     * user.home: 用户的home路径 例如: C:/Documents and Settings/Administrator
     * user.dir: 当前的工作路径 例如: D:/eclipse/workspace/test
     */
    private static Properties props;
   
    //使用一个Properties初始化系统变量
    private static native Properties initProperties(Properties props);

    /**
     *
     * 获取系统的全部属性
     *
     */
    public static Properties getProperties() {
    //安全检查
 if (security != null) {
     security.checkPropertiesAccess();
 }
 
 return props;
    }

    /**
     *
     * 设置系统属性
     *
     */
    public static void setProperties(Properties props) {
    //安全检查
 if (security != null) {
     security.checkPropertiesAccess();
 }
 
        if (props == null) {
         //如果props为空设置通过空的的Properties初始化它
            props = new Properties();
            initProperties(props);
        }
       
       
 System.props = props;
    }

    /**
     *
     * 通过key获得对应的属性
     *
     */
    public static String getProperty(String key) {
    //key为null或空字符串抛出异常 
 if (key == null) {
     throw new NullPointerException("key can't be null");
 }
 if (key.equals("")) {
     throw new IllegalArgumentException("key can't be empty");
 }
 
 //安全检查
 if (security != null) {
     security.checkPropertyAccess(key);
 }
 return props.getProperty(key);
    }

    /**
     *
     * 通过key获得属性值
     *
     * @param      key   属性的键值
     * @param      def   属性的默认值
     *
     */
    public static String getProperty(String key, String def) {
 if (key == null) {
     throw new NullPointerException("key can't be null");
 }
 if (key.equals("")) {
     throw new IllegalArgumentException("key can't be empty");
 }
 
 if (security != null) {
     security.checkPropertyAccess(key);
 }
 
 return props.getProperty(key, def);
    }

    /**
     *
     * 设置指定属性的值
     *
     */
    public static String setProperty(String key, String value) {
 if (key == null) {
     throw new NullPointerException("key can't be null");
 }
 if (key.equals("")) {
     throw new IllegalArgumentException("key can't be empty");
 }
 if (security != null)
     security.checkPermission(new PropertyPermission(key,
  SecurityConstants.PROPERTY_WRITE_ACTION));
 return (String) props.setProperty(key, value);
    }

    /**
     *
     * @deprecated
     *
     */
    public static String getenv(String name) {
 throw new Error("getenv no longer supported, use properties and -D instead: " + name);
    }

    /**
     *
     * 退出虚拟机
     *
     * @param status 状态码,约定0表示
     * @throws  SecurityException
     *
     */
    public static void exit(int status) {
    //实际调用的是Runtime的相应方法
 Runtime.getRuntime().exit(status);
    }

    /**
     *
     * 手动进行垃圾回收
     *
     */
    public static void gc() {
    //实际调用的是Runtime的相应方法
 Runtime.getRuntime().gc();
    }

    /**
     *
     * 调用所有失效对象的finalize方法
     *
     */
    public static void runFinalization() {
    //实际调用的是Runtime的相应方法
 Runtime.getRuntime().runFinalization();
    }

    /**
     * @deprecated
     */
    public static void runFinalizersOnExit(boolean value) {
    //实际调用的是Runtime的相应方法  
 Runtime.getRuntime().runFinalizersOnExit(value);
    }

    /**
     *
     * Loads a code file with the specified filename from the local file
     * system as a dynamic library. The filename
     * argument must be a complete path name.
     *
     */
    public static void load(String filename) {
    //实际调用的是Runtime的方法 
 Runtime.getRuntime().load0(getCallerClass(), filename);
    }

    /**
     *
     * 加载动态链接库
     *
     */
    public static void loadLibrary(String libname) {
 Runtime.getRuntime().loadLibrary0(getCallerClass(), libname);
    }

    /**
     * Maps a library name into a platform-specific string representing
     * a native library.
     */
    public static native String mapLibraryName(String libname);

    private static InputStream nullInputStream() throws NullPointerException {
    //正常返回null
 if (currentTimeMillis() > 0)
     return null;
 
 //currentTimeMillis不大于0抛出异常
 throw new NullPointerException();
    }

    private static PrintStream nullPrintStream() throws NullPointerException {
    //正常返回null
 if (currentTimeMillis() > 0)
     return null;
 
 //currentTimeMillis不大于0抛出异常
 throw new NullPointerException();
    }

    /**
     * Initialize the system class.  Called after thread initialization.
     *
     * 初始化System类,在线程初始化后调用
     */
    private static void initializeSystemClass() {
    //创建一个空的Properties,并赋给当前属性对象
 props = new Properties();
 //用默认初始化System属性
 initProperties(props);
 
 sun.misc.Version.init();
 
 //初始化标准输入,标准输出,标准错误
 FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
 FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
 FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
 
 setIn0(new BufferedInputStream(fdIn));
 setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
 setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));

 loadLibrary("zip");

        Terminator.setup();
       
    //设置最大内存数
  sun.misc.VM.maxDirectMemory();

 sun.misc.VM.booted();
    }

    //获取调用者的Class对象
    static Class getCallerClass() {
        // NOTE use of more generic Reflection.getCallerClass()
        return Reflection.getCallerClass(3);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值