通过确定读取量可以在循环中读取位吗?

时间:2019-05-24 22:12:02

标签: java bit datainputstream

有一个DataInputStream可读取

我遇到了一种特定的算法:

  1. int [] nmbrs = new int [64];

  2. 读取4位无符号整数。将读取的值赋给“长度”

  3. 表示(int i = 0; i <64; i ++)

    3.1以nmbrs [i]

  4. 读取一个长度位无符号整数
  5. 从第1点开始重复并将其写入nmbrs2阵列

是否可以用Java编写?怎么写?

2 个答案:

答案 0 :(得分:2)

  

是否可以用Java编写?怎么写?

Java没有提供用于以小于一个字节为单位的I / O的机制,但是您可以在面向字节的I / O之上实现它。您需要在读取时一次缓冲一个或多个字节,并跟踪该缓冲区中的位级别位置。

还请注意,这对(逻辑)位顺序问题很敏感-也就是说,您是从最高位到最低位还是以其他方式读出位?

答案 1 :(得分:1)

创建一个BitInputStream类,该类从基础DataInputStream读取位。

赞:

public final class BitInputStream implements Closeable {

    private final InputStream in;
    private final ByteOrder streamBitOrder;
    private int bits;
    private byte bitsLeft;

    public BitInputStream(InputStream in) {
        this(in, ByteOrder.BIG_ENDIAN);
    }

    public BitInputStream(InputStream in, ByteOrder bitOrder) {
        Objects.requireNonNull(in);
        Objects.requireNonNull(bitOrder);
        this.in = in;
        this.streamBitOrder = bitOrder;
    }

    @Override
    public void close() throws IOException {
        this.in.close();
    }

    public int readBit() throws IOException {
        if (this.bitsLeft == 0) {
            if ((this.bits = this.in.read()) == -1)
                throw new EOFException();
            this.bitsLeft = 8;
        }
        int bitIdx = (this.streamBitOrder == ByteOrder.BIG_ENDIAN ? this.bitsLeft - 1 : 8 - this.bitsLeft);
        this.bitsLeft--;
        return (this.bits >> bitIdx) & 1;
    }

    public int readInt() throws IOException {
        return readInt(Integer.SIZE, this.streamBitOrder);
    }

    public int readInt(ByteOrder bitOrder) throws IOException {
        return readInt(Integer.SIZE, bitOrder);
    }

    public int readInt(int len) throws IOException {
        return readInt(len, this.streamBitOrder);
    }

    public int readInt(int len, ByteOrder bitOrder) throws IOException {
        if (len == 0)
            return 0;
        if (len < 0 || len > Integer.SIZE)
            throw new IllegalArgumentException("Invalid len: " + len + " (must be 0-" + Integer.SIZE + ")");
        int value = 0;
        if (bitOrder == ByteOrder.BIG_ENDIAN) {
            for (int i = 0; i < len; i++)
                value = (value << 1) | readBit();
        } else {
            for (int i = 0; i < len; i++)
                value |= readBit() << i;
        }
        return value;
    }

}

测试

public static void main(String[] args) throws Exception {
    String bitData = "0101 00001 00001 00010 00011 00101 01000 01101 10101" // 5: 1, 1, 2, 3, 5, 8, 13, 21
                  + " 0011 000 001 010 011 100 101 110 111";                // 3: 0, 1, 2, 3, 4, 5, 6, 7
    BigInteger bi = new BigInteger(bitData.replaceAll(" ", ""), 2);
    System.out.println("0x" + bi.toString(16) + " = 0b" + bi.toString(2));
    byte[] byteData = bi.toByteArray();
    try (BitInputStream in = new BitInputStream(new ByteArrayInputStream(byteData))) {
        int[] nmbrs = readNmbrs(in);
        int[] nmbrs2 = readNmbrs(in);
        System.out.println(Arrays.toString(nmbrs));
        System.out.println(Arrays.toString(nmbrs2));
    }
}
private static int[] readNmbrs(BitInputStream in) throws IOException {
    int[] nmbrs = new int[8];
    int length = in.readInt(4);
    for (int i = 0; i < nmbrs.length; i++)
        nmbrs[i] = in.readInt(length);
    return nmbrs;
}

输出

0x5084432a1b53053977 = 0b10100001000010001000011001010100001101101010011000001010011100101110111
[1, 1, 2, 3, 5, 8, 13, 21]
[0, 1, 2, 3, 4, 5, 6, 7]