From 26fca5945dd94514f206e8f1d1efdf2d63dcf751 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 31 Jan 2026 07:11:28 -0500 Subject: [PATCH] Remove extra array allocations from TarHeader.GetLongMetadataStream --- .../src/System/Formats/Tar/TarHeader.Write.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs index e464b89bae9211..8a4ee54bac4f9f 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs @@ -439,11 +439,9 @@ internal async Task WriteAsGnuAsync(Stream archiveStream, Memory buffer, C private static MemoryStream GetLongMetadataStream(string text) { - MemoryStream data = new MemoryStream(); - data.Write(Encoding.UTF8.GetBytes(text)); - data.WriteByte(0); // Add a null terminator at the end of the string, _size will be calculated later - data.Position = 0; - return data; + byte[] arr = new byte[Encoding.UTF8.GetByteCount(text) + 1]; // +1 for null terminator + Encoding.UTF8.GetBytes(text, arr); + return new MemoryStream(arr); } private TarHeader GetGnuLongLinkMetadataHeader()