예제 #1
0
        public void FileAsync_Compress_Decompress()
        {
            var filename = FileTool.GetTempFileName();

            using (var fs = FileAsync.OpenWrite(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Compress)) {
                    gzip.Write(PlainBytes, 0, PlainBytes.Length);
                }

            var fi = new FileInfo(filename);

            Assert.IsTrue(fi.Exists);
            Assert.IsTrue(PlainBytes.Length > fi.Length);


            var outStream = new MemoryStream((int)fi.Length * 2);

            using (var fs = FileAsync.OpenRead(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
                    var readCount = 0;
                    var buffer    = new byte[CompressorTool.BUFFER_SIZE];

                    while ((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outStream.Write(buffer, 0, readCount);
                    }

                    var output = outStream.ToArray();
                    Assert.AreEqual(PlainBytes.Length, output.Length);
                    Assert.AreEqual(PlainBytes, output);
                }

            fi = new FileInfo(filename);
            fi.Delete();
        }
예제 #2
0
        public void FileAsync_Compress_Decompress_Async()
        {
            var filename = FileTool.GetTempFileName();

            using (var fs = FileAsync.OpenWrite(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Compress)) {
                    gzip.WriteAsync(PlainBytes, 0, PlainBytes.Length).Wait();
                }

            var fi = new FileInfo(filename);

            Assert.IsTrue(fi.Exists);
            Assert.IsTrue(PlainBytes.Length > fi.Length);

            using (var fs = FileAsync.OpenRead(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
                    var output = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result);

                    Assert.AreEqual(PlainBytes.Length, output.Length);
                    Assert.AreEqual(PlainBytes, output);
                }

            fi = new FileInfo(filename);
            fi.Delete();
        }
예제 #3
0
        public void Can_WriteAsync_And_ReadAllText()
        {
            if (IsDebugEnabled)
            {
                log.Debug("Write Asynchronous and Real all text...");
            }

            var filename = FileTool.GetTempFileName("ASYNC_");

            using (var fs = FileAsync.OpenWrite(filename)) {
                Task chainTask = null;

                for (int i = 0; i < RunCount; i++)
                {
                    var lineNo = i.ToString() + ": ";
                    var buffer = Encoding.UTF8.GetBytes(lineNo);

                    if (chainTask == null)
                    {
                        chainTask = fs.WriteAsync(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        chainTask.ContinueWith(_ => fs.WriteAsync(buffer, 0, buffer.Length));
                    }

                    chainTask = chainTask.ContinueWith(_ => fs.WriteAsync(MessageBuffer, 0, MessageBuffer.Length));
                }

                chainTask.Wait();
            }

            var fileText = With.TryFunctionAsync(() => FileAsync.ReadAllText(filename, Encoding.UTF8).Result, () => string.Empty);

            Assert.IsNotEmpty(fileText);

            Console.WriteLine("Contents = " + Environment.NewLine + fileText);

            With.TryAction(() => FileTool.DeleteFile(filename));
        }