按照bit位读取Java中的IO流

private static int value;
private static int next = 7;

//读取一个bit位的值
public static int readBit(InputStream inputStream) throws IOException {
    if (next == 7){
        value = inputStream.read();
        if (value == -1){
            return -1;
        }
    }
    int result = (value &(1 << next))>>>next;  
    next--;
    if (next == -1){
        next=7;
    }
    return result;
}

  /**
* 读取size个bit。
* */

public int[] readBits(int size,InputStream inputStream) throws Exception {
    int[] result = new int[size];
    for (int i = 0; i < size; i++) {
        int v = readBit(inputStream);
        if (v == -1) {
            throw new RuntimeException();
        }
        result[i] = v;
    }
    return result;
}

猜你喜欢

转载自blog.csdn.net/fengkuagn123/article/details/88777193