请教大侠,一个关于System.in.read()的问题

CunningBoy 2005-09-16 05:15:49
小弟最近无事随便翻翻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()是在什么地方实现的。

特此问题向各位大侠请教。
望不吝赐教!
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
super_zzw 2005-09-16
  • 打赏
  • 举报
回复
setIn0是个本地方法,你可以去看看JNI方面的资料就明白了
CunningBoy 2005-09-16
  • 打赏
  • 举报
回复
To gogon:

1、是不是setIn0(new BufferedInputStream(fdIn))将System.in重定向到FileDescriptor.in的?如何确定是将System.in重定向?因为,这两句中并没有哪个变量是从System.in而来的。

2、请问setIn0()的函数实现是什么样的?

谢谢!
kulapca 2005-09-16
  • 打赏
  • 举报
回复
up
gogon 2005-09-16
  • 打赏
  • 举报
回复
Field Detail
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));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