原地址:http://www.unity蛮牛.com/blog-1801-799.html
 
ByteArrary(优化数据存储和数据流)
分类:unity3D学习篇 评论:1 条 阅读:336 次 2014-6-2 22:58
 
[code]csharpcode:
001 | 
public class ByteArray | 
 
003 | 
    private MemoryStream m_Stream = new MemoryStream(); | 
 
004 | 
    private BinaryReader m_Reader = null; | 
 
005 | 
    private BinaryWriter m_Writer = null; | 
 
012 | 
    public ByteArray(MemoryStream ms) | 
 
018 | 
    public ByteArray(byte[] buffer) | 
 
020 | 
        m_Stream = new MemoryStream(buffer); | 
 
026 | 
        m_Writer = new BinaryWriter(m_Stream); | 
 
027 | 
        m_Reader = new BinaryReader(m_Stream); | 
 
032 | 
        get { return (int)m_Stream.Length; } | 
 
037 | 
        get { return (int)m_Stream.Position; } | 
 
038 | 
        set { m_Stream.Position = value; } | 
 
043 | 
        get { return m_Stream.GetBuffer(); } | 
 
046 | 
    internal MemoryStream MemoryStream { get { return m_Stream; } } | 
 
048 | 
    public bool ReadBoolean() | 
 
050 | 
        return m_Reader.ReadBoolean(); | 
 
053 | 
    public byte ReadByte() | 
 
055 | 
        return m_Reader.ReadByte(); | 
 
058 | 
    public void ReadBytes(byte[] bytes, uint offset, uint length) | 
 
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); | 
 
066 | 
    public double ReadDouble() | 
 
068 | 
        return m_Reader.ReadDouble(); | 
 
071 | 
    public float ReadFloat() | 
 
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++) | 
 
078 | 
            invertedBytes[j] = bytes[i]; | 
 
080 | 
        float value = BitConverter.ToSingle(invertedBytes, 0); | 
 
083 | 
        // return m_Reader.ReadFloat(); | 
 
088 | 
        return m_Reader.ReadInt32(); | 
 
091 | 
    public short ReadShort() | 
 
093 | 
        return m_Reader.ReadInt16(); | 
 
096 | 
    public byte ReadUnsignedByte() | 
 
098 | 
        return m_Reader.ReadByte(); | 
 
101 | 
    public uint ReadUnsignedInt() | 
 
103 | 
        return (uint)m_Reader.ReadInt32(); | 
 
106 | 
    public ushort ReadUnsignedShort() | 
 
108 | 
        return m_Reader.ReadUInt16(); | 
 
111 | 
    public string ReadUTF() | 
 
113 | 
        return m_Reader.ReadString(); | 
 
116 | 
    public string ReadUTFBytes(uint length) | 
 
120 | 
        UTF8Encoding utf8 = new UTF8Encoding(false, true); | 
 
121 | 
        byte[] encodedBytes = m_Reader.ReadBytes((int)length); | 
 
122 | 
        string decodedString = utf8.GetString(encodedBytes, 0, encodedBytes.Length); | 
 
123 | 
        return decodedString; | 
 
126 | 
    public void WriteBoolean(bool value) | 
 
128 | 
        m_Writer.BaseStream.WriteByte(value ? ((byte)1) : ((byte)0)); | 
 
129 | 
        // m_Writer.WriteBoolean(value); | 
 
131 | 
    public void WriteByte(byte value) | 
 
133 | 
        m_Writer.BaseStream.WriteByte(value); | 
 
134 | 
        // m_Writer.WriteByte(value); | 
 
136 | 
    public void WriteBytes(byte[] buffer) | 
 
138 | 
        for (int i = 0; buffer != null && i < buffer.Length; i++) | 
 
139 | 
            m_Writer.BaseStream.WriteByte(buffer[i]); | 
 
141 | 
    public void WriteBytes(byte[] bytes, int offset, int length) | 
 
143 | 
        for (int i = offset; i < offset + length; i++) | 
 
144 | 
            m_Writer.BaseStream.WriteByte(bytes[i]); | 
 
146 | 
    public void WriteDouble(double value) | 
 
148 | 
        byte[] bytes = BitConverter.GetBytes(value); | 
 
149 | 
        WriteBigEndian(bytes); | 
 
151 | 
    public void WriteFloat(float value) | 
 
153 | 
        byte[] bytes = BitConverter.GetBytes(value); | 
 
154 | 
        WriteBigEndian(bytes); | 
 
156 | 
    private void WriteBigEndian(byte[] bytes) | 
 
160 | 
        for (int i = bytes.Length - 1; i >= 0; i--) | 
 
162 | 
            m_Writer.BaseStream.WriteByte(bytes[i]); | 
 
166 | 
    public void WriteInt32(int value) | 
 
168 | 
        byte[] bytes = BitConverter.GetBytes(value); | 
 
169 | 
        WriteBigEndian(bytes); | 
 
172 | 
    public void WriteInt(int value) | 
 
177 | 
    public void WriteShort(int value) | 
 
179 | 
        byte[] bytes = BitConverter.GetBytes((ushort)value); | 
 
180 | 
        WriteBigEndian(bytes); | 
 
183 | 
    public void WriteUnsignedInt(uint value) | 
 
185 | 
        WriteInt32((int)value); | 
 
188 | 
    public void WriteUTF(string value) | 
 
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); | 
 
