-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It focuses on low memory usage
- Loading branch information
Showing
16 changed files
with
3,033 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace OpenMcdf | ||
{ | ||
class ByteArrayPool : IByteArrayPool | ||
{ | ||
public byte[] Rent(int minimumLength) | ||
{ | ||
for (var i = 0; i < _bufferList.Length; i++) | ||
{ | ||
var buffer = _bufferList[i]; | ||
|
||
if (buffer != null && buffer.Length >= minimumLength) | ||
{ | ||
_bufferList[i] = null; | ||
return buffer; | ||
} | ||
} | ||
|
||
return new byte[minimumLength]; | ||
} | ||
|
||
public void Return(byte[] byteList) | ||
{ | ||
if (byteList == null || byteList.Length >= MaxBufferLength) | ||
{ | ||
return; | ||
} | ||
|
||
for (var i = 0; i < _bufferList.Length; i++) | ||
{ | ||
var buffer = _bufferList[i]; | ||
if (buffer is null || byteList.Length > buffer.Length) | ||
{ | ||
buffer = byteList; | ||
_bufferList[i] = buffer; | ||
} | ||
} | ||
} | ||
|
||
private readonly byte[][] _bufferList = new byte[4][]; | ||
private const int MaxBufferLength = 8 * 1024; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace OpenMcdf | ||
{ | ||
class ForwardSeekStream : Stream | ||
{ | ||
public ForwardSeekStream(Stream sourceStream, IByteArrayPool byteArrayPool) | ||
{ | ||
_sourceStream = sourceStream; | ||
_byteArrayPool = byteArrayPool; | ||
} | ||
|
||
public override void Flush() | ||
{ | ||
_sourceStream.Flush(); | ||
} | ||
|
||
public long CurrentPosition { private set; get; } | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
var n = _sourceStream.Read(buffer, offset, count); | ||
CurrentPosition += n; | ||
return n; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
if (offset == 0 && origin == SeekOrigin.Begin) | ||
{ | ||
return CurrentPosition; | ||
} | ||
|
||
if (offset == CurrentPosition) return CurrentPosition; | ||
if (offset > CurrentPosition) | ||
{ | ||
int length = (int) (offset - CurrentPosition); | ||
|
||
var currentReadCount = 0; | ||
const int defaultMaxBufferLength = 4096; | ||
var bufferLength = Math.Min(length, defaultMaxBufferLength); | ||
var byteList = _byteArrayPool.Rent(bufferLength); | ||
while (currentReadCount < length) | ||
{ | ||
var size = length - currentReadCount; | ||
size = Math.Min(size, bufferLength); | ||
|
||
currentReadCount += Read(byteList, 0, size); | ||
} | ||
|
||
_byteArrayPool.Return(byteList); | ||
|
||
Debug.Assert(offset == CurrentPosition); | ||
return CurrentPosition; | ||
} | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
_sourceStream.SetLength(value); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_sourceStream.Write(buffer, offset, count); | ||
} | ||
|
||
public override bool CanRead => _sourceStream.CanRead; | ||
|
||
public override bool CanSeek => true; | ||
|
||
public override bool CanWrite => _sourceStream.CanWrite; | ||
|
||
public override long Length => _sourceStream.Length; | ||
|
||
public override long Position | ||
{ | ||
get => _sourceStream.Position; | ||
set => _sourceStream.Position = value; | ||
} | ||
|
||
private readonly Stream _sourceStream; | ||
private readonly IByteArrayPool _byteArrayPool; | ||
} | ||
} |
Oops, something went wrong.