Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jan 3, 2018
1 parent 679ed59 commit fb4e6e2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>)array))
Initialize(ptr, len, len, FileAccess.Read);
}
Expand Down
18 changes: 10 additions & 8 deletions src/mscorlib/src/System/IO/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,16 +767,17 @@ public virtual int Read(Span<byte> 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<byte> 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);
Expand All @@ -793,14 +794,15 @@ public virtual void Write(ReadOnlySpan<byte> 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<byte> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/mscorlib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ unsafe static internal byte ConvertToNative(char managedChar, bool fBestFit, boo

static internal char ConvertToManaged(byte nativeChar)
{
Span<byte> bytes = stackalloc byte[1];
Span<byte> bytes = new Span<byte>(ref nativeChar, 1);
bytes[0] = nativeChar;
string str = Encoding.Default.GetString(bytes);
return str[0];
Expand Down

0 comments on commit fb4e6e2

Please sign in to comment.