TastyDomainDriven.File.FileRecord.WriteContentToStreamAsync C# (CSharp) Method

WriteContentToStreamAsync() public method

public WriteContentToStreamAsync ( Stream stream ) : System.Threading.Tasks.Task
stream Stream
return System.Threading.Tasks.Task
        public async Task WriteContentToStreamAsync(Stream stream)
        {
            using (var sha1 = new SHA1Managed())
            {
                // version, ksz, vsz, key, value, sha1
                using (var memory = new MemoryStream())
                {
                    using (var crypto = new CryptoStream(memory, sha1, CryptoStreamMode.Write))
                    using (var binary = new BinaryWriter(crypto, Encoding.UTF8))
                    {
                        binary.Write(this.Version);
                        binary.Write(this.Name);
                        binary.Write(this.Bytes.Length);
                        binary.Write(this.Bytes);
                    }
                    var bytes = memory.ToArray();

                    stream.Write(bytes, 0, bytes.Length);
                }
                this.Hash = sha1.Hash;

                await stream.WriteAsync(sha1.Hash, 0, sha1.Hash.Length);
            }
        }

Usage Example

Example #1
0
        public async Task Append(string streamName, byte[] data, long expectedStreamVersion = -1)
        {
            var path       = new FileInfo(this.options.FileStream(options.MasterStreamName));
            var streampath = this.options.FileStream(streamName);

            FileStream filestream;

            if (!path.Directory.Exists)
            {
                path.Directory.Create();
            }

            if (!System.IO.File.Exists(path.FullName))
            {
                filestream = System.IO.File.Open(path.FullName, FileMode.CreateNew, FileAccess.Write, FileShare.Write);
            }
            else
            {
                filestream = System.IO.File.Open(path.FullName, FileMode.Append, FileAccess.Write, FileShare.Write);
            }

            using (Stream s = filestream)
            {
                var record = new FileRecord(data, streamName, expectedStreamVersion + 1);
                await record.WriteContentToStreamAsync(s);
            }
        }
All Usage Examples Of TastyDomainDriven.File.FileRecord::WriteContentToStreamAsync