Exemplo n.º 1
0
  public static void test2() throws Exception {
    byte[] bytes =
        new byte[] {
          (byte) 0x12,
          (byte) 0x34,
          (byte) 0x56,
          (byte) 0x78,
          (byte) 0x9a,
          (byte) 0xbc,
          (byte) 0xde,
          (byte) 0xff
        };

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    BitOutputStream out = new BitOutputStream(bOut);
    for (int i = 0; i < bytes.length; i++) {
      out.writeBits(1, 1);
      out.writeBits(bytes[i], 8);
      out.writeLongBits(0x123456789abcdef0L, 64);
      out.writeLongBits(-1, 64);
      out.writeLongBits(0x80123456789abcdeL, 64);
    }
    out.close();

    byte[] toRead = bOut.toByteArray();
    System.out.println("Written length:" + toRead.length);

    ByteArrayInputStream bIn = new ByteArrayInputStream(toRead);
    BitInputStream in = new BitInputStream(bIn);
    for (int i = 0; i < bytes.length; i++) {
      int test = in.readBits(1);
      int val = in.readBits(8);
      long l1 = in.readLongBits(64);
      long l2 = in.readLongBits(64);
      long l3 = in.readLongBits(64);
      System.out.print("[" + Integer.toHexString(val) + "]");
      System.out.println(
          "("
              + Long.toHexString(l1)
              + ", "
              + Long.toHexString(l2)
              + ", "
              + Long.toHexString(l3)
              + ")");
    }
    System.out.println();
  }