Search in sources :

Example 1 with FileCacheImageInputStream

use of javax.imageio.stream.FileCacheImageInputStream in project jdk8u_jdk by JetBrains.

the class ReadBitsTest method main.

public static void main(String[] args) throws IOException {
    // 10101001 01010101
    byte[] buffer = new byte[] { (byte) 169, (byte) 85 };
    InputStream ins = new ByteArrayInputStream(buffer);
    ImageInputStream in = new FileCacheImageInputStream(ins, null);
    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Initial bit offset != 0!");
    }
    // 1
    int bit0 = in.readBit();
    if (bit0 != 1) {
        throw new RuntimeException("First bit != 1");
    }
    if (in.getBitOffset() != 1) {
        throw new RuntimeException("Second bit offset != 1");
    }
    // 01010 = 10
    long bits1 = in.readBits(5);
    if (bits1 != 10) {
        throw new RuntimeException("Bits 1-5 != 10 (= " + bits1 + ")");
    }
    if (in.getBitOffset() != 6) {
        throw new RuntimeException("Third bit offset != 6");
    }
    // 0
    int bit1 = in.readBit();
    if (bit1 != 0) {
        throw new RuntimeException("Bit 6 != 0");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Third bit offset != 7");
    }
    // 10101010 = 170
    long bits2 = in.readBits(8);
    if (bits2 != 170) {
        throw new RuntimeException("Bits 7-14 != 170 (= " + bits2 + ")");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Fourth bit offset != 7");
    }
    // 1
    int bit2 = in.readBit();
    if (bit2 != 1) {
        throw new RuntimeException("Bit 15 != 1");
    }
    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Fifth bit offset != 0");
    }
    in.close();
}
Also used : FileCacheImageInputStream(javax.imageio.stream.FileCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileCacheImageInputStream(javax.imageio.stream.FileCacheImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) FileCacheImageInputStream(javax.imageio.stream.FileCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 FileCacheImageInputStream (javax.imageio.stream.FileCacheImageInputStream)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1