ByteArrary(优化数据存储和数据流)

原地址:http://www.unity蛮牛.com/blog-1801-799.html

 

 

ByteArrary(优化数据存储和数据流)

分类:unity3D学习篇 评论:1 条 阅读:336 次 2014-6-2 22:58

[code]csharpcode:

001 public class ByteArray
002 {
003     private MemoryStream m_Stream = new MemoryStream();
004     private BinaryReader m_Reader = null;
005     private BinaryWriter m_Writer = null;
006  
007     public ByteArray()
008     {
009         Init();
010     }
011  
012     public ByteArray(MemoryStream ms)
013     {
014         m_Stream = ms;
015         Init();
016     }
017  
018     public ByteArray(byte[] buffer)
019     {
020         m_Stream = new MemoryStream(buffer);
021         Init();
022     }
023  
024     private void Init()
025     {
026         m_Writer = new BinaryWriter(m_Stream);
027         m_Reader = new BinaryReader(m_Stream);
028     }
029  
030     public int Length
031     {
032         get return (int)m_Stream.Length; }
033     }
034  
035     public int Postion
036     {
037         get return (int)m_Stream.Position; }
038         set { m_Stream.Position = value; }
039     }
040  
041     public byte[] Buffer
042     {
043         get return m_Stream.GetBuffer(); }
044     }
045  
046     internal MemoryStream MemoryStream { get return m_Stream; } }
047  
048     public bool ReadBoolean()
049     {
050         return m_Reader.ReadBoolean();
051     }
052  
053     public byte ReadByte()
054     {
055         return m_Reader.ReadByte();
056     }
057  
058     public void ReadBytes(byte[] bytes, uint offset, uint length)
059     {
060         byte[] tmp = m_Reader.ReadBytes((int)length);
061         for (int i = 0; i < tmp.Length; i++)
062             bytes[i + offset] = tmp[i];
063         //m_Reader.ReadBytes(bytes, offset, length);
064     }
065  
066     public double ReadDouble()
067     {
068         return m_Reader.ReadDouble();
069     }
070  
071     public float ReadFloat()
072     {
073         byte[] bytes = m_Reader.ReadBytes(4);
074         byte[] invertedBytes = new byte[4];
075         //Grab the bytes in reverse order from the backwards index
076         for (int i = 3, j = 0; i >= 0; i--, j++)
077         {
078             invertedBytes[j] = bytes[i];
079         }
080         float value = BitConverter.ToSingle(invertedBytes, 0);
081         return value;
082  
083         // return m_Reader.ReadFloat();
084     }
085  
086     public int ReadInt()
087     {
088         return m_Reader.ReadInt32();
089     }
090  
091     public short ReadShort()
092     {
093         return m_Reader.ReadInt16();
094     }
095  
096     public byte ReadUnsignedByte()
097     {
098         return m_Reader.ReadByte();
099     }
100  
101     public uint ReadUnsignedInt()
102     {
103         return (uint)m_Reader.ReadInt32();
104     }
105  
106     public ushort ReadUnsignedShort()
107     {
108         return m_Reader.ReadUInt16();
109     }
110  
111     public string ReadUTF()
112     {
113         return m_Reader.ReadString();
114     }
115  
116     public string ReadUTFBytes(uint length)
117     {
118         if (length == 0)
119             return string.Empty;
120         UTF8Encoding utf8 = new UTF8Encoding(falsetrue);
121         byte[] encodedBytes = m_Reader.ReadBytes((int)length);
122         string decodedString = utf8.GetString(encodedBytes, 0, encodedBytes.Length);
123         return decodedString;
124     }
125  
126     public void WriteBoolean(bool value)
127     {
128         m_Writer.BaseStream.WriteByte(value ? ((byte)1) : ((byte)0));
129         // m_Writer.WriteBoolean(value);
130     }
131     public void WriteByte(byte value)
132     {
133         m_Writer.BaseStream.WriteByte(value);
134         // m_Writer.WriteByte(value);
135     }
136     public void WriteBytes(byte[] buffer)
137     {
138         for (int i = 0; buffer != null && i < buffer.Length; i++)
139             m_Writer.BaseStream.WriteByte(buffer[i]);
140     }
141     public void WriteBytes(byte[] bytes, int offset, int length)
142     {
143         for (int i = offset; i < offset + length; i++)
144             m_Writer.BaseStream.WriteByte(bytes[i]);
145     }
146     public void WriteDouble(double value)
147     {
148         byte[] bytes = BitConverter.GetBytes(value);
149         WriteBigEndian(bytes);
150     }
151     public void WriteFloat(float value)
152     {
153         byte[] bytes = BitConverter.GetBytes(value);
154         WriteBigEndian(bytes);
155     }
156     private void WriteBigEndian(byte[] bytes)
157     {
158         if (bytes == null)
159             return;
160         for (int i = bytes.Length - 1; i >= 0; i--)
161         {
162             m_Writer.BaseStream.WriteByte(bytes[i]);
163         }
164     }
165  
166     public void WriteInt32(int value)
167     {
168         byte[] bytes = BitConverter.GetBytes(value);
169         WriteBigEndian(bytes);
170     }
171  
172     public void WriteInt(int value)
173     {
174         WriteInt32(value);
175     }
176  
177     public void WriteShort(int value)
178     {
179         byte[] bytes = BitConverter.GetBytes((ushort)value);
180         WriteBigEndian(bytes);
181     }
182  
183     public void WriteUnsignedInt(uint value)
184     {
185         WriteInt32((int)value);
186     }
187  
188     public void WriteUTF(string value)
189     {
190         UTF8Encoding utf8Encoding = new UTF8Encoding();
191         int byteCount = utf8Encoding.GetByteCount(value);
192         byte[] buffer = utf8Encoding.GetBytes(value);
193         WriteShort(byteCount);
194         if (buffer.Length > 0)
195             m_Writer.Write(buffer);
196     }
197  
198     public void WriteUTFBytes(string value)
199     {
200         UTF8Encoding utf8Encoding = new UTF8Encoding();
201         byte[] buffer = utf8Encoding.GetBytes(value);
202         if (buffer.Length > 0)
203             m_Writer.Write(buffer);
204     }
205  
206     public void WriteStringBytes(string value)
207     {
208         UTF8Encoding utf8Encoding = new UTF8Encoding();
209         byte[] buffer = utf8Encoding.GetBytes(value);
210         if (buffer.Length > 0)
211         {
212             m_Writer.Write(buffer.Length);
213             m_Writer.Write(buffer);
214         }
215     }
216 }

