123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942 |
- using ET;
- using System;
- using System.Collections.Generic;
- [Serializable]
- public class ByteBuffer
- {
-
- private byte[] buf;
-
- private int readIndex = 0;
-
- private int writeIndex = 0;
-
- private int markReadIndex = 0;
-
- private int markWirteIndex = 0;
-
- private int capacity;
-
- [StaticField]
- private static List<ByteBuffer> pool = new List<ByteBuffer>();
- [StaticField]
- private static int poolMaxCount = 200;
-
- private bool isPool = false;
-
-
-
-
- private ByteBuffer(int capacity)
- {
- this.buf = new byte[capacity];
- this.capacity = capacity;
- this.readIndex = 0;
- this.writeIndex = 0;
- }
-
-
-
-
- private ByteBuffer(byte[] bytes)
- {
- this.buf = new byte[bytes.Length];
- Array.Copy(bytes, 0, buf, 0, buf.Length);
- this.capacity = buf.Length;
- this.readIndex = 0;
- this.writeIndex = bytes.Length + 1;
- }
-
-
-
-
-
-
-
-
-
- public static ByteBuffer Allocate(int capacity, bool fromPool = false)
- {
- if(!fromPool)
- {
- return new ByteBuffer(capacity);
- }
- lock (pool)
- {
- ByteBuffer bbuf;
- if (pool.Count == 0)
- {
- bbuf = new ByteBuffer(capacity)
- {
- isPool = true
- };
- return bbuf;
- }
- int lastIndex = pool.Count - 1;
- bbuf = pool[lastIndex];
- pool.RemoveAt(lastIndex);
- if (!bbuf.isPool)
- {
- bbuf.isPool = true;
- }
- return bbuf;
- }
- }
-
-
-
-
-
-
-
-
- public static ByteBuffer Allocate(byte[] bytes, bool fromPool = false)
- {
- if (!fromPool)
- {
- return new ByteBuffer(bytes);
- }
- lock (pool)
- {
- ByteBuffer bbuf;
- if (pool.Count == 0)
- {
- bbuf = new ByteBuffer(bytes)
- {
- isPool = true
- };
- return bbuf;
- }
- int lastIndex = pool.Count - 1;
- bbuf = pool[lastIndex];
- bbuf.WriteBytes(bytes);
- pool.RemoveAt(lastIndex);
- if (!bbuf.isPool)
- {
- bbuf.isPool = true;
- }
- return bbuf;
- }
- }
-
-
-
-
-
- private int FixLength(int value)
- {
- if (value == 0)
- {
- return 1;
- }
- value--;
- value |= value >> 1;
- value |= value >> 2;
- value |= value >> 4;
- value |= value >> 8;
- value |= value >> 16;
- return value + 1;
- }
-
-
-
-
-
- private byte[] Flip(byte[] bytes)
- {
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(bytes);
- }
- return bytes;
- }
-
-
-
-
-
-
- private int FixSizeAndReset(int currLen, int futureLen)
- {
- if (futureLen > currLen)
- {
-
- int size = FixLength(currLen) * 2;
- if (futureLen > size)
- {
-
- size = FixLength(futureLen) * 2;
- }
- byte[] newbuf = new byte[size];
- Array.Copy(buf, 0, newbuf, 0, currLen);
- buf = newbuf;
- capacity = size;
- }
- return futureLen;
- }
-
-
-
-
-
-
- public void WriteBytes(byte[] bytes, int startIndex, int length)
- {
- int offset = length - startIndex;
- if (offset <= 0) return;
- int total = offset + writeIndex;
- int len = buf.Length;
- FixSizeAndReset(len, total);
- for (int i = writeIndex, j = startIndex; i < total; i++, j++)
- {
- buf[i] = bytes[j];
- }
- writeIndex = total;
- }
-
-
-
-
-
- public void WriteBytes(byte[] bytes, int length)
- {
- WriteBytes(bytes, 0, length);
- }
-
-
-
-
- public void WriteBytes(byte[] bytes)
- {
- WriteBytes(bytes, bytes.Length);
- }
-
-
-
-
- public void Write(ByteBuffer buffer)
- {
- if (buffer == null) return;
- if (buffer.ReadableBytes <= 0) return;
- WriteBytes(buffer.ToArray());
- }
-
-
-
-
- public void WriteShort(short value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteUshort(ushort value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteInt(int value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteUint(uint value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteLong(long value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteUlong(ulong value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteFloat(float value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteByte(byte value)
- {
- int afterLen = writeIndex + 1;
- int len = buf.Length;
- FixSizeAndReset(len, afterLen);
- buf[writeIndex] = value;
- writeIndex = afterLen;
- }
-
-
-
-
- public void WriteByte(int value)
- {
- byte b = (byte)value;
- WriteByte(b);
- }
-
-
-
-
- public void WriteDouble(double value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteChar(char value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public void WriteBoolean(bool value)
- {
- WriteBytes(Flip(BitConverter.GetBytes(value)));
- }
-
-
-
-
- public byte ReadByte()
- {
- byte b = buf[readIndex];
- readIndex++;
- return b;
- }
-
-
-
-
-
-
- private byte[] Get(int index, int len)
- {
- byte[] bytes = new byte[len];
- Array.Copy(buf, index, bytes, 0, len);
- return Flip(bytes);
- }
-
-
-
-
-
- private byte[] Read(int len)
- {
- byte[] bytes = Get(readIndex, len);
- readIndex += len;
- return bytes;
- }
-
-
-
-
- public ushort ReadUshort()
- {
- return BitConverter.ToUInt16(Read(2), 0);
- }
-
-
-
-
- public short ReadShort()
- {
- return BitConverter.ToInt16(Read(2), 0);
- }
-
-
-
-
- public uint ReadUint()
- {
- return BitConverter.ToUInt32(Read(4), 0);
- }
-
-
-
-
- public int ReadInt()
- {
- return BitConverter.ToInt32(Read(4), 0);
- }
-
-
-
-
- public ulong ReadUlong()
- {
- return BitConverter.ToUInt64(Read(8), 0);
- }
-
-
-
-
- public long ReadLong()
- {
- return BitConverter.ToInt64(Read(8), 0);
- }
-
-
-
-
- public float ReadFloat()
- {
- return BitConverter.ToSingle(Read(4), 0);
- }
-
-
-
-
- public double ReadDouble()
- {
- return BitConverter.ToDouble(Read(8), 0);
- }
-
-
-
-
- public char ReadChar()
- {
- return BitConverter.ToChar(Read(2), 0);
- }
-
-
-
-
- public bool ReadBoolean()
- {
- return BitConverter.ToBoolean(Read(1), 0);
- }
-
-
-
-
-
-
- public void ReadBytes(byte[] disbytes, int disstart, int len)
- {
- int size = disstart + len;
- for (int i = disstart; i < size; i++)
- {
- disbytes[i] = this.ReadByte();
- }
- }
-
-
-
-
-
- public byte GetByte(int index)
- {
- return buf[index];
- }
-
-
-
-
- public byte GetByte()
- {
- return GetByte(readIndex);
- }
-
-
-
-
-
- public double GetDouble(int index)
- {
- return BitConverter.ToDouble(Get(index, 8), 0);
- }
-
-
-
-
- public double GetDouble()
- {
- return GetDouble(readIndex);
- }
-
-
-
-
-
- public float GetFloat(int index)
- {
- return BitConverter.ToSingle(Get(index, 4), 0);
- }
-
-
-
-
- public float GetFloat()
- {
- return GetFloat(readIndex);
- }
-
-
-
-
-
- public long GetLong(int index)
- {
- return BitConverter.ToInt64(Get(index, 8), 0);
- }
-
-
-
-
- public long GetLong()
- {
- return GetLong(readIndex);
- }
-
-
-
-
-
- public ulong GetUlong(int index)
- {
- return BitConverter.ToUInt64(Get(index, 8), 0);
- }
-
-
-
-
- public ulong GetUlong()
- {
- return GetUlong(readIndex);
- }
-
-
-
-
-
- public int GetInt(int index)
- {
- return BitConverter.ToInt32(Get(index, 4), 0);
- }
-
-
-
-
- public int GetInt()
- {
- return GetInt(readIndex);
- }
-
-
-
-
-
- public uint GetUint(int index)
- {
- return BitConverter.ToUInt32(Get(index, 4), 0);
- }
-
-
-
-
- public uint GetUint()
- {
- return GetUint(readIndex);
- }
-
-
-
-
-
- public int GetShort(int index)
- {
- return BitConverter.ToInt16(Get(index, 2), 0);
- }
-
-
-
-
- public int GetShort()
- {
- return GetShort(readIndex);
- }
-
-
-
-
-
- public int GetUshort(int index)
- {
- return BitConverter.ToUInt16(Get(index, 2), 0);
- }
-
-
-
-
- public int GetUshort()
- {
- return GetUshort(readIndex);
- }
-
-
-
-
-
- public char GetChar(int index)
- {
- return BitConverter.ToChar(Get(index, 2), 0);
- }
-
-
-
-
- public char GetChar()
- {
- return GetChar(readIndex);
- }
-
-
-
-
-
- public bool GetBoolean(int index)
- {
- return BitConverter.ToBoolean(Get(index, 1), 0);
- }
-
-
-
-
- public bool GetBoolean()
- {
- return GetBoolean(readIndex);
- }
-
-
-
- public void DiscardReadBytes()
- {
- if (readIndex <= 0) return;
- int len = buf.Length - readIndex;
- byte[] newbuf = new byte[len];
- Array.Copy(buf, readIndex, newbuf, 0, len);
- buf = newbuf;
- writeIndex -= readIndex;
- markReadIndex -= readIndex;
- if (markReadIndex < 0)
- {
-
- markReadIndex = 0;
- }
- markWirteIndex -= readIndex;
- if (markWirteIndex < 0 || markWirteIndex < readIndex || markWirteIndex < markReadIndex)
- {
- markWirteIndex = writeIndex;
- }
- readIndex = 0;
- }
-
-
-
- public int ReaderIndex
- {
- get
- {
- return readIndex;
- }
- set
- {
- if (value < 0) return;
- readIndex = value;
- }
- }
-
-
-
- public int WriterIndex
- {
- get
- {
- return writeIndex;
- }
- set
- {
- if (value < 0) return;
- writeIndex = value;
- }
- }
-
-
-
- public void MarkReaderIndex()
- {
- markReadIndex = readIndex;
- }
-
-
-
- public void MarkWriterIndex()
- {
- markWirteIndex = writeIndex;
- }
-
-
-
- public void ResetReaderIndex()
- {
- readIndex = markReadIndex;
- }
-
-
-
- public void ResetWriterIndex()
- {
- writeIndex = markWirteIndex;
- }
-
-
-
-
- public int ReadableBytes
- {
- get
- {
- return writeIndex - readIndex;
- }
- }
-
-
-
-
- public int Capacity
- {
- get
- {
- return this.capacity;
- }
- }
-
-
-
-
- public byte[] ToArray()
- {
- byte[] bytes = new byte[writeIndex];
- Array.Copy(buf, 0, bytes, 0, bytes.Length);
- return bytes;
- }
-
- public byte[] GetBuffer()
- {
- return buf;
- }
-
-
-
-
- public ByteBuffer Copy()
- {
- if (buf == null)
- {
- return new ByteBuffer(16);
- }
- if (readIndex < writeIndex)
- {
- byte[] newbytes = new byte[writeIndex - readIndex];
- Array.Copy(buf, readIndex, newbytes, 0, newbytes.Length);
- ByteBuffer buffer = new ByteBuffer(newbytes.Length);
- buffer.WriteBytes(newbytes);
- buffer.isPool = this.isPool;
- return buffer;
- }
- return new ByteBuffer(16);
- }
-
-
-
-
- public ByteBuffer Clone()
- {
- if (buf == null)
- {
- return new ByteBuffer(16);
- }
- ByteBuffer newBuf = new ByteBuffer(buf)
- {
- capacity = this.capacity,
- readIndex = this.readIndex,
- writeIndex = this.writeIndex,
- markReadIndex = this.markReadIndex,
- markWirteIndex = this.markWirteIndex,
- isPool = this.isPool
- };
- return newBuf;
- }
-
-
-
-
- public void ForEach(Action<byte> action)
- {
- for(int i = 0; i < this.ReadableBytes; i++)
- {
- action.Invoke(this.buf[i]);
- }
- }
-
-
-
- public void Clear()
- {
-
- for(int i = 0; i < buf.Length; i++)
- {
- buf[i] = 0;
- }
- readIndex = 0;
- writeIndex = 0;
- markReadIndex = 0;
- markWirteIndex = 0;
- capacity = buf.Length;
- }
-
-
-
- public void Dispose()
- {
- if (isPool)
- {
- lock (pool)
- {
- if (pool.Count < poolMaxCount)
- {
- this.Clear();
- pool.Add(this);
- } else
- {
- readIndex = 0;
- writeIndex = 0;
- markReadIndex = 0;
- markWirteIndex = 0;
- capacity = 0;
- buf = null;
- }
- }
- }
- else
- {
- readIndex = 0;
- writeIndex = 0;
- markReadIndex = 0;
- markWirteIndex = 0;
- capacity = 0;
- buf = null;
- }
- }
- }
|