BinaryReader: Use cached default encoding instance (#8017)
authorJustin Van Patten <jvp@justinvp.com>
Thu, 10 Nov 2016 14:58:33 +0000 (06:58 -0800)
committerJan Kotas <jkotas@microsoft.com>
Thu, 10 Nov 2016 14:58:33 +0000 (06:58 -0800)
A new instance of UTF8Encoding used to be created every time
BinaryReader.ctor(Stream) was called, which created an instance of
UTF8Encoding that has no preamble. However, BinaryReader does not use
the preamble at all, so it doesn't matter if the encoding has a preamble
or not. Thus, the cached Encoding.UTF8 instance can be used (which has a
preamble).

src/mscorlib/src/System/IO/BinaryReader.cs

index ef8245f..c7eff0f 100644 (file)
@@ -40,7 +40,7 @@ namespace System.IO {
         private bool     m_isMemoryStream; // "do we sit on MemoryStream?" for Read/ReadInt32 perf
         private bool     m_leaveOpen;
 
-        public BinaryReader(Stream input) : this(input, new UTF8Encoding(), false) {
+        public BinaryReader(Stream input) : this(input, Encoding.UTF8, false) {
         }
 
         public BinaryReader(Stream input, Encoding encoding) : this(input, encoding, false) {