1 Star 2 Fork 0

leon86/LSocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
NetStream.cs 3.64 KB
一键复制 编辑 原始数据 按行查看 历史
leon86 提交于 2018-12-06 19:26 . my first commit
using System;
using System.IO;
using System.Net;
namespace Socket2
{
public class NetStream
{
private readonly MemoryStream stream;
private readonly BinaryReader reader;
private readonly BinaryWriter writer;
public NetStream(byte[] buffer = null)
{
if (buffer == null)
{
this.stream = new MemoryStream();
}
else
{
this.stream = new MemoryStream(buffer);
}
this.reader = new BinaryReader(this.stream);
this.writer = new BinaryWriter(this.stream);
}
public void Close()
{
this.stream.Close();
this.reader.Close();
this.writer.Close();
}
public long ReadInt64()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());
}
public int ReadInt32()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());
}
public short ReadInt16()
{
return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());
}
public byte ReadByte()
{
return this.reader.ReadByte();
}
public byte[] ReadByte(int Len)
{
return this.reader.ReadBytes(Len);
}
public string ReadString8()
{
return System.Text.Encoding.UTF8.GetString
(
this.reader.ReadBytes(ReadByte())
);
}
public string ReadString16()
{
return System.Text.Encoding.UTF8.GetString
(
this.reader.ReadBytes(ReadInt16())
);
}
public long Seek(long offset)
{
return this.stream.Seek(offset, SeekOrigin.Begin);
}
// -------------------------------------------------------------------------------
public void WriteByte(byte value)
{
this.writer.Write(value);
}
public void WriteByte(byte[] value)
{
this.writer.Write(value);
}
public void WriteInt16(short value)
{
this.writer.Write
(
BitConverter.GetBytes
(
IPAddress.HostToNetworkOrder(value)
)
);
}
public void WriteInt32(int value)
{
this.writer.Write
(
BitConverter.GetBytes
(
IPAddress.HostToNetworkOrder(value)
)
);
}
public void WriteInt64(long value)
{
this.writer.Write
(
BitConverter.GetBytes
(
IPAddress.HostToNetworkOrder(value)
)
);
}
public void WriteString8(string value)
{
WriteByte
(
(byte)value.Length
);
this.writer.Write
(
System.Text.Encoding.UTF8.GetBytes(value)
);
}
public void WriteString16(string value)
{
WriteInt16
(
(short)value.Length
);
this.writer.Write
(
System.Text.Encoding.UTF8.GetBytes(value)
);
}
public byte[] ToArray()
{
return this.stream.ToArray();
}
public int GetLength()
{
return (int)this.stream.Length;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/leon86/LSocket.git
git@gitee.com:leon86/LSocket.git
leon86
LSocket
LSocket
master

搜索帮助