public override void ReadInternal(byte[] b, int offset, int length) { if (b.Length == 0) { return; } LiteFileInfo fileInfo = _db.FileStorage.FindById(_name); if (offset < _position) { fileInfo = _db.FileStorage.FindById(_name); } if (fileInfo == null) { return; } if (fileInfo.Length > 0) { using (var stream = new MemoryStream()) { fileInfo.CopyTo(stream); stream.Position = _position; stream.Read(b, offset, length); } GC.Collect(); } _position += length; }
private void DownloadFile(LiteFileInfo file, Stream stream) { file.CopyTo(stream); if (stream.Length > file.Length) { stream.SetLength(file.Length); } }
public async Task <Stream> GetImageAsync(string path) { try { if (path.StartsWith("/")) { return(await ImageClient.GetStreamAsync("original" + path)); } else if (!path.StartsWith("w")) { return(await YouTubeClient.GetStreamAsync(String.Format("{0}/0.jpg", path))); } string id = path.ComputeHash(); LiteFileInfo fileInfo = FileStorage.FindById(id); if (fileInfo == null) { try { using (Stream stream = await ImageClient.GetStreamAsync(path)) { if (stream != null) { using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); if (memoryStream.Length > 0) { memoryStream.Position = 0; fileInfo = FileStorage.Upload(id, path, memoryStream); } } } } } catch (Exception) { return(null); } } if (fileInfo == null) { return(null); } MemoryStream cachedStream = new MemoryStream(); fileInfo.CopyTo(cachedStream); cachedStream.Position = 0; return(cachedStream); } catch (Exception) { return(null); } }
/// <inheritdoc/> public IEnumerable <Tuple <HashDigest <SHA256>, long> > IterateStateReferences( string @namespace, Address address) { var fileId = $"{StateRefIdPrefix}{@namespace}/{address.ToHex()}"; LiteFileInfo file = _db.FileStorage.FindById(fileId); if (file is null || file.Length == 0) { yield break; } int hashSize = HashDigest <SHA256> .Size; int stateReferenceSize = hashSize + sizeof(long); if (file.Length % stateReferenceSize != 0) { throw new FileLoadException( $"State references file's size ({file.Length}) should be multiple of " + $"state reference entry size {stateReferenceSize})." ); } using (var stream = new MemoryStream()) { // Note that a stream made by file.OpenRead() does not support // .Seek() operation --- although it implements the interface, // the method throws a NotSupportedException. file.CopyTo(stream); var buffer = new byte[stateReferenceSize]; long position = stream.Seek(0, SeekOrigin.End); for (var i = 1; position - buffer.Length >= 0; i++) { position = stream.Seek(-buffer.Length * i, SeekOrigin.End); stream.Read(buffer, 0, buffer.Length); byte[] hashBytes = buffer.Take(hashSize).ToArray(); long index = BitConverter.ToInt64(buffer, hashSize); yield return(Tuple.Create( new HashDigest <SHA256>(hashBytes), index )); } } }
/// <inheritdoc/> public Transaction <T> GetTransaction <T>(TxId txid) where T : IAction, new() { LiteFileInfo file = _db.FileStorage.FindById(TxFileId(txid)); if (file is null) { return(null); } using (var stream = new MemoryStream()) { file.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); return(Transaction <T> .FromBencodex(stream.ToArray())); } }
/// <inheritdoc/> public AddressStateMap GetBlockStates(HashDigest <SHA256> blockHash) { LiteFileInfo file = _db.FileStorage.FindById(BlockStateFileId(blockHash)); if (file is null) { return(null); } using (var stream = new MemoryStream()) { file.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); var formatter = new BinaryFormatter(); return((AddressStateMap)formatter.Deserialize(stream)); } }
/// <inheritdoc/> public Block <T> GetBlock <T>(HashDigest <SHA256> blockHash) where T : IAction, new() { LiteFileInfo file = _db.FileStorage.FindById(BlockFileId(blockHash)); if (file is null) { return(null); } using (var stream = new MemoryStream()) { file.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); var formatter = new BencodexFormatter <RawBlock>(); RawBlock rawBlock = (RawBlock)formatter.Deserialize(stream); HashDigest <SHA256>?previousHash = null; if (rawBlock.PreviousHash != null) { previousHash = new HashDigest <SHA256>(rawBlock.PreviousHash); } return(new Block <T>( index: rawBlock.Index, difficulty: rawBlock.Difficulty, nonce: new Nonce(rawBlock.Nonce), miner: new Address(rawBlock.Miner), previousHash: previousHash, timestamp: DateTimeOffset.ParseExact( rawBlock.Timestamp, Block <T> .TimestampFormat, CultureInfo.InvariantCulture ).ToUniversalTime(), transactions: GetTransactions <T>(rawBlock.Transactions) )); } }
/// <inheritdoc/> public void StoreStateReference <T>( string @namespace, IImmutableSet <Address> addresses, Block <T> block) where T : IAction, new() { int hashSize = HashDigest <SHA256> .Size; byte[] hashBytes = block.Hash.ToByteArray(); byte[] indexBytes = BitConverter.GetBytes(block.Index); foreach (Address address in addresses) { string addrHex = address.ToHex(); var fileId = $"{StateRefIdPrefix}{@namespace}/{addrHex}"; if (!_db.FileStorage.Exists(fileId)) { _db.FileStorage.Upload( fileId, addrHex, new MemoryStream()); } LiteFileInfo file = _db.FileStorage.FindById(fileId); using (var temp = new MemoryStream()) { file.CopyTo(temp); temp.Seek(0, SeekOrigin.Begin); byte[] prev = temp.ToArray(); using (LiteFileStream stream = file.OpenWrite()) { stream.Write(prev, 0, prev.Length); stream.Write(hashBytes, 0, hashSize); stream.Write(indexBytes, 0, sizeof(long)); } } } }