Changeset 2027


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

Added write(String bitString) method

File:
1 edited

Legend:

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

    r1629 r2027  
    2222Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    2323*/ 
     24package loci.formats; 
    2425 
    25 package loci.formats; 
     26import java.util.*; 
    2627 
    2728/** 
     
    7677  } 
    7778 
     79  /** 
     80   *  Writes the bits represented by a bit string to the buffer. 
     81   *  All characters in the string must be 0 or 1, or this will 
     82   *  throw an IllegalArgumentException */ 
     83    
     84  public void write(String bitString) { 
     85    for(int i = 0; i < bitString.length(); i++) { 
     86      if('1' == bitString.charAt(i)) { 
     87        int b = 1 << (7 - bit); 
     88        buf[index] |= b; 
     89      } else if('0' != bitString.charAt(i)) { 
     90        throw new IllegalArgumentException(bitString.charAt(i) + "found at " 
     91            + "character " + i + "; 0 or 1 expected. Write only partially " 
     92            + "completed."); 
     93      } 
     94      bit++; 
     95      if (bit > 7) { 
     96        bit = 0; 
     97        index++; 
     98        if (index >= buf.length) { 
     99          // buffer is full; increase the size 
     100          byte[] newBuf = new byte[buf.length * 2]; 
     101          System.arraycopy(buf, 0, newBuf, 0, buf.length); 
     102          buf = newBuf; 
     103        } 
     104      } 
     105    } 
     106  } 
     107 
    78108  /** Gets an array containing all bits written thus far. */ 
    79109  public byte[] toByteArray() { 
     
    90120  public static void main(String[] args) { 
    91121    int max = 50000; 
    92  
    93122    // randomize values 
    94123    System.out.println("Generating random list of " + max + " values"); 
     
    117146      } 
    118147    } 
     148     
     149    // Testing string functionality 
     150    Random r = new Random(); 
     151    System.out.println("Generating 5000 random bits for String test"); 
     152    StringBuffer sb = new StringBuffer(5000); 
     153    for(int i = 0; i < 5000; i++) { 
     154      sb.append(r.nextInt(2)); 
     155    } 
     156    out = new BitWriter(); 
     157    System.out.println("Writing values to byte array"); 
     158    out.write(sb.toString()); 
     159    System.out.println("Reading values from byte array"); 
     160    bb = new BitBuffer(out.toByteArray()); 
     161    for(int i = 0; i < 5000; i++) { 
     162      int value = bb.getBits(1); 
     163      int expected = (sb.charAt(i) == '1') ? 1 : 0; 
     164      if(value != expected) { 
     165        System.out.println("Bit #" + i + " does not match (got " + value + 
     166          "; expected " + expected + "."); 
     167      } 
     168    } 
    119169  } 
    120170 
Note: See TracChangeset for help on using the changeset viewer.