123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.IO.Compression;
- namespace Engine.CLoader
- {
- /// <summary>配置文件字节流解析对象</summary>
- 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;
- }
- /// <summary>byte[] 数据原</summary>
- public byte[] Data
- {
- get { return data; }
- set { data = value; }
- }
- /// <summary>可读字节数,修改读取位置可修改此值</summary>
- public int ByteLength
- {
- get { return data.Length - byteIndex; }
- }
- /// <summary>总字节数据</summary>
- public int Length
- {
- get { return data.Length; }
- }
- /// <summary>当前读取位置</summary>
- 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;
- }
- /// <summary>压缩字节流 zip压缩</summary>
- public void Compress()
- {
- }
- /// <summary>解压缩字节流</summary>
- public void Decompress()
- {
- }
- }
- }
|