Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Anonymous
Wednesday, May 23rd, 2012 at 4:47:25pm MDT 

  1. package TDC.Compression.Arithmetic;
  2.  
  3. import java.io.InputStream;
  4. import java.io.IOException;
  5.  
  6. /**
  7. * Reads input from an underlying input stream a bit at a time. Bits are
  8. * returned as booleans, with
  9. * <code>true=1</code> and
  10. * <code>false=0</code>.
  11. * @author Inna Tuzhikova
  12. *
  13. */
  14. public final class BitInput {
  15.  
  16.     /**
  17.      * Constructs a bit input from an underlying input stream.
  18.      *
  19.      * @param in Input stream backing this bit input.
  20.      * @throws IOException If there is an exception reading from the specified
  21.      * input stream.
  22.      */
  23.     public BitInput(InputStream in) throws IOException {
  24.         _in = in;
  25.         readAhead();
  26.     }
  27.  
  28.     /**
  29.      * Returns number of bits available for reading. Will always be
  30.      * <code>0</code> or
  31.      * <code>1</code>.
  32.      *
  33.      * @return Number of bits available for reading.
  34.      * @throws IOException If there is an exception checking available bytes in
  35.      * the underlying input stream.
  36.      */
  37.     public long available() throws IOException {
  38.         return endOfStream() ? 0 : 1;
  39.     }
  40.  
  41.     /**
  42.      * Closes the underlying input stream.
  43.      *
  44.      * @throws IOException If there is an exception closing the underlying input
  45.      * stream.
  46.      */
  47.     public void close() throws IOException {
  48.         _in.close();
  49.     }
  50.  
  51.     /**
  52.      * Returns
  53.      * <code>true</code> if all of the available bits have been read.
  54.      *
  55.      * @return
  56.      * <code>true</code> if all of the available bits have been read.
  57.      */
  58.     public boolean endOfStream() {
  59.         return _endOfStream;
  60.     }
  61.  
  62.     /**
  63.      * Reads the next bit from the input stream. Returns garbage if reading
  64.      * while available() is false.
  65.      *
  66.      * @return The boolean value of the next bit,
  67.      * <code>true</code>=1,
  68.      * <code>false</code>=0.
  69.      * @throws IOException If there is an exception reading a byte from the
  70.      * underlying stream.
  71.      */
  72.     public boolean readBit() throws IOException {
  73.         if (_nextBitIndex > 0) {
  74.             // inspects bit in buffered byte
  75.             return ((_nextByte & (1 << _nextBitIndex--)) != 0);
  76.         }
  77.         // on last bit in byte; buffer new byte
  78.         boolean result = ((_nextByte & 1) != 0);
  79.         readAhead();
  80.         return result;
  81.     }
  82.     /**
  83.      * Underlying input stream.
  84.      */
  85.     private final InputStream _in;
  86.     /**
  87.      * Buffered byte from which bits are read.
  88.      */
  89.     private int _nextByte; // implied = 0;
  90.     /**
  91.      * Position of next bit in the buffered byte.
  92.      */
  93.     private int _nextBitIndex;
  94.     /**
  95.      * Set to true when all bits have been read.
  96.      */
  97.     private boolean _endOfStream = false;
  98.  
  99.     /**
  100.      * Reads the next byte from the input stream into
  101.      * <code>_nextByte</code>.
  102.      *
  103.      * @throws IOException If there is an IOException reading from the stream.
  104.      */
  105.     private void readAhead() throws IOException {
  106.         if (_endOfStream) {
  107.             return;
  108.         }
  109.         _nextByte = _in.read();
  110.         if (_nextByte == -1) {
  111.             _endOfStream = true;
  112.             return;
  113.         }
  114.         _nextBitIndex = 7;
  115.     }
  116. }

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right