深入了解LINUX下IO模式(二)——什么是面向流,什么是面向缓冲区

面向流,面向缓冲区,这是JAVA中的概念,和操作系统无关


具体怎么看呢,废话少说直接上源码


第一段是JAVA中的标准IO

java.net.SocketInputStream.read
 
int read(byte b[], int off, int length, int timeout) throws IOException {
int n;
 
 
 
// EOF already encountered
if (eof) {
return -1;
}
 
// connection reset
if (impl.isConnectionReset()) {
throw new SocketException("Connection reset");
}
 
// bounds check
if (length <= 0 || off < 0 || off + length > b.length) {
if (length == 0) {
return 0;
}
throw new ArrayIndexOutOfBoundsException();
}
 
boolean gotReset = false;
 
// acquire file descriptor and do the read
FileDescriptor fd = impl.acquireFD();
try {
n = socketRead(fd, b, off, length, timeout);//native函数
if (n > 0) {
return n;
}
} catch (ConnectionResetException rstExc) {
gotReset = true;
} finally {
impl.releaseFD();
}
 
/*
* We receive a "connection reset" but there may be bytes still
* buffered on the socket
*/
if (gotReset) {
impl.setConnectionResetPending();
impl.acquireFD();
try {
n = socketRead(fd, b, off, length, timeout);//native函数,这里从内核态中读取数据到数组b中
if (n > 0) {
return n;
}
} catch (ConnectionResetException rstExc) {
} finally {
impl.releaseFD();
}
}
 
/*
* If we get here we are at EOF, the socket has been closed,
* or the connection has been reset.
*/
if (impl.isClosedOrPending()) {
throw new SocketException("Socket closed");
}
if (impl.isConnectionResetPending()) {
impl.setConnectionReset();
}
if (impl.isConnectionReset()) {
throw new SocketException("Connection reset");
}
eof = true;
return -1;
}
  

相关推荐