小弟最近无事随便翻翻Java的代码,发现常用的System.in.read()方法是一个abstract方法,我的疑问时,System.in.read()的实现是如何的。下面是System.java文件中对in的定义:
/**
* The "standard" input stream. This stream is already
* open and ready to supply input data. Typically this stream
* corresponds to keyboard input or another input source specified by
* the host environment or user.
*/
public final static InputStream in = nullInputStream();...... /**
* The following two methods exist because in, out, and err must be
* initialized to null. The compiler, however, cannot be permitted to
* inline access to them, since they are later set to more sensible values
* by initializeSystemClass().
*/
private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0)
return null;
throw new NullPointerException();
}...... /**
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
props = new Properties();
initProperties(props);
sun.misc.Version.init();
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)); // Load the zip library now in order to keep java.util.zip.ZipFile
// from trying to use itself to load this library later.
loadLibrary("zip"); // Currently File.deleteOnExit is built on JVM_Exit, which is a
// separate mechanism from shutdown hooks. Unfortunately in order to
// work properly JVM_Exit implicitly requires that Java signal
// handlers be set up for HUP, TERM, and INT (where available). If
// File.deleteOnExit were implemented in terms of shutdown hooks this
// call to Terminator.setup() could be removed.
Terminator.setup(); // Set the maximum amount of direct memory. This value is controlled
// by the vm option -XX:MaxDirectMemorySize=<size>. This method acts
// as an initializer only if it is called before sun.misc.VM.booted().
sun.misc.VM.maxDirectMemory(); // Subsystems that are invoked during initialization can invoke
// sun.misc.VM.isBooted() in order to avoid doing things that should
// wait until the application class loader has been set up.
sun.misc.VM.booted();
}我对initializeSystemClass()方法中做的操作看不太明白,主要是不懂in的是哪个类的实例,in.read()是在什么地方实现的。特此问题向各位大侠请教。
望不吝赐教!
/**
* The "standard" input stream. This stream is already
* open and ready to supply input data. Typically this stream
* corresponds to keyboard input or another input source specified by
* the host environment or user.
*/
public final static InputStream in = nullInputStream();...... /**
* The following two methods exist because in, out, and err must be
* initialized to null. The compiler, however, cannot be permitted to
* inline access to them, since they are later set to more sensible values
* by initializeSystemClass().
*/
private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0)
return null;
throw new NullPointerException();
}...... /**
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
props = new Properties();
initProperties(props);
sun.misc.Version.init();
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)); // Load the zip library now in order to keep java.util.zip.ZipFile
// from trying to use itself to load this library later.
loadLibrary("zip"); // Currently File.deleteOnExit is built on JVM_Exit, which is a
// separate mechanism from shutdown hooks. Unfortunately in order to
// work properly JVM_Exit implicitly requires that Java signal
// handlers be set up for HUP, TERM, and INT (where available). If
// File.deleteOnExit were implemented in terms of shutdown hooks this
// call to Terminator.setup() could be removed.
Terminator.setup(); // Set the maximum amount of direct memory. This value is controlled
// by the vm option -XX:MaxDirectMemorySize=<size>. This method acts
// as an initializer only if it is called before sun.misc.VM.booted().
sun.misc.VM.maxDirectMemory(); // Subsystems that are invoked during initialization can invoke
// sun.misc.VM.isBooted() in order to avoid doing things that should
// wait until the application class loader has been set up.
sun.misc.VM.booted();
}我对initializeSystemClass()方法中做的操作看不太明白,主要是不懂in的是哪个类的实例,in.read()是在什么地方实现的。特此问题向各位大侠请教。
望不吝赐教!
解决方案 »
- java.lang.NoClassDefFoundError: oracle/jdbc/OracleDriver
- 哪错了?为什么?20分!
- 当有很多packages和其他jar文件时候,如何制作jar文件?
- 如何让一个Calendar对象的“日”加两天或减两天。
- JInternalFrame的repaint()的问题!
- Struts里FormFile作文件上传下载时编码问题
- 求教一个问题,两个方法共享一个栈中的数据如何实现@
- 关于IO中scanner的分隔符以及输出问题
- 大家好,java好.
- byte[1024] 这个有多大
- Canvas为什么会有这些奇怪问题啊!
- 怎么判断字符是英文字符还是中文字符?谢谢
in
public static final FileDescriptor inA handle to the standard input stream. Usually, this file descriptor is not used directly, but rather via the input stream known as System.in. See Also:
System.in
这是FileDescriptor 中关于FileDescrptor.in的描述
下面是System中将System.in指向了由此产生的流。
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
setIn0(new BufferedInputStream(fdIn));