Edward Eddy67716
Edward Eddy67716

Reputation: 69

Is there a way to write groups of bits less than a byte in Java

If I was to try to make a program that outputs .Flac files in Java, Flac-format, It appears that I would need to output values less than a byte. Is it even possible to output values less than a byte such as a nybble or a 5-bit number?

Upvotes: 0

Views: 94

Answers (2)

Edward Eddy67716
Edward Eddy67716

Reputation: 69

In the end I think I can do this using bit shifting and logical commands such as & and | opperators.

Upvotes: 0

VGR
VGR

Reputation: 44338

ImageOutputStream.writeBits should do what you want.

You create an ImageOutputStream using the ImageIO.createImageOutputStream method:

Path flacPath = Paths.get(filename);

try (ImageOutputStream out = ImageIO.createImageOutputStream(
    new BufferedOutputStream(Files.newOutputStream(flacPath)))) {

    out.writeBits(0b1110101011, 10);
    // etc.
}

Upvotes: 1

Related Questions