using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Text;
/// <summary>
/// 数据转换.
/// </summary>
public class MemoryStreamTool:MemoryStream{
public MemoryStreamTool(byte[] buffer) : base(buffer)
{
}
public MemoryStreamTool() { }
#region int
/// <summary>
/// 从流中读取一个int数据.
/// </summary>
/// <returns></returns>
public int ReadInt()
{
byte[] arr = new byte[4];
base.Read(arr,0,arr.Length);
return BitConverter.ToInt32(arr,0);
}
/// <summary>
/// 把一个int数组写入流.
/// </summary>
/// <param name="value"></param>
public void WriteInt(int value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr,0,arr.Length);
}
#endregion
#region uint
/// <summary>
/// 从流中读取一个uint数据.
/// </summary>
/// <returns></returns>
public uint ReaduInt()
{
byte[] arr = new byte[4];
base.Read(arr, 0, arr.Length);
return BitConverter.ToUInt32(arr,0);
}
/// <summary>
/// 把一个uint数组写入流.
/// </summary>
/// <param name="value"></param>
public void WriteuInt(uint value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region short
/// <summary>
/// 从流中读取一个short数据
/// </summary>
/// <returns></returns>
public short ReadShort()
{
byte[] arr = new byte[2];
base.Read(arr,0,arr.Length);
return BitConverter.ToInt16(arr,0);
}
/// <summary>
/// 把一个short数据写入流中.
/// </summary>
/// <param name="value"></param>
public void WriteShort(short value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr,0,arr.Length);
}
#endregion
#region ushort
/// <summary>
/// 从流中读取一个ushort数据
/// </summary>
/// <returns></returns>
public ushort ReaduShort()
{
byte[] arr = new byte[2];
base.Read(arr, 0, arr.Length);
return BitConverter.ToUInt16(arr, 0);
}
/// <summary>
/// 把一个short数据写入流中.
/// </summary>
/// <param name="value"></param>
public void WriteuShort(ushort value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region long
/// <summary>
/// 从流中读取一个long数据.
/// </summary>
/// <returns></returns>
public long ReadLong()
{
byte[] arr = new byte[8];
base.Read(arr,0,arr.Length);
return BitConverter.ToInt64(arr,0);
}
/// <summary>
/// 把一个long数据写入流.
/// </summary>
/// <param name="value"></param>
public void WriteLong(long value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr,0,arr.Length);
}
#endregion
#region ulong
/// <summary>
/// 从流中读取一个ulong数据.
/// </summary>
/// <returns></returns>
public ulong ReaduLong()
{
byte[] arr = new byte[8];
base.Read(arr, 0, arr.Length);
return BitConverter.ToUInt64(arr, 0);
}
/// <summary>
/// 把一个ulong数据写入流.
/// </summary>
/// <param name="value"></param>
public void WriteuLong(ulong value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region double
/// <summary>
/// 从流中读取一个double数据.
/// </summary>
/// <returns></returns>
public double ReadDouble()
{
byte[] arr = new byte[8];
base.Read(arr,0,arr.Length);
return BitConverter.ToDouble(arr,0);
}
/// <summary>
/// 把一个double数据写入流中.
/// </summary>
/// <param name="value"></param>
public void WriteDouble(double value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr,0,arr.Length);
}
#endregion
#region float
/// <summary>
/// 从流中读取一个float数据.
/// </summary>
/// <returns></returns>
public float ReadFloat()
{
byte[] arr = new byte[4];
base.Read(arr, 0, arr.Length);
return BitConverter.ToSingle(arr, 0);
}
/// <summary>
/// 把一个float数组写入流.
/// </summary>
/// <param name="value"></param>
public void WriteFloat(float value)
{
byte[] arr = BitConverter.GetBytes(value);
base.Write(arr, 0, arr.Length);
}
#endregion
#region bool
/// <summary>
/// 从流中读取一个bool数据.
/// </summary>
/// <returns></returns>
public bool ReadBool()
{
return base.ReadByte() == 1;
}
/// <summary>
/// 把一个bool数据写入流.
/// </summary>
/// <param name="value"></param>
public void WriteBool(bool value)
{
base.WriteByte((byte)(value==true?1:0));
}
#endregion
#region string
/// <summary>
/// 从流中读取一个string数组.
/// </summary>
/// <returns></returns>
public string ReadUTF8String()
{
ushort len = this.ReaduShort();
byte[] arr = new byte[len];
base.Read(arr,0,len);
return Encoding.UTF8.GetString(arr);
}
/// <summary>
/// 把一个string数据写入流.
/// </summary>
/// <param name="str"></param>
public void WriteUTF8String(string str)
{
byte[] arr = Encoding.UTF8.GetBytes(str);
if (arr.Length > 65535)
{
throw new InvalidCastException("字符串超出范围.");
}
WriteuShort((ushort)arr.Length);
base.Write(arr,0,arr.Length);
}
#endregion
}
测试后得到以下结果:
int在内存中占用4个字节
double在内存中占用8个字节
long在内存中占用8个字节
float在内存中占用4个字节
short在内存中占用2个字节
bool在内存中占用1个字节
此文章只是为了记录一下学习笔记,如有不足还望多多指点!