今天在维护一个旧项目的时候,看到一个方法把string
转换为 byte[]
用的是写入内存流的,而后ToArray()
,由于日常都是用System.Text.Encoding.UTF8.GetBytes(string)
,恰好这里遇到一个安全的问题,就想把它重构了。html
因为这个是已经找不到原来开发的人员,因此也无从问当时为何要这么作,我想就算找到应该他也不知道当时为何要这么作。git
因为这个是线上跑了好久的项目,因此须要作一下测试,万一真里面真的是有历史缘由呢!因而就有了这篇文章。github
byte
数组的函数(确保重构先后一致),没找到有系统自带,因此写了一个BytesEquals
比较字节数组是否彻底相等,方法比较简单,就不作介绍macos
public static bool BytesEquals(byte[] array1, byte[] array2) { if (array1 == null && array2 == null) return true; if (Array.ReferenceEquals(array1, array2)) return true; if (array1?.Length != array2?.Length) return false; for (int i = 0; i < array1.Length; i++) { if (array1[i] != array2[i]) return false; } return true; }
原始方法(使用StreamWriter)数组
public static byte[] StringToBytes(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); using (var ms = new System.IO.MemoryStream()) using (var streamWriter = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8)) { streamWriter.Write(value); streamWriter.Flush(); return ms.ToArray(); } }
重构(使用Encoidng)安全
public static byte[] StringToBytes(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); return System.Text.Encoding.UTF8.GetBytes(value); }
dotnet new xunit -n 'Demo.StreamWriter.UnitTests'
[Fact] public void BytesEqualsTest_Equals_ReturnTrue() { ... } [Fact] public void BytesEqualsTest_NotEquals_ReturnFalse() { ... } [Fact] public void StringToBytes_Equals_ReturnTrue() { ... }
dotnet test
StringToBytes_Equals_ReturnTrue
未能经过单元测试这个未能经过,重构后的生成的字节数组与原始不一致函数
StringToBytes_Equals_ReturnTrue
, 发现bytesWithStream
比 bytesWithEncoding
在数组头多了三个字节(不少人都能猜到这个是UTF8的BOM)+ bytesWithStream[0] = 239 + bytesWithStream[1] = 187 + bytesWithStream[2] = 191 bytesWithStream[3] = 72 bytesWithStream[4] = 101 bytesWithEncoding[0] = 72 bytesWithEncoding[0] = 101
不了解BOM,能够看看这篇文章Byte order mark性能
从文章能够明确多出来字节就是UTF8-BOM,问题来了,为何StreamWriter
会多出来BOM,而Encoding.UTF8
没有,都是用同一个编码单元测试
StreamWriter
测试
public StreamWriter(Stream stream) : this(stream, UTF8NoBOM, 1024, leaveOpen: false) { } public StreamWriter(Stream stream, Encoding encoding) : this(stream, encoding, 1024, leaveOpen: false) { }
private static Encoding UTF8NoBOM => EncodingCache.UTF8NoBOM; internal static readonly Encoding UTF8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
能够看到StreamWriter
, 默认是使用UTF8NoBOM
, 可是在这里指定了System.Text.Encoding.UTF8
,根据encoderShouldEmitUTF8Identifier
这个参数决定是否写入BOM,最终是在Flush
写入
private void Flush(bool flushStream, bool flushEncoder) { ... if (!_haveWrittenPreamble) { _haveWrittenPreamble = true; ReadOnlySpan<byte> preamble = _encoding.Preamble; if (preamble.Length > 0) { _stream.Write(preamble); } } int bytes = _encoder.GetBytes(_charBuffer, 0, _charPos, _byteBuffer, 0, flushEncoder); _charPos = 0; if (bytes > 0) { _stream.Write(_byteBuffer, 0, bytes); } ... }
Flush
最终也是使用_encoder.GetBytes
获取字节数组写入流中,而System.Text.Encoding.UTF8.GetBytes()
最终也是使用这个方法。
System.Text.Encoding.UTF8.GetBytes
public virtual byte[] GetBytes(string s) { if (s == null) { throw new ArgumentNullException("s", SR.ArgumentNull_String); } int byteCount = GetByteCount(s); byte[] array = new byte[byteCount]; int bytes = GetBytes(s, 0, s.Length, array, 0); return array; }
若是要达到和原来同样的效果,只须要在最终返回结果加上UTF8.Preamble
, 修改以下
public static byte[] StringToBytes(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); - return System.Text.Encoding.UTF8.GetBytes(value); + var bytes = System.Text.Encoding.UTF8.GetBytes(value); + var result = new byte[bytes.Length + 3]; + Array.Copy(Encoding.UTF8.GetPreamble(), result, 3); + Array.Copy(bytes, 0, result, 3, bytes.Length); + return result; }
可是对于这样修改感受是不必,由于这个最终是传给一个对外接口,因此只能对那个接口作测试,最终结果也是不须要这个BOM
排除了StreamWriter
没有作特殊处理,能够用System.Text.Encoding.UTF8.GetBytes()
重构。还有就是效率问题,虽然直观上看到使用StreamWriter
最终都是使用Encoder.GetBytes
方法,并且还多了两次资源对申请和释放。可是仍是用基准测试才能直观看出其中差异。
基准测试使用BenchmarkDotNet,BenchmarkDotNet这里以前有介绍过
BenchmarksTests
目录并建立基准项目mkdir BenchmarksTests && cd BenchmarksTests && dotnet new benchmark -b StreamVsEncoding
dotnet add reference ../../src/Demo.StreamWriter.csproj
注意:Demo.StreamWriter须要Release编译
[SimpleJob(launchCount: 10)] [MemoryDiagnoser] public class StreamVsEncoding { [Params("Hello Wilson!", "使用【BenchmarkDotNet】基准测试,Encoding vs Stream")] public string _stringValue; [Benchmark] public void Encoding() => StringToBytesWithEncoding.StringToBytes(_stringValue); [Benchmark] public void Stream() => StringToBytesWithStream.StringToBytes(_stringValue); }
dotnet build && sudo dotnet benchmark bin/Release/netstandard2.0/BenchmarksTests.dll --filter 'StreamVsEncoding'
注意:macos 须要sudo权限
Method | _stringValue | Mean | Error | StdDev | Median | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Encoding | Hello Wilson! | 107.4 ns | 0.61 ns | 2.32 ns | 106.9 ns | 0.0355 | - | - | 112 B |
Stream | Hello Wilson! | 565.1 ns | 4.12 ns | 18.40 ns | 562.3 ns | 1.8196 | - | - | 5728 B |
Encoding | 使用【Be(...)tream [42] | 166.3 ns | 1.00 ns | 3.64 ns | 165.4 ns | 0.0660 | - | - | 208 B |
Stream | 使用【Be(...)tream [42] | 584.6 ns | 3.65 ns | 13.22 ns | 580.8 ns | 1.8349 | - | - | 5776 B |
执行时间相差了4~5倍, 内存使用率相差 20 ~ 50倍,差距还比较大。
StreamWriter
默认是没有BOM,若指定System.Text.Encoding.UTF8
,会在Flush
字节数组开头添加BOMSystem.Text.Encoding.UTF8.GetBytes
要高效System.Text.Encoding.UTF8.GetBytes
是不会本身添加BOM,提供Encoding.UTF8.GetPreamble()
获取BOM转发请标明出处:http://www.javashuo.com/article/p-ekhzzjvf-ne.html
示例代码