public override void ExecuteCommand() { // Open the file and make it the object content using (Stream strm = new FileStream(Arguments[0], FileMode.Open, FileAccess.Read)) { // Create the object DatabaseObject obj = new DatabaseObject(Type ?? "blob", new StreamObjectContent(strm)); if (Store) { Console.WriteLine(Database.StoreObject(obj)); } else { Console.WriteLine(Database.ComputeHash(obj)); } } }
public void Encode(DatabaseObject obj, Stream target) { if (obj == null) { throw new ArgumentNullException("obj"); } if (target == null) { throw new ArgumentNullException("target"); } // Write header using (BinaryWriter writer = new BinaryWriter(new DisposeProtectedStream(target), Encoding.ASCII)) { writer.Write(obj.Type.ToString().ToLowerInvariant().ToCharArray()); writer.Write(' '); writer.Write(obj.Content.Length.ToString().ToCharArray()); writer.Write((byte)0); } // Write content target.Write(obj.Content, 0, obj.Length); }
public virtual string StoreObject(DatabaseObject obj) { if (obj == null) { throw new ArgumentNullException("obj"); } var tup = ComputeHashCore(obj); string hash = tup.Item1; byte[] encoded = tup.Item2; using (Stream strm = Storage.OpenWrite(hash, create: true)) { strm.Write(encoded, 0, encoded.Length); } return hash; }
public string ComputeHash(DatabaseObject obj) { if (obj == null) { throw new ArgumentNullException("obj"); } return ComputeHashCore(obj).Item1; }
private Tuple<string, byte[]> ComputeHashCore(DatabaseObject obj) { // Encode the data to a memory stream. // PERF: This is not a great plan methinks... If this becomes a memory sink, we should write it to a file maybe? // Alternatively we could stream the data out to a temp file while ALSO building the hash, then move the temp file to the right place // based on the hash byte[] encoded; using (MemoryStream encoderOutput = new MemoryStream()) { Codec.Encode(obj, encoderOutput); encoderOutput.Flush(); encoded = encoderOutput.ToArray(); } // Hash the data string hash = HashGenerator.HashData(encoded); return Tuple.Create(hash, encoded); }
private DatabaseObject ConstructFromDelta(DatabaseObject source, byte[] deltaData) { byte[] data; using (MemoryStream output = new MemoryStream()) using (MemoryStream input = new MemoryStream(source.Content)) using (MemoryStream delta = new MemoryStream(deltaData)) { Delta.Decode(input, delta, output); output.Flush(); data = output.ToArray(); } return new DatabaseObject(source.Type, data); }