protected override Stream DoGetOutputStream(string contentName, string encoding, FileMode fileMode, FileShare fileShare) { MeteringStream stream; MemoryStream memoryStream; encoding = null; if (!this.Exists && (fileMode != FileMode.CreateNew || fileMode != FileMode.OpenOrCreate)) { throw new FileNodeNotFoundException(this.Address); } stream = new MeteringStream(memoryStream = new MemoryStream()); stream.BeforeClose += delegate { lock (this) { byte[] newValue; newValue = new byte[(int)memoryStream.Length]; Array.Copy(memoryStream.GetBuffer(), newValue, (int)memoryStream.Length); this.Value = newValue; } }; return(stream); }
public override HashValue ComputeHash(long offset, long length) { HashValue retval; if (length == 0) { return(new HashValue(this.Algorithm.ComputeHash(new byte[0]), this.ServiceType.AlgorithmName, offset, 0L)); } if (offset == 0 && length == -1 && ServiceType.AlgorithmType != null) { var retvaln = GetHashFromCache(this.OperatingNode, this.ServiceType.AlgorithmName); if (retvaln != null) { return(retvaln.Value); } } var stream = this.OperatingNode.GetContent().GetInputStream(); if (!(offset == 0 && length == -1 && !stream.CanSeek)) { stream = new PartialStream(stream, offset, length); if (length <= 0) { stream = new BufferedStream(stream, 128 * 1024); } else { stream = new BufferedStream(stream, Math.Min(128 * 1024, (int)length)); } } var meteringStream = new MeteringStream(stream); using (meteringStream) { retval = new HashValue(this.Algorithm.ComputeHash(meteringStream), this.ServiceType.AlgorithmName, offset, Convert.ToInt64(meteringStream.ReadMeter.Value)); } if (offset == 0 && length == -1 && ServiceType.AlgorithmType != null) { try { // SaveHashToCache(this.OperatingNode, this.ServiceType.AlgorithmName, retval); } catch (IOException) { // TODO: Log } } return(retval); }
public override HashValue ComputeHash(long offset, long length) { MeteringStream stream; if (offset == 0 && length == -1 && !this.OperatingStream.CanSeek) { var retval = StandardFileHashingService.GetHashFromCache(this.OperatingNode, this.ServiceType.AlgorithmName); if (retval != null) { return(retval.Value); } stream = new MeteringStream(this.OperatingStream); return(new HashValue(this.hashAlgorithm.ComputeHash(stream), this.ServiceType.AlgorithmName, 0, Convert.ToInt32(stream.ReadMeter.Value))); } else { if (offset == 0 && length == this.OperatingNode.Length) { if (StandardFileHashingService.ConfigurationSection.Default.CanCacheHash(this.OperatingNode)) { string s; if ((s = Convert.ToString(this.OperatingNode.Attributes["extended:" + ServiceType.AlgorithmName])) != "") { return(new HashValue(TextConversion.FromHexString(s), this.ServiceType.AlgorithmName, 0, -1)); } } } if (!this.OperatingStream.CanSeek) { throw new NotSupportedException("ComputeHash_StreamCannotSeek"); } stream = new MeteringStream(new PartialStream(this.OperatingStream, offset, length)); return(new HashValue(this.hashAlgorithm.ComputeHash(stream), this.ServiceType.AlgorithmName, offset, Convert.ToInt64(stream.ReadMeter.Value))); } }