2012-12-13 15 views
2

我在查看Java bytecode instructions的列表,我注意到沒有任何I/O指令。這讓我很感興趣。當JVM不支持I/O指令時,JVM如何執行System.out.println等方法?Java虛擬機如何處理I/O操作?

如果它使用某種形式的內存映射I/O,那麼它如何與操作系統通信以讀取文件描述符等? JVM是否實現了自己的抽象層來處理I/O操作?是否在C/C++中實現了Java I/O包(java.io & java.nio)?

+0

無論是接口連接是存儲器映射,I/O(隔離)映射還是轉移到IOP都不是本級AFAIK關心的問題。 – axiom

回答

4

如果您查看庫源代碼,您會發現所有與低級API(OS等)的接口都使用本機代碼完成。

例如,採取FileOutputStream

/** 
* Opens a file, with the specified name, for writing. 
* @param name name of file to be opened 
*/ 
private native void open(String name) throws FileNotFoundException; 

/** 
* Writes the specified byte to this file output stream. Implements 
* the <code>write</code> method of <code>OutputStream</code>. 
* 
* @param  b the byte to be written. 
* @exception IOException if an I/O error occurs. 
*/ 
public native void write(int b) throws IOException; 

/** 
* Writes a sub array as a sequence of bytes. 
* @param b the data to be written 
* @param off the start offset in the data 
* @param len the number of bytes that are written 
* @exception IOException If an I/O error has occurred. 
*/ 
private native void writeBytes(byte b[], int off, int len) throws IOException; 

然後還有相應的C文件的(通常OS特異性)。

+0

對於那些不熟悉術語的人來說,「本地代碼」是指用Java以外的語言實現的代碼。 –