commons-io.jar里面的IOUtils.toByteArray的查看

看到程序里面用到了一个commons-io.jar里面的一个方法,就查看一下源码是怎么实现的;

        String path ="C:\\Users\\Administrator\\Pictures\\Camera Roll\\Think_Black.jpg";
        FileInputStream fileInputStream = new FileInputStream(path);
        byte[] bytes =IOUtils.toByteArray(fileInputStream);

 下面是查看的源码

public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Throwable var2 = null;

        byte[] var3;
        try {
            copy((InputStream)input, (OutputStream)output);
            var3 = output.toByteArray();
        } catch (Throwable var12) {
            var2 = var12;
            throw var12;
        } finally {
            if (output != null) {
                if (var2 != null) {
                    try {
                        output.close();
                    } catch (Throwable var11) {
                        var2.addSuppressed(var11);
                    }
                } else {
                    output.close();
                }
            }

        }

        return var3;
    }

		/**
		*Integer.MAX_VALUE :A constant holding the maximum value an int can have, 2^31-1.
		*
		*/
    public static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyLarge(input, output);
        return count > 2147483647L ? -1 : (int)count;
    }
    
    /**
    *(2147483647L+1)/4096=2的19次方,为什么是4096呢?
    *1 byte = 8 bit ,bit是0和1表示,即byte的取值范围是256,
    * 请问为什么byte[4096]标注的是4096,有什么说法?
    */
    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
        return copy(input, output, 4096);
    }
    
    public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        return copyLarge(input, output, new byte[bufferSize]);
    }
    
    public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
        long count;
        int n;
        //input结束的时候读取的是长度是-1
        for(count = 0L; -1 != (n = input.read(buffer)); count += (long)n) {
            output.write(buffer, 0, n);
        }

        return count;
    }
    
    
    /**
    *JDK inputStream里面的方法
    *Reads some number of bytes from the input stream and stores them into the buffer array b.
    */
		public int read(byte[] b)
         throws IOException
    /**
    *JDK outputStream里面的方法
    *Writes b.length bytes from the specified byte array to this output stream. 
    *The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). 
    */
		public void write(byte[] b)
           throws IOException
   /**
   *JDK ByteArrayOutputStream的方法
   *Creates a newly allocated byte array. 
   *Its size is the current size of this output stream and the valid contents of the buffer have been copied into it. 
   */  
   public byte[] toByteArray()

 

你可能感兴趣的:(commons-io.jar里面的IOUtils.toByteArray的查看)