| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace Lattice.ECS.Core |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// 位流 - FrameSync 风格的高效二进制序列化 |
| | | 11 | | /// |
| | | 12 | | /// 特性: |
| | | 13 | | /// 1. 位级别的读写(Bit-packing) |
| | | 14 | | /// 2. 支持有符号/无符号整数变长编码 |
| | | 15 | | /// 3. 支持原始内存拷贝 |
| | | 16 | | /// 4. 可扩展的缓冲区 |
| | | 17 | | /// </summary> |
| | | 18 | | public unsafe class BitStream |
| | | 19 | | { |
| | | 20 | | #region 字段 |
| | | 21 | | |
| | | 22 | | /// <summary>数据缓冲区</summary> |
| | | 23 | | private byte[] _buffer; |
| | | 24 | | |
| | | 25 | | /// <summary>当前逻辑终点(读取模式下可能小于缓冲区长度)。</summary> |
| | | 26 | | private int _byteLimit; |
| | | 27 | | |
| | | 28 | | /// <summary>当前字节位置</summary> |
| | | 29 | | private int _bytePosition; |
| | | 30 | | |
| | | 31 | | /// <summary>当前位位置(0-7)</summary> |
| | | 32 | | private int _bitPosition; |
| | | 33 | | |
| | | 34 | | /// <summary>是否正在写入</summary> |
| | 204 | 35 | | public bool IsWriting { get; set; } |
| | | 36 | | |
| | | 37 | | /// <summary>是否正在读取</summary> |
| | 98 | 38 | | public bool IsReading => !IsWriting; |
| | | 39 | | |
| | | 40 | | /// <summary>当前字节位置</summary> |
| | 0 | 41 | | public int BytePosition => _bytePosition; |
| | | 42 | | |
| | | 43 | | /// <summary>缓冲区长度</summary> |
| | 0 | 44 | | public int Length => _buffer.Length; |
| | | 45 | | |
| | | 46 | | /// <summary>已使用的字节数</summary> |
| | 1 | 47 | | public int BytesUsed => _bytePosition + (_bitPosition > 0 ? 1 : 0); |
| | | 48 | | |
| | | 49 | | #endregion |
| | | 50 | | |
| | | 51 | | #region 构造函数 |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// 创建位流(写入模式) |
| | | 55 | | /// </summary> |
| | 2 | 56 | | public BitStream(int initialCapacity = 1024) |
| | | 57 | | { |
| | 2 | 58 | | _buffer = new byte[initialCapacity]; |
| | 2 | 59 | | _byteLimit = _buffer.Length; |
| | 2 | 60 | | _bytePosition = 0; |
| | 2 | 61 | | _bitPosition = 0; |
| | 2 | 62 | | IsWriting = true; |
| | 2 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// 创建位流(从现有数据读取) |
| | | 67 | | /// </summary> |
| | 1 | 68 | | public BitStream(byte[] data, int offset = 0, int length = -1) |
| | | 69 | | { |
| | 1 | 70 | | _buffer = data; |
| | 1 | 71 | | _byteLimit = length >= 0 ? System.Math.Min(data.Length, offset + length) : data.Length; |
| | 1 | 72 | | _bytePosition = offset; |
| | 1 | 73 | | _bitPosition = 0; |
| | 1 | 74 | | IsWriting = false; |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | #region 基础位操作 |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// 写入一个位 |
| | | 83 | | /// </summary> |
| | | 84 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 85 | | public void WriteBit(bool value) |
| | | 86 | | { |
| | 96 | 87 | | if (IsReading) |
| | 0 | 88 | | throw new InvalidOperationException("Cannot write in reading mode"); |
| | | 89 | | |
| | 96 | 90 | | EnsureCapacity(_bytePosition + 1); |
| | | 91 | | |
| | 96 | 92 | | if (value) |
| | | 93 | | { |
| | 11 | 94 | | _buffer[_bytePosition] |= (byte)(1 << _bitPosition); |
| | | 95 | | } |
| | | 96 | | |
| | 96 | 97 | | _bitPosition++; |
| | 96 | 98 | | if (_bitPosition >= 8) |
| | | 99 | | { |
| | 12 | 100 | | _bitPosition = 0; |
| | 12 | 101 | | _bytePosition++; |
| | | 102 | | } |
| | 96 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// 读取一个位 |
| | | 107 | | /// </summary> |
| | | 108 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 109 | | public bool ReadBit() |
| | | 110 | | { |
| | 96 | 111 | | if (IsWriting) |
| | 0 | 112 | | throw new InvalidOperationException("Cannot read in writing mode"); |
| | | 113 | | |
| | 96 | 114 | | if (_bytePosition >= _byteLimit) |
| | 0 | 115 | | throw new EndOfStreamException($"Attempted to read beyond buffer length ({_byteLimit} bytes at position |
| | | 116 | | |
| | 96 | 117 | | bool value = (_buffer[_bytePosition] & (1 << _bitPosition)) != 0; |
| | | 118 | | |
| | 96 | 119 | | _bitPosition++; |
| | 96 | 120 | | if (_bitPosition >= 8) |
| | | 121 | | { |
| | 12 | 122 | | _bitPosition = 0; |
| | 12 | 123 | | _bytePosition++; |
| | | 124 | | } |
| | | 125 | | |
| | 96 | 126 | | return value; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// 写入指定数量的位 |
| | | 131 | | /// </summary> |
| | | 132 | | public void WriteBits(uint value, int bits) |
| | | 133 | | { |
| | 3 | 134 | | if (bits < 0 || bits > 32) |
| | 0 | 135 | | throw new ArgumentOutOfRangeException(nameof(bits)); |
| | | 136 | | |
| | 198 | 137 | | for (int i = 0; i < bits; i++) |
| | | 138 | | { |
| | 96 | 139 | | WriteBit(((value >> i) & 1) != 0); |
| | | 140 | | } |
| | 3 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// 读取指定数量的位 |
| | | 145 | | /// </summary> |
| | | 146 | | public uint ReadBits(int bits) |
| | | 147 | | { |
| | 3 | 148 | | if (bits < 0 || bits > 32) |
| | 0 | 149 | | throw new ArgumentOutOfRangeException(nameof(bits)); |
| | | 150 | | |
| | 3 | 151 | | uint value = 0; |
| | 198 | 152 | | for (int i = 0; i < bits; i++) |
| | | 153 | | { |
| | 96 | 154 | | if (ReadBit()) |
| | 11 | 155 | | value |= (1u << i); |
| | | 156 | | } |
| | 3 | 157 | | return value; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | #endregion |
| | | 161 | | |
| | | 162 | | #region 整数序列化 |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// 写入 int(32位) |
| | | 166 | | /// </summary> |
| | | 167 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 168 | | public void WriteInt(int value) |
| | | 169 | | { |
| | 1 | 170 | | WriteBits((uint)value, 32); |
| | 1 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// 读取 int(32位) |
| | | 175 | | /// </summary> |
| | | 176 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 177 | | public int ReadInt() |
| | | 178 | | { |
| | 1 | 179 | | return (int)ReadBits(32); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// 写入 uint(32位) |
| | | 184 | | /// </summary> |
| | | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 186 | | public void WriteUInt(uint value) |
| | | 187 | | { |
| | 0 | 188 | | WriteBits(value, 32); |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// 读取 uint(32位) |
| | | 193 | | /// </summary> |
| | | 194 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 195 | | public uint ReadUInt() |
| | | 196 | | { |
| | 0 | 197 | | return ReadBits(32); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// 写入 short(16位) |
| | | 202 | | /// </summary> |
| | | 203 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 204 | | public void WriteShort(short value) |
| | | 205 | | { |
| | 0 | 206 | | WriteBits((uint)(ushort)value, 16); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// 读取 short(16位) |
| | | 211 | | /// </summary> |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | public short ReadShort() |
| | | 214 | | { |
| | 0 | 215 | | return (short)ReadBits(16); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// 写入 ushort(16位) |
| | | 220 | | /// </summary> |
| | | 221 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 222 | | public void WriteUShort(ushort value) |
| | | 223 | | { |
| | 0 | 224 | | WriteBits(value, 16); |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// 读取 ushort(16位) |
| | | 229 | | /// </summary> |
| | | 230 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 231 | | public ushort ReadUShort() |
| | | 232 | | { |
| | 0 | 233 | | return (ushort)ReadBits(16); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// 写入 byte(8位) |
| | | 238 | | /// </summary> |
| | | 239 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 240 | | public void WriteByte(byte value) |
| | | 241 | | { |
| | 0 | 242 | | WriteBits(value, 8); |
| | 0 | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// 读取 byte(8位) |
| | | 247 | | /// </summary> |
| | | 248 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 249 | | public byte ReadByte() |
| | | 250 | | { |
| | 0 | 251 | | return (byte)ReadBits(8); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// 写入 bool(1位) |
| | | 256 | | /// </summary> |
| | | 257 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 258 | | public void WriteBool(bool value) |
| | | 259 | | { |
| | 0 | 260 | | WriteBit(value); |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// 读取 bool(1位) |
| | | 265 | | /// </summary> |
| | | 266 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 267 | | public bool ReadBool() |
| | | 268 | | { |
| | 0 | 269 | | return ReadBit(); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// 写入 long(64位) |
| | | 274 | | /// </summary> |
| | | 275 | | public void WriteLong(long value) |
| | | 276 | | { |
| | 0 | 277 | | WriteBits((uint)value, 32); |
| | 0 | 278 | | WriteBits((uint)(value >> 32), 32); |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// 读取 long(64位) |
| | | 283 | | /// </summary> |
| | | 284 | | public long ReadLong() |
| | | 285 | | { |
| | 0 | 286 | | long low = ReadBits(32); |
| | 0 | 287 | | long high = ReadBits(32); |
| | 0 | 288 | | return low | (high << 32); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// 写入 ulong(64位) |
| | | 293 | | /// </summary> |
| | | 294 | | public void WriteULong(ulong value) |
| | | 295 | | { |
| | 1 | 296 | | WriteBits((uint)value, 32); |
| | 1 | 297 | | WriteBits((uint)(value >> 32), 32); |
| | 1 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// 读取 ulong(64位) |
| | | 302 | | /// </summary> |
| | | 303 | | public ulong ReadULong() |
| | | 304 | | { |
| | 1 | 305 | | ulong low = ReadBits(32); |
| | 1 | 306 | | ulong high = ReadBits(32); |
| | 1 | 307 | | return low | (high << 32); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | #endregion |
| | | 311 | | |
| | | 312 | | #region 变长整数(VarInt) |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// 写入变长 uint(小值更省空间) |
| | | 316 | | /// </summary> |
| | | 317 | | public void WriteVarUInt(uint value) |
| | | 318 | | { |
| | | 319 | | // 每个字节使用 7 位数据 + 1 位继续标志 |
| | 0 | 320 | | while (value >= 0x80) |
| | | 321 | | { |
| | 0 | 322 | | WriteByte((byte)(value | 0x80)); |
| | 0 | 323 | | value >>= 7; |
| | | 324 | | } |
| | 0 | 325 | | WriteByte((byte)value); |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// 读取变长 uint |
| | | 330 | | /// </summary> |
| | | 331 | | public uint ReadVarUInt() |
| | | 332 | | { |
| | 0 | 333 | | uint value = 0; |
| | 0 | 334 | | int shift = 0; |
| | | 335 | | byte b; |
| | | 336 | | |
| | | 337 | | do |
| | | 338 | | { |
| | 0 | 339 | | b = ReadByte(); |
| | 0 | 340 | | value |= (uint)(b & 0x7F) << shift; |
| | 0 | 341 | | shift += 7; |
| | 0 | 342 | | } while ((b & 0x80) != 0); |
| | | 343 | | |
| | 0 | 344 | | return value; |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// 写入变长 int(ZigZag 编码) |
| | | 349 | | /// </summary> |
| | | 350 | | public void WriteVarInt(int value) |
| | | 351 | | { |
| | | 352 | | // ZigZag 编码:将符号位移到最低位 |
| | 0 | 353 | | uint encoded = (uint)((value << 1) ^ (value >> 31)); |
| | 0 | 354 | | WriteVarUInt(encoded); |
| | 0 | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// 读取变长 int |
| | | 359 | | /// </summary> |
| | | 360 | | public int ReadVarInt() |
| | | 361 | | { |
| | 0 | 362 | | uint encoded = ReadVarUInt(); |
| | 0 | 363 | | return (int)((encoded >> 1) ^ -(encoded & 1)); |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | #endregion |
| | | 367 | | |
| | | 368 | | #region 原始内存操作 |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// 写入原始字节数组 |
| | | 372 | | /// </summary> |
| | | 373 | | public void WriteBytes(byte[] data, int offset, int length) |
| | | 374 | | { |
| | | 375 | | // 对齐到字节边界 |
| | 0 | 376 | | if (_bitPosition != 0) |
| | | 377 | | { |
| | 0 | 378 | | _bitPosition = 0; |
| | 0 | 379 | | _bytePosition++; |
| | | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | EnsureCapacity(_bytePosition + length); |
| | | 383 | | |
| | 0 | 384 | | Buffer.BlockCopy(data, offset, _buffer, _bytePosition, length); |
| | 0 | 385 | | _bytePosition += length; |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | /// <summary> |
| | | 389 | | /// 读取原始字节数组 |
| | | 390 | | /// </summary> |
| | | 391 | | public void ReadBytes(byte[] destination, int offset, int length) |
| | | 392 | | { |
| | | 393 | | // 对齐到字节边界 |
| | 0 | 394 | | if (_bitPosition != 0) |
| | | 395 | | { |
| | 0 | 396 | | _bitPosition = 0; |
| | 0 | 397 | | _bytePosition++; |
| | | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | if (_bytePosition + length > _byteLimit) |
| | 0 | 401 | | throw new EndOfStreamException(); |
| | | 402 | | |
| | 0 | 403 | | Buffer.BlockCopy(_buffer, _bytePosition, destination, offset, length); |
| | 0 | 404 | | _bytePosition += length; |
| | 0 | 405 | | } |
| | | 406 | | |
| | | 407 | | /// <summary> |
| | | 408 | | /// 写入非托管内存 |
| | | 409 | | /// </summary> |
| | | 410 | | public void WriteMemory(void* source, int length) |
| | | 411 | | { |
| | | 412 | | // 对齐到字节边界 |
| | 0 | 413 | | if (_bitPosition != 0) |
| | | 414 | | { |
| | 0 | 415 | | _bitPosition = 0; |
| | 0 | 416 | | _bytePosition++; |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | EnsureCapacity(_bytePosition + length); |
| | | 420 | | |
| | 0 | 421 | | fixed (byte* dest = &_buffer[_bytePosition]) |
| | | 422 | | { |
| | 0 | 423 | | Buffer.MemoryCopy(source, dest, length, length); |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | _bytePosition += length; |
| | 0 | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// 读取到非托管内存 |
| | | 431 | | /// </summary> |
| | | 432 | | public void ReadMemory(void* destination, int length) |
| | | 433 | | { |
| | | 434 | | // 对齐到字节边界 |
| | 0 | 435 | | if (_bitPosition != 0) |
| | | 436 | | { |
| | 0 | 437 | | _bitPosition = 0; |
| | 0 | 438 | | _bytePosition++; |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | if (_bytePosition + length > _byteLimit) |
| | 0 | 442 | | throw new EndOfStreamException(); |
| | | 443 | | |
| | 0 | 444 | | fixed (byte* src = &_buffer[_bytePosition]) |
| | | 445 | | { |
| | 0 | 446 | | Buffer.MemoryCopy(src, destination, length, length); |
| | | 447 | | } |
| | | 448 | | |
| | 0 | 449 | | _bytePosition += length; |
| | 0 | 450 | | } |
| | | 451 | | |
| | | 452 | | #endregion |
| | | 453 | | |
| | | 454 | | #region 辅助方法 |
| | | 455 | | |
| | | 456 | | /// <summary> |
| | | 457 | | /// 获取写入的数据 |
| | | 458 | | /// </summary> |
| | | 459 | | public byte[] ToArray() |
| | | 460 | | { |
| | 1 | 461 | | int length = BytesUsed; |
| | 1 | 462 | | byte[] result = new byte[length]; |
| | 1 | 463 | | Buffer.BlockCopy(_buffer, 0, result, 0, length); |
| | 1 | 464 | | return result; |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// 获取当前已写入数据的只读视图,不发生额外分配。 |
| | | 469 | | /// </summary> |
| | | 470 | | public ReadOnlySpan<byte> GetWrittenSpan() |
| | | 471 | | { |
| | 0 | 472 | | return new ReadOnlySpan<byte>(_buffer, 0, BytesUsed); |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// 重置流(用于复用) |
| | | 477 | | /// </summary> |
| | | 478 | | public void Reset() |
| | | 479 | | { |
| | 0 | 480 | | _bytePosition = 0; |
| | 0 | 481 | | _bitPosition = 0; |
| | 0 | 482 | | if (IsWriting) |
| | | 483 | | { |
| | 0 | 484 | | _byteLimit = _buffer.Length; |
| | | 485 | | } |
| | 0 | 486 | | } |
| | | 487 | | |
| | | 488 | | /// <summary> |
| | | 489 | | /// 确保容量 |
| | | 490 | | /// </summary> |
| | | 491 | | private void EnsureCapacity(int required) |
| | | 492 | | { |
| | 96 | 493 | | if (required <= _buffer.Length) |
| | 96 | 494 | | return; |
| | | 495 | | |
| | 0 | 496 | | int newCapacity = System.Math.Max(required, _buffer.Length * 2); |
| | 0 | 497 | | byte[] newBuffer = new byte[newCapacity]; |
| | 0 | 498 | | Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _buffer.Length); |
| | 0 | 499 | | _buffer = newBuffer; |
| | 0 | 500 | | _byteLimit = _buffer.Length; |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | #endregion |
| | | 504 | | |
| | | 505 | | #region 缓冲区安全检查 |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// 检查剩余写入空间是否足够 |
| | | 509 | | /// </summary> |
| | | 510 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 511 | | public bool CanWrite(int bytes) => _bytePosition + bytes <= _buffer.Length; |
| | | 512 | | |
| | | 513 | | /// <summary> |
| | | 514 | | /// 检查剩余读取空间是否足够 |
| | | 515 | | /// </summary> |
| | | 516 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 517 | | public bool CanRead(int bytes) => _bytePosition + bytes <= _byteLimit; |
| | | 518 | | |
| | | 519 | | /// <summary> |
| | | 520 | | /// 验证可以读取指定数量的字节 |
| | | 521 | | /// </summary> |
| | | 522 | | public void VerifyCanRead(int bytes, string operation = "read") |
| | | 523 | | { |
| | 0 | 524 | | if (!CanRead(bytes)) |
| | 0 | 525 | | throw new EndOfStreamException( |
| | 0 | 526 | | $"Insufficient data to {operation}. " + |
| | 0 | 527 | | $"Required: {bytes} bytes, Available: {_byteLimit - _bytePosition} bytes " + |
| | 0 | 528 | | $"(at position {_bytePosition})"); |
| | 0 | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// 获取剩余可读字节数 |
| | | 533 | | /// </summary> |
| | 0 | 534 | | public int RemainingBytes => System.Math.Max(0, _byteLimit - _bytePosition); |
| | | 535 | | |
| | | 536 | | /// <summary> |
| | | 537 | | /// 获取当前位置信息(用于调试) |
| | | 538 | | /// </summary> |
| | | 539 | | public string PositionInfo => |
| | 0 | 540 | | $"Byte: {_bytePosition}, Bit: {_bitPosition}, Length: {_byteLimit}"; |
| | | 541 | | |
| | | 542 | | #endregion |
| | | 543 | | } |
| | | 544 | | } |