Ejemplo n.º 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
            }
            if ((uint)offset > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.offset);
            }
            if ((uint)count > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.count);
            }
            if ((uint)buffer.Length < (uint)(offset + count))
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            EnsureNotClosed();

            int read = Math.Min(count, _buffer.ReadableBytes);

            if (0u >= (uint)read)
            {
                return(0);
            }
            _ = _buffer.ReadBytes(buffer, offset, read);
            return(read);
        }
Ejemplo n.º 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
            }
            if (offset < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.offset);
            }
            if (count < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.count);
            }
            if (buffer.Length - offset < count)
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            EnsureNotClosed();

            int read = Math.Min(count, _buffer.ReadableBytes);

            if (0u >= (uint)read)
            {
                return(0);
            }
            _ = _buffer.ReadBytes(buffer, offset, read);
            return(read);
        }
Ejemplo n.º 3
0
        public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (buffer is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
            }
            if ((uint)offset > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.offset);
            }
            if ((uint)count > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.count);
            }
            if ((uint)buffer.Length < (uint)(offset + count))
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            EnsureNotClosed();
            EnsureWriteable();

            // If cancellation is already requested, bail early
            if (cancellationToken.IsCancellationRequested)
            {
                return(TaskUtil.FromCanceled(cancellationToken));
            }

            try
            {
                _ = _buffer.WriteBytes(buffer, offset, count);
                return(TaskUtil.Completed);
            }
            //catch (OperationCanceledException oce)
            //{
            //    return Task.FromCancellation<VoidTaskResult>(oce);
            //}
            catch (Exception exception)
            {
                return(TaskUtil.FromException(exception));
            }
        }
Ejemplo n.º 4
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
            }
            if ((uint)offset > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.offset);
            }
            if ((uint)count > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.count);
            }
            if ((uint)buffer.Length < (uint)(offset + count))
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            EnsureNotClosed();
            EnsureWriteable();

            _ = _buffer.WriteBytes(buffer, offset, count);
        }
Ejemplo n.º 5
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
            }
            if (offset < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.offset);
            }
            if (count < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum(ExceptionArgument.count);
            }
            if (buffer.Length - offset < count)
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            EnsureNotClosed();
            EnsureWriteable();

            _ = _buffer.WriteBytes(buffer, offset, count);
        }