예제 #1
0
        public async Task DownloadAsync(string destination, CancellationToken ct, IProgress <DataTransferProgress> progress)
        {
            var temp = MaskedFile.GetMaskedPathFromFile(destination);

            try
            {
                using (var fs = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.Delete))
                    using (var stream = await OpenStreamAsync())
                    {
                        var mediaStream = stream as MediaStream;
                        if (progress != null)
                        {
                            progress.Report(new DataTransferProgress(default(FileSize), null, default(FileSize)));
                        }
                        var buffer           = new byte[4096];
                        var transferredBytes = 0L;
                        while (true)
                        {
                            var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length);

                            transferredBytes += readBytes;
                            var t = new FileSize(transferredBytes);
                            if (progress != null && mediaStream != null)
                            {
                                progress.Report(new DataTransferProgress(t, mediaStream.Size, mediaStream.DataPerSecond));
                            }
                            if (readBytes == 0)
                            {
                                break;
                            }
                            await fs.WriteAsync(buffer, 0, readBytes);
                        }
#if !NET35
                        var m = stream as MediaStream;
                        if (m != null)
                        {
                            var response = await m.Manager.GetResponseAsync();

                            if (response != null)
                            {
                                var lm = response.Content.Headers.LastModified;
                                if (lm != null)
                                {
                                    fs.Dispose();
                                    File.SetLastWriteTimeUtc(temp, lm.Value.UtcDateTime);
                                }
                            }
                        }
#endif
                    }
                MaskedFile.PublishMaskedFile(temp, destination);
            }
            finally
            {
                File.Delete(temp);
            }
        }
예제 #2
0
        //private Dictionary<Type, int> Stats_ObjectCountByType = new Dictionary<Type, int>();
        //private Dictionary<Type, int> Stats_ArraySizes = new Dictionary<Type, int>();

        public static void WriteFile <T>(string path, T obj)
        {
            var temp = MaskedFile.GetMaskedPathFromFile(path);

            try
            {
                using (var fileStream = File.Open(temp, FileMode.Create, FileAccess.Write, FileShare.Delete))
                    using (var bw = new BinaryWriter(fileStream, Encoding.UTF8))
                        using (var ser = new BinSerSerializer(bw))
                        {
                            ser.WriteObject <object>(obj);
                        }
                File.Delete(path);
                MaskedFile.PublishMaskedFile(temp, path);
            }
            finally
            {
                MaskedFile.TryDeleteTempFile(temp);
            }
        }