InputStream中read()方法

 public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }

有一点很坑的地方就是如果一次读取位数过多,可能会读不到= =

An attempt is made to read as many as
     * <code>len</code> bytes, but a smaller number may be read.

 

posted @ 2016-07-25 09:57  孙孙孙孙孙小M  阅读(459)  评论(0编辑  收藏  举报