C#字节缓冲类ByteBufferWord格式文档下载.docx

上传人:b****6 文档编号:21962072 上传时间:2023-02-02 格式:DOCX 页数:16 大小:17.20KB
下载 相关 举报
C#字节缓冲类ByteBufferWord格式文档下载.docx_第1页
第1页 / 共16页
C#字节缓冲类ByteBufferWord格式文档下载.docx_第2页
第2页 / 共16页
C#字节缓冲类ByteBufferWord格式文档下载.docx_第3页
第3页 / 共16页
C#字节缓冲类ByteBufferWord格式文档下载.docx_第4页
第4页 / 共16页
C#字节缓冲类ByteBufferWord格式文档下载.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

C#字节缓冲类ByteBufferWord格式文档下载.docx

《C#字节缓冲类ByteBufferWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《C#字节缓冲类ByteBufferWord格式文档下载.docx(16页珍藏版)》请在冰豆网上搜索。

C#字节缓冲类ByteBufferWord格式文档下载.docx

///构造方法

paramname="

capacity"

>

缓存字节数组的长度<

/param>

publicByteBuffer(intcapacity)

{

buf=newbyte[capacity];

this.capacity=capacity;

}

bytes"

字节数组<

publicByteBuffer(byte[]bytes)

buf=bytes;

this.capacity=bytes.Length;

///构建一个capacity长度的字节缓存区ByteBuffer对象

returns>

ByteBuffer实例<

/returns>

publicstaticByteBufferAllocate(intcapacity)

returnnewByteBuffer(capacity);

///构建一个以bytes为字节缓存区的ByteBuffer对象,一般不推荐使用

publicstaticByteBufferAllocate(byte[]bytes)

returnnewByteBuffer(bytes);

///根据length长度,确定大于此leng的最近的2次方数,如length=7,则返回值为8

length"

长度<

int<

privateintFixLength(intlength)

intn=2;

intb=2;

while(b<

length)

b=2<

<

n;

n++;

returnb;

///翻转字节数组,如果本地字节序列为低字节序列,则进行翻转以转换为高字节序列

byte[]<

privatebyte[]flip(byte[]bytes)

if(BitConverter.IsLittleEndian)

Array.Reverse(bytes);

returnbytes;

///确定内部字节缓存数组的大小

currLen"

futureLen"

privateintFixSizeAndReset(intcurrLen,intfutureLen)

if(futureLen>

currLen)

//以原大小的2次方数的两倍确定内部字节缓存区大小

intsize=FixLength(currLen)*2;

size)

//以将来的大小的2次方的两倍确定内部字节缓存区大小

size=FixLength(futureLen)*2;

byte[]newbuf=newbyte[size];

Array.Copy(buf,0,newbuf,0,currLen);

buf=newbuf;

capacity=newbuf.Length;

returnfutureLen;

///将bytes字节数组从startIndex开始的length字节写入到此缓存区

startIndex"

publicvoidWriteBytes(byte[]bytes,intstartIndex,intlength)

lock(this)

intoffset=length-startIndex;

if(offset<

=0)return;

inttotal=offset+writeIndex;

intlen=buf.Length;

FixSizeAndReset(len,total);

for(inti=writeIndex,j=startIndex;

i<

total;

i++,j++)

buf[i]=bytes[j];

writeIndex=total;

///将字节数组中从0到length的元素写入缓存区

publicvoidWriteBytes(byte[]bytes,intlength)

WriteBytes(bytes,0,length);

///将字节数组全部写入缓存区

publicvoidWriteBytes(byte[]bytes)

WriteBytes(bytes,bytes.Length);

///将一个ByteBuffer的有效字节区写入此缓存区中

buffer"

publicvoidWrite(ByteBufferbuffer)

if(buffer==null)return;

if(buffer.ReadableBytes()<

WriteBytes(buffer.ToArray());

///将一个字符串写入此缓存区中

value"

publicvoidWirteUTFString(stringvalue){

WriteBytes(Encoding.UTF8.GetBytes(value));

///写入一个int16数据

publicvoidWriteShort(shortvalue)

WriteBytes(flip(BitConverter.GetBytes(value)));

///写入一个uint16数据

publicvoidWriteUshort(ushortvalue)

///写入一个int32数据

publicvoidWriteInt(intvalue)

//byte[]array=newbyte[4];

//for(inti=3;

i>

=0;

i--)

//{

//array[i]=(byte)(value&

0xff);

