我的服务器-定义字节流操作类

        对于网络通信有所了解的都应该知道,数据在网络传输的格式必须以字节流的形式进行,那么免不了要对字节流进行写入和读出操作,为了方便后面的操作,我们有必要封装一个读写字节流的操作类,在这里我定义了一个字节流的操作类ByteBuffer类,用于将各个类型数据写入流中,也可从字节流中读取各种类型的数据:

using System.IO;  
using System.Text;  
using System;  
  
namespace Net {  
    public class ByteBuffer {  
        MemoryStream stream = null;  
        BinaryWriter writer = null;  
        BinaryReader reader = null;  
  
        public ByteBuffer() {  
            stream = new MemoryStream();  
            writer = new BinaryWriter(stream);  
        }  
  
        public ByteBuffer(byte[] data) {  
            if (data != null) {  
                stream = new MemoryStream(data);  
                reader = new BinaryReader(stream);  
            } else {  
                stream = new MemoryStream();  
                writer = new BinaryWriter(stream);  
            }  
        }  
  
        public void Close() {  
            if (writer != null) writer.Close();  
            if (reader != null) reader.Close();  
  
            stream.Close();  
            writer = null;  
            reader = null;  
            stream = null;  
        }  
  
        public void WriteByte(byte v) {  
            writer.Write(v);  
        }  
  
        public void WriteInt(int v) {  
            writer.Write((int)v);  
        }  
  
        public void WriteShort(ushort v) {  
            writer.Write((ushort)v);  
        }  
  
        public void WriteLong(long v) {  
            writer.Write((long)v);  
        }  
  
        public void WriteFloat(float v) {  
            byte[] temp = BitConverter.GetBytes(v);  
            Array.Reverse(temp);  
            writer.Write(BitConverter.ToSingle(temp, 0));  
        }  
  
        public void WriteDouble(double v) {  
            byte[] temp = BitConverter.GetBytes(v);  
            Array.Reverse(temp);  
            writer.Write(BitConverter.ToDouble(temp, 0));  
        }  
  
        public void WriteString(string v) {  
            byte[] bytes = Encoding.UTF8.GetBytes(v);  
            writer.Write((ushort)bytes.Length);  
            writer.Write(bytes);  
        }  
  
        public void WriteBytes(byte[] v) {  
            writer.Write((int)v.Length);  
            writer.Write(v);  
        }  
  
        public byte ReadByte() {  
            return reader.ReadByte();  
        }  
  
        public int ReadInt() {  
            return (int)reader.ReadInt32();  
        }  
  
        public ushort ReadShort() {  
            return (ushort)reader.ReadInt16();  
        }  
  
        public long ReadLong() {  
            return (long)reader.ReadInt64();  
        }  
  
        public float ReadFloat() {  
            byte[] temp = BitConverter.GetBytes(reader.ReadSingle());  
            Array.Reverse(temp);  
            return BitConverter.ToSingle(temp, 0);  
        }  
  
        public double ReadDouble() {  
            byte[] temp = BitConverter.GetBytes(reader.ReadDouble());  
            Array.Reverse(temp);  
            return BitConverter.ToDouble(temp, 0);  
        }  
  
        public string ReadString() {  
            ushort len = ReadShort();  
            byte[] buffer = new byte[len];  
            buffer = reader.ReadBytes(len);  
            return Encoding.UTF8.GetString(buffer);  
        }  
  
        public byte[] ReadBytes() {  
            int len = ReadInt();  
            return reader.ReadBytes(len);  
        }  
  
        public byte[] ToBytes() {  
            writer.Flush();  
            return stream.ToArray();  
        }  
  
        public void Flush() {  
            writer.Flush();  
        }  
    }  
}  

使用此操作类进行读写数据的操作范例如下:

  • 读取数据:
//result是字节数组byte[],从中读取两个int类型的数据  
 ByteBuffer buff = new ByteBuffer(result);  
 int len = buff.ReadShort();  
 int protoId = buff.ReadShort();  

 

  • 写入数据:
//result是字节数组byte[],从写入两个不同类型的数据  
ByteBuffer buff = new ByteBuffer();  
int protoId = ProtoDic.GetProtoIdByProtoType(0);  
buff.WriteShort((ushort)protoId);  
buff.WriteBytes(ms.ToArray());  
byte[] result = buff.ToBytes();  

 

转载于:https://my.oschina.net/u/3184885/blog/1486716

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值