Skip to content

Commit

Permalink
Add leaveOpen parameter to OutputStreamDataOutput, #265
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 25, 2024
1 parent ef23995 commit 86e18f0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected virtual void WriteTargetMap(string filename)
//new File(filename).getParentFile().mkdirs();
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
using Stream os = new FileStream(filename, FileMode.Create, FileAccess.Write);
using var @out = new OutputStreamDataOutput(os); // LUCENENET: CA2000: Use using statement
using var @out = new OutputStreamDataOutput(os, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.WriteHeader(@out, BinaryDictionary.TARGETMAP_HEADER, BinaryDictionary.VERSION);

int numSourceIds = lastSourceId + 1;
Expand Down Expand Up @@ -328,7 +328,7 @@ protected virtual void WritePosDict(string filename)
//new File(filename).getParentFile().mkdirs();
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
using Stream os = new FileStream(filename, FileMode.Create, FileAccess.Write);
using var @out = new OutputStreamDataOutput(os); // LUCENENET: CA2000: Use using statement
using var @out = new OutputStreamDataOutput(os, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.WriteHeader(@out, BinaryDictionary.POSDICT_HEADER, BinaryDictionary.VERSION);
@out.WriteVInt32(posDict.Count);
foreach (string s in posDict)
Expand All @@ -355,7 +355,7 @@ protected virtual void WriteDictionary(string filename)
//new File(filename).getParentFile().mkdirs();
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
using Stream os = new FileStream(filename, FileMode.Create, FileAccess.Write);
using var @out = new OutputStreamDataOutput(os); // LUCENENET: CA2000: Use using statement
using var @out = new OutputStreamDataOutput(os, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.WriteHeader(@out, BinaryDictionary.DICT_HEADER, BinaryDictionary.VERSION);
@out.WriteVInt32(m_buffer.Position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void Write(string baseDir)
//new File(filename).getParentFile().mkdirs();
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(baseDir));
using Stream os = new FileStream(filename, FileMode.Create, FileAccess.Write);
using var @out = new OutputStreamDataOutput(os); // LUCENENET: CA2000: Use using statement
using var @out = new OutputStreamDataOutput(os, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.WriteHeader(@out, CharacterDefinition.HEADER, CharacterDefinition.VERSION);
@out.WriteBytes(characterCategoryMap, 0, characterCategoryMap.Length);
for (int i = 0; i < CharacterDefinition.CLASS_COUNT; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Write(string baseDir)
//new File(filename).getParentFile().mkdirs();
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
using Stream os = new FileStream(filename, FileMode.Create, FileAccess.Write);
using var @out = new OutputStreamDataOutput(os); // LUCENENET: CA2000: Use using statement
using var @out = new OutputStreamDataOutput(os, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.WriteHeader(@out, ConnectionCosts.HEADER, ConnectionCosts.VERSION);
@out.WriteVInt32(forwardSize);
@out.WriteVInt32(backwardSize);
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Suggest/Suggest/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ public virtual bool Load(Stream input)
/// </summary>
public virtual bool Store(Stream output)
{
DataOutput dataOut = new OutputStreamDataOutput(output);
var dataOut = new OutputStreamDataOutput(output, leaveOpen: true);
try
{
return Store(dataOut);
}
finally
{
IOUtils.Dispose(output);
IOUtils.Dispose(dataOut, output); // LUCENENET specific - dispose of dataOut
}
}

Expand Down
29 changes: 26 additions & 3 deletions src/Lucene.Net/Store/OutputStreamDataOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,33 @@ namespace Lucene.Net.Store
public class OutputStreamDataOutput : DataOutput, IDisposable
{
private readonly Stream _os;
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="OutputStreamDataOutput"/> with the specified <paramref name="os"/> (output stream).
/// </summary>
/// <param name="os">The output stream to write to.</param>
/// <exception cref="ArgumentNullException">If <paramref name="os"/> is <c>null</c>.</exception>
public OutputStreamDataOutput(Stream os)
{
this._os = os ?? throw new ArgumentNullException(nameof(os)); // LUCENENET specific - added null guard clause
}

/// <inheritdoc cref="OutputStreamDataOutput(Stream)"/>
/// <summary>
/// Initializes a new instance of <see cref="OutputStreamDataOutput"/> with the specified <paramref name="os"/> (output 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 OutputStreamDataOutput(Stream os, bool leaveOpen)
: this(os)
{
this.leaveOpen = leaveOpen;
}

public override void WriteByte(byte b)
{
_os.WriteByte(b);
Expand Down Expand Up @@ -66,8 +86,11 @@ protected virtual void Dispose(bool disposing)

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

0 comments on commit 86e18f0

Please sign in to comment.