Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Data.Sqlite: Add Span overloads to SqliteBlob #24503

Merged
1 commit merged into from
Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
bricelam marked this conversation as resolved.
Show resolved Hide resolved
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