今天看了A神的那个RPG教程,里面有一个类是ByteArray,在网上找了应该是个累死优化数据存储和数据流的一个类 ,在网上找了一下前辈们写的教程拿到这里与大家分享一下
 
 
ByteArray() 
创建一个表示填充的字节数组的 ByteArray 实例,以便使用此类中的方法和属性来优化数据存储和数据流。 ByteArray 
compress(algorithm:String):void 
压缩字节数组。 ByteArray 
hasOwnProperty(name:String):Boolean 
指示对象是否已经定义了指定的属性。 Object 
isPrototypeOf(theClass:Object):Boolean 
指示 Object 类的实例是否在指定为参数的对象的原型链中。 Object 
propertyIsEnumerable(name:String):Boolean 
指示指定的属性是否存在、是否可枚举。 Object 
readBoolean():Boolean 
从字节流中读取布尔值。 ByteArray 
readByte():int 
从字节流中读取带符号的字节。 ByteArray 
readBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 
从字节流中读取 length 参数指定的数据字节数。 ByteArray 
readDouble():Number 
从字节流中读取一个 IEEE 754 双精度(64 位)浮点数。 ByteArray 
readFloat():Number 
从字节流中读取一个 IEEE 754 单精度(32 位)浮点数。 ByteArray 
readInt():int 
从字节流中读取一个带符号的 32 位整数。 ByteArray 
readMultiByte(length:uint, charSet:String):String 
使用指定的字符集从字节流中读取指定长度的多字节字符串。 ByteArray 
readObject():* 
从字节数组中读取一个以 AMF 序列化格式进行编码的对象。 ByteArray 
readShort():int 
从字节流中读取一个带符号的 16 位整数。 ByteArray 
readUnsignedByte():uint 
从字节流中读取无符号的字节。 ByteArray 
readUnsignedInt():uint 
从字节流中读取一个无符号的 32 位整数。 ByteArray 
readUnsignedShort():uint 
从字节流中读取一个无符号的 16 位整数。 ByteArray 
readUTF():String 
从字节流中读取一个 UTF-8 字符串。 ByteArray 
readUTFBytes(length:uint):String 
从字节流中读取一个由 length 参数指定的 UTF-8 字节序列,并返回一个字符串。 ByteArray 
setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void 
设置循环操作动态属性的可用性。 Object 
toString():String 
将字节数组转换为字符串。 ByteArray 
uncompress(algorithm:String):void 
解压缩字节数组。 ByteArray 
valueOf():Object 
返回指定对象的原始值。 Object 
writeBoolean(value:Boolean):void 
写入布尔值。 ByteArray 
writeByte(value:int):void 
在字节流中写入一个字节。 ByteArray 
writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 
将指定字节数组 bytes(起始偏移量为 bytes,从 0 开始的索引)中包含 length 个字节的字节序列写入字节流。 ByteArray 
writeDouble(value:Number):void 
在字节流中写入一个 IEEE 754 双精度(64 位)浮点数。 ByteArray 
writeFloat(value:Number):void 
在字节流中写入一个 IEEE 754 单精度(32 位)浮点数。 ByteArray 
writeInt(value:int):void 
在字节流中写入一个带符号的 32 位整数。 ByteArray 
writeMultiByte(value:String, charSet:String):void 
使用指定的字符集将多字节字符串写入字节流。 ByteArray 
writeObject(object:*):void 
将对象以 AMF 序列化格式写入字节数组。 ByteArray 
writeShort(value:int):void 
在字节流中写入一个 16 位整数。 ByteArray 
writeUnsignedInt(value:uint):void 
在字节流中写入一个无符号的 32 位整数。 ByteArray 
writeUTF(value:String):void 
将 UTF-8 字符串写入字节流。 ByteArray 
writeUTFBytes(value:String):void 
将 UTF-8 字符串写入字节流。 ByteArray

 

posted @ 2014-08-14 08:03  ing...  阅读(693)  评论(0编辑  收藏  举报