Java. Lang. System

Source: Internet
Author: User
Tags file separator

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;

/**
*
* The system class provides standard input, standard output, and standard error output, and provides the ability to obtain system attributes.
* It provides the ability to load files and link libraries, as well as a local method for copying arrays quickly. All of its methods are static
*
* Comment by liqiang
*
* @ Author Arthur van hoff
*
*/
Public final class system {
Private Static native void registernatives ();
Static {
Registernatives ();
}

// Private constructor, which has no instances
Private system (){
}

// Use NULL for initialization. If currenttimemillis is not greater than 0, an exception is thrown.
// Indicates the standard input.
Public final static inputstream in = nullinputstream ();

// Use NULL for initialization. If currenttimemillis is not greater than 0, an exception is thrown.
// Indicates the standard output.
Public final static printstream out = nullprintstream ();

// Use NULL for initialization. If currenttimemillis is not greater than 0, an exception is thrown.
// Indicates the standard error output.
Public final static printstream err = nullprintstream ();

// Security Manager
Private Static securitymanager SECURITY = NULL;

/**
*
* Reset standard input
*
*/
Public static void setin (inputstream in ){
// Security check
Checkio ();
 
// Reset the standard input local method
Setin0 (in );
}

/**
*
* Reset standard output
*
*/
Public static void setout (printstream out ){
// Security check
Checkio ();
 
// Reset the local method of standard output
Setout0 (out );
}

/**
*
* Reset standard output
*
*/
Public static void seterr (printstream ERR ){
// Security check
Checkio ();
 
// Reset the local method of standard output
Seterr0 (ERR );
}

Private Static void checkio (){
// Security check
If (Security! = NULL)
Security. checkpermission (New runtimepermission ("setio "));
}

// Reset the standard input local method
Private Static native void setin0 (inputstream in );
// Reset the local method of standard output
Private Static native void setout0 (printstream out );
// Reset the local method of standard error output
Private Static native void seterr0 (printstream ERR );

/**
*
* Set security manager
*
*/
Public static
Void setsecuritymanager (final securitymanager s ){
Try {
// Can I process the "Java. Lang" package?
S. checkpackageaccess ("Java. Lang ");
} Catch (exception e ){
// Null operation
}

// Set the security manager
Setsecuritymanager0 (s );
}

Private Static synchronized
Void setsecuritymanager0 (final securitymanager s ){
If (Security! = NULL ){
// Check whether the current security manager has the ability to change the security manager.
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 );
}

/**
*
* Obtain the security manager
*
*/
Public static securitymanager getsecuritymanager (){
Return security;
}

/**
*
* Number of milliseconds from January 1, 1970 UTC to the current time
*
*/
Public static native long currenttimemillis ();

/**
*
* Copy the elements of the source array to the target array.
*
* @ Param SRC Source Array
* @ Param srcpos start position of source array copy
* @ Param DEST target array
* @ Param destpos start position of the target array
* @ Param length: Number of copies
*
*/
Public static native void arraycopy (Object SRC, int srcpos,
Object DEST, int destpos,
Int length );

/**
*
* Returns the hashcode value of object X, regardless of whether the actual class of object X has overwritten the hashcode method.
* Return the return value of the hashcode method of the original object. If the object is null, return 0.
*
*/
Public static native int identityhashcode (Object X );

/**
*
* System attribute. the following objects must be set.
*
* The following attributes should be set
* Java. Version: Java version, for example, 1.4.2 _ 05
* Java. Vendor: vendor of Java, such as Sun Microsystems Inc.
* Java. Vendor. url: Java vendor URL for example: http://java.sun.com/
* Java. Home: the location of the Java Runtime Environment, for example, C:/j2sdk1.4.2 _ 05/JRE.
* Java. Class. Version: the version of the Java class file, for example, 48.0.
* Java. Class. Path: The classpath environment variable set by the system.
* OS. Name: Operating System name, for example, Windows 2000
* OS. Arch: operating system architecture, for example, x86
* OS. Version: operating system version, for example, 5.0
* File. separator: file separator example :/
* Path. separator: path separator example :;
* Line. separator: line break for example: "/N"
* User. Name: user name, for example, Administrator
* User. Home: the user's home path, for example, C:/Documents and Settings/Administrator
* User. dir: current working path, for example, D:/Eclipse/workspace/test
*/
Private Static Properties props;

// Use a properties to initialize the system variable
Private Static native properties initproperties (properties props );

/**
*
* Obtain all system attributes.
*
*/
Public static properties getproperties (){
// Security check
If (Security! = NULL ){
Security. checkpropertiesaccess ();
}
 
Return props;
}

/**
*
* Set System Properties
*
*/
Public static void setproperties (properties props ){
// Security check
If (Security! = NULL ){
Security. checkpropertiesaccess ();
}
 
If (props = NULL ){
// If props is empty, initialize it through null Properties
Props = new properties ();
Initproperties (props );
}


System. Props = props;
}

/**
*
* Obtain the corresponding attributes using the key.
*
*/
Public static string getproperty (string key ){
// If the key is null or a Null String, an exception is thrown.
If (Key = NULL ){
Throw new nullpointerexception ("key can't be null ");
}
If (key. Equals ("")){
Throw new illegalargumentexception ("key can't be empty ");
}
 
// Security check
If (Security! = NULL ){
Security. checkpropertyaccess (key );
}
Return props. getproperty (key );
}

/**
*
* Get the attribute value through the key
*
* @ Param key: key value of the property
* @ Param def default value of the attribute
*
*/
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 );
}

/**
*
* Set the value of the specified attribute
*
*/
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 );
}

/**
*
* Log out of the VM.
*
* @ Param status code, indicating 0
* @ Throws securityexception
*
*/
Public static void exit (INT status ){
// Actually call the corresponding method of Runtime
Runtime. getruntime (). Exit (Status );
}

/**
*
* Manual garbage collection
*
*/
Public static void GC (){
// Actually call the corresponding method of Runtime
Runtime. getruntime (). GC ();
}

/**
*
* Call the Finalize method of all invalid objects
*
*/
Public static void runfinalization (){
// Actually call the corresponding method of Runtime
Runtime. getruntime (). runfinalization ();
}

/**
* @ Deprecated
*/
Public static void runfinalizersonexit (Boolean value ){
// Actually call the corresponding method of 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 ){
// The actual call is the runtime method.
Runtime. getruntime (). load0 (getcallerclass (), filename );
}

/**
*
* Load the dynamic link library
*
*/
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 {
// Normal return null
If (currenttimemillis ()> 0)
Return NULL;
 
// Currenttimemillis not greater than 0 throw an exception
Throw new nullpointerexception ();
}

Private Static printstream nullprintstream () throws nullpointerexception {
// Normal return null
If (currenttimemillis ()> 0)
Return NULL;
 
// Currenttimemillis not greater than 0 throw an exception
Throw new nullpointerexception ();
}

/**
* Initialize the system class. Called after thread initialization.
*
* Initialize the system class, called after the thread Initialization
*/
Private Static void initializesystemclass (){
// Create an empty properties and assign it to the current property object
Props = new properties ();
// Use the default initialization system attribute
Initproperties (props );
 
Sun. Misc. version. INIT ();
 
// Initialize standard input, standard output, and standard error
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 ();

// Set the maximum memory size
Sun. Misc. VM. maxdirectmemory ();

Sun. Misc. VM. Booted ();
}

// Obtain the caller's Class Object
Static class getcallerclass (){
// Note use of more generic reflection. getcallerclass ()
Return reflection. getcallerclass (3 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.