Unity封装MemoryStream进行内存读写
MMO_MemoryStream类
using System;
using System.IO;
using System.Text;
public class MMO_MemoryStream : MemoryStream
{
public MMO_MemoryStream()
{
}
public MMO_MemoryStream(byte[] buffer) :base(buffer)
{
}
#region Short
public short ReadShort()
{
byte[] arr = new byte[2];
base.Read(arr, 0, arr.Length);
return BitConverter.ToInt16(arr, 0);
}
public void WriteShort(short value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr,0,arr.Length);
}
#endregion
#region UShort
public ushort ReadUShort()
{
byte[] arr = new byte[2];
base.Read(arr, 0, arr.Length);
return BitConverter.ToUInt16(arr, 0);
}
public void WriteUShort(ushort value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region Int
public int ReadInt()
{
byte[] arr = new byte[4];
base.Read(arr, 0, arr.Length);
return BitConverter.ToInt32(arr, 0);
}
public void WriteInt(int value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region UInt
public uint ReadUInt()
{
byte[] arr = new byte[4];
base.Read(arr, 0, arr.Length);
return BitConverter.ToUInt32(arr, 0);
}
public void WriteUInt(uint value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region Float
public float ReadFloat()
{
byte[] arr = new byte[4];
base.Read(arr, 0, arr.Length);
return BitConverter.ToSingle(arr, 0);
}
public void WriteFloat(float value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region Double
public double ReadDouble()
{
byte[] arr = new byte[8];
base.Read(arr, 0, arr.Length);
return BitConverter.ToDouble(arr, 0);
}
public void WriteDouble(double value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region Bool
public bool ReadBool()
{
return base.ReadByte() == 1;
}
public void WriteBool(bool value)
{
base.WriteByte((byte)(value == true ? 1 : 0));
}
#endregion
#region String
public string ReadString()
{
ushort len = this.ReadUShort();
byte[] arr = new byte[len];
base.Read(arr, 0, len);
return Encoding.UTF8.GetString(arr, 0, len);
}
public void WriteString(string value)
{
byte[] arr = Encoding.UTF8.GetBytes(value);
if (arr.Length> 65535)
{
throw new Exception("长度超出范围");
}
this.WriteUShort((ushort)value.Length);
base.Write(arr, 0, arr.Length);
}
#endregion
}
Test测试
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
Item item = new Item();
item.age = 18;
item.name = "zzs";
item.score = 100;
byte[] arr = null;
using (MMO_MemoryStream ms = new MMO_MemoryStream())
{
ms.WriteInt(item.age);
ms.WriteString(item.name);
ms.WriteInt(item.score);
arr = ms.ToArray();
}
for (int i = 0; i < arr.Length; i++)
{
Debug.Log(string.Format("{0}:{1}", "arr" + i, arr[i]));
}
Item item2 = new Item();
using (MMO_MemoryStream ms = new MMO_MemoryStream(arr))
{
item2.age = ms.ReadInt();
item2.name = ms.ReadString();
item2.score = ms.ReadInt();
}
Debug.Log(item2.age);
Debug.Log(item2.name);
Debug.Log(item2.score);
}
}
public class Item
{
public int age;
public string name;
public int score;
}