【Net】StreamWriter.Write 的一點注意事項


背景

今天在維護一個舊項目的時候,看到一個方法把string 轉換為 byte[] 用的是寫入內存流的,然后ToArray(),因為平常都是用System.Text.Encoding.UTF8.GetBytes(string) ,剛好這里遇到一個安全的問題,就想把它重構了。

由於這個是已經找不到原來開發的人員,所以也無從問當時為什么要這么做,我想就算找到應該他也不知道當時為什么要這么做。

由於這個是線上跑了很久的項目,所以需要做一下測試,萬一真里面真的是有歷史原因呢!於是就有了這篇文章。

重構過程

  1. 需要一個比較byte數組的函數(確保重構前后一致),沒找到有系統自帶,所以寫了一個
  2. 重構方法(使用Encoding)
  3. 單元測試
  4. 基准測試(或許之前是為了性能考慮,因為這個方法調用次數也不少)

字節數組比較方法:BytesEquals

比較字節數組是否完全相等,方法比較簡單,就不做介紹

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);
}

單元測試

  • BytesEquals 單元測試
  1. 新建單元測試項目
dotnet new xunit -n 'Demo.StreamWriter.UnitTests' 
  1. 編寫單元測試
[Fact]
public void BytesEqualsTest_Equals_ReturnTrue()
{
    ...
}

[Fact]
public void BytesEqualsTest_NotEquals_ReturnFalse()
{
    ...
}

[Fact]
public void StringToBytes_Equals_ReturnTrue()
{
    ...
}

  1. 執行單元測試
dotnet test
  1. StringToBytes_Equals_ReturnTrue 未能通過單元測試

這個未能通過,重構后的生成的字節數組與原始不一致

排查過程

  1. 調試StringToBytes_Equals_ReturnTrue , 發現bytesWithStreambytesWithEncoding 在數組頭多了三個字節(很多人都能猜到這個是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這里之前有介紹過

  1. 創建BenchmarksTests目錄並創建基准項目
mkdir BenchmarksTests && cd BenchmarksTests &&  dotnet new benchmark -b StreamVsEncoding
  1. 添加引用
dotnet add reference ../../src/Demo.StreamWriter.csproj

注意:Demo.StreamWriter需要Release編譯

  1. 編寫基准測試
[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);
}
  1. 編譯 && 運行基准測試
dotnet build && sudo dotnet benchmark bin/Release/netstandard2.0/BenchmarksTests.dll --filter 'StreamVsEncoding'

注意:macos 需要sudo權限

  1. 查看結果
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倍,差距還比較大。

總結

  1. StreamWriter 默認是沒有BOM,若指定System.Text.Encoding.UTF8,會在Flush字節數組開頭添加BOM
  2. 字符串轉換字節數組使用System.Text.Encoding.UTF8.GetBytes 要高效
  3. System.Text.Encoding.UTF8.GetBytes 是不會自己添加BOM,提供Encoding.UTF8.GetPreamble()獲取BOM
  4. UTF8 已經不推薦推薦在前面加BOM

轉發請標明出處:https://www.cnblogs.com/WilsonPan/p/13524885.html
示例代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM