Skip to content
Merged
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BinaryWriter : IDisposable, IAsyncDisposable
private readonly Encoding _encoding;
private readonly bool _leaveOpen;
private readonly bool _useFastUtf8;
private readonly bool _isDerivedWriter;

// Protected default constructor that sets the output stream
// to a null stream (a bit bucket).
Expand All @@ -31,6 +32,7 @@ protected BinaryWriter()
OutStream = Stream.Null;
_encoding = Encoding.UTF8;
_useFastUtf8 = true;
_isDerivedWriter = GetType() != typeof(BinaryWriter);
}

// BinaryWriter never emits a BOM, so can use Encoding.UTF8 fast singleton
Expand All @@ -54,6 +56,7 @@ public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
_encoding = encoding;
_leaveOpen = leaveOpen;
_useFastUtf8 = encoding.IsUTF8CodePage && encoding.EncoderFallback.MaxCharCount <= 1;
_isDerivedWriter = GetType() != typeof(BinaryWriter);
}

// Closes this writer and releases any system resources associated with the
Expand Down Expand Up @@ -353,7 +356,10 @@ public virtual void Write(string value)

if (_useFastUtf8)
{
if (value.Length <= 127 / 3)
// If this is a non-derived BinaryWriter, then we can bypass the Write7BitEncodedInt call.
// But when this is a derived instance, call must not bypass it for compatibility reasons
// as it calls the virtual Write(int) overload.
if (!_isDerivedWriter && value.Length <= 127 / 3)
{
// Max expansion: each char -> 3 bytes, so 127 bytes max of data, +1 for length prefix
Span<byte> buffer = stackalloc byte[128];
Expand Down