I\'m looking for the implementation of MemoryStream which does not allocate memory as one big block, but rather a collection of chunks. I want to store a few GB of data in m
Here is a full implementation:
///
/// Defines a MemoryStream that does not sit on the Large Object Heap, thus avoiding memory fragmentation.
///
public sealed class ChunkedMemoryStream : Stream
{
///
/// Defines the default chunk size. Currently defined as 0x10000.
///
public const int DefaultChunkSize = 0x10000; // needs to be < 85000
private List _chunks = new List();
private long _position;
private int _chunkSize;
private int _lastChunkPos;
private int _lastChunkPosIndex;
///
/// Initializes a new instance of the class.
///
public ChunkedMemoryStream()
: this(DefaultChunkSize)
{
}
///
/// Initializes a new instance of the class.
///
/// Size of the underlying chunks.
public ChunkedMemoryStream(int chunkSize)
: this(null)
{
}
///
/// Initializes a new instance of the class based on the specified byte array.
///
/// The array of unsigned bytes from which to create the current stream.
public ChunkedMemoryStream(byte[] buffer)
: this(DefaultChunkSize, buffer)
{
}
///
/// Initializes a new instance of the class based on the specified byte array.
///
/// Size of the underlying chunks.
/// The array of unsigned bytes from which to create the current stream.
public ChunkedMemoryStream(int chunkSize, byte[] buffer)
{
FreeOnDispose = true;
ChunkSize = chunkSize;
_chunks.Add(new byte[chunkSize]);
if (buffer != null)
{
Write(buffer, 0, buffer.Length);
Position = 0;
}
}
///
/// Gets or sets a value indicating whether to free the underlying chunks on dispose.
///
/// true if [free on dispose]; otherwise, false .
public bool FreeOnDispose { get; set; }
///
/// Releases the unmanaged resources used by the and optionally releases the managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool disposing)
{
if (FreeOnDispose)
{
if (_chunks != null)
{
_chunks = null;
_chunkSize = 0;
_position = 0;
}
}
base.Dispose(disposing);
}
///
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// This implementation does nothing.
///
public override void Flush()
{
// do nothing
}
///
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
///
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source.
/// The zero-based byte offset in at which to begin storing the data read from the current stream.
/// The maximum number of bytes to be read from the current stream.
///
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
///
///
/// The sum of and is larger than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// Methods were called after the stream was closed.
///
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset");
if (count < 0)
throw new ArgumentOutOfRangeException("count");
if ((buffer.Length - offset) < count)
throw new ArgumentException(null, "count");
CheckDisposed();
int chunkIndex = (int)(_position / ChunkSize);
if (chunkIndex == _chunks.Count)
return 0;
int chunkPos = (int)(_position % ChunkSize);
count = (int)Math.Min(count, Length - _position);
if (count == 0)
return 0;
int left = count;
int inOffset = offset;
int total = 0;
do
{
int toCopy = Math.Min(left, ChunkSize - chunkPos);
Buffer.BlockCopy(_chunks[chunkIndex], chunkPos, buffer, inOffset, toCopy);
inOffset += toCopy;
left -= toCopy;
total += toCopy;
if ((chunkPos + toCopy) == ChunkSize)
{
if (chunkIndex == (_chunks.Count - 1))
{
// last chunk
break;
}
chunkPos = 0;
chunkIndex++;
}
else
{
chunkPos += toCopy;
}
}
while (left > 0);
_position += total;
return total;
}
///
/// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
///
///
/// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
///
///
/// Methods were called after the stream was closed.
///
public override int ReadByte()
{
CheckDisposed();
if (_position >= Length)
return -1;
byte b = _chunks[(int)(_position / ChunkSize)][_position % ChunkSize];
_position++;
return b;
}
///
/// When overridden in a derived class, sets the position within the current stream.
///
/// A byte offset relative to the parameter.
/// A value of type indicating the reference point used to obtain the new position.
///
/// The new position within the current stream.
///
///
/// Methods were called after the stream was closed.
///
public override long Seek(long offset, SeekOrigin origin)
{
CheckDisposed();
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = Length + offset;
break;
}
return Position;
}
private void CheckDisposed()
{
if (_chunks == null)
throw new ObjectDisposedException(null, "Cannot access a disposed stream");
}
///
/// When overridden in a derived class, sets the length of the current stream.
///
/// The desired length of the current stream in bytes.
///
/// Methods were called after the stream was closed.
///
public override void SetLength(long value)
{
CheckDisposed();
if (value < 0)
throw new ArgumentOutOfRangeException("value");
if (value > Length)
throw new ArgumentOutOfRangeException("value");
long needed = value / ChunkSize;
if ((value % ChunkSize) != 0)
{
needed++;
}
if (needed > int.MaxValue)
throw new ArgumentOutOfRangeException("value");
if (needed < _chunks.Count)
{
int remove = (int)(_chunks.Count - needed);
for (int i = 0; i < remove; i++)
{
_chunks.RemoveAt(_chunks.Count - 1);
}
}
_lastChunkPos = (int)(value % ChunkSize);
}
///
/// Converts the current stream to a byte array.
///
/// An array of bytes
public byte[] ToArray()
{
CheckDisposed();
byte[] bytes = new byte[Length];
int offset = 0;
for (int i = 0; i < _chunks.Count; i++)
{
int count = (i == (_chunks.Count - 1)) ? _lastChunkPos : _chunks[i].Length;
if (count > 0)
{
Buffer.BlockCopy(_chunks[i], 0, bytes, offset, count);
offset += count;
}
}
return bytes;
}
///
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
///
/// An array of bytes. This method copies bytes from to the current stream.
/// The zero-based byte offset in at which to begin copying bytes to the current stream.
/// The number of bytes to be written to the current stream.
///
/// The sum of and is greater than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// Methods were called after the stream was closed.
///
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset");
if (count < 0)
throw new ArgumentOutOfRangeException("count");
if ((buffer.Length - offset) < count)
throw new ArgumentException(null, "count");
CheckDisposed();
int chunkPos = (int)(_position % ChunkSize);
int chunkIndex = (int)(_position / ChunkSize);
if (chunkIndex == _chunks.Count)
{
_chunks.Add(new byte[ChunkSize]);
}
int left = count;
int inOffset = offset;
do
{
int copied = Math.Min(left, ChunkSize - chunkPos);
Buffer.BlockCopy(buffer, inOffset, _chunks[chunkIndex], chunkPos, copied);
inOffset += copied;
left -= copied;
if ((chunkPos + copied) == ChunkSize)
{
chunkIndex++;
chunkPos = 0;
if (chunkIndex == _chunks.Count)
{
_chunks.Add(new byte[ChunkSize]);
}
}
else
{
chunkPos += copied;
}
}
while (left > 0);
_position += count;
if (chunkIndex == (_chunks.Count - 1))
{
if ((chunkIndex > _lastChunkPosIndex) ||
((chunkIndex == _lastChunkPosIndex) && (chunkPos > _lastChunkPos)))
{
_lastChunkPos = chunkPos;
_lastChunkPosIndex = chunkIndex;
}
}
}
///
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
///
/// The byte to write to the stream.
///
/// Methods were called after the stream was closed.
///
public override void WriteByte(byte value)
{
CheckDisposed();
int chunkIndex = (int)(_position / ChunkSize);
int chunkPos = (int)(_position % ChunkSize);
if (chunkPos > (ChunkSize - 1)) //changed from (chunkPos >= (ChunkSize - 1))
{
chunkIndex++;
chunkPos = 0;
if (chunkIndex == _chunks.Count)
{
_chunks.Add(new byte[ChunkSize]);
}
}
_chunks[chunkIndex][chunkPos++] = value;
_position++;
if (chunkIndex == (_chunks.Count - 1))
{
if ((chunkIndex > _lastChunkPosIndex) ||
((chunkIndex == _lastChunkPosIndex) && (chunkPos > _lastChunkPos)))
{
_lastChunkPos = chunkPos;
_lastChunkPosIndex = chunkIndex;
}
}
}
///
/// Writes to the specified stream.
///
/// The stream.
public void WriteTo(Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
CheckDisposed();
for (int i = 0; i < _chunks.Count; i++)
{
int count = (i == (_chunks.Count - 1)) ? _lastChunkPos : _chunks[i].Length;
stream.Write(_chunks[i], 0, count);
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
///
///
/// true if the stream supports reading; otherwise, false.
///
public override bool CanRead
{
get
{
return true;
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
///
///
/// true if the stream supports seeking; otherwise, false.
///
public override bool CanSeek
{
get
{
return true;
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
///
///
/// true if the stream supports writing; otherwise, false.
///
public override bool CanWrite
{
get
{
return true;
}
}
///
/// When overridden in a derived class, gets the length in bytes of the stream.
///
///
///
/// A long value representing the length of the stream in bytes.
///
///
/// Methods were called after the stream was closed.
///
public override long Length
{
get
{
CheckDisposed();
if (_chunks.Count == 0)
return 0;
return (_chunks.Count - 1) * ChunkSize + _lastChunkPos;
}
}
///
/// Gets or sets the size of the underlying chunks. Cannot be greater than or equal to 85000.
///
/// The chunks size.
public int ChunkSize
{
get
{
return _chunkSize;
}
set
{
if ((value <= 0) || (value >= 85000))
throw new ArgumentOutOfRangeException("value");
_chunkSize = value;
}
}
///
/// When overridden in a derived class, gets or sets the position within the current stream.
///
///
///
/// The current position within the stream.
///
///
/// Methods were called after the stream was closed.
///
public override long Position
{
get
{
CheckDisposed();
return _position;
}
set
{
CheckDisposed();
if (value < 0)
throw new ArgumentOutOfRangeException("value");
if (value > Length)
throw new ArgumentOutOfRangeException("value");
_position = value;
}
}
}