Java IO 文件拷贝功能的实现(3)

源码已经很清楚的写出来了,”@param b the buffer into which the data is read.”,参数是要读取到内存中的字节数组,如果再跟下去,我们发现,其实这个方法是调用了 read( byte b[] , int off , int len )

这里写图片描述


其实最后,还是使用的 read() 方法,我们只不过是调用了已经封装好的一些方法。

对于 write( byte b[], int off, int len )方法,源码如下:

/**
    * Writes <code>len</code> bytes from the specified byte array
    * starting at offset <code>off</code> to this output stream.
    * The general contract for <code>write(b, off, len)</code> is that
    * some of the bytes in the array <code>b</code> are written to the
    * output stream in order; element <code>b[off]</code> is the first
    * byte written and <code>b[off+len-1]</code> is the last byte written
    * by this operation.
    * <p>
    * The <code>write</code> method of <code>OutputStream</code> calls
    * the write method of one argument on each of the bytes to be
    * written out. Subclasses are encouraged to override this method and
    * provide a more efficient implementation.
    * <p>
    * If <code>b</code> is <code>null</code>, a
    * <code>NullPointerException</code> is thrown.
    * <p>
    * If <code>off</code> is negative, or <code>len</code> is negative, or
    * <code>off+len</code> is greater than the length of the array
    * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
    *
    * @param      b    the data.
    * @param      off  the start offset in the data.
    * @param      len  the number of bytes to write.
    * @exception  IOException  if an I/O error occurs. In particular,
    *            an <code>IOException</code> is thrown if the output
    *            stream is closed.
    */
    public void write(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
                  ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        for (int i = 0 ; i < len ; i++) {
            write(b[off + i]);
        }
    }

同样的,这个方法也是对 write( int b ) 方法的封装。

另外我加了可以测试耗费时间的一个小功能,原理很简单,就是使用 currentTimeMillis() 方法,源码如下:

/**
    * Returns the current time in milliseconds.  Note that
    * while the unit of time of the return value is a millisecond,
    * the granularity of the value depends on the underlying
    * operating system and may be larger.  For example, many
    * operating systems measure time in units of tens of
    * milliseconds.
    *
    * <p> See the description of the class <code>Date</code> for
    * a discussion of slight discrepancies that may arise between
    * "computer time" and coordinated universal time (UTC).
    *
    * @return  the difference, measured in milliseconds, between
    *          the current time and midnight, January 1, 1970 UTC.
    * @see    java.util.Date
    */
    public static native long currentTimeMillis();

可以看到,这个方法的返回值是当前时间到1970年1月1日的间隔,单位是毫秒。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/08c8c9bc49a7c38c0d1602da8ec8d0a6.html