Skip to content

Commit

Permalink
Made MemoryStream provision pluggable.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Jun 16, 2016
1 parent 6fb0e0d commit b295908
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1,719 deletions.
32 changes: 14 additions & 18 deletions Src/Workshell.PE/ImageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Text;
using System.Threading.Tasks;

using Microsoft.IO;

using Workshell.PE.Native;

namespace Workshell.PE
Expand All @@ -16,7 +14,7 @@ namespace Workshell.PE
public class ImageReader : IDisposable, ISupportsBytes
{

class PreloadedInformation
private class PreloadedInformation
{

public IMAGE_DOS_HEADER DOSHeader;
Expand All @@ -35,12 +33,11 @@ class PreloadedInformation

}

static RecyclableMemoryStreamManager memory_manager;

private bool _disposed;
private Stream _stream;
private bool _own_stream;
private LocationCalculator _calc;
private IMemoryStreamProvider _memory_stream_provider;

private DOSHeader _dos_header;
private DOSStub _dos_stub;
Expand All @@ -53,17 +50,13 @@ class PreloadedInformation
private bool _is_clr;
private bool _is_signed;

static ImageReader()
{
memory_manager = new RecyclableMemoryStreamManager();
}

private ImageReader(Stream sourceStream, bool ownStream)
{
_disposed = false;
_stream = sourceStream;
_own_stream = ownStream;
_calc = null;
_memory_stream_provider = new DefaultMemoryStreamProvider();

_dos_header = null;
_dos_stub = null;
Expand Down Expand Up @@ -348,7 +341,7 @@ public Stream GetStream()

public byte[] GetBytes()
{
using (var mem = memory_manager.GetStream())
using (var mem = _memory_stream_provider.GetStream())
{
_stream.Seek(0,SeekOrigin.Begin);
_stream.CopyTo(mem,4096);
Expand Down Expand Up @@ -410,19 +403,22 @@ private void Load()

#endregion

#region Static Properties
#region Properties

internal static RecyclableMemoryStreamManager MemoryManager
public IMemoryStreamProvider MemoryStreamProvider
{
get
{
return memory_manager;
return _memory_stream_provider;
}
}

#endregion
set
{
_memory_stream_provider = value;

#region Properties
if (_memory_stream_provider == null)
_memory_stream_provider = new DefaultMemoryStreamProvider();
}
}

public bool Is32Bit
{
Expand Down
34 changes: 34 additions & 0 deletions Src/Workshell.PE/MemoryStreamProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Workshell.PE
{

public interface IMemoryStreamProvider
{

MemoryStream GetStream();
MemoryStream GetStream(byte[] buffer);

}

public sealed class DefaultMemoryStreamProvider : IMemoryStreamProvider
{

public MemoryStream GetStream()
{
return new MemoryStream();
}

public MemoryStream GetStream(byte[] buffer)
{
return new MemoryStream(buffer);
}

}

}
153 changes: 0 additions & 153 deletions Src/Workshell.PE/Microsoft/Events.cs

This file was deleted.

Loading

0 comments on commit b295908

Please sign in to comment.