Changeset 2028


Ignore:
Timestamp:
01/04/07 21:55:46 (13 years ago)
Author:
nor
Message:

Added comments, test method

File:
1 edited

Legend:

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

    r1512 r2028  
    2424 
    2525package loci.formats; 
     26// Used in main method test. 
     27import loci.formats.BitWriter; 
     28import java.util.Random; 
    2629 
    2730/** 
     
    3134public class BitBuffer { 
    3235 
     36  // Various bitmasks for the 0000xxxx side of a byte 
    3337  private static final int[] BACK_MASK = { 
    34     0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F 
     38    0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F 
    3539  }; 
    3640 
     41  // Various bitmasks for the xxxx0000 side of a byte 
    3742  private static final int[] FRONT_MASK = { 
    3843    0x0000, 0x0080, 0x00C0, 0x00E0, 0x00F0, 0x00F8, 0x00FC, 0x00FE 
     
    4550  private boolean eofFlag; 
    4651 
     52  /** 
     53   *  Default constructor. 
     54   */ 
    4755  public BitBuffer(byte[] byteBuffer) { 
    4856    this.byteBuffer = byteBuffer; 
     
    5260  } 
    5361 
     62  /** 
     63   * Returns an int value representing the value of the bits read from 
     64   * the byte array, from the current position. Bits are extracted from the 
     65   * "left side" or high side of the byte.<p> 
     66   * The current position is modified by this call.<p> 
     67   * Bits are pushed into the int from the right, endianness is not 
     68   * considered by the method on its own. So, if 5 bits were read from the 
     69   * buffer "10101", the int would be the integer representation of 
     70   * 000...0010101 on the target machine. <p> 
     71   * In general, this also means the result will be positive unless a full 
     72   * 32 bits are read. <p> 
     73   * Requesting more than 32 bits is allowed, but only up to 32 bits worth of 
     74   * data will be returned (the last 32 bits read). <p> 
     75   * 
     76   * @param bitsToRead the number of bits to read from the bit buffer 
     77   * @return the value of the bits read 
     78   */ 
    5479  public int getBits(int bitsToRead) { 
    5580    if (bitsToRead == 0) return 0; 
     
    5782    int toStore = 0; 
    5883    while (bitsToRead != 0 && !eofFlag) { 
     84      // if we need to read from more than the current byte in the buffer... 
    5985      if (bitsToRead >= 8 - currentBit) { 
    60         if (currentBit == 0) { // special 
     86        if (currentBit == 0) { 
     87          // we can read in a whole byte, so we'll do that. 
    6188          toStore = toStore << 8; 
    6289          int cb = ((int) byteBuffer[currentByte]); 
     
    6491          bitsToRead -= 8; 
    6592          currentByte++; 
    66         } 
    67         else { 
     93        } else { 
     94          // otherwise, only read the appropriate number of bits off the back 
     95          // side of the byte, in order to "finish" the current byte in the 
     96          // buffer. 
    6897          toStore = toStore << (8 - currentBit); 
    6998          toStore += ((int) 
     
    73102          currentByte++; 
    74103        } 
    75       } 
    76       else { 
     104      } else { 
     105        // We will be able to finish using the current byte. 
     106        // read the appropriate number of bits off the front side of the byte, 
     107        // then push them into the int. 
    77108        toStore = toStore << bitsToRead; 
    78109        int cb = ((int) byteBuffer[currentByte]); 
     
    83114        bitsToRead = 0; 
    84115      } 
     116      // If we reach the end of the buffer, return what we currently have. 
    85117      if (currentByte == eofByte) { 
    86118        eofFlag = true; 
     
    90122    return toStore; 
    91123  } 
    92  
     124   
     125  /** 
     126   * Testing method 
     127   * @param args Ignored. 
     128   */ 
     129   
     130  public static void main(String args[]) { 
     131    int trials = 50000; 
     132    int[] nums = new int[trials]; 
     133    int[] len = new int[trials]; 
     134    BitWriter bw = new BitWriter(); 
     135     
     136    Random r = new Random(); 
     137    System.out.println("Generating " + trials + " trials."); 
     138    System.out.println("Writing to byte array"); 
     139    // we want the trials to be able to be all possible bit lengths. 
     140    // r.nextInt() by itself is not sufficient... in 50000 trials it would be 
     141    // extremely unlikely to produce bit strings of 1 bit. 
     142    // instead, we randomly choose from 0 to 2^(i % 32). 
     143    // Except, 1 << 31 is a negative number in two's complement, so we make it 
     144    // a random number in the entire range. 
     145    for(int i = 0; i < trials; i++) { 
     146      if(31 == i % 32) { 
     147        nums[i] = r.nextInt(); 
     148      } else { 
     149        nums[i] = r.nextInt(1 << (i % 32)); 
     150      } 
     151      // How many bits are required to represent this number? 
     152      len[i] = (Integer.toBinaryString(nums[i])).length(); 
     153      bw.write(nums[i], len[i]); 
     154    } 
     155    BitBuffer bb = new BitBuffer(bw.toByteArray()); 
     156    int readint; 
     157    System.out.println("Reading from BitBuffer"); 
     158    for(int i = 0; i < trials; i++) { 
     159      readint = bb.getBits(len[i]); 
     160      if(readint != nums[i]) { 
     161        System.out.println("Error at #" + i + ": " + readint + " received, " + 
     162                           nums[i] + " expected."); 
     163      } 
     164    } 
     165  } 
    93166} 
Note: See TracChangeset for help on using the changeset viewer.