public static async Task ReadBytesAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { if (stream == null) { throw new ArgumentNullException("stream"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || offset + count > buffer.Length) { throw new ArgumentOutOfRangeException("count"); } while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToRead = Math.Min(count, backingBytes.Count); var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, cancellationToken).ConfigureAwait(false); if (bytesRead == 0) { throw new EndOfStreamException(); } offset += bytesRead; count -= bytesRead; } }
public static async Task WriteBytesAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { if (stream == null) { throw new ArgumentNullException("stream"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || offset + count > buffer.Length) { throw new ArgumentOutOfRangeException("count"); } while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToWrite = Math.Min(count, backingBytes.Count); await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, cancellationToken).ConfigureAwait(false); offset += bytesToWrite; count -= bytesToWrite; } }
/// <inheritdoc/> public ArraySegment <byte> AccessBackingBytes(int position) { EnsureValidPosition(position); ThrowIfDisposed(); var segment = _buffer.AccessBackingBytes(position + _offset); var count = Math.Min(segment.Count, _length - position); return(new ArraySegment <byte>(segment.Array, segment.Offset, count)); }
public static async Task WriteBufferAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToWrite = Math.Min(count, backingBytes.Count - backingBytes.Offset); await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, cancellationToken); offset += bytesToWrite; count -= bytesToWrite; } }
public static async Task WriteBufferAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToWrite = Math.Min(count, backingBytes.Count - backingBytes.Offset); await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, cancellationToken).ConfigureAwait(false); offset += bytesToWrite; count -= bytesToWrite; } }
public static async Task FillBufferAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToRead = Math.Min(count, backingBytes.Count); var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, cancellationToken); if (bytesRead == 0) { throw new EndOfStreamException(); } offset += bytesRead; count -= bytesRead; } }
public static async Task FillBufferAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToRead = Math.Min(count, backingBytes.Count); var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, cancellationToken).ConfigureAwait(false); if (bytesRead == 0) { throw new EndOfStreamException(); } offset += bytesRead; count -= bytesRead; } }
public static void WriteBytes(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { Ensure.IsNotNull(stream, nameof(stream)); Ensure.IsNotNull(buffer, nameof(buffer)); Ensure.IsBetween(offset, 0, buffer.Length, nameof(offset)); Ensure.IsBetween(count, 0, buffer.Length - offset, nameof(count)); while (count > 0) { cancellationToken.ThrowIfCancellationRequested(); var backingBytes = buffer.AccessBackingBytes(offset); var bytesToWrite = Math.Min(count, backingBytes.Count); stream.Write(backingBytes.Array, backingBytes.Offset, bytesToWrite); // TODO: honor cancellationToken? offset += bytesToWrite; count -= bytesToWrite; } }
public static async Task WriteBytesAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { Ensure.IsNotNull(stream, nameof(stream)); Ensure.IsNotNull(buffer, nameof(buffer)); Ensure.IsBetween(offset, 0, buffer.Length, nameof(offset)); Ensure.IsBetween(count, 0, buffer.Length - offset, nameof(count)); while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToWrite = Math.Min(count, backingBytes.Count); await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, cancellationToken).ConfigureAwait(false); offset += bytesToWrite; count -= bytesToWrite; } }
public static void ReadBytes(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { Ensure.IsNotNull(stream, nameof(stream)); Ensure.IsNotNull(buffer, nameof(buffer)); Ensure.IsBetween(offset, 0, buffer.Length, nameof(offset)); Ensure.IsBetween(count, 0, buffer.Length - offset, nameof(count)); while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToRead = Math.Min(count, backingBytes.Count); var bytesRead = stream.Read(backingBytes.Array, backingBytes.Offset, bytesToRead); // TODO: honor cancellationToken? if (bytesRead == 0) { throw new EndOfStreamException(); } offset += bytesRead; count -= bytesRead; } }
public static async Task ReadBytesAsync(this Stream stream, IByteBuffer buffer, int offset, int count, CancellationToken cancellationToken) { Ensure.IsNotNull(stream, nameof(stream)); Ensure.IsNotNull(buffer, nameof(buffer)); Ensure.IsBetween(offset, 0, buffer.Length, nameof(offset)); Ensure.IsBetween(count, 0, buffer.Length - offset, nameof(count)); while (count > 0) { var backingBytes = buffer.AccessBackingBytes(offset); var bytesToRead = Math.Min(count, backingBytes.Count); var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, cancellationToken).ConfigureAwait(false); if (bytesRead == 0) { throw new EndOfStreamException(); } offset += bytesRead; count -= bytesRead; } }
/// <summary> /// Writes a slice to the stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="slice">The slice.</param> public static void WriteSlice(this BsonStream stream, IByteBuffer slice) { if (stream == null) { throw new ArgumentNullException("stream"); } if (slice == null) { throw new ArgumentNullException("slice"); } var position = 0; var count = slice.Length; while (count > 0) { var segment = slice.AccessBackingBytes(position); var partialCount = Math.Min(count, segment.Count); stream.WriteBytes(segment.Array, segment.Offset, partialCount); position += count; count -= partialCount; } }
private int FindNullByte() { var position = _position; while (position < _length) { var segment = _buffer.AccessBackingBytes(position); var endOfSegmentIndex = segment.Offset + segment.Count; for (var index = segment.Offset; index < endOfSegmentIndex; index++) { if (segment.Array[index] == 0) { return(position + (index - segment.Offset)); } } position += segment.Count; } throw new EndOfStreamException(); }
/// <summary> /// Writes a slice to the stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="slice">The slice.</param> public static void WriteSlice(this BsonStream stream, IByteBuffer slice) { if (stream == null) { throw new ArgumentNullException("stream"); } if (slice == null) { throw new ArgumentNullException("slice"); } var position = 0; var count = slice.Length; while (count > 0) { var segment = slice.AccessBackingBytes(position); var partialCount = Math.Min(count, segment.Count); stream.WriteBytes(segment.Array, segment.Offset, partialCount); position += partialCount; count -= partialCount; } }
// private methods private int GetResponseTo(IByteBuffer message) { var backingBytes = message.AccessBackingBytes(8); return(BitConverter.ToInt32(backingBytes.Array, backingBytes.Offset)); }
// public methods /// <inheritdoc/> public void EfficientCopyTo(Stream destination) { long remainingCount; while ((remainingCount = Length - Position) > 0) { var segment = _buffer.AccessBackingBytes((int)Position); var count = (int)Math.Min(segment.Count, remainingCount); destination.Write(segment.Array, segment.Offset, count); Position += count; } }
// private methods private int GetResponseTo(IByteBuffer message) { var backingBytes = message.AccessBackingBytes(8); return BitConverter.ToInt32(backingBytes.Array, backingBytes.Offset); }