|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace Native.Csharp.Sdk.Cqp.Other |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// <see cref="BinaryReader"/> 类的扩展方法集 |
| 11 | + /// </summary> |
| 12 | + public static class BinaryReaderExpand |
| 13 | + { |
| 14 | + #region --公开方法-- |
| 15 | + /// <summary> |
| 16 | + /// 获取基础流的剩余长度 |
| 17 | + /// </summary> |
| 18 | + /// <param name="binary"></param> |
| 19 | + /// <returns></returns> |
| 20 | + public static long Length (this BinaryReader binary) |
| 21 | + { |
| 22 | + return binary.BaseStream.Length - binary.BaseStream.Position; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// 从字节数组中的指定点开始,从流中读取所有字节。 |
| 27 | + /// </summary> |
| 28 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 29 | + /// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 30 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 31 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 32 | + public static byte[] ReadAll_Ex (this BinaryReader binary) |
| 33 | + { |
| 34 | + return GetBinary (binary, binary.BaseStream.Length, false); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// 从字节数组中的指定点开始,从流中读取指定字节长度。 |
| 39 | + /// </summary> |
| 40 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 41 | + /// <param name="len">要读取的字节数。</param> |
| 42 | + /// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 43 | + /// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception> |
| 44 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 45 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 46 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 47 | + public static byte[] ReadBin_Ex (this BinaryReader binary, long len) |
| 48 | + { |
| 49 | + return GetBinary (binary, len); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// 从字节数组中的指定点开始,从流中读取 2 字节长度并反序为 <see cref="int"/> 值。 |
| 54 | + /// </summary> |
| 55 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 56 | + /// <returns>读入 2 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 57 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 58 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 59 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 60 | + public static short ReadInt16_Ex (this BinaryReader binary) |
| 61 | + { |
| 62 | + return BitConverter.ToInt16 (GetBinary (binary, 2, true), 0); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// 从字节数组中的指定点开始,从流中读取 4 字节长度并反序为 <see cref="int"/> 值。 |
| 67 | + /// </summary> |
| 68 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 69 | + /// <returns>读入 4 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 70 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 71 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 72 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 73 | + public static int ReadInt32_Ex (this BinaryReader binary) |
| 74 | + { |
| 75 | + return BitConverter.ToInt32 (GetBinary (binary, 4, true), 0); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// 从字节数组中的指定点开始,从流中读取 8 字节长度并反序为 <see cref="long"/> 值。 |
| 80 | + /// </summary> |
| 81 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 82 | + /// <returns>读入 8 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 83 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 84 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 85 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 86 | + public static long ReadInt64_Ex (this BinaryReader binary) |
| 87 | + { |
| 88 | + return BitConverter.ToInt64 (GetBinary (binary, 8, true), 0); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// 从字节数组中的指定点开始,从流中读取指定字节长度。 |
| 93 | + /// </summary> |
| 94 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 95 | + /// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 96 | + /// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception> |
| 97 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 98 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 99 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 100 | + public static byte[] ReadToken_Ex (this BinaryReader binary) |
| 101 | + { |
| 102 | + short len = ReadInt16_Ex (binary); |
| 103 | + return GetBinary (binary, len); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// 从字节数组中的指定点开始,从流中读取指定编码的字符串。 |
| 108 | + /// </summary> |
| 109 | + /// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param> |
| 110 | + /// <param name="encoding"></param> |
| 111 | + /// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns> |
| 112 | + /// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception> |
| 113 | + /// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception> |
| 114 | + /// <exception cref="ObjectDisposedException">流已关闭。</exception> |
| 115 | + /// <exception cref="IOException">出现 I/O 错误。</exception> |
| 116 | + public static string ReadString_Ex (this BinaryReader binary, Encoding encoding = null) |
| 117 | + { |
| 118 | + if (encoding == null) |
| 119 | + { |
| 120 | + encoding = Encoding.ASCII; |
| 121 | + } |
| 122 | + |
| 123 | + return encoding.GetString (ReadToken_Ex (binary)); |
| 124 | + } |
| 125 | + #endregion |
| 126 | + |
| 127 | + #region --私有方法-- |
| 128 | + private static byte[] GetBinary (BinaryReader binary, long len, bool isReverse = false) |
| 129 | + { |
| 130 | + byte[] buffer = new byte[len]; |
| 131 | + binary.Read (buffer, 0, buffer.Length); |
| 132 | + if (isReverse) |
| 133 | + { |
| 134 | + buffer = buffer.Reverse ().ToArray (); |
| 135 | + } |
| 136 | + return buffer; |
| 137 | + } |
| 138 | + #endregion |
| 139 | + } |
| 140 | +} |
0 commit comments