-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from soukoku/v4-support-bg-handling
Added possibility to handle transferred data by app in another thread…
- Loading branch information
Showing
7 changed files
with
154 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,31 +1,63 @@ | ||
using System; | ||
using System.Buffers; | ||
|
||
namespace NTwain.Data | ||
{ | ||
/// <summary> | ||
/// Simple struct with bytes buffer and the valid data length. | ||
/// Simple thing with shared bytes buffer and the valid data length. | ||
/// </summary> | ||
public struct BufferedData | ||
public class BufferedData : IDisposable | ||
{ | ||
// experiment using array pool for things transferred in memory. | ||
// this can pool up to a "normal" max of legal size paper in 24 bit at 300 dpi (~31MB) | ||
// so the array max is made with 32 MB. Typical usage should be a lot less. | ||
internal static readonly ArrayPool<byte> MemPool = ArrayPool<byte>.Create(32 * 1024 * 1024, 8); | ||
|
||
public BufferedData(int size) | ||
{ | ||
_buffer = MemPool.Rent(size); | ||
_length = size; | ||
_fromPool = true; | ||
} | ||
|
||
internal BufferedData(byte[] data, int size, bool fromPool) | ||
{ | ||
_buffer = data; | ||
_length = size; | ||
_fromPool = fromPool; | ||
} | ||
|
||
bool _fromPool; | ||
|
||
/// <summary> | ||
/// Bytes buffer. This may be bigger than the data size | ||
/// and contain invalid data. | ||
/// </summary> | ||
public byte[]? Buffer; | ||
byte[]? _buffer; | ||
public byte[]? Buffer { get; } | ||
|
||
/// <summary> | ||
/// Actual usable data length in the buffer. | ||
/// </summary> | ||
public int Length; | ||
int _length; | ||
|
||
/// <summary> | ||
/// As a span of usable data. | ||
/// </summary> | ||
/// <returns></returns> | ||
public ReadOnlySpan<byte> AsSpan() | ||
{ | ||
if (Buffer != null) return Buffer.AsSpan(0, Length); | ||
if (_buffer != null) return _buffer.AsSpan(0, _length); | ||
return Span<byte>.Empty; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_fromPool && _buffer != null) | ||
{ | ||
MemPool.Return(_buffer); | ||
_buffer = null; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.