comons - io. jar 에 있 는 IOUtils. toByteArray 보기

프로그램 에 comons - 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()

좋은 웹페이지 즐겨찾기