Reputation: 7
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...
here is my 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 >= 4){
if ((feld [i] == true) && (feld[i-1] == true) && (feld [i-2] == true) && (feld [i-3] == true) && (feld [i-4] == true)){
w.writeNextBit(false);
}else{
if((feld [i] == false) && (feld[i-1] == false) && (feld [i-2] == false) && (feld [i-3] == false) && (feld [i-4] == false)){
w.writeNextBit(true);
}
}
}
}
}
Upvotes: 0
Views: 488
Reputation: 43738
These statements
for(; r.hasMoreData(); length++){
}
cause an infinite loop. You do nothing with the Reader inside the loop, so the condition won't change.
Upvotes: 0