コード例 #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();
  }
コード例 #2
0
  public static void test1() throws Exception {

    for (int count = 1; count <= 32; count++) {
      System.out.println("Count:" + count);
      byte[] bytes =
          new byte[] {
            (byte) 0x12,
            (byte) 0x34,
            (byte) 0x56,
            (byte) 0x78,
            (byte) 0x9a,
            (byte) 0xbc,
            (byte) 0xde,
            (byte) 0xff
          };
      int total = 8 * 8;

      ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
      BitInputStream in = new BitInputStream(bIn);

      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      BitOutputStream out = new BitOutputStream(bOut);

      int bitsRead = 0;
      while (bitsRead <= (total - count)) {
        int val = in.readBits(count);
        out.writeBits(val, count);
        // System.out.println( Integer.toHexString( in.readBits(count) ) );
        bitsRead += count;
      }

      byte[] result = bOut.toByteArray();
      for (int i = 0; i < result.length; i++)
        System.out.print("[" + Integer.toHexString(result[i] & 0xff) + "]");
      System.out.println();
    }
  }