1 /* VMSystem.java -- helper for java.lang.system
2    Copyright (C) 1998, 2002, 2004, 2010  Free Software Foundation, Inc.
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 
VMSystem()60   private VMSystem() {} // Prohibits instantiation.
61 
62   /**
63    * Copy one array onto another from <code>src[srcStart]</code> ...
64    * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
65    * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
66    * neither array may be null, they must be of compatible types, and the
67    * start and length must fit within both arrays. Then the copying starts,
68    * and proceeds through increasing slots.  If src and dest are the same
69    * array, this will appear to copy the data to a temporary location first.
70    * An ArrayStoreException in the middle of copying will leave earlier
71    * elements copied, but later elements unchanged.
72    *
73    * @param src the array to copy elements from
74    * @param srcStart the starting position in src
75    * @param dest the array to copy elements to
76    * @param destStart the starting position in dest
77    * @param len the number of elements to copy
78    * @throws NullPointerException if src or dest is null
79    * @throws ArrayStoreException if src or dest is not an array, if they are
80    *         not compatible array types, or if an incompatible runtime type
81    *         is stored in dest
82    * @throws IndexOutOfBoundsException if len is negative, or if the start or
83    *         end copy position in either array is out of bounds
84    */
arraycopy(Object src, int srcStart, Object dest, int destStart, int len)85   static native void arraycopy(Object src, int srcStart,
86                                Object dest, int destStart, int len);
87 
88   /**
89    * Get a hash code computed by the VM for the Object. This hash code will
90    * be the same as Object's hashCode() method.  It is usually some
91    * convolution of the pointer to the Object internal to the VM.  It
92    * follows standard hash code rules, in that it will remain the same for a
93    * given Object for the lifetime of that Object.
94    *
95    * @param o the Object to get the hash code for
96    * @return the VM-dependent hash code for this Object
97    */
identityHashCode(Object o)98   static native int identityHashCode(Object o);
99 
100   /**
101    * Set {@link System#in} to a new InputStream.
102    *
103    * @param in the new InputStream
104    * @see #setIn(InputStream)
105    */
setIn(InputStream in)106   static native void setIn(InputStream in);
107 
108   /**
109    * Set {@link System#out} to a new PrintStream.
110    *
111    * @param out the new PrintStream
112    * @see #setOut(PrintStream)
113    */
setOut(PrintStream out)114   static native void setOut(PrintStream out);
115 
116   /**
117    * Set {@link System#err} to a new PrintStream.
118    *
119    * @param err the new PrintStream
120    * @see #setErr(PrintStream)
121    */
setErr(PrintStream err)122   static native void setErr(PrintStream err);
123 
124   /**
125    * Get the current time, measured in the number of milliseconds from the
126    * beginning of Jan. 1, 1970. This is gathered from the system clock, with
127    * any attendant incorrectness (it may be timezone dependent).
128    *
129    * @return the current time
130    * @see java.util.Date
131    */
currentTimeMillis()132   static native long currentTimeMillis();
133 
134   /**
135    * <p>
136    * Returns the current value of a nanosecond-precise system timer.
137    * The value of the timer is an offset relative to some arbitrary fixed
138    * time, which may be in the future (making the value negative).  This
139    * method is useful for timing events where nanosecond precision is
140    * required.  This is achieved by calling this method before and after the
141    * event, and taking the difference betweent the two times:
142    * </p>
143    * <p>
144    * <code>long startTime = System.nanoTime();</code><br />
145    * <code>... <emph>event code</emph> ...</code><br />
146    * <code>long endTime = System.nanoTime();</code><br />
147    * <code>long duration = endTime - startTime;</code><br />
148    * </p>
149    * <p>
150    * Note that the value is only nanosecond-precise, and not accurate; there
151    * is no guarantee that the difference between two values is really a
152    * nanosecond.  Also, the value is prone to overflow if the offset
153    * exceeds 2^63.
154    * </p>
155    *
156    * @return the time of a system timer in nanoseconds.
157    * @since 1.5
158    */
nanoTime()159   static native long nanoTime();
160 
161   /**
162    * Returns a list of 'name=value' pairs representing the current environment
163    * variables.
164    *
165    * @return a list of 'name=value' pairs.
166    */
environ()167   static native List environ();
168 
169   /**
170    * Helper method which creates the standard input stream.
171    * VM implementors may choose to construct these streams differently.
172    * This method can also return null if the stream is created somewhere
173    * else in the VM startup sequence.
174    */
makeStandardInputStream()175   static InputStream makeStandardInputStream()
176   {
177     return new BufferedInputStream(new FileInputStream(FileDescriptor.in));
178   }
179 
180   /**
181    * Helper method which creates the standard output stream.
182    * VM implementors may choose to construct these streams differently.
183    * This method can also return null if the stream is created somewhere
184    * else in the VM startup sequence.
185    */
makeStandardOutputStream()186   static PrintStream makeStandardOutputStream()
187   {
188     return new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
189   }
190 
191   /**
192    * Helper method which creates the standard error stream.
193    * VM implementors may choose to construct these streams differently.
194    * This method can also return null if the stream is created somewhere
195    * else in the VM startup sequence.
196    */
makeStandardErrorStream()197   static PrintStream makeStandardErrorStream()
198   {
199     return new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
200   }
201 
202   /**
203    * Gets the value of an environment variable.
204    * Always returning null is a valid (but not very useful) implementation.
205    *
206    * @param name The name of the environment variable (will not be null).
207    * @return The string value of the variable or null when the
208    *         environment variable is not defined.
209    */
getenv(String name)210   static native String getenv(String name);
211 }
212