Replace UTF8Encoding(false) with Encoding.Default (#18144)

This commit is contained in:
xtqqczze
2022-09-27 10:47:55 +05:00
committed by GitHub
parent dfa012d048
commit fd67038ed8
5 changed files with 23 additions and 16 deletions
@@ -4340,7 +4340,7 @@ end {
// Variable which controls the encoding for piping data to a NativeCommand
new SessionStateVariableEntry(
SpecialVariables.OutputEncoding,
Utils.utf8NoBom,
Encoding.Default,
RunspaceInit.OutputEncodingDescription,
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding))),
@@ -2057,11 +2057,12 @@ namespace System.Management.Automation
// Get the encoding for writing to native command. Note we get the Encoding
// from the current scope so a script or function can use a different encoding
// than global value.
Encoding pipeEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as System.Text.Encoding ??
Utils.utf8NoBom;
Encoding outputEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as Encoding;
_streamWriter = new StreamWriter(process.StandardInput.BaseStream, pipeEncoding);
_streamWriter.AutoFlush = true;
_streamWriter = new StreamWriter(process.StandardInput.BaseStream, outputEncoding ?? Encoding.Default)
{
AutoFlush = true
};
_inputFormat = inputFormat;
@@ -1380,9 +1380,6 @@ namespace System.Management.Automation
// Add-Member ScriptProperty Preamble { $this.GetEncoding().GetPreamble() -join "-" } -PassThru |
// Format-Table -Auto
internal static readonly UTF8Encoding utf8NoBom =
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
#if !UNIX
/// <summary>
/// Queues a CLR worker thread with impersonation of provided Windows identity.
@@ -1243,7 +1243,7 @@ namespace System.Management.Automation.Host
{
static Encoding GetPathEncoding(string path)
{
using StreamReader reader = new StreamReader(path, Utils.utf8NoBom, detectEncodingFromByteOrderMarks: true);
using StreamReader reader = new StreamReader(path, Encoding.Default, detectEncodingFromByteOrderMarks: true);
_ = reader.Read();
return reader.CurrentEncoding;
}
@@ -1271,7 +1271,7 @@ namespace System.Management.Automation.Host
// file permissions.
_contentWriter = new StreamWriter(
new FileStream(this.Path, FileMode.Append, FileAccess.Write, FileShare.Read),
Utils.utf8NoBom);
Encoding.Default);
}
_contentWriter.AutoFlush = true;
@@ -18,6 +18,7 @@ using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using Microsoft.Win32.SafeHandles;
@@ -2404,23 +2405,31 @@ namespace System.Management.Automation.Runspaces
if (startInfo.RedirectStandardInput)
{
Debug.Assert(stdinFd >= 0, "Invalid Fd");
standardInput = new StreamWriter(OpenStream(stdinFd, FileAccess.Write),
Utils.utf8NoBom, StreamBufferSize)
standardInput = new StreamWriter(
OpenStream(stdinFd, FileAccess.Write),
Encoding.Default,
StreamBufferSize)
{ AutoFlush = true };
}
if (startInfo.RedirectStandardOutput)
{
Debug.Assert(stdoutFd >= 0, "Invalid Fd");
standardOutput = new StreamReader(OpenStream(stdoutFd, FileAccess.Read),
startInfo.StandardOutputEncoding ?? Utils.utf8NoBom, true, StreamBufferSize);
standardOutput = new StreamReader(
OpenStream(stdoutFd, FileAccess.Read),
startInfo.StandardOutputEncoding ?? Encoding.Default,
detectEncodingFromByteOrderMarks: true,
StreamBufferSize);
}
if (startInfo.RedirectStandardError)
{
Debug.Assert(stderrFd >= 0, "Invalid Fd");
standardError = new StreamReader(OpenStream(stderrFd, FileAccess.Read),
startInfo.StandardErrorEncoding ?? Utils.utf8NoBom, true, StreamBufferSize);
standardError = new StreamReader(
OpenStream(stderrFd, FileAccess.Read),
startInfo.StandardErrorEncoding ?? Encoding.Default,
detectEncodingFromByteOrderMarks: true,
StreamBufferSize);
}
return childPid;