MtasBitInputStream.java.html 5.14 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasBitInputStream.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> &gt; <a href="index.source.html" class="el_package">mtas.codec.payload</a> &gt; <span class="el_source">MtasBitInputStream.java</span></div><h1>MtasBitInputStream.java</h1><pre class="source lang-java linenums">package mtas.codec.payload;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * The Class MtasBitInputStream.
 */
public class MtasBitInputStream extends ByteArrayInputStream {

  /** The bit buffer. */
<span class="fc" id="L12">  private int bitBuffer = 0;</span>

  /** The bit count. */
<span class="fc" id="L15">  private int bitCount = 0;</span>

  /**
   * Instantiates a new mtas bit input stream.
   *
   * @param buf the buf
   */
  public MtasBitInputStream(byte[] buf) {
<span class="fc" id="L23">    super(buf);</span>
<span class="fc" id="L24">  }</span>

  /**
   * Read bit.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public int readBit() throws IOException {
<span class="fc bfc" id="L33" title="All 2 branches covered.">    if (bitCount == 0) {</span>
<span class="fc" id="L34">      bitBuffer = read();</span>
<span class="pc bpc" id="L35" title="1 of 2 branches missed.">      if (bitBuffer == -1) {</span>
<span class="nc" id="L36">        throw new IOException(&quot;no more bits&quot;);</span>
      }
    }
<span class="fc" id="L39">    int value = (bitBuffer &gt;&gt; bitCount) &amp; 1;</span>
<span class="fc" id="L40">    bitCount++;</span>
<span class="fc bfc" id="L41" title="All 2 branches covered.">    if (bitCount &gt; 7) {</span>
<span class="fc" id="L42">      bitCount = 0;</span>
    }
<span class="fc" id="L44">    return value;</span>
  }

  /**
   * Read remaining bytes.
   *
   * @return the byte[]
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public byte[] readRemainingBytes() throws IOException {
<span class="nc bnc" id="L54" title="All 2 branches missed.">    if (this.available() &gt; 0) {</span>
<span class="nc" id="L55">      byte[] b = new byte[this.available()];</span>
<span class="nc bnc" id="L56" title="All 2 branches missed.">      if(read(b)&gt;=0) {</span>
<span class="nc" id="L57">        return b;</span>
      } else {
<span class="nc" id="L59">        throw new IOException(&quot;returned negative number of remaining bytes&quot;);</span>
      }
    } else {
<span class="nc" id="L62">      throw new IOException(&quot;no more bytes&quot;);</span>
    }
  }

  /**
   * Read elias gamma coding integer.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public int readEliasGammaCodingInteger() throws IOException {
<span class="fc" id="L73">    int value = readEliasGammaCodingPositiveInteger();</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed.">    if ((value % 2) == 0) {</span>
<span class="nc" id="L75">      return (-value) / 2;</span>
    } else {
<span class="fc" id="L77">      return (value - 1) / 2;      </span>
    }
  }

  /**
   * Read elias gamma coding non negative integer.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public int readEliasGammaCodingNonNegativeInteger() throws IOException {
<span class="fc" id="L88">    int value = readEliasGammaCodingPositiveInteger();</span>
<span class="fc" id="L89">    return (value - 1);</span>
  }

  /**
   * Read elias gamma coding positive integer.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public int readEliasGammaCodingPositiveInteger() throws IOException {
    int value;
<span class="fc" id="L100">    int counter = 0;</span>
<span class="fc" id="L101">    int bit = readBit();</span>
<span class="fc bfc" id="L102" title="All 2 branches covered.">    while (bit == 0) {</span>
<span class="fc" id="L103">      counter++;</span>
<span class="fc" id="L104">      bit = readBit();</span>
    }
<span class="fc" id="L106">    value = 1;</span>
<span class="fc bfc" id="L107" title="All 2 branches covered.">    for (int i = 0; i &lt; counter; i++) {</span>
<span class="fc" id="L108">      value = (2 * value) + readBit();</span>
    }
<span class="fc" id="L110">    return value;</span>
  }
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>