//value=value>

8;

//}

//Array.Reverse(array);

//Write(array);

///写入一个uint32数据

publicvoidWriteUint(uintvalue)

///写入一个int64数据

publicvoidWriteLong(longvalue)

///写入一个uint64数据

publicvoidWriteUlong(ulongvalue)

///写入一个float数据

publicvoidWriteFloat(floatvalue)

///写入一个byte数据

publicvoidWriteByte(bytevalue)

intafterLen=writeIndex+1;

FixSizeAndReset(len,afterLen);

buf[writeIndex]=value;

writeIndex=afterLen;

///写入一个double类型数据

publicvoidWriteDouble(doublevalue)

///读取一个字节

byte<

publicbyteReadByte()

byteb=buf[readIndex];

readIndex++;

///从读取索引位置开始读取len长度的字节数组

privatebyte[]Read(intlen)

byte[]bytes=newbyte[len];

Array.Copy(buf,readIndex,bytes,0,len);

readIndex+=len;

///读取一个uint16数据

ushort<

publicushortReadUshort()

returnBitConverter.ToUInt16(Read

(2),0);

///读取一个int16数据

short<

publicshortReadShort()

returnBitConverter.ToInt16(Read

(2),0);

///读取一个uint32数据

uint<

publicuintReadUint()

returnBitConverter.ToUInt32(Read(4),0);

///读取一个int32数据

publicintReadInt()

returnBitConverter.ToInt32(Read(4),0);

///读取一个uint64数据

ulong<

publiculongReadUlong()

returnBitConverter.ToUInt64(Read(8),0);

///读取一个long数据

long<

publiclongReadLong()

returnBitConverter.ToInt64(Read(8),0);

///读取一个float数据

float<

publicfloatReadFloat()

returnBitConverter.ToSingle(Read(4),0);

///读取一个double数据

double<

publicdoubleReadDouble()

returnBitConverter.ToDouble(Read(8),0);

///从读取索引位置开始读取len长度的字节到disbytes目标字节数组中

disstart"

目标字节数组的写入索引<

len"

读取长度<

publicvoidReadBytes(byte[]disbytes,intdisstart,intlen)

intsize=disstart+len;

for(inti=disstart;

size;

i++)

disbytes[i]=this.ReadByte();

///清除已读字节并重建缓存区

publicvoidDiscardReadBytes()

if(readIndex<

intlen=buf.Length-readIndex;

byte[]newbuf=newbyte[len];

Array.Copy(buf,readIndex,newbuf,0,len);

writeIndex-=readIndex;

markReadIndex-=readIndex;

if(markReadIndex<

0)

markReadIndex=readIndex;

markWirteIndex-=readIndex;

if(markWirteIndex<

0||markWirteIndex<

readIndex||markWirteIndex<

markReadIndex)

markWirteIndex=writeIndex;

readIndex=0;

///清空此对象

publicvoidClear()

buf=newbyte[buf.Length];

writeIndex=0;

markReadIndex=0;

markWirteIndex=0;

///设置开始读取的索引

index"

publicvoidSetReaderIndex(intindex)

if(index<

0)return;

readIndex=index;

///标记读取的索引位置

publicvoidMarkReaderIndex()

///标记写入的索引位置

publicvoidMarkWriterIndex()

///将读取的索引位置重置为标记的读取索引位置

publicvoidResetReaderIndex()

readIndex=markReadIndex;

///将写入的索引位置重置为标记的写入索引位置

publicvoidResetWriterIndex()

writeIndex=markWirteIndex;

///可读的有效字节数

publicintReadableBytes()

returnwriteIndex-readIndex;

///获取可读的字节数组

publicbyte[]ToArray()

byte[]bytes=newbyte[writeIndex];

Array.Copy(buf,0,bytes,0,bytes.Length);

///获取缓存区大小

publicintGetCapacity()

returnthis.capacity;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 人力资源管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1