您的位置:首页 > 移动开发 > Unity3D

在Unity3D中用C#封装一个类似AS3的ByteArray类(二)--升级版

2017-06-21 09:48 274 查看
大家好,又见面了。上篇写到了在unity3d用使用C#实现一个AS3版本的ByteArray。

没有实现大小序功能。后来在项目中使用,觉得还是有必要加上ENDIAN顺序。

查阅得知,flash as3的bytearray默认的ENDIAN是:BIG_ENDIAN。

但是对于机器码而言,有时候LITTLE_ENDIAN更合适。既然如此,我们在原来的代码上进行修改,添加支持。

注意:默认为BIG_ENDIAN,和flash as3中的默认值是一样的。

下面献上unity3d可以直接使用的C#版本的ByteArray类。在项目中实测运行良好。

public class ByteArray
{
private MemoryStream _memoryStream;
private const byte BooleanFalse = 2;
private const byte BooleanTrue = 3;
public string endian;

//字节顺序(AS3默认是BIG_ENDIAN)
public static string BIG_ENDIAN = "BIG_ENDIAN";
public static string LITTLE_ENDIAN = "LITTLE_ENDIAN";

public ByteArray ()
{
_memoryStream = new MemoryStream ();
endian = ByteArray.BIG_ENDIAN;
}

public ByteArray (MemoryStream ms)
{
_memoryStream = ms;
}

public ByteArray (byte[] buffer)
{
_memoryStream = new MemoryStream ();
_memoryStream.Write (buffer, 0, buffer.Length);
_memoryStream.Position = 0;
}

public void dispose ()
{
if (_memoryStream != null) {
_memoryStream.Close ();
_memoryStream.Dispose ();
}
_memoryStream = null;
}

public uint Length {
get {
return (uint)_memoryStream.Length;
}
}

public uint Position {
get { return (uint)_memoryStream.Position; }
set{ _memoryStream.Position = value; }
}

public uint BytesAvailable {
get { return Length - Position; }
}

public byte[] GetBuffer ()
{
return _memoryStream.GetBuffer ();
}

public byte[] ToArray ()
{
return _memoryStream.ToArray ();
}

public MemoryStream MemoryStream {
get {
return _memoryStream;
}
}

public bool ReadBoolean ()
{
return _memoryStream.ReadByte () == BooleanTrue;
}

public byte ReadByte ()
{
return (byte)_memoryStream.ReadByte ();
}

public void ReadBytes (byte[] bytes, uint offset, uint length)
{
_memoryStream.Read (bytes, (int)offset, (int)length);
}

public void ReadBytes (ByteArray bytes, uint offset, uint length)
{
uint tmp = bytes.Position;
int count = (int)(length != 0 ? length : BytesAvailable);
for (int i = 0; i < count; i++) {
bytes._memoryStream.Position = i + offset;
bytes._memoryStream.WriteByte (ReadByte ());
}
bytes.Position = tmp;
}

private byte[] priReadBytes (uint c)
{
byte[] a = new byte[c];
if (endian == ByteArray.BIG_ENDIAN) {
//BIG_ENDIAN
for (uint i = 0; i < c; i++) {
a [c - 1 - i] = (byte)_memoryStream.ReadByte ();
}
} else {
//LITTLE_ENDIAN
for (uint i = 0; i < c; i++) {
a [i] = (byte)_memoryStream.ReadByte ();
}
}
return a;
}

public double ReadDouble ()
{
byte[] bytes = priReadBytes (8);
double value = System.BitConverter.ToDouble (bytes, 0);
return value;
}

public float ReadFloat ()
{
byte[] bytes = priReadBytes (4);
float value = System.BitConverter.ToSingle (bytes, 0);
return value;
}

public int ReadInt ()
{
byte[] bytes = priReadBytes (4);
int value = System.BitConverter.ToInt32 (bytes, 0);
return value;
}

public short ReadShort ()
{
byte[] bytes = priReadBytes (2);
short value = System.BitConverter.ToInt16 (bytes, 0);
return value;
}

public string ReadUTF ()
{
uint length = (uint)ReadShort ();
return ReadUTFBytes (length);
}

public string ReadUTFBytes (uint length)
{
if (length == 0)
return string.Empty;
UTF8Encoding utf8 = new UTF8Encoding (false, true);
byte[] encodedBytes = new byte[length];
for (uint i = 0; i < length; i++) {
encodedBytes [i] = (byte)_memoryStream.ReadByte ();
}
string decodedString = utf8.GetString (encodedBytes, 0, encodedBytes.Length);
return decodedString;
}

//=========================================

public void WriteBoolean (bool value)
{
WriteByte ((byte)(value ? BooleanTrue : BooleanFalse));
}

public void WriteByte (byte value)
{
_memoryStream.WriteByte (value);
}

public void WriteBytes (byte[] bytes, int offset, int length)
{
for (int i = offset; i < offset + length; i++) {
if (i < bytes.Length) {
_memoryStream.WriteByte (bytes [i]);
} else {
break;
}
}
}

public void WriteBytes (ByteArray bytes, int offset, int length)
{
byte[] data = bytes.ToArray ();
WriteBytes (data, offset, length);
}

public void WriteDouble (double value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

private void WriteBigEndian (byte[] bytes)
{
if (bytes == null)
return;
if (endian == ByteArray.BIG_ENDIAN) {
//BIG_ENDIAN
for (int i = bytes.Length - 1; i >= 0; i--) {
WriteByte (bytes [i]);
}
} else {
//LITTLE_ENDIAN;
for (int i = 0; i < bytes.Length; i++) {
_memoryStream.WriteByte (bytes [i]);
}
}
}

public void WriteFloat (float value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

public void WriteInt (int value)
{
WriteInt32 (value);
}

private void WriteInt32 (int value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

public void WriteShort (short value)
{
byte[] bytes = System.BitConverter.GetBytes ((ushort)value);
WriteBigEndian (bytes);
}

public void WriteUTF (string value)
{
UTF8Encoding utf8Encoding = new UTF8Encoding ();
int byteCount = utf8Encoding.GetByteCount (value);
byte[] buffer = utf8Encoding.GetBytes (value);
this.WriteShort ((short)byteCount);
if (buffer != null && buffer.Length > 0) {
this.WriteBytes (buffer, 0, buffer.Length);
}
}
}
如果此代码对您有用,或者发现有任何问题,请给我留言,以便改进!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ByteArray as3 c# unity3d as