Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonio-leonardo/7004756e52d588a5ab1ee706b3c0fda8 to your computer and use it in GitHub Desktop.
Save antonio-leonardo/7004756e52d588a5ab1ee706b3c0fda8 to your computer and use it in GitHub Desktop.
MemoryMappedStream.cs
public class MemoryMappedStream : IDisposable
{
private bool disposedValue;
private readonly MemoryMappedFile _memoryMappedFile;
private MemoryMappedViewAccessor MemoryMappedViewAccessor { get; set; }
public bool IsMemoryNew
{
get
{
return _isMemoryNew;
}
}
private readonly bool _isMemoryNew;
private readonly byte[] _buffer;
public MemoryMappedStream(string memoryName, object data)
{
this._buffer = this.ObjectToByteArray(data);
try
{
this._memoryMappedFile = MemoryMappedFile.OpenExisting(memoryName);
this._isMemoryNew = false;
}
catch
{
this._memoryMappedFile = MemoryMappedFile.CreateNew(memoryName, this._buffer.Length);
this._isMemoryNew = true;
this.WriteObjectToMemoryMappedStream();
}
}
public object ReadObjectFromMemoryMappedStream()
{
if (this._isMemoryNew)
{
return null;
}
this.MemoryMappedViewAccessor = this._memoryMappedFile.CreateViewAccessor();
byte[] buffer = new byte[this.MemoryMappedViewAccessor.Capacity];
this.MemoryMappedViewAccessor.ReadArray<byte>(0, buffer, 0, buffer.Length);
return this.ByteArrayToObject(buffer);
}
public void WriteObjectToMemoryMappedStream()
{
this.MemoryMappedViewAccessor = this._memoryMappedFile.CreateViewAccessor(0, this._buffer.Length);
this.MemoryMappedViewAccessor.WriteArray<byte>(0, this._buffer, 0, this._buffer.Length);
}
private object ByteArrayToObject(byte[] buffer)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
return binaryFormatter.Deserialize(memoryStream);
}
}
private byte[] ObjectToByteArray(object inputObject)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, inputObject);
return memoryStream.ToArray();
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if(null != this.MemoryMappedViewAccessor)
{
this.MemoryMappedViewAccessor.Dispose();
}
if(null != this._memoryMappedFile)
{
this._memoryMappedFile.Dispose();
}
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~MemoryMappedStream()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment