System 源碼閱讀

System

  • 屬性說明
/**
 *  System 類包含了幾個有用的字段和方法,並且不能被實例化。
 *
 * @author  unascribed
 * @since   1.0
 */
public final class System {
    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /** Don't let anyone instantiate this class */
    private System() {
    }

    /**
     *  標準輸入流,默認爲鍵盤
     */
    public static final InputStream in = null;

    /**
     *  標識輸出流,默認爲控制檯
     */
    public static final PrintStream out = null;

    /**
     *  標準錯誤流,默認爲控制檯
     */
    public static final PrintStream err = null;

    /**
     *  此係統的安全管理器
     */
    private static volatile SecurityManager security;

    /**
     *  控制檯
     */
    private static volatile Console cons;

    /**
     *  系統屬性值
     *
     * The following properties are guaranteed to be defined:
     * <dl>
     * <dt>java.version         <dd>Java version number
     * <dt>java.version.date    <dd>Java version date
     * <dt>java.vendor          <dd>Java vendor specific string
     * <dt>java.vendor.url      <dd>Java vendor URL
     * <dt>java.vendor.version  <dd>Java vendor version
     * <dt>java.home            <dd>Java installation directory
     * <dt>java.class.version   <dd>Java class version number
     * <dt>java.class.path      <dd>Java classpath
     * <dt>os.name              <dd>Operating System Name
     * <dt>os.arch              <dd>Operating System Architecture
     * <dt>os.version           <dd>Operating System Version
     * <dt>file.separator       <dd>File separator ("/" on Unix)
     * <dt>path.separator       <dd>Path separator (":" on Unix)
     * <dt>line.separator       <dd>Line separator ("\n" on Unix)
     * <dt>user.name            <dd>User account name
     * <dt>user.home            <dd>User home directory
     * <dt>user.dir             <dd>User's current working directory
     * </dl>
     */
    private static Properties props;
  • 輸入輸出流及控制檯
    /**
     *  重設標準輸入流
     *
     * @since   1.1
     */
    public static void setIn(InputStream in) {
        checkIO();
        setIn0(in);
    }

    /**
     *  重設標準輸出流
     *
     * @param out   PrintStream 實例
     * @since   1.1
     */
    public static void setOut(PrintStream out) {
        checkIO();
        setOut0(out);
    }

    /**
     *  重設標準錯誤流
     *
     * @param err   PrintStream 實例
     * @since   1.1
     */
    public static void setErr(PrintStream err) {
        checkIO();
        setErr0(err);
    }

    private static native void setIn0(InputStream in);
    private static native void setOut0(PrintStream out);
    private static native void setErr0(PrintStream err);
  • 讀取系統屬性和環境變量的值
    /**
     *  讀取當前系統時序性
     *
     * <tr><th scope="row"><code>java.version</code></th>
     *     <td>Java Runtime Environment version, which may be interpreted
     *     as a {@link Runtime.Version}</td></tr>
     * <tr><th scope="row"><code>java.version.date</code></th>
     *     <td>Java Runtime Environment version date, in ISO-8601 YYYY-MM-DD
     *     format, which may be interpreted as a {@link
     *     java.time.LocalDate}</td></tr>
     * <tr><th scope="row"><code>java.vendor</code></th>
     *     <td>Java Runtime Environment vendor</td></tr>
     * <tr><th scope="row"><code>java.vendor.url</code></th>
     *     <td>Java vendor URL</td></tr>
     * <tr><th scope="row"><code>java.vendor.version</code></th>
     *     <td>Java vendor version</td></tr>
     * <tr><th scope="row"><code>java.home</code></th>
     *     <td>Java installation directory</td></tr>
     * <tr><th scope="row"><code>java.vm.specification.version</code></th>
     *     <td>Java Virtual Machine specification version which may be
     *     interpreted as a {@link Runtime.Version}</td></tr>
     * <tr><th scope="row"><code>java.vm.specification.vendor</code></th>
     *     <td>Java Virtual Machine specification vendor</td></tr>
     * <tr><th scope="row"><code>java.vm.specification.name</code></th>
     *     <td>Java Virtual Machine specification name</td></tr>
     * <tr><th scope="row"><code>java.vm.version</code></th>
     *     <td>Java Virtual Machine implementation version which may be
     *     interpreted as a {@link Runtime.Version}</td></tr>
     * <tr><th scope="row"><code>java.vm.vendor</code></th>
     *     <td>Java Virtual Machine implementation vendor</td></tr>
     * <tr><th scope="row"><code>java.vm.name</code></th>
     *     <td>Java Virtual Machine implementation name</td></tr>
     * <tr><th scope="row"><code>java.specification.version</code></th>
     *     <td>Java Runtime Environment specification version which may be
     *     interpreted as a {@link Runtime.Version}</td></tr>
     * <tr><th scope="row"><code>java.specification.vendor</code></th>
     *     <td>Java Runtime Environment specification  vendor</td></tr>
     * <tr><th scope="row"><code>java.specification.name</code></th>
     *     <td>Java Runtime Environment specification  name</td></tr>
     * <tr><th scope="row"><code>java.class.version</code></th>
     *     <td>Java class format version number</td></tr>
     * <tr><th scope="row"><code>java.class.path</code></th>
     *     <td>Java class path</td></tr>
     * <tr><th scope="row"><code>java.library.path</code></th>
     *     <td>List of paths to search when loading libraries</td></tr>
     * <tr><th scope="row"><code>java.io.tmpdir</code></th>
     *     <td>Default temp file path</td></tr>
     * <tr><th scope="row"><code>java.compiler</code></th>
     *     <td>Name of JIT compiler to use</td></tr>
     * <tr><th scope="row"><code>os.name</code></th>
     *     <td>Operating system name</td></tr>
     * <tr><th scope="row"><code>os.arch</code></th>
     *     <td>Operating system architecture</td></tr>
     * <tr><th scope="row"><code>os.version</code></th>
     *     <td>Operating system version</td></tr>
     * <tr><th scope="row"><code>file.separator</code></th>
     *     <td>File separator ("/" on UNIX)</td></tr>
     * <tr><th scope="row"><code>path.separator</code></th>
     *     <td>Path separator (":" on UNIX)</td></tr>
     * <tr><th scope="row"><code>line.separator</code></th>
     *     <td>Line separator ("\n" on UNIX)</td></tr>
     * <tr><th scope="row"><code>user.name</code></th>
     *     <td>User's account name</td></tr>
     * <tr><th scope="row"><code>user.home</code></th>
     *     <td>User's home directory</td></tr>
     * <tr><th scope="row"><code>user.dir</code></th>
     *     <td>User's current working directory</td></tr>
     * <p>
     *
     * @implNote In addition to the standard system properties, the system
     * properties may include the following keys:
     * <table class="striped">
     * <caption style="display:none">Shows property keys and associated values</caption>
     * <thead>
     * <tr><th scope="col">Key</th>
     *     <th scope="col">Description of Associated Value</th></tr>
     * </thead>
     * <tbody>
     * <tr><th scope="row">{@code jdk.module.path}</th>
     *     <td>The application module path</td></tr>
     * <tr><th scope="row">{@code jdk.module.upgrade.path}</th>
     *     <td>The upgrade module path</td></tr>
     * <tr><th scope="row">{@code jdk.module.main}</th>
     *     <td>The module name of the initial/main module</td></tr>
     * <tr><th scope="row">{@code jdk.module.main.class}</th>
     *     <td>The main class name of the initial module</td></tr>
     * </tbody>
     * </table>
     */
    public static Properties getProperties() {
        final SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertiesAccess();
        }

