Пример #1
0
        public void Save()
        {
            List <CompressedFileRecord> objSavedRecords = new List <CompressedFileRecord>();

            string strTempFilePath = Path.GetTempFileName();

            using (FileStream objTempFileStream = new FileStream(strTempFilePath, FileMode.Create, FileAccess.ReadWrite))
            {
                CompressedFileRecord[] objInsertedRecords = CompressedFileRecordManager.InsertedRecords;
                foreach (CompressedFileRecord objInsertedRecord in objInsertedRecords)
                {
                    using (FileStream objInputFile = new FileStream(objInsertedRecord.OriginalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        int intStartingPosition = (int)objTempFileStream.Position;
                        CompressionManager.Compress(objInputFile, objTempFileStream);

                        int intCompressedSize = (int)objTempFileStream.Position - intStartingPosition;
                        CompressedFileRecord objSavedRecord = new CompressedFileRecord(objInsertedRecord.OriginalFilePath, intCompressedSize, objInsertedRecord.RelativePath);
                        objSavedRecord.Keys.AddRange(objInsertedRecord.Keys);
                        objSavedRecords.Add(objSavedRecord);
                    }

                    objTempFileStream.Flush();
                }

                CompressedFileRecord[] objUpdatedRecords = CompressedFileRecordManager.UpdatedRecords;
                foreach (CompressedFileRecord objUpdatedRecord in objUpdatedRecords)
                {
                    int intFileStartIndex = CompressedFileRecordManager.FindFileStartIndex(objUpdatedRecord);
                    if (intFileStartIndex != -1)
                    {
                        CompressedStream.Position = intFileStartIndex;
                        CompressionManager.CopyToStream(CompressedStream, objTempFileStream, objUpdatedRecord.CompressedSize);
                        objSavedRecords.Add(objUpdatedRecord);
                    }

                    objTempFileStream.Flush();
                }

                objTempFileStream.Position = 0;
                CompressedStream.SetLength(0);
                CompressedStream.Position = 0;

                CompressedStream.Write(CompressedStreamSignature, 0, CompressedStreamSignature.Length);

                string strSavedRecords       = CompressedFileRecordManager.ToString(objSavedRecords.ToArray());
                byte[] bytSavedRecords       = ASCIIEncoding.ASCII.GetBytes(strSavedRecords);
                byte[] bytCompressedRecords  = CompressionManager.Compress(bytSavedRecords);
                int    intRecordLength       = bytCompressedRecords.Length;
                byte[] bytHeaderRecordLength = BitConverter.GetBytes(intRecordLength);
                CompressedStream.Write(bytHeaderRecordLength, 0, bytHeaderRecordLength.Length);
                CompressedStream.Write(bytCompressedRecords, 0, bytCompressedRecords.Length);

                CompressionManager.CopyToStream(objTempFileStream, CompressedStream);
                CompressedStream.Flush();
            }

            File.Delete(strTempFilePath);
            Refresh();
        }
Пример #2
0
        public ImapReplayCommand(string tag, string command, string resource, bool compressed = false)
        {
            CommandBuffer = Latin1.GetBytes(command);
            Compressed    = compressed;
            Command       = command;

            using (var stream = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Imap.Resources." + resource)) {
                using (var memory = new MemoryBlockStream()) {
                    using (Stream compress = new CompressedStream(memory)) {
                        using (var filtered = new FilteredStream(compressed ? compress : memory)) {
                            filtered.Add(new ImapReplayFilter("A########", tag));
                            filtered.Add(new Unix2DosFilter());
                            stream.CopyTo(filtered, 4096);
                            filtered.Flush();
                        }

                        Response = memory.ToArray();
                    }
                }
            }

            if (compressed)
            {
                using (var memory = new MemoryStream()) {
                    using (var compress = new CompressedStream(memory)) {
                        compress.Write(CommandBuffer, 0, CommandBuffer.Length);
                        compress.Flush();

                        CommandBuffer = memory.ToArray();
                    }
                }
            }
        }
Пример #3
0
        private static byte[] CompressDataWithDeflate(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                CompressedStream compressedStream = new CompressedStream(stream, StreamOperationMode.Write, new DeflateSettings());
                compressedStream.Write(data, 0, data.Length);
                compressedStream.Flush();

                return(stream.ToArray());
            }
        }
Пример #4
0
        public ImapReplayCommand(Encoding encoding, string command, ImapReplayCommandResponse response, bool compressed = false)
        {
            CommandBuffer = encoding.GetBytes(command);
            Compressed    = compressed;
            Encoding      = encoding;
            Command       = command;

            string text;

            if (response == ImapReplayCommandResponse.Plus)
            {
                text = "+\r\n";
            }
            else
            {
                var tokens = command.Split(' ');
                var cmd    = (tokens [1] == "UID" ? tokens [2] : tokens [1]).TrimEnd();
                var tag    = tokens [0];

                text = string.Format("{0} {1} {2} {3}\r\n", tag, response, cmd, response == ImapReplayCommandResponse.OK ? "completed" : "failed");
            }

            if (compressed)
            {
                using (var memory = new MemoryStream()) {
                    using (var compress = new CompressedStream(memory)) {
                        var buffer = encoding.GetBytes(text);

                        compress.Write(buffer, 0, buffer.Length);
                        compress.Flush();

                        Response = memory.ToArray();
                    }
                }

                using (var memory = new MemoryStream()) {
                    using (var compress = new CompressedStream(memory)) {
                        compress.Write(CommandBuffer, 0, CommandBuffer.Length);
                        compress.Flush();

                        CommandBuffer = memory.ToArray();
                    }
                }
            }
            else
            {
                Response = encoding.GetBytes(text);
            }
        }
