// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
///
/// The collection of extension methods for the type
///
public static class ImmutableArrayTestExtensions
{
///
/// Writes read-only array of bytes to the specified file.
///
/// Data to write to the file.
/// File path.
internal static void WriteToFile(this ImmutableArray bytes, string path)
{
Debug.Assert(!bytes.IsDefault);
const int bufferSize = 4096;
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize))
{
// PERF: Consider using an ObjectPool here
byte[] 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);
fileStream.Write(buffer, 0, length);
offset += length;
}
}
}
}
}