Trail: Essential Java Classes
|
Lesson: Accessing System Resources
|
|
Miscellaneous System Methods
The System class provides several methods that provide miscellaneous
functionality including copying arrays and getting the current time.
Copying Arrays
Use System's arraycopy method to efficiently copy
data from one array into another.
The arraycopy method requires five arguments:
public static
void arraycopy(Object source,
int srcIndex,
Object dest,
int destIndex,
int length,
The two Object arguments indicate the array to copy from and the
array to copy to. The three integer arguments indicate the starting
location in each the source and the destination array, and the number
of elements to copy. This diagram illustrates how the copy takes place:
The following program uses arraycopy to copy some elements
from the copyFrom array to the copyTo array.
public class ArrayCopyTest {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
The arraycopy method call in this example program begins
the copy at element number 2 in the source array. Recall that array
indices start at 0, so that the copy begins at the array element 'c'.
The arraycopy method call puts the copied elements into
the destination array beginning at the first element (element 0) in the
destination array copyTo. The copy copies 7 elements:
'c', 'a', 'f', 'f', 'e', 'i', and 'n'. Effectively, the arraycopy method
takes the "caffein" out of "decaffeinated", like this:
Note that the destination array must be allocated before you call
arraycopy and must be large enough to contain the data
being copied.
Getting the Current Time
The currentTimeMillis method returns
the current time in milliseconds since 00:00:00, January 1, 1970.
The currentTimeMillis method is commonly used during
performance tests: get the current time, perform the operation that
you want to time, get the current time again--the
difference in the two time samples is roughly the amount
of time that the operation took to perform.
Often in graphical user interfaces the time difference between mouse
clicks is used to determine whether a user double clicked.
The following applet uses currentTimeMillis to compute
the number of milliseconds between two mouse clicks. If the time period
between the clicks is smaller than 200 milliseconds, the two mouse clicks
are interpreted as a double mouse click.
Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go to example-1dot1/TimingIsEverything.html. For more information about running applets, refer to About Our Examples.
Here's the source
for the TimingIsEverything applet:
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TimingIsEverything extends java.applet.Applet {
public long firstClickTime = 0;
public String displayStr;
public void init() {
displayStr = "Double Click Me";
addMouseListener(new MyAdapter());
}
public void paint(Graphics g) {
g.drawRect(0, 0, getSize().width-1, getSize().height-1);
g.drawString(displayStr, 40, 30);
}
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
long clickTime = System.currentTimeMillis();
long clickInterval = clickTime - firstClickTime;
if (clickInterval < 200) {
displayStr = "Double Click!! (Interval = " + clickInterval + ")";
firstClickTime = 0;
} else {
displayStr = "Single Click!!";
firstClickTime = clickTime;
}
repaint();
}
}
}
You could use the return value from this method to compute
the current date and time. However, you'll probably find that it's
more convenient to get the current date and time from the
Date class.
Note:
Previous versions of the System class support two other time-related methods
besides the currentTimeMillis method: currentTime
and nowMillis. These two methods are now obsolete--you should
use currentTimeMillis instead.
Exiting the Runtime Environment
To exit the Java interpreter, call the System.exit method.
The interpreter exits with the integer exit code
that you specify to the
exit method.
System.exit(-1);
Note: The exit method causes the Java
interpreter to exit, not just your Java program. Use this
function with caution.
Security consideration: Invocation of the exit method is
subject to security restrictions. Depending on the browser that an
applet is running in, a call to exit from within an applet
will likely result in a SecurityException.
See
Security Restrictions for information about the security restrictions placed on applets.
Setting and Getting the Security Manager
The security manager is an object that enforces a certain security
policy for a Java application. You can set the current security manager
for your applications using System's setSecurityManager
method, and you can retrieve the current security manager using System's
getSecurityManager method. An application's security manager is a
java.lang.SecurityManager object.
Providing Your Own Security Manager
discusses security managers in detail and shows you how to create
and install your own security manager using these two System methods.
Security consideration: The security manager for an application can be
set only once. Typically, a browser sets its security manager during its
startup procedure. Thus, most of the time, applets cannot set the security
manager because it's already been set. A SecurityException will result if
your applet attempts to do so. See
Security Restrictions for information about the security restrictions placed on applets.
|