BufferedInputStream部分源码: public synchronized int read(byte b[], int off, int len)
throws IOException
{
getBufIfOpen(); // Check for closed stream
if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
} int n = 0;
for (;;) { //这里可以用while(true),效果一样,还是有区别的(比如效率上来说)
int nread = read1(b, off + n, len - n);
if (nread <= 0)
return (n == 0) ? nread : n;
n += nread;
if (n >= len)
return n;
// if not closed but no bytes available, return
InputStream input = in;
if (input != null && input.available() <= 0)
return n;
}
}System.arraycopy(buffer, 0, nbuf, 0, pos);//这是本志方法实现,效率是不是很好,copy的好快吗
public void close() throws IOException {
byte[] buffer;
while ( (buffer = buf) != null) {
if (bufUpdater.compareAndSet(this, buffer, null)) {
InputStream input = in;
in = null;
if (input != null)
input.close();
return;
}
// Else retry in case a new buf was CASed in fill()
}
} InputStream input = in;//这里in引用 protected volatile InputStream in;的in
而InputStrem的close()方法没有实现,在哪实现的?
throws IOException
{
getBufIfOpen(); // Check for closed stream
if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
} int n = 0;
for (;;) { //这里可以用while(true),效果一样,还是有区别的(比如效率上来说)
int nread = read1(b, off + n, len - n);
if (nread <= 0)
return (n == 0) ? nread : n;
n += nread;
if (n >= len)
return n;
// if not closed but no bytes available, return
InputStream input = in;
if (input != null && input.available() <= 0)
return n;
}
}System.arraycopy(buffer, 0, nbuf, 0, pos);//这是本志方法实现,效率是不是很好,copy的好快吗
public void close() throws IOException {
byte[] buffer;
while ( (buffer = buf) != null) {
if (bufUpdater.compareAndSet(this, buffer, null)) {
InputStream input = in;
in = null;
if (input != null)
input.close();
return;
}
// Else retry in case a new buf was CASed in fill()
}
} InputStream input = in;//这里in引用 protected volatile InputStream in;的in
而InputStrem的close()方法没有实现,在哪实现的?
打错了
System.arraycopy 效率已经算高啦,是JNI实现的代码
InputStream.close 当然是由子类实现,在BufferedInputStream中的InputStream是由外部传入的,肯定是一个InputStream的子类并已经实现了close方法
数组copy上面的arraycopy方法算快的了