Skip to content

Commit e864861

Browse files
committed
Fix AR and PE output stream flushing
1 parent 2a49db3 commit e864861

6 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎src/LibObjectFile.Tests/Ar/ArTests.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,22 @@ public void CheckLibraryWithELF()
313313
}
314314
}
315315

316+
[TestMethod]
317+
public void TestWriteFlushesOutputStream()
318+
{
319+
var archive = new ArArchiveFile();
320+
archive.AddFile(new ArBinaryFile
321+
{
322+
Name = "file.txt",
323+
Stream = new MemoryStream(Encoding.UTF8.GetBytes("content"))
324+
});
325+
using var output = new FlushTrackingStream();
326+
327+
archive.Write(output);
328+
329+
Assert.IsTrue(output.WasFlushed);
330+
}
331+
316332
[TestMethod]
317333
public async Task CheckCreateArLibrary()
318334
{
@@ -427,4 +443,4 @@ private static void CompareArFiles(ArArchiveFile archiveFile, ArArchiveFile file
427443
}
428444
}
429445
}
430-
}
446+
}

‎src/LibObjectFile.Tests/Elf/ElfSimpleTests.cs‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -673,15 +673,4 @@ public async Task TestElf(string name)
673673
{
674674
await LoadAndVerifyElf(name);
675675
}
676-
677-
private sealed class FlushTrackingStream : MemoryStream
678-
{
679-
public bool WasFlushed { get; private set; }
680-
681-
public override void Flush()
682-
{
683-
WasFlushed = true;
684-
base.Flush();
685-
}
686-
}
687676
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// This file is licensed under the BSD-Clause 2 license.
3+
// See the license.txt file in the project root for more information.
4+
5+
using System.IO;
6+
7+
namespace LibObjectFile.Tests;
8+
9+
internal sealed class FlushTrackingStream : MemoryStream
10+
{
11+
public bool WasFlushed { get; private set; }
12+
13+
public override void Flush()
14+
{
15+
WasFlushed = true;
16+
base.Flush();
17+
}
18+
}

‎src/LibObjectFile.Tests/PE/PEReaderTests.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ public void TestReadShortDosStub()
181181
Assert.AreEqual(56, readPE.DosStub.Length);
182182
}
183183

184+
[TestMethod]
185+
public void TestWriteFlushesOutputStream()
186+
{
187+
var pe = new PEFile();
188+
using var output = new FlushTrackingStream();
189+
190+
pe.Write(output);
191+
192+
Assert.IsTrue(output.WasFlushed);
193+
}
194+
184195
[TestMethod]
185196
public void TestPrinterUsesStableDateFormat()
186197
{

‎src/LibObjectFile/Ar/ArArchiveFile.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,13 @@ public static bool TryRead(Stream stream, ArArchiveFileReaderOptions options, ou
318318
/// Writes this 'ar' archive file to the specified stream.
319319
/// </summary>
320320
/// <param name="stream">The stream to write to.</param>
321+
/// <remarks>The stream is flushed before this method returns but is not disposed.</remarks>
321322
public void Write(Stream stream)
322323
{
323324
if (stream == null) throw new ArgumentNullException(nameof(stream));
324325
var writer = new ArArchiveFileWriter(this, stream);
325326
writer.Write();
327+
stream.Flush();
326328
}
327329

328330
protected override void UpdateLayoutCore(ArVisitorContext context)
@@ -400,4 +402,4 @@ public void UpdateLayout(DiagnosticBag diagnostics)
400402

401403
Size = size;
402404
}
403-
}
405+
}

‎src/LibObjectFile/PE/PEFile.Write.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ partial class PEFile
2121
/// Writes this PE file to the specified stream.
2222
/// </summary>
2323
/// <param name="stream">The stream to write to.</param>
24+
/// <remarks>The stream is flushed before this method returns but is not disposed.</remarks>
2425
public void Write(Stream stream, PEImageWriterOptions? options = null)
2526
{
2627
if (!TryWrite(stream, out var diagnostics, options))
@@ -35,6 +36,7 @@ public void Write(Stream stream, PEImageWriterOptions? options = null)
3536
/// <param name="stream">The stream to write to.</param>
3637
/// <param name="diagnostics">The output diagnostics</param>
3738
/// <returns><c>true</c> if writing was successful. otherwise <c>false</c></returns>
39+
/// <remarks>The stream is flushed after a successful write but is not disposed.</remarks>
3840
public bool TryWrite(Stream stream, out DiagnosticBag diagnostics, PEImageWriterOptions? options = null)
3941
{
4042
if (stream == null) throw new ArgumentNullException(nameof(stream));
@@ -67,6 +69,7 @@ public bool TryWrite(Stream stream, out DiagnosticBag diagnostics, PEImageWriter
6769

6870
// Write the PE file
6971
Write(peWriter);
72+
stream.Flush();
7073

7174
return !diagnostics.HasErrors;
7275
}

0 commit comments

Comments
 (0)