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

ReadContentFromStream() public method

public ReadContentFromStream ( BinaryReader binary ) : bool
binary System.IO.BinaryReader
return bool
        public bool ReadContentFromStream(BinaryReader binary)
        {
            try
            {
                var version = binary.ReadInt64();
                var name = binary.ReadString();
                var len = binary.ReadInt32();
                var bytes = binary.ReadBytes(len);
                var sha = binary.ReadBytes(20); // SHA1. TODO: verify data

                if (sha.All(s => s == 0))
                {
                    throw new InvalidOperationException("definitely failed");
                }

                this.Name = name;
                this.Bytes = bytes;
                this.Hash = sha;
                this.Version = version;
                return true;
            }
            catch (EndOfStreamException)
            {
                // we are done
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Usage Example

Example #1
0
        public List <FileRecord> Read(string filename)
        {
            var list    = new List <FileRecord>();
            int version = 1;

            if (!System.IO.File.Exists(filename))
            {
                return(list);
            }

            using (var fs = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var reader = new BinaryReader(fs))
                {
                    while (fs.Position < fs.Length)
                    {
                        try
                        {
                            var record = new FileRecord(version);
                            record.ReadContentFromStream(reader);
                            list.Add(record);
                            version++;
                        }
                        catch (IOException)
                        {
                        }
                    }
                }
            }

            return(list);
        }
All Usage Examples Of TastyDomainDriven.File.FileRecord::ReadContentFromStream