ReadFile2ByteArrayTest

package com.ghca.policeintf.test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by DUDU on 2017/11/2.
 */
public class ReadFile2ByteArrayTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\32-bit system ported file\\1`.rar");
        byte[] buffer = new byte[1024];
        int totalReadCount = 0;
        while(true) {
            int remaining = buffer.length - totalReadCount;
//                if (remaining > 0) {
//                    int currentReadCount = fis.read(buffer,totalReadCount,remaining);
//                    if (currentReadCount > 0) {
// totalReadCount = totalReadCount + currentReadCount;
//                    } else {
//                        break;
//                    }
// } else {//Expand buffer
//                    byte[] newBuffer = new byte[buffer.length * 2];
//                    System.arraycopy(buffer,0,newBuffer,0,totalReadCount);
//                    buffer = newBuffer;
//                }
            if (remaining <= 0) {//Expand the buffer first
                byte[] newBuffer = new byte[buffer.length * 2];
                System.arraycopy(buffer,0,newBuffer,0,buffer.length);
                buffer = newBuffer;
//                remaining = buffer.length - totalReadCount;
                continue;
            }
            int currentReadCount =  fis.read(buffer,totalReadCount,remaining);
            if (currentReadCount > 0) {
                totalReadCount = totalReadCount + currentReadCount;
            } else {
                break;
            }
        }
        System.out.println("Total number of bytes read:" + totalReadCount);
        System.out.println("buffer大小:" + buffer.length);
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606274&siteId=291194637