Changeset 2043


Ignore:
Timestamp:
01/06/07 16:19:54 (13 years ago)
Author:
nor
Message:

added skipBits(long bits) method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/BitBuffer.java

    r2039 r2043  
    5757    currentBit = 0; 
    5858    eofByte = byteBuffer.length; 
     59  } 
     60 
     61 
     62  public void skipBits(long bits) { 
     63    // TODO: Consider whether negative bits should be allowed as a "rewind" 
     64    if(bits < 0) { 
     65      throw new IllegalArgumentException("Bits to skip may not be negative"); 
     66    } 
     67    long skipBytes = bits / 8; 
     68    int skipBits = (int) bits % 8; 
     69    // If we would overflow the currentByte value, well, we're larger than 
     70    // the array anyway, so... 
     71    if(skipBytes + currentByte > Integer.MAX_VALUE) { 
     72      eofFlag = true; 
     73    } 
     74    currentByte = (int) (skipBytes + currentByte); 
     75    currentBit += skipBits; 
     76    if(currentBit > 8) { 
     77      currentByte++; 
     78      currentBit -= 8; 
     79    } 
     80    if(currentByte >= eofByte) { 
     81      eofFlag = true; 
     82    } 
    5983  } 
    6084 
     
    133157    int[] len = new int[trials]; 
    134158    BitWriter bw = new BitWriter(); 
     159    int totallen = 0; 
    135160 
    136161    Random r = new Random(); 
     
    152177      // How many bits are required to represent this number? 
    153178      len[i] = (Integer.toBinaryString(nums[i])).length(); 
     179      totallen += len[i]; 
    154180      bw.write(nums[i], len[i]); 
    155181    } 
     
    157183    int readint; 
    158184    System.out.println("Reading from BitBuffer"); 
     185    // Randomly skip or read bytes 
    159186    for(int i = 0; i < trials; i++) { 
    160       readint = bb.getBits(len[i]); 
    161       if(readint != nums[i]) { 
    162         System.out.println("Error at #" + i + ": " + readint + " received, " + 
    163                            nums[i] + " expected."); 
    164       } 
     187      int c = r.nextInt(100); 
     188      if(c > 50) { 
     189        readint = bb.getBits(len[i]); 
     190        if(readint != nums[i]) { 
     191          System.out.println("Error at #" + i + ": " + readint + " received, " + 
     192                             nums[i] + " expected."); 
     193        } 
     194      } 
     195      else { 
     196        bb.skipBits(len[i]); 
     197      } 
     198    } 
     199    // Test reading past end of buffer. 
     200    System.out.println("Testing end of buffer"); 
     201    bb = new BitBuffer(bw.toByteArray()); 
     202    // The total length could be mid byte. Add one byte to test. 
     203    bb.skipBits(totallen + 8); 
     204    int read = bb.getBits(1); 
     205    if(-1 != read) { 
     206      System.out.println("-1 expected at end of buffer, " + 
     207                         read + " received."); 
    165208    } 
    166209  } 
Note: See TracChangeset for help on using the changeset viewer.