NightOwl888 opened a new issue, #832:
URL: https://github.com/apache/lucenenet/issues/832
In Java it is common to use the `File` or `Path` object as a parameter to
methods, as they are accepted as parameters of stream constructors and many
other places.
However, in .NET, the corresponding objects (`TextWriter`, `Stream`,
`StreamWriter`, etc) don't accept a `FileInfo` as a parameter, they accept
strings. As such, we are commonly newing up these `FileInfo` objects, passing
them through our API and reading the original string back as
`FileInfo.FullName`. The `FileInfo` object is not even used in many cases.
To keep the API the same as Lucene, we should keep the overloads that accept
`FileInfo`, but add overloads that are identical in every way except that they
accept a `string` for a file name instead of a `FileInfo`. The `FileInfo`
overloads can then just pass `FileInfo.FullName` to the string overloads.
For example, we currently have
```c#
/// <summary>
/// Returns an <see cref="Stream"/> over the requested file,
identifying
/// the appropriate <see cref="Stream"/> instance similar to <see
cref="GetInputStream(FileInfo)"/>.
/// </summary>
public static Stream GetOutputStream(FileInfo file)
{
// First, create a FileInputStream, as this will be required by
all types.
// Wrap with BufferedInputStream for better performance
Stream os = new FileStream(file.FullName, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
return GetFileType(file).GetOutputStream(os);
}
```
And we should change it to
```c#
/// <summary>
/// Returns an <see cref="Stream"/> over the requested <paramref
name="file"/>, identifying
/// the appropriate <see cref="Stream"/> instance similar to <see
cref="GetInputStream(FileInfo)"/>.
/// </summary>
public static Stream GetOutputStream(FileInfo file)
=> GetOutputStream(file?.FullName);
/// <summary>
/// Returns an <see cref="Stream"/> over the requested <paramref
name="filePath"/>, identifying
/// the appropriate <see cref="Stream"/> instance similar to <see
cref="GetInputStream(string)"/>.
/// </summary>
public static Stream GetOutputStream(string filePath)
{
// First, create a FileInputStream, as this will be required by
all types.
// Wrap with BufferedInputStream for better performance
Stream os = new FileStream(filePath, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
return GetFileType(filePath).GetOutputStream(os);
}
```
There are several places throughout the project where we are instantiating
and then discarding `FileInfo` objects without even making use of their
functionality
([example](https://github.com/apache/lucenenet/blob/87add0272d1da1eaee68ca95e37841e90ec07346/src/Lucene.Net/Store/MMapDirectory.cs#L185-L186)).
Some could instead use static methods (i.e. `File.Exists`). In some other
cases, these `FileInfo` and `DirectoryInfo` instances are appropriate.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]