C# 二进制字节流读写封装

 
  1. using System.IO;

  2. using System.Net;

  3. using System;

  4.  
  5. namespace Framework

  6. {

  7. public class NetStream

  8. {

  9. private MemoryStream stream;

  10. private BinaryReader reader;

  11. private BinaryWriter writer;

  12.  
  13. public NetStream(byte[] buffer = null)

  14. {

  15. if (buffer == null)

  16. {

  17. this.stream = new MemoryStream();

  18. }

  19. else

  20. {

  21. this.stream = new MemoryStream(buffer);

  22. }

  23.  
  24. this.reader = new BinaryReader(this.stream);

  25. this.writer = new BinaryWriter(this.stream);

  26. }

  27.  
  28. public void Close()

  29. {

  30. this.stream.Close();

  31. this.reader.Close();

  32. this.writer.Close();

  33. }

  34.  
  35. //-------------------------------------------------------------------------------

  36.  
  37. public long ReadInt64()

  38. {

  39. return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());

  40. }

  41.  
  42. public int ReadInt32()

  43. {

  44. return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());

  45. }

  46.  
  47. public short ReadInt16()

  48. {

  49. return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());

  50. }

  51.  
  52. public byte ReadByte()

  53. {

  54. return this.reader.ReadByte();

  55. }

  56.  
  57. public float ReadFloat()

  58. {

  59. return this.reader.ReadSingle();

  60. }

  61.  
  62. public string ReadString8()

  63. {

  64. return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadByte()));

  65. }

  66.  
  67. public string ReadString16()

  68. {

  69. return System.Text.Encoding.UTF8.GetString(this.reader.ReadBytes(ReadInt16()));

  70. }

  71.  
  72. public long Seek(long offset)

  73. {

  74. return this.stream.Seek(offset, SeekOrigin.Begin);

  75. }

  76.  
  77. //-------------------------------------------------------------------------------

  78.  
  79. public void WriteByte(byte value)

  80. {

  81. this.writer.Write(value);

  82. }

  83.  
  84. public void WriteInt16(short value)

  85. {

  86. this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));

  87. }

  88.  
  89. public void WriteInt32(int value)

  90. {

  91. this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));

  92. }

  93.  
  94. public void WriteInt64(long value)

  95. {

  96. this.writer.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));

  97. }

  98.  
  99. public void WriteFloat(float value)

  100. {

  101. this.writer.Write(value);

  102. }

  103.  
  104. public void WriteString8(string value)

  105. {

  106. byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value);

  107.  
  108. WriteByte((byte) byteArray.Length);

  109.  
  110. this.writer.Write(byteArray);

  111. }

  112.  
  113. public void WriteString16(string value)

  114. {

  115. byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(value);

  116.  
  117. WriteInt16((short) byteArray.Length);

  118.  
  119. this.writer.Write(byteArray);

  120. }

  121.  
  122. public byte[] GetBuffer()

  123. {

  124. return this.stream.ToArray();

  125. }

  126.  
  127. public int GetLength()

  128. {

  129. return (int) this.stream.Length;

  130. }

  131. }

  132. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值