diff --git a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs index a401c853f491..dfcc05d066f6 100644 --- a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs +++ b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs @@ -29,12 +29,11 @@ internal PinnedBufferMemoryStream(byte[] array) { Debug.Assert(array != null, "Array can't be null"); - int len = array.Length; - _array = array; _pinningHandle = GCHandle.Alloc(array, GCHandleType.Pinned); // Now the byte[] is pinned for the lifetime of this instance. // But I also need to get a pointer to that block of memory... + int len = array.Length; fixed (byte* ptr = &MemoryMarshal.GetReference((Span)array)) Initialize(ptr, len, len, FileAccess.Read); } diff --git a/src/mscorlib/src/System/IO/Stream.cs b/src/mscorlib/src/System/IO/Stream.cs index 8a0759e57cce..d9ed08f737e0 100644 --- a/src/mscorlib/src/System/IO/Stream.cs +++ b/src/mscorlib/src/System/IO/Stream.cs @@ -767,16 +767,17 @@ public virtual int Read(Span destination) // Reads one byte from the stream by calling Read(byte[], int, int). // Will return an unsigned byte cast to an int or -1 on end of stream. - // This implementation should be overridden by any + // This implementation does not perform well because it allocates a new + // byte[] each time you call it, and should be overridden by any // subclass that maintains an internal buffer. Then, it can help perf // significantly for people who are reading one byte at a time. public virtual int ReadByte() { - Span bytes = stackalloc byte[1]; - int r = Read(bytes); + byte[] oneByteArray = new byte[1]; + int r = Read(oneByteArray, 0, 1); if (r == 0) return -1; - return bytes[0]; + return oneByteArray[0]; } public abstract void Write(byte[] buffer, int offset, int count); @@ -793,14 +794,15 @@ public virtual void Write(ReadOnlySpan source) } // Writes one byte from the stream by calling Write(byte[], int, int). - // This implementation should be overridden by any + // This implementation does not perform well because it allocates a new + // byte[] each time you call it, and should be overridden by any // subclass that maintains an internal buffer. Then, it can help perf // significantly for people who are writing one byte at a time. public virtual void WriteByte(byte value) { - Span bytes = stackalloc byte[1]; - bytes[0] = value; - Write(bytes); + byte[] oneByteArray = new byte[1]; + oneByteArray[0] = value; + Write(oneByteArray, 0, 1); } public static Stream Synchronized(Stream stream) diff --git a/src/mscorlib/src/System/StubHelpers.cs b/src/mscorlib/src/System/StubHelpers.cs index 90cf92552f96..ae614d87ac4b 100644 --- a/src/mscorlib/src/System/StubHelpers.cs +++ b/src/mscorlib/src/System/StubHelpers.cs @@ -46,7 +46,7 @@ unsafe static internal byte ConvertToNative(char managedChar, bool fBestFit, boo static internal char ConvertToManaged(byte nativeChar) { - Span bytes = stackalloc byte[1]; + Span bytes = new Span(ref nativeChar, 1); bytes[0] = nativeChar; string str = Encoding.Default.GetString(bytes); return str[0];