Advertising
- Anonymous
- Wednesday, May 23rd, 2012 at 4:47:25pm MDT
- package TDC.Compression.Arithmetic;
- import java.io.InputStream;
- import java.io.IOException;
- /**
- * Reads input from an underlying input stream a bit at a time. Bits are
- * returned as booleans, with
- * <code>true=1</code> and
- * <code>false=0</code>.
- * @author Inna Tuzhikova
- *
- */
- public final class BitInput {
- /**
- * Constructs a bit input from an underlying input stream.
- *
- * @param in Input stream backing this bit input.
- * @throws IOException If there is an exception reading from the specified
- * input stream.
- */
- _in = in;
- readAhead();
- }
- /**
- * Returns number of bits available for reading. Will always be
- * <code>0</code> or
- * <code>1</code>.
- *
- * @return Number of bits available for reading.
- * @throws IOException If there is an exception checking available bytes in
- * the underlying input stream.
- */
- return endOfStream() ? 0 : 1;
- }
- /**
- * Closes the underlying input stream.
- *
- * @throws IOException If there is an exception closing the underlying input
- * stream.
- */
- _in.close();
- }
- /**
- * Returns
- * <code>true</code> if all of the available bits have been read.
- *
- * @return
- * <code>true</code> if all of the available bits have been read.
- */
- public boolean endOfStream() {
- return _endOfStream;
- }
- /**
- * Reads the next bit from the input stream. Returns garbage if reading
- * while available() is false.
- *
- * @return The boolean value of the next bit,
- * <code>true</code>=1,
- * <code>false</code>=0.
- * @throws IOException If there is an exception reading a byte from the
- * underlying stream.
- */
- if (_nextBitIndex > 0) {
- // inspects bit in buffered byte
- return ((_nextByte & (1 << _nextBitIndex--)) != 0);
- }
- // on last bit in byte; buffer new byte
- boolean result = ((_nextByte & 1) != 0);
- readAhead();
- return result;
- }
- /**
- * Underlying input stream.
- */
- /**
- * Buffered byte from which bits are read.
- */
- private int _nextByte; // implied = 0;
- /**
- * Position of next bit in the buffered byte.
- */
- private int _nextBitIndex;
- /**
- * Set to true when all bits have been read.
- */
- private boolean _endOfStream = false;
- /**
- * Reads the next byte from the input stream into
- * <code>_nextByte</code>.
- *
- * @throws IOException If there is an IOException reading from the stream.
- */
- if (_endOfStream) {
- return;
- }
- _nextByte = _in.read();
- if (_nextByte == -1) {
- _endOfStream = true;
- return;
- }
- _nextBitIndex = 7;
- }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.