From 66f3e3c91fc966db11f692da9f17f4460d64d3c2 Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Fri, 10 Jan 2025 20:40:42 -0700 Subject: [PATCH] Use Encoding.Default instead of GetEncoding(0), #1076 --- .../ByTask/Feeds/ContentItemsSource.cs | 6 +++--- src/Lucene.Net.Benchmark/ByTask/Tasks/CreateIndexTask.cs | 2 +- src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs | 9 ++++++--- .../ByTask/Tasks/CreateIndexTaskTest.cs | 4 ++-- src/Lucene.Net.Tests.Demo/TestDemo.cs | 6 ++---- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Lucene.Net.Benchmark/ByTask/Feeds/ContentItemsSource.cs b/src/Lucene.Net.Benchmark/ByTask/Feeds/ContentItemsSource.cs index 7932f749ec..4b50076cdf 100644 --- a/src/Lucene.Net.Benchmark/ByTask/Feeds/ContentItemsSource.cs +++ b/src/Lucene.Net.Benchmark/ByTask/Feeds/ContentItemsSource.cs @@ -32,7 +32,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Feeds /// Base class for source of data for benchmarking. /// /// - /// Keeps track of various statistics, such as how many data items were generated, + /// Keeps track of various statistics, such as how many data items were generated, /// size in bytes etc. /// /// Supports the following configuration parameters: @@ -40,7 +40,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Feeds /// content.source.foreverspecifies whether to generate items forever (default=true). /// content.source.verbosespecifies whether messages should be output by the content source (default=false). /// content.source.encoding - /// specifies which encoding to use when + /// specifies which encoding to use when /// reading the files of that content source. Certain implementations may define /// a default value if this parameter is not specified. (default=null). /// @@ -199,7 +199,7 @@ public virtual void SetConfig(Config config) } else { - m_encoding = Encoding.GetEncoding(0); // Default system encoding + m_encoding = Encoding.Default; // Default system encoding } } diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/CreateIndexTask.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/CreateIndexTask.cs index 6fd4cba208..1e8ed83362 100644 --- a/src/Lucene.Net.Benchmark/ByTask/Tasks/CreateIndexTask.cs +++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/CreateIndexTask.cs @@ -215,7 +215,7 @@ public static IndexWriter ConfigureWriter(Config config, PerfRunData runData, Op else { FileInfo f = new FileInfo(infoStreamVal); - iwc.SetInfoStream(new StreamWriter(new FileStream(f.FullName, FileMode.Create, FileAccess.Write), Encoding.GetEncoding(0))); + iwc.SetInfoStream(new StreamWriter(new FileStream(f.FullName, FileMode.Create, FileAccess.Write), Encoding.Default)); } } IndexWriter writer = new IndexWriter(runData.Directory, iwc); diff --git a/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs b/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs index d08dab75a5..a223653dc9 100644 --- a/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs +++ b/src/Lucene.Net.Benchmark/Quality/Trec/QueryDriver.cs @@ -77,10 +77,13 @@ public static void Main(string[] args) string fieldSpec = args.Length == 5 ? args[4] : "T"; // default to Title-only if not specified. IndexSearcher searcher = new IndexSearcher(reader); - int maxResults = 1000; - string docNameField = "docname"; + const int maxResults = 1000; + const string docNameField = "docname"; - TextWriter logger = Console.Out; //new StreamWriter(Console, Encoding.GetEncoding(0)); + using TextWriter logger = new StreamWriter(System.Console.OpenStandardOutput(), Encoding.Default) + { + AutoFlush = true, + }; // use trec utilities to read trec topics into quality queries TrecTopicsReader qReader = new TrecTopicsReader(); diff --git a/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/CreateIndexTaskTest.cs b/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/CreateIndexTaskTest.cs index 00f85106e7..169b781504 100644 --- a/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/CreateIndexTaskTest.cs +++ b/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/CreateIndexTaskTest.cs @@ -56,7 +56,7 @@ public void TestInfoStream_SystemOutErr() TextWriter curOut = Console.Out; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Console.Out = new StreamWriter(baos, Encoding.GetEncoding(0)); + Console.Out = new StreamWriter(baos, Encoding.Default); try { PerfRunData runData = createPerfRunData("SystemOut"); @@ -72,7 +72,7 @@ public void TestInfoStream_SystemOutErr() TextWriter curErr = Console.Error; baos = new ByteArrayOutputStream(); - Console.Error = new StreamWriter(baos, Encoding.GetEncoding(0)); + Console.Error = new StreamWriter(baos, Encoding.Default); try { PerfRunData runData = createPerfRunData("SystemErr"); diff --git a/src/Lucene.Net.Tests.Demo/TestDemo.cs b/src/Lucene.Net.Tests.Demo/TestDemo.cs index 16b2379a53..9957f7b715 100644 --- a/src/Lucene.Net.Tests.Demo/TestDemo.cs +++ b/src/Lucene.Net.Tests.Demo/TestDemo.cs @@ -33,8 +33,7 @@ private void TestOneSearch(DirectoryInfo indexPath, string query, int expectedHi try { MemoryStream bytes = new MemoryStream(); - // .NET NOTE: GetEncoding(0) returns the current system's default encoding - var fakeSystemOut = new StreamWriter(bytes, Encoding.GetEncoding(0)); + var fakeSystemOut = new StreamWriter(bytes, Encoding.Default); Console.SetOut(fakeSystemOut); // LUCENENET specific: changed the arguments to act more like the dotnet.exe commands. // * only optional arguments start with - @@ -44,8 +43,7 @@ private void TestOneSearch(DirectoryInfo indexPath, string query, int expectedHi // it consistent with the lucene-cli utility. SearchFiles.Main(new string[] { indexPath.FullName, "--query", query }); fakeSystemOut.Flush(); - // .NET NOTE: GetEncoding(0) returns the current system's default encoding - string output = Encoding.GetEncoding(0).GetString(bytes.ToArray()); // intentionally use default encoding + string output = Encoding.Default.GetString(bytes.ToArray()); // intentionally use default encoding assertTrue("output=" + output, output.Contains(expectedHitCount + " total matching documents")); } finally