JAVA 비트플로우 버퍼 - 변경

2392 단어 java 구현
import java.io.InputStream;

public final class BitStream {
	private  final int RESELEN = 4096;//       
	private  InputStream  in;//   
	private byte[] bitBuf;//        
	private int bitPos; //   
	private int bytePos;//    
	private int endPos; //bitBuf         

	public BitStream(InputStream  in) {
		
		this.in = in;
		bitBuf = new byte[RESELEN + 4]; //      (    )  
		
	}
	
	public BitStream(InputStream  in,int size) {
		
		this.in = in;
		bitBuf = new byte[size + 4]; //      size(    )  
	}
	/**
	 * <p>
	 *   : bitBuf  len  ,RESELEN      4    32bit "2   ". 
	 * </p>
	 */
	public int append(int len) {
		if (len + endPos > RESELEN) {
			//    bytePos               
			System.arraycopy(bitBuf, bytePos, bitBuf, 0, endPos
					- bytePos);
			endPos -= bytePos;
			bitPos = bytePos = 0;
		}
		try {
			if(in.read(bitBuf, endPos, len)==-1)
				return -1;  
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		endPos += len;
		return len;
	}
	/**
	 * <p>
	 *   :   bitstream
	 * </p>
	 */
	public void resetIndex() {
		bytePos = bitPos = endPos = 0;
	}
	/**
	 * <p>
	 *   :    bitBuf  1 bit
	 * </p>
	 */
	public int get1Bit() {
		int bit = bitBuf[bytePos] << bitPos;
		bit >>= 7;
		bit &= 0x1;
		bitPos++;
		bytePos += bitPos >> 3;
		bitPos &= 0x7;
		return bit;
	}
	/**
	 * 
	 * <p>
	 *   :2 <= n <= 17 
	 * </p>
	 */
	public int getBits17(int n) {
		int iret = bitBuf[bytePos];
		iret <<= 8;
		iret |= bitBuf[bytePos + 1] & 0xff;
		iret <<= 8;
		iret |= bitBuf[bytePos + 2] & 0xff;
		iret <<= bitPos;
		iret &= 0xffffff; //  8  0;  
		iret >>= 24 - n;
		bitPos += n;
		bytePos += bitPos >> 3;
		bitPos &= 0x7;
		return iret;
	}
 
http://lfp001.iteye.com/blog/412745

좋은 웹페이지 즐겨찾기