198 | 
    public void WriteUTFBytes(string value) | 
 
200 | 
        UTF8Encoding utf8Encoding = new UTF8Encoding(); | 
 
201 | 
        byte[] buffer = utf8Encoding.GetBytes(value); | 
 
202 | 
        if (buffer.Length > 0) | 
 
203 | 
            m_Writer.Write(buffer); | 
 
206 | 
    public void WriteStringBytes(string value) | 
 
208 | 
        UTF8Encoding utf8Encoding = new UTF8Encoding(); | 
 
209 | 
        byte[] buffer = utf8Encoding.GetBytes(value); | 
 
210 | 
        if (buffer.Length > 0) | 
 
212 | 
            m_Writer.Write(buffer.Length); | 
 
213 | 
            m_Writer.Write(buffer); | 
 
 
 
 
今天看了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
 
 
 
 
 
 												
												
								- 0809MySQL实战系列:大字段如何优化|数据存储结构
		
转自https://yq.aliyun.com/articles/59256?spm=5176.100239.blogcont59257.9.5MLR2d 摘要: 背景 线上发现一张表,1亿的数据量, ...
		 
						- 移动互联网实战--资源类APP的数据存储处理和优化
		
前言: 对于资源类的APP, 其音频/图形占据了APP本身很大的比例. 如何存储和管理这些资源文件, 成了一个颇具挑战性的难点. 移动端的碎片化, 高中低端手机的并存, 需要开发者不光是具备基础的存储 ...
		 
						- 性能优化之数据存储&DOM编程
		
多读书多看报 数据存储 ·在javascript中,数据存储的位置会对代码整体性能产生重大的影响. ·数据存储共有4种方式:字面量.变量.数组.对象成员.   ·要理解变量的访问速度,就要理解作用域. ...
		 
						- Redis数据存储优化机制(转)
		
原文:Redis学习笔记4--Redis数据存储优化机制 1.zipmap优化hash: 前面谈到将一个对象存储在hash类型中会占用更少的内存,并且可以更方便的存取整个对象.省内存的原因是新建一个h ...
		 
						- TI C6000 数据存储处理与性能优化
		
存储器之于CPU好比仓库之于车间.车间加工过程中的原材料.半成品.成品等均需入出仓库,生产效率再快,如果仓库周转不善,也必然造成生产阻塞.如同仓库需要合理地规划管理一般,数据存储也需要恰当的处理技巧来 ...
		 
						- In-Memory:内存优化数据的持久化和还原
		
数据持久化是还原的前提,没有数据的持久化,就无法还原内存优化表的数据,SQL Server In-Memory OLTP的内存数据能够持久化存储,这意味着内存数据能够在SQL Server实例重启之后 ...
		 
						- LabVIEW(四):数据存储和文件IO
		
1.使用NI数据采集板卡来进行数据保存和文件I/O操作.2.在一个典型的测试测量系统当中,包括:信号调理.信号采集.信号分析.信号显示.数据存储.数据存储:将采集到的数据储存到磁盘上,以备日后离线分析 ...
		 
						- BigData NoSQL —— ApsaraDB HBase数据存储与分析平台概览
		
一.引言 时间到了2019年,数据库也发展到了一个新的拐点,有三个明显的趋势: 越来越多的数据库会做云原生(CloudNative),会不断利用新的硬件及云本身的优势打造CloudNative数据库, ...
		 
						- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
		
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello  ...
		 
		
	
随机推荐
	
									- 让backspace键默认为删除键
			
在/root/.bashrc  中插入一条: stty erase ^H
			 
						- linux系统启动流程
			
BIOS: (Basic Input Output System)基本输入输出系统,一般保存在主板上的BIOS芯片中 BIOS是计算机启动时运行的第一个程序,负责检查硬件并且查找可启动设备. 可启动设 ...
			 
						- HTML5读取本地文件 FileReader   API接口
			
1.FileReader接口的方法 FileReader接口有4个方法,其中3个用来读取文件,另一个用来中断读取.无论读取成功或失败,方法并不会返回读取结果,这一结果存储在result属性中. Fil ...
			 
						- Sublime Text 前端开发常用扩展插件推荐
			
Sublime Text 前端开发常用扩展插件推荐 Sublime Text Sublime Text 是程序员们公认的编码神奇,拥有漂亮的用户界面和强大的功能 更重要的是,Sublime Text  ...
			 
						- How to generate number Sequence[AX 2012]
			
Suppose we want create number sequence for Test field on form in General  ledger module Consideratio ...
			 
						- Python中Cookie的处理(二)cookielib库
			
Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持. 该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并 ...
			 
						- Java从入门到精通——数据库篇之OJDBC版本区别
			
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle   ...
			 
						- Oracle查询出最最近一次的一条记录
			
需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...
			 
						- 无法创建链接服务器 "TEST" 的 OLE DB 访问接口 "OraOLEDB.Oracle" 的实例
			
在使用SQLserver建立远程服务连接Oracle的时候出现先下面错误 出现这个错误,我找到最常见的两个原因 1.注册表 <1>按下WIN+R,打开“运行”窗口,输入“regedit”, ...
			 
						- DC-EPC小结
			
今晚上修完了最后2个学时的EPC(课程主页),这意味着本学期的DC和共20个学时的EPC到此结束,这有可能是我人生中最后一次上英语课. Tom是我DC课的老师,EPC起于Tom和Micheal的Deb ...