Skip to content

Commit

Permalink
Add leaveOpen parameter to InputStreamDataInput, #265
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 25, 2024
1 parent 760d01d commit ef23995
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected BinaryDictionary()
ByteBuffer buffer; // LUCENENET: IDE0059: Remove unnecessary value assignment

using (Stream mapIS = GetResource(TARGETMAP_FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(mapIS)) // LUCENENET: CA2000: Use using statement
using (var @in = new InputStreamDataInput(mapIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, TARGETMAP_HEADER, VERSION, VERSION);
targetMap = new int[@in.ReadVInt32()];
Expand All @@ -124,7 +124,7 @@ protected BinaryDictionary()
}

using (Stream posIS = GetResource(POSDICT_FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(posIS)) // LUCENENET: CA2000: Use using statement
using (var @in = new InputStreamDataInput(posIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, POSDICT_HEADER, VERSION, VERSION);
int posSize = @in.ReadVInt32();
Expand Down Expand Up @@ -152,7 +152,7 @@ protected BinaryDictionary()

using (Stream dictIS = GetResource(DICT_FILENAME_SUFFIX))
// no buffering here, as we load in one large buffer
using (var @in = new InputStreamDataInput(dictIS)) // LUCENENET: CA2000: Use using statement
using (var @in = new InputStreamDataInput(dictIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, DICT_HEADER, VERSION, VERSION);
int size = @in.ReadVInt32();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private enum CharacterClass : byte
private CharacterDefinition()
{
using Stream @is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX);
using var @in = new InputStreamDataInput(@is); // LUCENENET: CA2000: Use using statement
using var @in = new InputStreamDataInput(@is, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
@in.ReadBytes(characterCategoryMap, 0, characterCategoryMap.Length);
for (int i = 0; i < CLASS_COUNT; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Analysis.Kuromoji/Dict/ConnectionCosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private ConnectionCosts()
short[][] costs = null;

using (Stream @is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(@is)) // LUCENENET: CA2000: Use using statement
using (var @in = new InputStreamDataInput(@is, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
int forwardSize = @in.ReadVInt32();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,9 @@ public int[] GetMap()
}
AddDone(); // in case this wasn't previously called

var ifs = new FileStream(tmpfile, FileMode.OpenOrCreate, FileAccess.Read);
using (var @in = new InputStreamDataInput(ifs))
// LUCENENET specific - dispose of resources
using (var ifs = new FileStream(tmpfile, FileMode.OpenOrCreate, FileAccess.Read))
using (var @in = new InputStreamDataInput(ifs, leaveOpen: true))
{
map = new int[@in.ReadInt32()];
// NOTE: The current code assumes here that the map is complete,
Expand Down
29 changes: 26 additions & 3 deletions src/Lucene.Net/Store/InputStreamDataInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,33 @@ namespace Lucene.Net.Store
public class InputStreamDataInput : DataInput, IDisposable
{
private readonly Stream _is;
private int disposed = 0; // LUCENENET specific - allow double-dispose
private int disposed; // LUCENENET specific - allow double-dispose
private readonly bool leaveOpen; // LUCENENET specific - added to allow the stream to be left open

/// <summary>
/// Initializes a new instance of <see cref="InputStreamDataInput"/> with the specified <paramref name="is"/> (input stream).
/// </summary>
/// <param name="is">The input stream to read from.</param>
/// <exception cref="ArgumentNullException">If <paramref name="is"/> is <c>null</c>.</exception>
public InputStreamDataInput(Stream @is)
{
this._is = @is ?? throw new ArgumentNullException(nameof(@is)); // LUCENENET specific - added null guard clause
}

/// <inheritdoc cref="InputStreamDataInput(Stream)"/>
/// <summary>
/// Initializes a new instance of <see cref="InputStreamDataInput"/> with the specified <paramref name="is"/> (input stream) and <paramref name="leaveOpen"/> flag.
/// </summary>
/// <param name="leaveOpen">If <c>true</c>, the stream will not be disposed when this instance is disposed.</param>
/// <remarks>
/// LUCENENET specific - added to allow the stream to be left open.
/// </remarks>
public InputStreamDataInput(Stream @is, bool leaveOpen)
: this(@is)
{
this.leaveOpen = leaveOpen;
}

public override byte ReadByte()
{
int v = _is.ReadByte();
Expand Down Expand Up @@ -71,8 +91,11 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_is.Dispose();
if (!leaveOpen)
{
_is.Dispose();
}
}
}
}
}
}

0 comments on commit ef23995

Please sign in to comment.