diff --git a/src/Lucene.Net/Util/OfflineSorter.cs b/src/Lucene.Net/Util/OfflineSorter.cs index 333d340b33..afd208770b 100644 --- a/src/Lucene.Net/Util/OfflineSorter.cs +++ b/src/Lucene.Net/Util/OfflineSorter.cs @@ -554,6 +554,14 @@ public ByteSequencesWriter(FileInfo file) { } + /// + /// Constructs a to the provided . + /// is null or whitespace. + public ByteSequencesWriter(string path) + : this(NewBinaryWriterDataOutput(path)) + { + } + /// /// Constructs a to the provided . /// is null. @@ -573,15 +581,28 @@ private static BinaryWriterDataOutput NewBinaryWriterDataOutput(FileInfo file) if (file is null) throw new ArgumentNullException(nameof(file)); - string fileName = file.FullName; + return NewBinaryWriterDataOutput(file.FullName); + } + + /// + /// LUCENENET specific - ensures the file has been created with no BOM + /// if it doesn't already exist and opens the file for writing. + /// Java doesn't use a BOM by default. + /// + /// is null or whitespace. + private static BinaryWriterDataOutput NewBinaryWriterDataOutput(string path) + { + if (string.IsNullOrWhiteSpace(path)) + throw new ArgumentException($"{nameof(path)} may not be null or whitespace."); + // Create the file (without BOM) if it doesn't already exist - if (!File.Exists(fileName)) + if (!File.Exists(path)) { // Create the file - File.WriteAllText(fileName, string.Empty, new UTF8Encoding(false) /* No BOM */); + File.WriteAllText(path, string.Empty, new UTF8Encoding(false) /* No BOM */); } - return new BinaryWriterDataOutput(new BinaryWriter(new FileStream(fileName, FileMode.Open, FileAccess.Write))); + return new BinaryWriterDataOutput(new BinaryWriter(new FileStream(path, FileMode.Open, FileAccess.Write))); } /// @@ -673,6 +694,15 @@ public ByteSequencesReader(FileInfo file) { } + /// + /// Constructs a from the provided . + /// is null or whitespace. + // LUCENENET specific + public ByteSequencesReader(string path) + : this(!string.IsNullOrWhiteSpace(path) ? new FileInfo(path) : throw new ArgumentException($"{nameof(path)} may not be null or whitespace.")) + { + } + /// /// Constructs a from the provided . /// is null.