FIleInputStream中read和Socket中read源码分析

来源:互联网 发布:c语言中的echo 编辑:程序博客网 时间:2024/06/01 08:43

FileInputStream

public int read() throws IOException{
Return Stream.readSingleByte(this);
}

public static int readSingleByte(InputStream in) throws IOException{
byte[] buffer=new byte[1];
int result=in.read(buffer,0,1);
return(result!=-1)?buffer[0]&0xff:-1;
}
返回(int)buffer[0]&0xff http://blog.csdn.net/zhaomengszu/article/details/54562056

public int read(byte[] buffer,int byteOffset,int byteCount ) throws IOException{
return IoBridge.read(fd,buffer,byteOffset,byteCount);
}

public static int read(FileDescriptor fd,bytep[],int byteOffset,int byteCount) throws IOException{
Arrays.checkOffsetAndCount(bytes.length,byteOffset,byteCount);
If(byteCount==0){
Return 0;
}
Try{
Int readCount=libcore.os.read(fd,bytes,byteOffset,byteCount);
If(readCount==0){
Return -1;
}
Return readCount;
}catch(ErrnoException errorException){
If(errnoException.errno==EAGAIN){
//we read 0 rather than throw if we try to read from an empty non-blocking pipe
return 0;
}
throw errnoException.rethrowAsIOException();
}
}
EAGAIN:打开文件时设定了O_NONBLOCK标志,并且当前没有数据可读取

public int read(FileDescriptor fd,byte[] bytes,int byteOffset,int byteCount) throws ErronException{
return read readBytes(fd,bytes,byteOffset,byteCount);
}

private native int readBytes(FileDescriptor fd,object buffer,int offset,int byteCount) throw ErrnoException;

static jint Posix_readBytes(JNIEnv* env,jobject,jobject javaFd,jobject javaBytes,jint byteOffset,jint byteCount){
ScopedBytesRW bytes(env,javaBytes);
If(bytes.get()==null){
Return -1;
}
Int fd=jniGetFDFromFileDescriptor(ent,javaFd);
return ThrowIfMinusOne(env,”read”,TEMP_FAILURE_RETRY(read(fd,bytes.get()+byteOffset,byteCoun t)));
}
ssize_t read(int fd, void *buf, size_t count);
返回值:成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read返回0
参数 count是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移。注意这个读写位置和使用C标准I/O库时的读写位置有可能不同,这个读写位置是记在内核中的,而使用C标准I/O库时的读写位置是用户空间I/O缓冲区中的位置。比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O缓冲区中,再返回第一个字节,这时该文件在内核中记录的读写位置是1024,而在FILE结构体中记录的读写位置是1。注意返回值类型是ssize_t,表示有符号的size_t,这样既可以返回正的字节数、0(表示到达文件末尾)也可以返回负值-1(表示出错)。
read函数返回时,返回值说明了buf中前多少个字节是刚读上来的。有些情况下,实际读到的字节数(返回值)会小于请求读的字节数count,例如:读常规文件时,在读到count个字节之前已到达文件末尾。例如,距文件末尾还有30个字节而请求读100个字节,则read返回30,下次read将返回0。

Socket

public InputStream getInputStream() throws IOException{
checkOpenAndCreate(false);
if(isInputShutdown()){
throw new SocketException(Socket input is shutdown);
}
Return impl.getInputStream();
}

protected abstract InputStream getInputStream() throws IOException;

protected synchronized InputStream getInputStream() throws IOException{
checkNotClosed();
return new PlainSocketInputStream(this);
}

private static class plainSocketInputStream extends InputStream{
private final plainSocketImpl socketImpl;
public plainSocketInputStream(plainSocketImpl socketImpl){
this.socketImpl=socketImpl;
}
public int available()throws IOException{
return socketImpl.available();
}
public void close() throws IOException{
socketImpl.close();
}
public int read() throws IOException{
return Streams.readSingleByte(this);
}
public int read(byte[] buffer,int byteOffset,int byteCount)throws IOException{
return socketImpl.read(buffer.byteOffset,byteCount);
}
}

private int read(byte[] buffer,int offset,int byteCount)throws IOException{
if(byteCount==0){
return 0;
}
Arrays.checkOffsetAndCount(buffer.length,offset,byteCount);
if(shutdownInput){
Return -1;
}
int readCount=IoBridge.recv.from(true,fd,buffer,offset,byteCount,0,null,false);
//Return of zero bytes for a blocking socket means a timeout occurred
if(readCount==0){
throws new SocketTimeoutException();
}
//return of -1 indicates the peer was closed
If(readCount ==-1){
shutdownInput=true;
}
Return readCount;
}
return of -1 indicates the peer was closed
http://blog.csdn.net/ns_code/article/details/14642873