在网络通讯中,数据在网络传输的格式必须以字节流的形式进行,因此需要我们对字节流进行写入和读出的操作,下面将会封装两个类,用来将各种类型的数据写入字节流,和从字节流中读取各种类型的数据。如下读取类:NetBufferReaderusing System;
using System.IO;
using System.Text;
namespace Tool {
class NetBufferReader {
MemoryStream m_stream = null;
BinaryReader m_reader = null;
ushort m_dataLength;
public NetBufferReader(byte[] data) {
if(data != null) {
m_stream = new MemoryStream(data);
m_reader = new BinaryReader(m_stream);
m_dataLength = ReadUShort();
}
}
public byte ReadByte() {
return m_reader.ReadByte();
}
public int ReadInt() {
return m_reader.ReadInt32();
}
public uint ReadUInt() {
return m_reader.ReadUInt32();
}
public short ReadShort() {
return m_reader.ReadInt16();
}
public ushort ReadUShort() {
return m_reader.ReadUInt16();
}
public long ReadLong() {
return m_reader.ReadInt64();
}
public ulong ReadULong() {
return m_reader.ReadUInt64();
}
public float ReadFloat() {
byte[] temp = BitConverter.GetBytes(m_reader.ReadSingle());
Array.Reverse(temp);
return BitConverter.ToSingle(temp, 0);
}
public double ReadDouble() {
byte[] temp = BitConverter.GetBytes(m_reader.ReadDouble());
Array.Reverse(temp);
return BitConverter.ToDouble(temp, 0);
}
public string ReadString() {
ushort len = ReadUShort();
byte[] buffer = new byte[len];
buffer = m_reader.ReadBytes(len);
return Encoding.UTF8.GetString(buffer);
}
public byte[] ReadBytes() {
int len = ReadInt();
return m_reader.ReadBytes(len);
}
public void Close() {
if(m_reader != null) {
m_reader.Close();
}
if(m_stream != null) {
m_stream.Close();
}
m_reader = null;
m_stream = null;
}
}
}写入类:NetBufferWriterusing System;
using System.IO;
using System.Text;
namespace Tool {
class NetBufferWriter {
MemoryStream m_stream = null;
BinaryWriter m_writer = null;
int m_finishLength;
public int finishLength {
get { return m_finishLength; }
}
public NetBufferWriter() {
m_finishLength = 0;
m_stream = new MemoryStream();
m_writer = new BinaryWriter(m_stream);
}
public void WriteByte(byte v) {
m_writer.Write(v);
}
public void WriteInt(int v) {
m_writer.Write(v);
}
public void WriteUInt(uint v) {
m_writer.Write(v);
}
public void WriteShort(short v) {
m_writer.Write(v);
}
public void WriteUShort(ushort v) {
m_writer.Write(v);
}
public void WriteLong(long v) {
m_writer.Write(v);
}
public void WriteULong(ulong v) {
m_writer.Write(v);
}
public void WriteFloat(float v) {
byte[] temp = BitConverter.GetBytes(v);
Array.Reverse(temp);
m_writer.Write(BitConverter.ToSingle(temp, 0));
}
public void WriteDouble(double v) {
byte[] temp = BitConverter.GetBytes(v);
Array.Reverse(temp);
m_writer.Write(BitConverter.ToDouble(temp, 0));
}
public void WriteString(string v) {
byte[] bytes = Encoding.UTF8.GetBytes(v);
m_writer.Write((ushort)bytes.Length);
m_writer.Write(bytes);
}
public void WriteBytes(byte[] v) {
m_writer.Write(v.Length);
m_writer.Write(v);
}
public byte[] ToBytes() {
m_writer.Flush();
return m_stream.ToArray();
}
public void Close() {
m_writer.Close();
m_stream.Close();
m_writer = null;
m_stream = null;
}
/// <summary>
/// 将已写入的数据流,封装成一个新的数据流(现有数据长度+现有数据)
/// 数据转换,网络发送需要两部分数据,一是数据长度,二是主体数据
/// </summary>
public byte[] Finish() {
byte[] message = ToBytes();
MemoryStream ms = new MemoryStream();
ms.Position = 0;
BinaryWriter writer = new BinaryWriter(ms);
writer.Write((ushort)message.Length);
writer.Write(message);
writer.Flush();
byte[] result = ms.ToArray();
m_finishLength = result.Length;
return result;
}
}
}
---------------------
本文来自 王王王渣渣 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/wangjiangrong/article/details/80844392?utm_source=copy