12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System.IO;
- using System.Text;
- using TBL.CSharp.Serialization.Utilities;
- namespace TBL.CSharp.Serialization.Marshal.Serializer
- {
- /// <summary>
- /// Marshal 风格的二进制序列化器
- /// </summary>
- public unsafe partial class BinarySerializer
- {
- /// <summary>
- /// 是否需要端序转换
- /// </summary>
- private readonly bool _isNeedEndianConversion;
- /// <summary>
- /// 构造序列化器
- /// </summary>
- /// <param name="isNeedEndianConversion">是否需要字节序转换</param>
- public BinarySerializer(bool isNeedEndianConversion)
- {
- _isNeedEndianConversion = isNeedEndianConversion;
- }
- /// <summary>
- /// 创建和此序列化器字节序适配的 <see cref="BinaryReader"/>
- /// </summary>
- public BinaryReader CreateBinaryReader(Stream stream) => _isNeedEndianConversion
- ? new BinaryReaderWithEndianConversion(stream, Encoding.UTF8, true)
- : new BinaryReader(stream, Encoding.UTF8, true);
-
- /// <summary>
- /// 创建和此序列化器字节序适配的 <see cref="BinaryWriter"/>
- /// </summary>
- public BinaryWriter CreateBinaryWriter(Stream stream) => _isNeedEndianConversion
- ? new BinaryWriterWithEndianConversion(stream, Encoding.UTF8, true)
- : new BinaryWriter(stream, Encoding.UTF8, true);
- }
- }
|