StreamFactory
using kCura.WinEDDS.Exceptions;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Logger;
using Relativity.Logging;
using System;
using System.IO;
using System.Text;
namespace Relativity.DataExchange.Export.VolumeManagerV2.Metadata.Writers
{
public class StreamFactory : IStreamFactory
{
private readonly IFile _fileWrapper;
private readonly ILog _logger;
public StreamFactory(IFile fileWrapper, ILog logger)
{
_fileWrapper = fileWrapper;
_logger = logger;
}
public StreamWriter Create(StreamWriter currentStreamWriter, long lastStreamWriterPosition, string path, Encoding encoding, bool append)
{
if (currentStreamWriter != null)
try {
currentStreamWriter.Dispose();
} catch (Exception ex) {
_logger.LogWarning(ex, "Failed to close broken stream.", Array.Empty<object>());
}
try {
_logger.LogVerbose("Creating new file stream {path}.", new object[1] {
path.Secure()
});
FileStream stream;
if (append) {
_logger.LogVerbose("Opening file in append mode with given position, so truncating file.", Array.Empty<object>());
stream = _fileWrapper.ReopenAndTruncate(path, lastStreamWriterPosition);
} else
stream = _fileWrapper.Create(path, false);
return new StreamWriter(stream, encoding) {
BaseStream = {
Position = lastStreamWriterPosition
}
};
} catch (IOException ex2) {
_logger.LogError((Exception)ex2, "Failed to initialize stream {path} with encoding {encoding}.", new object[2] {
path.Secure(),
encoding
});
throw new FileWriteException(FileWriteException.DestinationFile.Generic, ex2);
}
}
}
}