小弟最近无事随便翻翻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
- 求助内部类可否实现两个接口的问题?
- j2ee 开源编程精要15讲 电子版 邬继承 电子工业出版社 那里能下到电子版??
- 为什么在我在编译JAVABean的时候找不到这些包啊?
- JDBC的驱动问题
- 强烈建议初学java者不要去用微软的J#!!!!建议大家封杀所有的J#的书籍!!!!!
- 不知为何错误!输入字符传入setBackground(Color.white)设定颜色。帮忙!帮忙!
- 关于java中file的大小
- 小妹又有一个很菜的问题了!!!!!!!!
- 为什么我的applet在浏览器里和appletview里运行得不一样?
- 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));