Skip to content
Merged
Show file tree
Hide file tree
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
64 changes: 38 additions & 26 deletions System.IO.Streams/MemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
public MemoryStream(byte[] buffer, bool isWritable)
{
_buffer = buffer ?? throw new ArgumentNullException();
ArgumentNullException.ThrowIfNull(buffer);

_buffer = buffer;
_length = _capacity = buffer.Length;
_expandable = false;
_origin = 0;
Expand Down Expand Up @@ -115,7 +116,7 @@
/// <value><see langword="true"/> if the stream supports writing; otherwise, <see langword="false"/>.</value>
/// <remarks>
/// <para>
/// If a class derived from <see cref="Stream"/> does not support reading, calls to the <see cref="SetLength(long)"/> and <see cref="Write"/> or <see cref="WriteByte"/> methods throw a <see cref="NotSupportedException"/>.

Check warning on line 119 in System.IO.Streams/MemoryStream.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Ambiguous reference in cref attribute: 'Write'. Assuming 'MemoryStream.Write(byte[], int, int)', but could have also matched other overloads including 'MemoryStream.Write(ReadOnlySpan<byte>)'.

See more on https://sonarcloud.io/project/issues?id=nanoframework_System.IO.Streams&issues=AZsUEZbhIU3dcu2ISl98&open=AZsUEZbhIU3dcu2ISl98&pullRequest=87

Check warning on line 119 in System.IO.Streams/MemoryStream.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

XML comment has cref attribute 'WriteByte' that could not be resolved

See more on https://sonarcloud.io/project/issues?id=nanoframework_System.IO.Streams&issues=AZsUEZbhIU3dcu2ISl99&open=AZsUEZbhIU3dcu2ISl99&pullRequest=87
/// </para>
/// <para>
/// If the stream is closed, this property returns <see langword="false"/>.
Expand Down Expand Up @@ -181,7 +182,7 @@
{
EnsureOpen();

if (value < 0 || value > MemStreamMaxLength)
if (value is < 0 or > MemStreamMaxLength)
{
throw new ArgumentOutOfRangeException();
}
Expand All @@ -196,7 +197,7 @@
{
EnsureOpen();

var bytesToRead = _length - _position;
int bytesToRead = _length - _position;

if (bytesToRead > buffer.Length)
{
Expand All @@ -209,7 +210,6 @@
}

new Span<byte>(_buffer, _position, bytesToRead).CopyTo(buffer);

_position += bytesToRead;

return bytesToRead;
Expand Down Expand Up @@ -384,10 +384,7 @@
EnsureOpen();
EnsureWritable();

if (buffer == null)
{
throw new ArgumentNullException();
}
ArgumentNullException.ThrowIfNull(buffer);

if (offset < 0 || count < 0)
{
Expand All @@ -399,44 +396,59 @@
throw new ArgumentException();
}

int i = _position + count;
int newPosition = _position + count;

// check for overflow
if (i > _length)
if (newPosition > _length)
{
if (i > _capacity)
if (newPosition > _capacity)
{
EnsureCapacity(i);
EnsureCapacity(newPosition);
}

_length = i;
_length = newPosition;
}

Array.Copy(buffer, offset, _buffer, _position, count);
_position = i;
new Span<byte>(buffer, offset, count).CopyTo(new Span<byte>(_buffer, _position, count));
_position = newPosition;
}

/// <inheritdoc/>
/// <exception cref="ObjectDisposedException"></exception>
/// <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>
/// <exception cref="ObjectDisposedException">The current stream instance is closed.</exception>
/// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the
/// data and is not expandable, and/or the stream is not writable.</exception>
/// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded</exception>
public override void WriteByte(byte value)
/// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded.</exception>
/// <remarks>
/// Use the <see cref="CanWrite"/> property to determine whether the current instance supports writing.
/// If the write operation is successful, the position within the stream advances by the number of bytes written.
/// If an exception occurs, the position within the stream remains unchanged.
/// </remarks>
public override void Write(ReadOnlySpan<byte> buffer)
{
EnsureOpen();
EnsureWritable();

if (_position >= _capacity)
int count = buffer.Length;
int newPosition = _position + count;

// check for overflow
if (newPosition > _length)
{
EnsureCapacity(_position + 1);
if (newPosition > _capacity)
{
EnsureCapacity(newPosition);
}

_length = newPosition;
}

_buffer[_position++] = value;
// Copy ReadOnlySpan to buffer
buffer.CopyTo(new Span<byte>(_buffer, _position, count));

if (_position > _length)
{
_length = _position;
}
_position = newPosition;
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions System.IO.Streams/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand All @@ -15,3 +18,6 @@
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Mark the assembly as CLS compliant
[assembly: CLSCompliant(true)]

25 changes: 19 additions & 6 deletions System.IO.Streams/SeekOrigin.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.IO
{
/// <summary>
/// Specifies the position in a stream to use for seeking.
/// </summary>
/// <remarks>These constants match Win32's FILE_BEGIN, FILE_CURRENT, and FILE_END</remarks>
/// <remarks>
/// <para>
/// <see cref="SeekOrigin"/> is used by the Seek methods of <see cref="Stream"/>, <see cref="MemoryStream"/>, and other streams.
/// </para>
/// <para>
/// These constants match Win32's FILE_BEGIN, FILE_CURRENT, and FILE_END values.
/// </para>
/// </remarks>
[Serializable]
public enum SeekOrigin
{
/// <summary>
/// Specifies the beginning of a stream.
/// </summary>
/// <remarks>
/// Seeking to the beginning of a stream sets the position to zero.
/// </remarks>
Begin = 0,

/// <summary>
/// Specifies the current position within a stream.
/// </summary>
/// <remarks>
/// Seeking relative to the current position allows you to move forward or backward from the current position by specifying a positive or negative offset.
/// </remarks>
Current = 1,

/// <summary>
/// Specifies the end of a stream.
/// </summary>
/// <remarks>
/// Seeking relative to the end of a stream allows you to position at or before the end by specifying a zero or negative offset.
/// </remarks>
End = 2,
}
}
Loading