Java Source Code: com.sun.java.help.search.BitBuffer


   1: /*
   2:  * @(#)BitBuffer.java    1.6 06/10/30
   3:  * 
   4:  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
   5:  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6:  * 
   7:  * This code is free software; you can redistribute it and/or modify it
   8:  * under the terms of the GNU General Public License version 2 only, as
   9:  * published by the Free Software Foundation.  Sun designates this
  10:  * particular file as subject to the "Classpath" exception as provided
  11:  * by Sun in the LICENSE file that accompanied this code.
  12:  * 
  13:  * This code is distributed in the hope that it will be useful, but WITHOUT
  14:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15:  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16:  * version 2 for more details (a copy is included in the LICENSE file that
  17:  * accompanied this code).
  18:  * 
  19:  * You should have received a copy of the GNU General Public License version
  20:  * 2 along with this work; if not, write to the Free Software Foundation,
  21:  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22:  * 
  23:  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  24:  * CA 95054 USA or visit www.sun.com if you need additional information or
  25:  * have any questions.
  26:  */
  27: 
  28: /**
  29:  * @date   2/17/98
  30:  * @author Jacek R. Ambroziak
  31:  * @group  Sun Microsystems Laboratories
  32:  */
  33: 
  34: /*
  35:   BitBuffer's client has to stick to a usage protocol:
  36:   
  37:   - start with new or cleared BitBuffer
  38:   - do appends for a while
  39:   - finish w/ close()
  40:   
  41:   */
  42: 
  43: package com.sun.java.help.search;
  44: import java.io.*;
  45: 
  46: class BitBuffer
  47:	  {
  48:  private static final int InitSize   = 256;
  49:  private static final int NBits      =  32;
  50:  private static final int BitsInByte =   8;
  51:  private static final int BytesInInt =   4;
  52:  
  53:  private int   _avail = NBits;
  54:  private int   _word = 0;
  55:  private int   _free = 0;
  56:  private int   _size = InitSize;
  57:  private int[] _array = new int[InitSize];
  58:
  59:  public void close()
  60:	    {
  61:    if (_avail < NBits)
  62:      store(_word << _avail);
  63:    else
  64:      _avail = 0;
  65:  }
  66:  
  67:  public void write(DataOutput out) throws IOException
  68:	    {
  69:    for (int i = 0; i < _free - 1; i++)
  70:      out.writeInt(_array[i]);
  71:    int word = _array[_free - 1];
  72:    int bytes = BytesInInt - _avail/BitsInByte;
  73:    int shift = NBits;
  74:    while (bytes-- > 0)
  75:      out.writeByte((word >>> (shift -= BitsInByte)) & 0xFF);
  76:  }
  77:
  78:  public void clear()
  79:	    {
  80:    _word   = 0;
  81:    _avail  = NBits;
  82:    _free   = 0;
  83:  }
  84:  
  85:	    public int byteCount() {
  86:    return _free*BytesInInt - _avail/BitsInByte;
  87:  }
  88:  
  89:	    public int bitCount() {
  90:    return NBits*_free - _avail;
  91:  }
  92:
  93:  public void setFrom(BitBuffer rhs)
  94:	    {
  95:    _word  = rhs._word;
  96:    _avail = rhs._avail;
  97:    if ((_free = rhs._free) > _size)
  98:      _array = new int[_size = rhs._free];
  99:    System.arraycopy(rhs._array, 0, _array, 0, _free);
 100:  }
 101:  
 102:  private void growArray(int newSize)
 103:	    {
 104:    int[] newArray = new int[_size = newSize];
 105:    System.arraycopy(_array, 0, newArray, 0, _free);
 106:    _array = newArray;
 107:  }
 108:
 109:  private void store(int value)
 110:	    {
 111:    if (_free == _size)
 112:      growArray(_size * 2);
 113:    _array[_free++] = value;
 114:  }
 115:
 116:  public void append(int bit)
 117:	    {
 118:    _word = (_word << 1) | bit;
 119:    if (--_avail == 0)
 120:	        {
 121:    store(_word);
 122:    _word = 0;
 123:    _avail = NBits;
 124:      }
 125:  }
 126:  
 127:  public void append(int source, int kBits)
 128:	    {
 129:    if (kBits < _avail)
 130:	        {
 131:    _word = (_word << kBits) | source;
 132:    _avail -= kBits;
 133:      }
 134:    else if (kBits > _avail)
 135:	        {
 136:    int leftover = kBits - _avail;
 137:    store((_word << _avail) | (source >>> leftover));
 138:    _word = source;
 139:    _avail = NBits - leftover;
 140:      }
 141:    else
 142:	        {
 143:    store((_word << kBits) | source);
 144:    _word = 0;
 145:    _avail = NBits;
 146:      }
 147:  }
 148:  
 149:  public void concatenate(BitBuffer bb)
 150:	    {
 151:    if (NBits*(_size - _free) + _avail < bb.bitCount())
 152:      growArray(_free + bb._free + 1);
 153:
 154:    if (_avail == 0)
 155:	        {
 156:    System.arraycopy(bb._array, 0, _array, _free, bb._free);
 157:    _avail = bb._avail;
 158:    _free += bb._free;
 159:      }
 160:    else
 161:	        {
 162:    int tp = _free - 1;    // target
 163:    int sp = 0;        // source
 164:	      do {
 165:      _array[tp++] |= bb._array[sp] >>> (NBits - _avail);
 166:      _array[tp] = bb._array[sp++] << _avail;
 167:    }
 168:    while (sp < bb._free);
 169:    _free += bb._free;
 170:    if ((_avail += bb._avail) >= NBits)
 171:	        {
 172:        _avail -= NBits;
 173:        _free--;
 174:      }
 175:      }
 176:  }
 177:}