views:

286

answers:

4

I need to read file stream as bits and then I should be able to write bits to file again. Are there any classes for this purpose?

+5  A: 

As far as I know, there is no built in way to do it directly at the bit level. There are ways to read/write byte by byte using the built in streams. You could write a wrapper on a stream that looks like it reads and writes bits.

If you want something that is already written, look for open source projects that do certain kinds of audio or video encoding, since there are things like that. For instance, the FLAC codec had a BitInputStream that might meet your needs.

Uri
+3  A: 

I was pretty happy with the colt library from CERN. Can't remember if it supports File I/O but I used it to read bitstreams, analyse and modify them and write them back to files.

Andreas_D
+1 Was about to go and look for that link when I saw your answer :) The colt bit manipulation stuff is fairly comprehensive. You may want to implement a stream wrapper from scratch however, since you could get better performance by repeatedly shifting a long and returning the next bit, rather than repeatedly calling BitVector.get(n) with colt.
Mike Houston
A: 

Preon might be what you are looking for. Looking at your questions, I could imagine that Preon might be even more than what you are looking for.

Think of Preon as a library that provides to bitstream encoded content what Hibernate aims to be to relational databases, and JAXB to XML. Now, it exists of a couple of modules. One of those modules is preon-binding. That's the actual data binding framework. (Driven by annotations.)

However, preon-binding is built on top of preon-bitbuffer (in later incarnations re-dubbed to preon-io). That library has BitBuffer type of abstraction for accessing bitstream compressed data.

alt text

The Preon release last summer does not have support for encoding yet. That's work in progress.

Wilfred Springer