Ejemplo n.º 1
0
        public async Task <LockedStream> OpenLockedStreamAsync(string path, FileAccess access)
        {
            var container = await GetContainerAsync();

            var blockBlob = container.GetBlockBlobReference(path);

            var blobStream = access == FileAccess.Read
                    ? await blockBlob.OpenReadAsync()
                    : await blockBlob.OpenWriteAsync();

            var result = LockedStream.FromStream(path, blobStream, access, null);

            return(result);
        }
        public override async Task <LockedStream> OpenLockedStreamAsync(string path, FileAccess access)
        {
            if (access == FileAccess.Read && await ExistsAsync(path))
            {
                return(await Disk.OpenLockedStreamAsync(path, access));
            }
            else
            {
                var lockedStream = await Disk.OpenLockedStreamAsync(path, FileAccess.ReadWrite); //overwrite access to read/write so we can upload online

                return(LockedStream.FromStream(path, lockedStream, access, onCloseAction: s =>
                {
                    var(objInfo, compressed) = MapPathToObjectInfo[path];
                    UploadFileAsync(path, objInfo, compressed, s).Wait();
                }));
            }
        }
Ejemplo n.º 3
0
        public static async Task TestSimple()
        {
            IDataStore   fileInfo = New <IDataStore>(_davidCopperfieldTxtPath);
            LockedStream lockedStreamCopy;

            using (LockedStream lockedStream = LockedStream.OpenWrite(fileInfo))
            {
                lockedStreamCopy = lockedStream;
                Assert.That(await Task.Run(() => New <FileLocker>().IsLocked(fileInfo)), "The file should be locked now.");
                Assert.That(lockedStream.CanRead, "The stream should be readable.");
                Assert.That(lockedStream.CanSeek, "The stream should be seekable.");
                Assert.That(lockedStream.CanWrite, "The stream should be writeable.");
                Assert.That(lockedStream.Length, Is.EqualTo(Resources.david_copperfield.Length), "The length should be the same as the string.");

                byte[] b    = new byte[1];
                int    read = lockedStream.Read(b, 0, 1);
                Assert.That(read, Is.EqualTo(1), "There should be one byte read.");
                Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes(Resources.david_copperfield.Substring(0, 1))[0]), "The byte read should be the first character of the resource.");
                Assert.That(lockedStream.Position, Is.EqualTo(1), "After reading the first byte, the position should be at one.");

                lockedStream.Write(b, 0, 1);
                lockedStream.Position = 1;
                read = lockedStream.Read(b, 0, 1);
                Assert.That(read, Is.EqualTo(1), "There should be one byte read.");
                Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes(Resources.david_copperfield.Substring(0, 1))[0]), "The byte read should be the first character of the resource.");
                Assert.That(lockedStream.Position, Is.EqualTo(2), "After reading the second byte, the position should be at two.");

                lockedStream.Seek(-1, SeekOrigin.End);
                Assert.That(lockedStream.Position, Is.EqualTo(lockedStream.Length - 1), "The position should be set by the Seek().");

                lockedStream.SetLength(5);
                lockedStream.Seek(0, SeekOrigin.End);
                Assert.That(lockedStream.Position, Is.EqualTo(5), "After setting the length to 5, seeking to the end should set the position at 5.");

                Assert.DoesNotThrow(() => { lockedStream.Flush(); }, "It's hard to test Flush() behavior here, not worth the trouble, but it should not throw!");
            }
            Assert.That(!await Task.Run(() => New <FileLocker>().IsLocked(fileInfo)), "The file should be unlocked now.");
            Assert.Throws <ObjectDisposedException>(() => { lockedStreamCopy.Position = 0; }, "The underlying stream should be disposed.");
        }