using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.IO.Compression; namespace Engine.CLoader { /// 配置文件字节流解析对象 public class CByteArray { public const int BYTE_LEN = 1; public const int SHORT16_LEN = 2; public const int INT32_LEN = 4; public const int FLOAT_LEN = 4; public const int LONG_LEN = 8; public const int DOUBLE_LEN = 8; private byte[] data; private int byteIndex = 0; public CByteArray(byte[] bytes = null) { data = (bytes == null) ? new byte[0] : bytes; } /// byte[] 数据原 public byte[] Data { get { return data; } set { data = value; } } /// 可读字节数,修改读取位置可修改此值 public int ByteLength { get { return data.Length - byteIndex; } } /// 总字节数据 public int Length { get { return data.Length; } } /// 当前读取位置 public int Position { get { return byteIndex; } set { byteIndex = value; } } public byte ReadByte() { return this.ReadBytes(byteIndex)[0]; } public bool ReadBool() { byte bt = this.ReadByte(); return (bt == (byte)1) ? true : false; } public short ReadShort() { return BitConverter.ToInt16(this.ReadBytes(SHORT16_LEN), 0); } public int ReadInt() { return BitConverter.ToInt32(this.ReadBytes(INT32_LEN), 0); } public long ReadLong() { return BitConverter.ToInt64(this.ReadBytes(LONG_LEN), 0); } public float ReadFloat() { return BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0); } public double ReadDouble() { return BitConverter.ToDouble(this.ReadBytes(DOUBLE_LEN), 0); } public string ReadString() { return Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort())); } public CByteArray ReadUnit(int leng) { return new CByteArray(this.ReadBytes(leng)); } public byte[] ReadBytes(int leng) { if (leng > this.ByteLength) { throw new Exception("超出字节流长度"); } //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题 byte[] byets = new byte[leng]; for (int i = 0; i < leng; i++) { byets[i] = data[byteIndex]; byteIndex++; } return byets; } public void WriteByte(byte by) { byte[] bytes = new byte[BYTE_LEN]; bytes[0] = by; this.WriteBytes(bytes); } public void WriteBool(bool flag) { byte b = (byte)0; if (flag) b = (byte)1; this.WriteByte(b); } public void WriteShort(short value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteInt(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteLong(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteDouble(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteFloat(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteString(string str) { short len = (short)Encoding.UTF8.GetByteCount(str); this.WriteShort(len); byte[] bytes = new byte[len]; Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0); this.WriteBytes(bytes); } public void WriteUnit(byte[] bytes) { this.WriteInt(bytes.Length); this.WriteBytes(bytes); } public void WriteBytes(byte[] bytes) { byteIndex = data.Length; int leng = bytes.Length; //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题 byte[] newData = new byte[byteIndex + leng]; data.CopyTo(newData, 0); Array.Copy(bytes, 0, newData, byteIndex, leng); data = newData; byteIndex = data.Length; } public void Dispose() { data = null; } /// 压缩字节流 zip压缩 public void Compress() { } /// 解压缩字节流 public void Decompress() { } } }