Skip to content

Commit

Permalink
Lucene.Net.Util.OfflineSorter (ByteSequencesReader + ByteSequencesWri…
Browse files Browse the repository at this point in the history
…ter): Added constructor overloads to pass the file name as a string (.NET convention)
  • Loading branch information
NightOwl888 committed Oct 31, 2022
1 parent 244ee24 commit a2b4c31
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/Lucene.Net/Util/OfflineSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ public ByteSequencesWriter(FileInfo file)
{
}

/// <summary>
/// Constructs a <see cref="ByteSequencesWriter"/> to the provided <see cref="FileInfo"/>. </summary>
/// <exception cref="ArgumentException"><paramref name="path"/> is <c>null</c> or whitespace.</exception>
public ByteSequencesWriter(string path)
: this(NewBinaryWriterDataOutput(path))
{
}

/// <summary>
/// Constructs a <see cref="ByteSequencesWriter"/> to the provided <see cref="DataOutput"/>. </summary>
/// <exception cref="ArgumentNullException"><paramref name="os"/> is <c>null</c>.</exception>
Expand All @@ -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);
}

/// <summary>
/// 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.
/// </summary>
/// <exception cref="ArgumentException"><paramref name="path"/> is <c>null</c> or whitespace.</exception>
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)));
}

/// <summary>
Expand Down Expand Up @@ -673,6 +694,15 @@ public ByteSequencesReader(FileInfo file)
{
}

/// <summary>
/// Constructs a <see cref="ByteSequencesReader"/> from the provided <paramref name="path"/>. </summary>
/// <exception cref="ArgumentException"><paramref name="path"/> is <c>null</c> or whitespace.</exception>
// LUCENENET specific
public ByteSequencesReader(string path)
: this(!string.IsNullOrWhiteSpace(path) ? new FileInfo(path) : throw new ArgumentException($"{nameof(path)} may not be null or whitespace."))
{
}

/// <summary>
/// Constructs a <see cref="ByteSequencesReader"/> from the provided <see cref="DataInput"/>. </summary>
/// <exception cref="ArgumentNullException"><paramref name="inputStream"/> is <c>null</c>.</exception>
Expand Down

0 comments on commit a2b4c31

Please sign in to comment.