- 14
- 0
- 约1.04万字
- 约 16页
- 2017-02-09 发布于重庆
- 举报
FileInputStream源码分析
近做一个很简单的函数,要两个返回值,我打算有一个类封装一下返回,后来领导要求使用StringBuilder返回,不用StringBuffer,因为局部变量StringBuffer最后还是要编译为StringBuilder,因为StringBuilder没有任何同步。
问了一下大家的看法,风南说InputStream 的int read(byte[] b) 从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中,也是类似的,看来有必要研究一下IO了。
InputStream类中重要的函数,只有一个抽象的有用,
public abstract int read() throws IOException;
其他两个read的意义不大。
public int read(byte b[], int off, int len) throws IOException 和public int read(byte b[]) throws IOException {return read(b, 0, b.length);}
下面看它的子类FileInputStream的实现,
/* File Descriptor - handle to the open file */
private FileDescriptor fd;
这个很重要,这是文件句柄,C语言使用fopen函数的都知道。
/*** Opens the specified file for reading.* @param name the name of the file*/private native void open(String name) throws FileNotFoundException;/*** Reads a byte of data from this input stream. This method blocks* if no input is yet available.** @return the next byte of data, or -1 if the end of the* file is reached.* @exception IOException if an I/O error occurs.*/public native int read() throws IOException;/*** Reads a subarray 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 int readBytes(byte b[], int off, int len) throws IOException;
子类修改了父的实现
/*** Reads up to b.length bytes of data from this input* stream into an array of bytes. This method blocks until some input* is available.** @param b the buffer into which the data is read.* @return the total number of bytes read into the buffer, or* -1 if there is no more data because the end of* the file has been reached.* @exception IOException if an I/O error occurs.*/public int read(byte b[]) throws IOException {return readBytes(b, 0, b.length);}/*** Reads up to len bytes of data from this input stream* into an array of bytes. If len is not zero, the method* blocks until some input is available; otherwise, no* bytes are read a
您可能关注的文档
最近下载
- 部编版语文小学六年级下册第一单元集体备课(教材解读).pptx VIP
- 电子商务进农村培训PPT最新版PPT课件.pptx VIP
- 起重机双折线卷筒的相关参数研究与确定.pdf VIP
- 风对起飞、着陆的影响及修正.ppt VIP
- 2025-2030中国AG玻璃行业供需形势与竞争格局分析研究报告.docx
- 元好问摸鱼儿.ppt VIP
- 湖南师大附中2025届高三语文月考试卷“不可谏”和“想当初”写作指导及范文.docx VIP
- (高清版)DB42∕T 1770-2021 《建筑节能门窗工程技术标准》.pdf VIP
- 3-6岁幼儿家庭亲子阅读文章.doc VIP
- 2026年县直事业单位招聘职业能力测试题库精选.docx VIP
原创力文档

文档评论(0)