📄 abstractbuffer.cs
字号:
namespace NCindy.Buffer
{
using NCindy;
using System;
using System.Text;
public abstract class AbstractBuffer : IBuffer, IDisposable
{
protected bool bigEndian = true;
private int capacity;
private int limit;
private int mark = -1;
private readonly int offset;
protected bool permanent;
private int position;
private readonly object positionSyncRoot = new object();
protected bool readOnly;
protected bool released;
protected object syncRoot = new object();
protected AbstractBuffer(int offset, int capacity)
{
this.offset = offset;
this.capacity = capacity;
this.Limit = capacity;
}
public IBuffer AsReadOnlyBuffer()
{
AbstractBuffer buffer = (AbstractBuffer) this.Duplicate();
buffer.ReadOnly = true;
return buffer;
}
protected static void CheckBounds(int offset, int length, int size)
{
if ((((offset | length) | (offset + length)) | (size - (offset + length))) < 0)
{
throw new IndexOutOfRangeException();
}
}
protected void CheckReadonly()
{
if (this.ReadOnly)
{
throw new ReadOnlyBufferException();
}
}
protected void CheckReleased()
{
if (this.Released)
{
throw new ReleasedBufferException();
}
}
public IBuffer Clear()
{
this.position = 0;
this.limit = this.capacity;
this.mark = -1;
return this;
}
public abstract IBuffer Compact();
public void Dispose()
{
this.Release();
}
public string Dump()
{
byte[] src = this.GetInnerByteArray();
string text = "\r\n";
int num = 0x10;
int num2 = (int) Math.Ceiling(((double) this.capacity) / ((double) num));
StringBuilder builder = new StringBuilder((num2 * num) * 6);
builder.Append(this.ToString()).Append(text);
byte[] dst = new byte[num];
int num3 = 0;
for (int i = 0; i < num2; i++)
{
if (num3 != 0)
{
builder.Append(text);
}
string text2 = string.Format("{0:x}", num3);
for (int j = 8 - text2.Length; j > 0; j--)
{
builder.Append(0);
}
builder.Append(text2).Append("h:");
if ((src.Length - num3) < num)
{
dst = new byte[src.Length - num3];
}
Buffer.BlockCopy(src, num3, dst, 0, dst.Length);
for (int k = 0; k < num; k++)
{
if ((num3 == this.position) || (num3 == this.limit))
{
builder.Append("'");
}
else
{
builder.Append(" ");
}
num3++;
if (k < dst.Length)
{
string text3 = string.Format("{0:x}", dst[k] & 0xff);
if (text3.Length < 2)
{
builder.Append(0);
}
builder.Append(text3);
}
else
{
builder.Append(" ");
}
}
builder.Append(" ; ");
foreach (char ch in Encoding.Default.GetString(dst))
{
builder.Append(char.IsControl(ch) ? '.' : ch);
}
}
return builder.ToString();
}
public abstract IBuffer Duplicate();
public IBuffer Flip()
{
this.limit = this.position;
this.position = 0;
this.mark = -1;
return this;
}
public byte Get()
{
return this.InternalGet(this.GetIndex(1));
}
public byte Get(int index)
{
return this.InternalGet(this.GetIndex(index, 1));
}
protected int GetIndex(int length)
{
this.CheckReleased();
if ((this.limit - this.position) < length)
{
throw new BufferUnderflowException();
}
int num = this.position + this.offset;
this.position += length;
return num;
}
protected int GetIndex(int i, int length)
{
this.CheckReleased();
if (((length < 0) || (i < 0)) || (length > (this.limit - i)))
{
throw new IndexOutOfRangeException();
}
return (i + this.offset);
}
public abstract byte[] GetInnerByteArray();
protected int GetMark()
{
return this.mark;
}
public int IndexOf(byte[] value)
{
int index;
if (value.Length == 0)
{
return this.position;
}
int num = this.GetIndex(0);
int num2 = (num + this.Remaining) - value.Length;
byte num3 = value[0];
Label_0025:
index = num;
while (index <= num2)
{
if (this.InternalGet(index) == num3)
{
for (int i = 1; i < value.Length; i++)
{
if (this.InternalGet(index + i) != value[i])
{
goto Label_0025;
}
}
return ((index - num) + this.position);
}
index++;
}
return -1;
}
internal abstract byte InternalGet(int index);
protected IBuffer InternalMark(int newMark)
{
if (newMark < 0)
{
this.mark = -1;
}
else
{
if (newMark > this.position)
{
throw new IndexOutOfRangeException();
}
this.mark = newMark;
}
return this;
}
internal abstract void InternalPut(int index, byte byteValue);
protected abstract void InternalRelease();
public IBuffer Mark()
{
return this.InternalMark(this.position);
}
public IBuffer Put(IBuffer source)
{
return this.Put(source, source.Remaining);
}
public IBuffer Put(IBuffer source, int length)
{
CheckBounds(0, length, source.Remaining);
int num = this.PutIndex(length);
for (int i = 0; i < length; i++)
{
this.InternalPut(num + i, source.Get());
}
return this;
}
protected int PutIndex(int length)
{
this.CheckReleased();
this.CheckReadonly();
if ((this.limit - this.position) < length)
{
throw new BufferOverflowException();
}
int num = this.position + this.offset;
this.position += length;
return num;
}
protected int PutIndex(int index, int length)
{
this.CheckReleased();
this.CheckReadonly();
if (((length < 0) || (index < 0)) || (length > (this.limit - index)))
{
throw new IndexOutOfRangeException();
}
return (index + this.offset);
}
public virtual void Release()
{
if (!this.permanent && !this.released)
{
lock (this.syncRoot)
{
if (this.permanent || this.released)
{
try
{
this.InternalRelease();
}
finally
{
this.released = true;
}
}
}
}
}
public IBuffer Reset()
{
if (this.mark < 0)
{
throw new InvalidMarkException();
}
this.position = this.mark;
return this;
}
public IBuffer Rewind()
{
this.position = 0;
this.mark = -1;
return this;
}
public IBuffer Skip(int size)
{
if (size != 0)
{
this.Position = this.position + size;
}
return this;
}
public abstract IBuffer Slice();
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(base.GetType().Name);
builder.Append("[pos=").Append(this.position);
builder.Append(" lim=").Append(this.limit);
builder.Append(" cap=").Append(this.capacity);
builder.Append("]");
return builder.ToString();
}
public bool BigEndian
{
get
{
return this.bigEndian;
}
set
{
this.bigEndian = value;
}
}
public virtual int Capacity
{
get
{
return this.capacity;
}
protected set
{
if (value < 0)
{
throw new ArgumentException();
}
this.capacity = value;
this.Limit = Math.Min(this.limit, this.capacity);
}
}
public bool HasRemaining
{
get
{
return (this.limit > this.position);
}
}
internal abstract byte[] InnerByteArray { get; }
public int Limit
{
get
{
return this.limit;
}
set
{
if ((value > this.capacity) || (value < 0))
{
throw new ArgumentException();
}
this.limit = value;
this.Position = Math.Min(this.position, this.limit);
}
}
public virtual bool Permanent
{
get
{
return this.permanent;
}
set
{
this.CheckReleased();
this.permanent = value;
}
}
public int Position
{
get
{
return this.position;
}
set
{
if ((value > this.limit) || (value < 0))
{
throw new ArgumentException();
}
lock (this.positionSyncRoot)
{
this.position = value;
}
if (this.mark > this.position)
{
this.mark = -1;
}
}
}
public bool ReadOnly
{
get
{
return this.readOnly;
}
protected set
{
this.readOnly = value;
}
}
public virtual bool Released
{
get
{
return this.released;
}
}
public int Remaining
{
get
{
return (this.limit - this.position);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -