@@ -15,10 +15,10 @@ namespace Microsoft.NET.HostModel
1515 /// </summary>
1616 public sealed class ResourceUpdater : IDisposable
1717 {
18- private readonly FileStream stream ;
18+ private readonly FileStream _stream ;
1919 private readonly PEReader _reader ;
2020 private ResourceData _resourceData ;
21- private readonly bool leaveOpen ;
21+ private readonly bool _leaveOpen ;
2222
2323 /// <summary>
2424 /// Create a resource updater for the given PE file.
@@ -39,18 +39,24 @@ public ResourceUpdater(string peFile)
3939 /// </summary>
4040 public ResourceUpdater ( FileStream stream , bool leaveOpen = false )
4141 {
42- this . stream = stream ;
43- this . leaveOpen = leaveOpen ;
42+ #if NET
43+ ArgumentNullException . ThrowIfNull ( stream ) ;
44+ #endif
45+
46+ _stream = stream ;
47+ _leaveOpen = leaveOpen ;
4448 try
4549 {
46- this . stream . Seek ( 0 , SeekOrigin . Begin ) ;
47- _reader = new PEReader ( this . stream , PEStreamOptions . LeaveOpen ) ;
50+ _stream . Seek ( 0 , SeekOrigin . Begin ) ;
51+ _reader = new PEReader ( _stream , PEStreamOptions . LeaveOpen ) ;
4852 _resourceData = new ResourceData ( _reader ) ;
4953 }
5054 catch ( Exception )
5155 {
5256 if ( ! leaveOpen )
53- this . stream ? . Dispose ( ) ;
57+ {
58+ _stream . Dispose ( ) ;
59+ }
5460 throw ;
5561 }
5662 }
@@ -185,12 +191,12 @@ public void Update()
185191
186192 int trailingSectionVirtualStart = rsrcVirtualAddress + rsrcOriginalVirtualSize ;
187193 int trailingSectionStart = rsrcPointerToRawData + rsrcOriginalRawDataSize ;
188- int trailingSectionLength = ( int ) ( stream . Length - trailingSectionStart ) ;
194+ int trailingSectionLength = ( int ) ( _stream . Length - trailingSectionStart ) ;
189195
190196 bool needsMoveTrailingSections = ! isRsrcIsLastSection && delta > 0 ;
191197 long finalImageSize = trailingSectionStart + Math . Max ( delta , 0 ) + trailingSectionLength ;
192198
193- using ( var mmap = MemoryMappedFile . CreateFromFile ( stream , null , finalImageSize , MemoryMappedFileAccess . ReadWrite , HandleInheritability . None , true ) )
199+ using ( var mmap = MemoryMappedFile . CreateFromFile ( _stream , null , finalImageSize , MemoryMappedFileAccess . ReadWrite , HandleInheritability . None , true ) )
194200 using ( MemoryMappedViewAccessor accessor = mmap . CreateViewAccessor ( 0 , finalImageSize , MemoryMappedFileAccess . ReadWrite ) )
195201 {
196202 int peSignatureOffset = ReadI32 ( accessor , PEOffsets . DosStub . PESignatureOffset ) ;
@@ -327,19 +333,10 @@ private static void ThrowExceptionForInvalidUpdate()
327333
328334 public void Dispose ( )
329335 {
330- Dispose ( true ) ;
331- GC . SuppressFinalize ( this ) ;
332- }
333-
334- private void Dispose ( bool disposing )
335- {
336- if ( disposing )
336+ _reader . Dispose ( ) ;
337+ if ( ! _leaveOpen )
337338 {
338- _reader . Dispose ( ) ;
339- if ( ! leaveOpen )
340- {
341- stream . Dispose ( ) ;
342- }
339+ _stream . Dispose ( ) ;
343340 }
344341 }
345342 }
0 commit comments