        return props;
    }

    /**
     *  讀取指定名稱的系統屬性
     *
     * @param      key  系統屬性的名稱
     */
    public static String getProperty(String key) {
        checkKey(key);
        final SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertyAccess(key);
        }

        return props.getProperty(key);
    }

    /**
     *  讀取指定名稱 key 的系統屬性值,如果不存在,則使用默認值
     *
     * @param      key  系統屬性的名稱
     * @param      def  默認值
     */
    public static String getProperty(String key, String def) {
        checkKey(key);
        final SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertyAccess(key);
        }

        return props.getProperty(key, def);
    }

    /**
     *  獲取當前系統的環境變量映射
     *
     * @since  1.5
     */
    public static java.util.Map<String,String> getenv() {
        final SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("getenv.*"));
        }

        return ProcessEnvironment.getenv();
    }

    /**
     *  讀取指定名稱的環境變量
     *
     * @param  name 環境變量名稱
     */
    public static String getenv(String name) {
        final SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("getenv."+name));
        }

        return ProcessEnvironment.getenv(name);
    }
  • 其他常用方法
    /**
     *  將源數組的子數組拷貝到目標數組中
     *
     * @param      src      源數組
     * @param      srcPos   源數組起始索引,包括   
     * @param      dest     目標數組
     * @param      destPos  目標數組起始索引,包括
     * @param      length   從源數組拷貝的元素個數
     */
    @HotSpotIntrinsicCandidate
    public static native void arraycopy(Object src,  int  srcPos,
            Object dest, int destPos,
            int length);

    /**
     *  返回當前 JVM 關聯的控制檯
     *
     * @since   1.6
     */
    public static Console console() {
        Console c;
        if ((c = cons) == null) {
            synchronized (System.class) {
                if ((c = cons) == null) {
                    cons = c = SharedSecrets.getJavaIOAccess().console();
                }
            }
        }
        return c;
    }

    /**
     *  1970-01-01 UTC 午夜到此刻之間的毫秒數
     */
    @HotSpotIntrinsicCandidate
    public static native long currentTimeMillis();

    /**
     *  返回當前虛擬機高分辨率的時間源,以納秒爲單位,只能用於計算時間差
     *
     * @since 1.5
     */
    @HotSpotIntrinsicCandidate
    public static native long nanoTime();

    /**
     *  主動終止當前虛擬機,非 0 的退出狀態表示異常終止。
     *
     * @param      status   退出狀態值
     */
    public static void exit(int status) {
        Runtime.getRuntime().exit(status);
    }

    /**
     *  主動運行垃圾收集器,不建議調用
     */
    public static void gc() {
        Runtime.getRuntime().gc();
    }

    /**
     *  計算指定對象的哈希值,null 對象爲 0
     *
     * @param x 待計算哈希值的目標對象
     * @since   1.1
     */
    @HotSpotIntrinsicCandidate
    public static native int identityHashCode(Object x);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章