How to send a sequence of BITS to a file

V

victoragus

Hi to everybody,
I'm having problems to find a way to send a sequence of bits to a
file, with no using a huge quantity of bytes to do that.
What I'm actually want to do is to send 4 bits, then 5 bits, plus 7
after that, and that should take 2 bytes in the file, and not 3 as we
are used to.
If somebody is asking himself why I need it, it's to implement a
compressing-data protocol. That's why I need as few space as I can
take.
Thanks again.

Agustín Crespo. (e-mail address removed) and (e-mail address removed)
UCLM
 
G

Gordon Beaton

I'm having problems to find a way to send a sequence of bits to a
file, with no using a huge quantity of bytes to do that. What I'm
actually want to do is to send 4 bits, then 5 bits, plus 7 after
that, and that should take 2 bytes in the file, and not 3 as we are
used to.

When was it ever necessary to use 3 bytes to store 16 bits?

Store your bits in bytes, and whenever a byte is "full", write it to a
FileOutputStream in the normal manner. Write subsequent bits to a new
"empty" byte, etc.

This might help:
http://archive.devx.com/java/free/articles/gt052002/gt052002-1.asp

/gordon
 
B

Boudewijn Dijkstra

Hi to everybody,
I'm having problems to find a way to send a sequence of bits to a
file, with no using a huge quantity of bytes to do that.
What I'm actually want to do is to send 4 bits, then 5 bits, plus 7
after that, and that should take 2 bytes in the file, and not 3 as we
are used to.

You can shift the bits into an int, and then from the int into the file.
 
A

Alex Hunsley

Hi to everybody,
I'm having problems to find a way to send a sequence of bits to a
file, with no using a huge quantity of bytes to do that.
What I'm actually want to do is to send 4 bits, then 5 bits, plus 7
after that, and that should take 2 bytes in the file, and not 3 as we
are used to.
If somebody is asking himself why I need it, it's to implement a
compressing-data protocol. That's why I need as few space as I can
take.
Thanks again.

Agustín Crespo. (e-mail address removed) and (e-mail address removed)
UCLM

Not sure what already exists in this area, but the design you are
looking for is a buffer, more specifically, a bit buffer. This BitBuffer
class would allow you to write bits, any amount at a time, to it; when
the bit buffer notices it has enough bits to send a kilobyte (or
whatever value you choose) to the actual file, it does so,
transparently. Also be aware that the BitBuffer would have to allow the
caller to call a close function that flushes the remaining data in the
buffer to the file.

Another consideration is how to handle the remaining free bits of data
in the last byte if the amount of bits you write do not form a whole
number of bytes: a simple policy to address this would be to have the
file store explicitly how many bits it is storing (or perhaps just how
many bits are actual data in the last byte).

alex
 
A

Alex Hunsley

Hi to everybody,
I'm having problems to find a way to send a sequence of bits to a
file, with no using a huge quantity of bytes to do that.
What I'm actually want to do is to send 4 bits, then 5 bits, plus 7
after that, and that should take 2 bytes in the file, and not 3 as we
are used to.
If somebody is asking himself why I need it, it's to implement a
compressing-data protocol. That's why I need as few space as I can
take.
Thanks again.

Agustín Crespo. (e-mail address removed) and (e-mail address removed)
UCLM

Further to my other reply, I didn't make this clear: you can't send bits
to a file on their own; you must be sending bytes. The BitBuffer I
mentioned allows the caller to think they are writing bits at a time,
where in reality only whole bytes are getting written, when enough data
accumulates (or the BitBuffer is closed).

HTH,
alex
 
T

Thomas Weidenfeller

I'm having problems to find a way to send a sequence of bits to a
file,

Can we please start with getting the terminology right? You don't "send"
data to a file, you write it.

Second, please consider posting beginner's questions to
comp.lang.java.help in the future.
What I'm actually want to do is to send 4 bits, then 5 bits, plus 7
after that, and that should take 2 bytes in the file, and not 3 as we
are used to.

You want to have a buffer. In principle, a single byte would do as a
buffer, but for efficiency reasons consider using e.g. a byte array of
some reasonable size.

/Thomas
 
G

Greg Smith

Can we please start with getting the terminology right? You don't "send"
data to a file, you write it.
please, programmers have been sending data to abstract devices for
years. for al you know it is a message packet that gets sent. it could
be a serial tape drive on an 802.11b wireless network. in which case
it is actually "sent". keep nit picking in comp.lang.java.activism.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top