Skip to content

Commit

Permalink
Microsoft.Data.Sqlite: Add Span overloads to SqliteBlob
Browse files Browse the repository at this point in the history
Fixes #16187
  • Loading branch information
bricelam committed Mar 25, 2021
1 parent f3fc0ce commit 08be92c
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ public override int Read(byte[] buffer, int offset, int count)
throw new ArgumentException(Resources.InvalidOffsetAndCount);
}

return Read(buffer.AsSpan(offset, count));
}

/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the
/// number of bytes read.
/// </summary>
/// <param name="buffer">
/// A region of memory. When this method returns, the contents of this region are replaced by the bytes read
/// from the current source.
/// </param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes allocated in
/// the buffer if that many bytes are not currently available, or zero (0) if the end of the stream has been
/// reached.
/// </returns>
#if NET
public override int Read(Span<byte> buffer)
#else
public virtual int Read(Span<byte> buffer)
#endif
{
if (_blob == null)
{
throw new ObjectDisposedException(objectName: null);
Expand All @@ -178,12 +200,13 @@ public override int Read(byte[] buffer, int offset, int count)
position = Length;
}

var count = buffer.Length;
if (position + count > Length)
{
count = (int)(Length - position);
}

var rc = sqlite3_blob_read(_blob, buffer.AsSpan(offset, count), (int)position);
var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position);
SqliteException.ThrowExceptionForRC(rc, _db);
_position += count;
return count;
Expand All @@ -198,11 +221,6 @@ public override int Read(byte[] buffer, int offset, int count)
/// <param name="count">The number of bytes to be written to the current stream.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (!CanWrite)
{
throw new NotSupportedException(Resources.WriteNotSupported);
}

if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
Expand All @@ -229,18 +247,40 @@ public override void Write(byte[] buffer, int offset, int count)
throw new ObjectDisposedException(objectName: null);
}

Write(buffer.AsSpan(offset, count));
}

/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by
/// the number of bytes written.
/// </summary>
/// <param name="buffer">
/// A region of memory. This method copies the contents of this region to the current stream.
/// </param>
#if NET
public override void Write(ReadOnlySpan<byte> buffer)
#else
public virtual void Write(ReadOnlySpan<byte> buffer)
#endif
{
if (!CanWrite)
{
throw new NotSupportedException(Resources.WriteNotSupported);
}

var position = _position;
if (position > Length)
{
position = Length;
}

var count = buffer.Length;
if (position + count > Length)
{
throw new NotSupportedException(Resources.ResizeNotSupported);
}

var rc = sqlite3_blob_write(_blob, buffer.AsSpan(offset, count), (int)position);
var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position);
SqliteException.ThrowExceptionForRC(rc, _db);
_position += count;
}
Expand Down

0 comments on commit 08be92c

Please sign in to comment.