title: Regarding Bit stuffing for Java
body: <p>I am programming for a assignment regarding data transferring with bit stuffing, in which the an inversed bit will be added to the data stream when 5 identical bits are transferred (i.e. after 5 "0", a "1" will be added; 5 "1", a "0" will be added).
  I know how to get a bit from an original text document and how to write it into a new text document. However, for some reason my programming parts regarding the adding inversed bits go wrong, it seems to be a endless loop...</p>
  
  <p>here is my code</p>
  
  <pre><code>static void addBitStuffing(Reader r, Writer w) { //Reader is an object class read the data from an original text document, Writer write the read data to a new text document
      int length = 0;
      for(; r.hasMoreData(); length++){           
      }
      boolean [] feld = new boolean [length];
      int i = 0;
      while(r.hasMoreData()){  //hasMoreData check if there is more data in a data stream after the pointer, i.e. if there is none, it would have a value of "false"
          boolean bit = r.getNextBit();
          w.writeNextBit(bit);
          feld[i] = bit;
          i++;
  
          if (i &gt;= 4){
              if ((feld [i] == true) &amp;&amp; (feld[i-1] == true) &amp;&amp; (feld [i-2] == true) &amp;&amp; (feld [i-3] == true) &amp;&amp; (feld [i-4] == true)){
                  w.writeNextBit(false);
              }else{
                  if((feld [i] == false) &amp;&amp; (feld[i-1] == false) &amp;&amp; (feld [i-2] == false) &amp;&amp; (feld [i-3] == false) &amp;&amp; (feld [i-4] == false)){
                      w.writeNextBit(true);
                  }
              }       
          }
      }
  }
  </code></pre>