TransactionFileStream

Ecng.IO

Represents a transactional file stream that writes data to a temporary file. Changes are only committed to the target file when Commit is called. If disposed without commit, changes are rolled back (temporary file is deleted).

Herda de: Stream

Implementa: ICommitable, IAsyncCommitable

Construtores

TransactionFileStream
public TransactionFileStream(string name, FileMode mode)
transactionFileStream = TransactionFileStream(name, mode)

Initializes a new instance of the TransactionFileStream class.

name
The name of the target file.
mode
The file mode that specifies the type of operations to be performed on the file.
TransactionFileStream
public TransactionFileStream(IFileSystem fileSystem, string name, FileMode mode)
transactionFileStream = TransactionFileStream(fileSystem, name, mode)

Initializes a new instance of the TransactionFileStream class.

fileSystem
IFileSystem
name
The name of the target file.
mode
The file mode that specifies the type of operations to be performed on the file.

Propriedades

CanRead
public override bool CanRead { get; }
value = transactionFileStream.CanRead

Gets a value indicating whether the temporary file stream supports reading. Always returns false.

CanSeek
public override bool CanSeek { get; }
value = transactionFileStream.CanSeek

Gets a value indicating whether the stream supports seeking. Always returns false - this is an append-only stream.

CanWrite
public override bool CanWrite { get; }
value = transactionFileStream.CanWrite

Gets a value indicating whether the underlying temporary file stream supports writing.

IsCommitted
public bool IsCommitted { get; }
value = transactionFileStream.IsCommitted

Gets a value indicating whether the transaction has ever been committed.

Length
public override long Length { get; }
value = transactionFileStream.Length

Gets the length in bytes of the underlying temporary file stream.

Position
public override long Position { get; set; }
value = transactionFileStream.Position
transactionFileStream.Position = value

Gets the current position within the stream. Setting position is not supported - this is an append-only stream.

Métodos

Commit
public void Commit()
transactionFileStream.Commit()

Commits the transaction, moving the temporary file to the target location. Can be called multiple times. After commit, stream continues appending.

CommitAsync
public ValueTask CommitAsync(CancellationToken cancellationToken)
result = transactionFileStream.CommitAsync(cancellationToken)

Asynchronously commits the transaction, moving the temporary file to the target location. Can be called multiple times. After commit, stream continues appending.

cancellationToken
CancellationToken

Retorna: ValueTask

Dispose
protected override void Dispose(bool disposing)
transactionFileStream.Dispose(disposing)

Releases the unmanaged resources used by the TransactionFileStream and optionally releases the managed resources. If Commit was never called, performs rollback by deleting the temporary file. If Commit() failed, preserves .tmp file for manual recovery.

disposing
true to release both managed and unmanaged resources; false to release only unmanaged resources.
Flush
public override void Flush()
transactionFileStream.Flush()

Clears all buffers for the current stream and causes any buffered data to be written to the underlying file.

Read
public override int Read(byte[] buffer, int offset, int count)
result = transactionFileStream.Read(buffer, offset, count)

Reading is not supported by the TransactionFileStream.

buffer
The buffer to read data into.
offset
The byte offset in buffer at which to begin storing the data read from the underlying stream.
count
The maximum number of bytes to be read from the underlying stream.

Retorna: This method always throws a NotSupportedException.

Seek
public override long Seek(long offset, SeekOrigin origin)
result = transactionFileStream.Seek(offset, origin)

Seeking is not supported. This is an append-only stream.

SetLength
public override void SetLength(long value)
transactionFileStream.SetLength(value)

Setting length is not supported. This is an append-only stream.

Write
public override void Write(byte[] buffer, int offset, int count)
transactionFileStream.Write(buffer, offset, count)

Writes a sequence of bytes to the temporary file stream.

buffer
An array of bytes. This is the buffer that contains the data to write to the temporary file.
offset
The zero-based byte offset in buffer at which to begin copying bytes to the temporary file.
count
The number of bytes to be written to the temporary file.