Beispiel #1
0
        public void WriteAllBytes_SequenceMin3_Decoding()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(3, memoryStream);

            rleStreamWriter.WriteAllByte(new byte[] { 123, 34, 127, 127, 2, 12, 13, 13, 13, 13, 13, 13, 13, 13, 33 });

            CollectionAssert.AreEqual(new byte[] { 123, 34, 127, 127, 2, 12, 13, 13, 13, 5, 33 }, memoryStream.ToArray());
        }
Beispiel #2
0
        public void Write_OneByte_LengthEqualOne()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);

            rleStreamWriter.WriteByte(127);

            Assert.AreEqual(1, memoryStream.Length);
        }
Beispiel #3
0
        public void WriteAllBytes_TwoTheSameBytesAndSequance2AndOneOther()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);

            rleStreamWriter.WriteAllByte(new byte[] { 127, 127, 127, 127, 12 });

            CollectionAssert.AreEqual(new byte[] { 127, 127, 2, 12 }, memoryStream.ToArray());
        }
Beispiel #4
0
        public void Write_TwoTheSameBytes_LengthEqualTwo()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);

            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(127);

            Assert.AreEqual(2, memoryStream.Length);
        }
Beispiel #5
0
        public void Write_OneByte()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);
            var b = (byte)127;

            rleStreamWriter.WriteByte(b);

            memoryStream.Position = 0;
            Assert.AreEqual(b, memoryStream.ReadByte());
        }
Beispiel #6
0
        public void Write_TwoTheSameBytesAndOneOther_ThirdByteEqualOne()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);

            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(100);

            memoryStream.Position = 2;
            Assert.AreEqual(0, memoryStream.ReadByte());
        }
Beispiel #7
0
        public void Flush_ThreeTheSameBytes_LengthEqualThree()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);

            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(127);

            rleStreamWriter.Flush();

            Assert.AreEqual(3, memoryStream.Length);
        }
Beispiel #8
0
        public void Write_255TheSameBytesSequence_LengthEqualThree()
        {
            const int sequenceMin     = 2;
            var       memoryStream    = new MemoryStream();
            var       rleStreamWriter = new RleStreamWriter(sequenceMin, memoryStream);

            for (int i = 0; i < 255 + sequenceMin; i++)
            {
                rleStreamWriter.WriteByte(127);
            }

            Assert.AreEqual(3, memoryStream.Length);
        }
Beispiel #9
0
        public void Write_TwoTheSameBytesAndOneOther_FourthByteEqualOne()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);
            var thirdByte       = (byte)100;

            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(127);
            rleStreamWriter.WriteByte(thirdByte);

            memoryStream.Position = 3;
            Assert.AreEqual(thirdByte, memoryStream.ReadByte());
        }
Beispiel #10
0
        public void Write_ThreeTheSameBytes_ThirdByteEqualOne()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);
            var b = (byte)127;

            rleStreamWriter.WriteByte(b);
            rleStreamWriter.WriteByte(b);
            rleStreamWriter.WriteByte(b);

            memoryStream.Position = 2;
            Assert.AreEqual(-1, memoryStream.ReadByte());
        }
Beispiel #11
0
        public void Write_TwoTwoDifferentBytes()
        {
            var memoryStream    = new MemoryStream();
            var rleStreamWriter = new RleStreamWriter(2, memoryStream);
            var b1 = (byte)127;
            var b2 = (byte)100;

            rleStreamWriter.WriteByte(b1);
            rleStreamWriter.WriteByte(b2);

            memoryStream.Position = 0;
            Assert.AreEqual(b1, memoryStream.ReadByte());
            Assert.AreEqual(b2, memoryStream.ReadByte());
        }
Beispiel #12
0
        [Test] public void Write_255TheSameBytesSequence_ThirdByteEqual255()
        {
            const int sequenceMin     = 2;
            var       memoryStream    = new MemoryStream();
            var       rleStreamWriter = new RleStreamWriter(sequenceMin, memoryStream);

            for (int i = 0; i < 255 + sequenceMin; i++)
            {
                rleStreamWriter.WriteByte(127);
            }

            memoryStream.Position = 2;
            Assert.AreEqual(255, memoryStream.ReadByte());
        }
Beispiel #13
0
        public void CompressFile(string fileFullPath)
        {
            var  fileMode = File.Exists(FullPath(_filePath, _fileName)) ? FileMode.Append : FileMode.CreateNew;
            long position = 0;
            long lenght   = 0;

            using (var fileStreamReader = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
                using (var rleFileStreamWriter = new RleStreamWriter(_sequenceMin,
                                                                     new FileStream(FullPath(_filePath, _fileName), fileMode, FileAccess.Write)))
                {
                    int b;
                    while ((b = fileStreamReader.ReadByte()) >= 0)
                    {
                        position += rleFileStreamWriter.WriteByte((byte)b);
                        lenght++;
                    }
                }

            AddCompresionData(fileFullPath, position, lenght);
        }