⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bytablebitset.java

📁 实现数据库的storage manager 功能
💻 JAVA
字号:
/*
 * Copyright (c) 2000-2004, Rickard C鰏ter, Martin Svensson
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution. 
 * Neither the name of SICS nor the names of its contributors 
 * may be used to endorse or promote products derived from this software 
 * without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

package com.mellowtech.disc;

import java.util.*;
import java.io.*;
import java.nio.*;

/**
 * <code>BytableBitSet</code> is a BitSet that is suitable for file storage since
 * it extends ByteStorable. This bitSet is wrapper for an ordinary java.util.BitSet.
 *
 * @author Rickard C鰏ter
 * @version 1.0
 * @see java.util.BitSet
 */
public class BytableBitSet extends ByteStorable {
  
  private BitSet mSet;
 
  /**
   * Creates a new <code>BytableBitSet</code> instance.
   *
   */
  public BytableBitSet(){
    mSet = new BitSet();
  }

  /**
   * Creates a new <code>BytableBitSet</code> instance.
   *
   * @param set the bitSet to use
   */
  public BytableBitSet(BitSet set){
    mSet = set;
  }

  /**
   * Get the bitset this BytableBitSet contains
   *
   * @return a <code>BitSet</code> value
   */
  public BitSet getBitSet(){
    return mSet;
  }

  /**
   * Set the n'th position in this bitSet.
   *
   * @param n position to set
   * @see java.util.BitSet#set(n)
   */
  public void set(int n) {
    mSet.set(n);
  }
  
  /**
   * Describe <code>objectByteSize</code> method here.
   *
   * @param o a <code>Serializable</code> value
   * @return an <code>int</code> value
   */
  protected int objectByteSize(Serializable o) {
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(bos);
      oos.writeObject(o);
      return bos.size();
    }
    catch(Exception e) {
      return 0;
    }
  }

  /**
   * Describe <code>objectToByteBuffer</code> method here.
   *
   * @param o a <code>Serializable</code> value
   * @return a <code>ByteBuffer</code> value
   */
  protected ByteBuffer objectToByteBuffer(Serializable o) {
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(bos);
      oos.writeObject(o);
      ByteBuffer bb = ByteBuffer.allocate(bos.size());
      bb.put(bos.toByteArray());
      bb.rewind();
      return bb;
    }
    catch(Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
  /**
   * Describe <code>objectFromByteBuffer</code> method here.
   *
   * @param bb a <code>ByteBuffer</code> value
   * @return a <code>Serializable</code> value
   */
  protected Serializable objectFromByteBuffer(ByteBuffer bb) {
    try {
      ByteArrayInputStream bis 
	= new ByteArrayInputStream(bb.array(), bb.arrayOffset()+bb.position(), 
				   bb.limit()-bb.position());
      ObjectInputStream ois = new ObjectInputStream(bis);
      Serializable o = (Serializable) ois.readObject();
      return o;
    }
    catch(Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
  
  public int byteSize() {
    return 4 + objectByteSize(mSet);
  }
  
  public int byteSize(ByteBuffer bb){
    int pos = bb.position();
    int size = bb.getInt();
    bb.position(pos);
    return size;
  }
    
  public void toBytes(ByteBuffer bb){
    int size = byteSize();
    bb.putInt(size);
    if (size != 0)
      bb.put(objectToByteBuffer(mSet));
  }
  
  public ByteStorable fromBytes(ByteBuffer bb) {
    BytableBitSet bitset = new BytableBitSet();
    int size = bb.getInt();
    if (size == 0)
      return bitset;
    bitset.mSet = (BitSet) objectFromByteBuffer(bb);
    bb.position(bb.position() + size - 4);
    return bitset;
  }

  public int compareTo(Object obj1){
    return 0;
  }

  public String toString() {
    return getBitSet().toString();
  }

  /*
  public static void main(String[] args) {
    BytableBitSet bb = new BytableBitSet();
    bb.getBitSet().set(100);
    bb.getBitSet().set(57);
    
    System.out.println(bb);
  
    ByteBuffer buf = ByteBuffer.allocate(bb.byteSize());
    bb.toBytes(buf);
    buf.flip();
    BytableBitSet bb2 = new BytableBitSet();
    bb2 = (BytableBitSet) bb2.fromBytes(buf);
    System.out.println(bb2);
    System.out.println(buf);
    System.out.println(bb.byteSize());
    System.out.println(bb2.byteSize()); 
  }
  */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -