Skip to content

Commit 9008d69

Browse files
authored
perf(Bundle): improve performance use stream (#669)
* refactor: 使用流提高性能 * refactor: 提高性能 * refactor: 更改缓存大小 * refactor: 精简代码 * refactor: 更正逻辑判断
1 parent 34194f1 commit 9008d69

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

tools/BootstrapBlazor.CssBundler/Bundler.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using System.Buffers;
56
using System.Text;
67

78
namespace BootstrapBlazor.CssBundler;
@@ -66,14 +67,25 @@ static void DoBundler(string bundlerFile, BundlerOptions option)
6667
{
6768
continue;
6869
}
69-
70-
using var reader = File.OpenText(inputFile);
71-
var content = reader.ReadToEnd();
72-
writer.Write(Encoding.UTF8.GetBytes(content));
73-
reader.Close();
70+
using var reader = File.OpenRead(inputFile);
71+
if (!IsUtf8Bom(reader))
72+
{
73+
reader.Seek(0, SeekOrigin.Begin);
74+
}
75+
reader.CopyTo(writer);
7476
}
7577
writer.Close();
7678

7779
Console.WriteLine($"Bundler Completed .... {option.OutputFileName}");
7880
}
81+
82+
private static bool IsUtf8Bom(Stream stream)
83+
{
84+
Span<byte> buffer = stackalloc byte[3];
85+
if (stream.Read(buffer) != buffer.Length)
86+
{
87+
return false;
88+
}
89+
return buffer.SequenceEqual(Encoding.UTF8.GetPreamble());
90+
}
7991
}

0 commit comments

Comments
 (0)