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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java缓冲区实现,包括完整的源代码: ``` public class MyBuffer { private char[] buffer; private int capacity; private int position; private int limit; private int mark = -1; public MyBuffer(int capacity) { this.capacity = capacity; buffer = new char[capacity]; limit = capacity; } public int capacity() { return capacity; } public int position() { return position; } public void position(int newPosition) { if (newPosition > limit || newPosition < 0) { throw new IllegalArgumentException(); } position = newPosition; if (mark > position) { mark = -1; } } public int limit() { return limit; } public void limit(int newLimit) { if (newLimit > capacity || newLimit < 0) { throw new IllegalArgumentException(); } limit = newLimit; if (position > limit) { position = limit; } if (mark > limit) { mark = -1; } } public void mark() { mark = position; } public void reset() { if (mark == -1) { throw new InvalidMarkException(); } position = mark; } public boolean isReadOnly() { return false; } public boolean hasRemaining() { return position < limit; } public int remaining() { return limit - position; } public char get() { if (position == limit) { throw new BufferUnderflowException(); } return buffer[position++]; } public MyBuffer put(char c) { if (position == limit) { throw new BufferOverflowException(); } buffer[position++] = c; return this; } public MyBuffer flip() { limit = position; position = 0; mark = -1; return this; } public MyBuffer rewind() { position = 0; mark = -1; return this; } public MyBuffer clear() { position = 0; limit = capacity; mark = -1; return this; } } ``` 该缓冲区实现具有基本的get和put操作,以及flip,rewind和clear方法。它还具有标记的功能,可以在指定位置设置标记,并在需要时将缓冲区重置为该标记。此外,它还包括一些异常处理,例如BufferUnderflowException和BufferOverflowException。 请注意,此实现是基于字符数组的,因此只适用于char类型数据。如果需要支持其他数据类型,请相应地更改实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值