/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Compilers/Test/Core/FX/ImmutableArrayTestExtensions.cs
50 строк
2 KB
Tomas Matousek
Move
23 окт 2020, 21:06
23 окт 2020, 21:06
d49cd2b
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #nullable disable using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.IO; namespace Microsoft.CodeAnalysis.Test.Utilities { /// <summary> /// The collection of extension methods for the <see cref="ImmutableArray{T}"/> type /// </summary> public static class ImmutableArrayTestExtensions { private const int BufferSize = 4096; /// <summary> /// Writes read-only array of bytes to the specified file. /// </summary> /// <param name="bytes">Data to write to the file.</param> /// <param name="path">File path.</param> internal static void WriteToFile(this ImmutableArray<byte> bytes, string path) { using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, BufferSize); WriteToStream(bytes, fileStream); } internal static void WriteToStream(this ImmutableArray<byte> bytes, Stream stream) { const int bufferSize = 4096; // PERF: Consider using an ObjectPool<byte[]> here var buffer = new byte[Math.Min(bufferSize, bytes.Length)]; int offset = 0; while (offset < bytes.Length) { int length = Math.Min(bufferSize, bytes.Length - offset); bytes.CopyTo(offset, buffer, 0, length); stream.Write(buffer, 0, length); offset += length; } } } }