1 /* VMSystem.java -- helper for java.lang.system
2 Copyright (C) 1998, 2002, 2004 Free Software Foundation
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.lang;
39
40 import java.util.List;
41
42 import java.io.BufferedInputStream;
43 import java.io.BufferedOutputStream;
44 import java.io.FileDescriptor;
45 import java.io.FileInputStream;
46 import java.io.FileOutputStream;
47 import java.io.InputStream;
48 import java.io.PrintStream;
49
50 /**
51 * VMSystem is a package-private helper class for System that the
52 * VM must implement.
53 *
54 * @author John Keiser
55 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
56 */
57 final class VMSystem
58 {
59 /**
60 * Copy one array onto another from <code>src[srcStart]</code> ...
61 * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
62 * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
63 * neither array may be null, they must be of compatible types, and the
64 * start and length must fit within both arrays. Then the copying starts,
65 * and proceeds through increasing slots. If src and dest are the same
66 * array, this will appear to copy the data to a temporary location first.
67 * An ArrayStoreException in the middle of copying will leave earlier
68 * elements copied, but later elements unchanged.
69 *
70 * @param src the array to copy elements from
71 * @param srcStart the starting position in src
72 * @param dest the array to copy elements to
73 * @param destStart the starting position in dest
74 * @param len the number of elements to copy
75 * @throws NullPointerException if src or dest is null
76 * @throws ArrayStoreException if src or dest is not an array, if they are
77 * not compatible array types, or if an incompatible runtime type
78 * is stored in dest
79 * @throws IndexOutOfBoundsException if len is negative, or if the start or
80 * end copy position in either array is out of bounds
81 */
82 static native void arraycopy(Object src, int srcStart,
83 Object dest, int destStart, int len);
84
85 /**
86 * Get a hash code computed by the VM for the Object. This hash code will
87 * be the same as Object's hashCode() method. It is usually some
88 * convolution of the pointer to the Object internal to the VM. It
89 * follows standard hash code rules, in that it will remain the same for a
90 * given Object for the lifetime of that Object.
91 *
92 * @param o the Object to get the hash code for
93 * @return the VM-dependent hash code for this Object
94 */
95 static native int identityHashCode(Object o);
96
97 /**
98 * Convert a library name to its platform-specific variant.
99 *
100 * @param libname the library name, as used in <code>loadLibrary</code>
101 * @return the platform-specific mangling of the name
102 * @XXX Add this method
103 static native String mapLibraryName(String libname);
104 */
105
106 /**
107 * Set {@link System#in} to a new InputStream.
108 *
109 * @param in the new InputStream
110 * @see #setIn(InputStream)
111 */
112 static native void setIn(InputStream in);
113
114 /**
115 * Set {@link System#out} to a new PrintStream.
116 *
117 * @param out the new PrintStream
118 * @see #setOut(PrintStream)
119 */
120 static native void setOut(PrintStream out);
121
122 /**
123 * Set {@link System#err} to a new PrintStream.
124 *
125 * @param err the new PrintStream
126 * @see #setErr(PrintStream)
127 */
128 static native void setErr(PrintStream err);
129
130 /**
131 * Get the current time, measured in the number of milliseconds from the
132 * beginning of Jan. 1, 1970. This is gathered from the system clock, with
133 * any attendant incorrectness (it may be timezone dependent).
134 *
135 * @return the current time
136 * @see java.util.Date
137 */
138 public static native long currentTimeMillis();
139
140 /**
141 * <p>
142 * Returns the current value of a nanosecond-precise system timer.
143 * The value of the timer is an offset relative to some arbitrary fixed
144 * time, which may be in the future (making the value negative). This
145 * method is useful for timing events where nanosecond precision is
146 * required. This is achieved by calling this method before and after the
147 * event, and taking the difference betweent the two times:
148 * </p>
149 * <p>
150 * <code>long startTime = System.nanoTime();</code><br />
151 * <code>... <emph>event code</emph> ...</code><br />
152 * <code>long endTime = System.nanoTime();</code><br />
153 * <code>long duration = endTime - startTime;</code><br />
154 * </p>
155 * <p>
156 * Note that the value is only nanosecond-precise, and not accurate; there
157 * is no guarantee that the difference between two values is really a
158 * nanosecond. Also, the value is prone to overflow if the offset
159 * exceeds 2^63.
160 * </p>
161 *
162 * @return the time of a system timer in nanoseconds.
163 * @since 1.5
164 */
165 public static long nanoTime()
166 {
167 return currentTimeMillis() * 1000;
168 }
169
170 /**
171 * Returns a list of 'name=value' pairs representing the current environment
172 * variables.
173 *
174 * @return a list of 'name=value' pairs.
175 */
176 static native List environ();
177
178 /**
179 * Helper method which creates the standard input stream.
180 * VM implementors may choose to construct these streams differently.
181 * This method can also return null if the stream is created somewhere
182 * else in the VM startup sequence.
183 */
184 static InputStream makeStandardInputStream()
185 {
186 return new BufferedInputStream(new FileInputStream(FileDescriptor.in));
187 }
188
189 /**
190 * Helper method which creates the standard output stream.
191 * VM implementors may choose to construct these streams differently.
192 * This method can also return null if the stream is created somewhere
193 * else in the VM startup sequence.
194 */
195 static PrintStream makeStandardOutputStream()
196 {
197 return new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
198 }
199
200 /**
201 * Helper method which creates the standard error stream.
202 * VM implementors may choose to construct these streams differently.
203 * This method can also return null if the stream is created somewhere
204 * else in the VM startup sequence.
205 */
206 static PrintStream makeStandardErrorStream()
207 {
208 return new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
209 }
210
211 /**
212 * Gets the value of an environment variable.
213 * Always returning null is a valid (but not very useful) implementation.
214 *
215 * @param name The name of the environment variable (will not be null).
216 * @return The string value of the variable or null when the
217 * environment variable is not defined.
218 */
219 static native String getenv(String name);
220 }