Пример #5
0
        public void TestArgumentExceptions()
        {
            using (var stream = new CompressedStream(new DummyNetworkStream())) {
                var buffer = new byte[16];

                Assert.Throws <ArgumentNullException> (() => stream.Read(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Read(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Read(buffer, 0, -1));
                Assert.AreEqual(0, stream.Read(buffer, 0, 0));

                Assert.Throws <ArgumentNullException> (async() => await stream.ReadAsync(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.ReadAsync(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.ReadAsync(buffer, 0, -1));

                Assert.Throws <ArgumentNullException> (() => stream.Write(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Write(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Write(buffer, 0, -1));
                stream.Write(buffer, 0, 0);

                Assert.Throws <ArgumentNullException> (async() => await stream.WriteAsync(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.WriteAsync(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.WriteAsync(buffer, 0, -1));
            }
        }
Пример #6
0
        public ImapReplayCommand(Encoding encoding, string command, string resource, bool compressed = false)
        {
            string tag = null;

            CommandBuffer = encoding.GetBytes(command);
            Compressed    = compressed;
            Encoding      = encoding;
            Command       = command;

            if (command.StartsWith("A00000", StringComparison.Ordinal))
            {
                tag = command.Substring(0, 9);
            }

            using (var stream = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Imap.Resources." + resource)) {
                using (var memory = new MemoryBlockStream()) {
                    using (Stream compress = new CompressedStream(memory)) {
                        using (var filtered = new FilteredStream(compressed ? compress : memory)) {
                            if (tag != null)
                            {
                                filtered.Add(new ImapReplayFilter("A########", tag));
                            }

                            filtered.Add(new Unix2DosFilter());
                            stream.CopyTo(filtered, 4096);
                            filtered.Flush();
                        }

                        Response = memory.ToArray();
                    }
                }
            }

            if (compressed)
            {
                using (var memory = new MemoryStream()) {
                    using (var compress = new CompressedStream(memory)) {
                        compress.Write(CommandBuffer, 0, CommandBuffer.Length);
                        compress.Flush();

                        CommandBuffer = memory.ToArray();
                    }
                }
            }
        }
Пример #7
0
        public ImapReplayCommand(string command, byte[] response, bool compressed = false)
        {
            Command       = command;
            Response      = response;
            Compressed    = compressed;
            CommandBuffer = Latin1.GetBytes(command);

            if (compressed)
            {
                using (var memory = new MemoryStream()) {
                    using (var compress = new CompressedStream(memory)) {
                        compress.Write(CommandBuffer, 0, CommandBuffer.Length);
                        compress.Flush();

                        CommandBuffer = memory.ToArray();
                    }
                }
            }
        }
Пример #8
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_streamToCompress == null)
            {
                throw new ObjectDisposedException(typeof(ZipOutputStream).Name);
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "Cannot be negative.");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Cannot be negative.");
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentException("The sum of offset and count is greater than the byte array length.");
            }

            _bufferController = new BufferController(buffer, offset, count);
            _backlogs         = _bufferController.Append(_backlogs).ToList();
            while (_bufferController.Availability > 0 && !_eof)
            {
                var bytesRead = _streamToCompress.Read(_buffer, 0, _buffer.Length);
                if (bytesRead == 0)
                {
                    CompressedStream.CloseEntry();
                    CompressedStream.Close();
                    _eof = true;
                }
                else
                {
                    CompressedStream.Write(_buffer, 0, bytesRead);
                    CompressedStream.Flush();
                }
            }
            return(_bufferController.Count);
        }
Пример #9
0
        public static bool Compress(Stream sourceStream, Stream destinationStream)
        {
            sourceStream.Position      = 0;
            destinationStream.Position = 0;

            using (CompressedStream compStream = new CompressedStream(destinationStream, CompressionMethod.Deflated, CompressionLevel.Highest))
            {
                compStream.Transient = true;

                byte[] buffer = new byte[32768];

                int bytesRead = 0;

                while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    compStream.Write(buffer, 0, bytesRead);
                }
            }

            return(true);
        }
Пример #10
0
        public void TestReadWrite()
        {
            using (var stream = new CompressedStream(new DummyNetworkStream())) {
                string    command          = "A00000001 APPEND INBOX (\\Seen \\Draft) {4096+}\r\nFrom: Sample Sender <*****@*****.**>\r\nTo: Sample Recipient <*****@*****.**>\r\nSubject: This is a test message...\r\nDate: Mon, 22 Oct 2018 18:22:56 EDT\r\nMessage-Id: <*****@*****.**>\r\n\r\nTesting... 1. 2. 3.\r\nTesting.\r\nOver and out.\r\n";
                var       output           = Encoding.ASCII.GetBytes(command);
                const int compressedLength = 221;
                var       buffer           = new byte[1024];
                int       n;

                stream.Write(output, 0, output.Length);
                stream.Flush();

                Assert.AreEqual(compressedLength, stream.BaseStream.Position, "Compressed output length");

                stream.BaseStream.Position = 0;

                n = stream.Read(buffer, 0, buffer.Length);
                Assert.AreEqual(output.Length, n, "Decompressed input length");

                var text = Encoding.ASCII.GetString(buffer, 0, n);
                Assert.AreEqual(command, text);
            }
        }
Пример #11
0
        private static void DecoratorSection()
        {
            var stream = new CompressedStream(new EncryptedStream(new CloudStream()));

            stream.Write("1234-1234-1234-1234");
        }
Пример #12
0
        private static byte[] CompressDataWithDeflate(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                CompressedStream compressedStream = new CompressedStream(stream, StreamOperationMode.Write, new DeflateSettings());
                compressedStream.Write(data, 0, data.Length);
                compressedStream.Flush();

                return stream.ToArray();
            }
        }