Пример #1
0
        private void TestRandomWriteOperation(FileStream targetStream, AlignedWriteStream writeStream,
            byte[] data, int offset, int count)
        {
            long possition = targetStream.Position;
            long length = targetStream.Length;

            targetStream.Write(data, offset, count);
            writeStream.Write(data, offset, count);

            Assert.AreEqual(targetStream.Position, writeStream.Position, string.Format("Position mismatch (Original {0})", possition));
            Assert.AreEqual(targetStream.Length, writeStream.Length, string.Format("Length mismatch (Original {0})", length));
        }
Пример #2
0
 private void TestWriteStreamRandomOperations(int count, string inputfile, string outputfile1, string outputfile2, int alignmentSize,
     Func<FileStream, AlignedWriteStream, byte[], WriteSreamOperations> testOperation)
 {
     byte[] data = File.ReadAllBytes(inputfile);
     List<Tuple<WriteSreamOperations, long, long, long, long, long, long>> operations =
         new List<Tuple<WriteSreamOperations, long, long, long, long, long, long>>();
     using (FileStream targetFileStream1 = new FileStream(outputfile1, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
     {
         using (FileStream targetFileStream2 = new FileStream(outputfile2, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 8,
             FileOptions.WriteThrough | Win32.FileFlagNoBuffering))
         {
             using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream2, alignmentSize))
             {
                 while (0 < count)
                 {
                     long position = targetFileStream1.Position;
                     long length = targetFileStream1.Length;
                     WriteSreamOperations operation = testOperation(targetFileStream1, writeStream, data);
                     targetFileStream1.Flush();
                     byte[] data1 = this.ReadFile(outputfile1);
                     byte[] data2 = this.ReadFile(outputfile2);
                     if (Math.Abs(data1.Length - data2.Length) > alignmentSize * 2)
                     {
                         writeStream.Flush();
                         data2 = this.ReadFile(outputfile2);
                     }
                     Assert.IsTrue(Math.Abs(data1.Length - data2.Length) < alignmentSize*2, "Length mismatch");
                     int i = 0;
                     if (data1.Length < data2.Length)
                     {
                         i = this.FindNoneZero(data2, data1.Length);
                         Assert.IsTrue(-1 == i, "Junk found");
                     }
                     i = this.CompareBuffers(data1, data2, 0, Math.Min(data1.Length, data2.Length));
                     if (-1 != i)
                     {
                         writeStream.Flush();
                         data2 = this.ReadFile(outputfile2);
                         if (data1.Length < data2.Length)
                         {
                             i = this.FindNoneZero(data2, data1.Length);
                             Assert.IsTrue(-1 == i, "Junk found");
                         }
                         i = this.CompareBuffers(data1, data2, 0, Math.Min(data1.Length, data2.Length));
                         Assert.IsTrue(-1 == i, "Data mismatch");
                     }
                     count--;
                     operations.Add(new Tuple<WriteSreamOperations, long, long, long, long, long, long>(
                         operation, position, length, targetFileStream1.Position, targetFileStream1.Length,
                         targetFileStream2.Position, targetFileStream2.Length));
                 }
             }
         }
     }
     byte[] fdata1 = this.ReadFile(outputfile1);
     byte[] fdata2 = this.ReadFile(outputfile2);
     Assert.IsTrue(Math.Abs(fdata1.Length - fdata2.Length) < alignmentSize * 2, "Length mismatch");
     Assert.IsTrue(-1 == this.CompareBuffers(fdata1, fdata2, 0, fdata1.Length), "Data mismatch");
 }
Пример #3
0
        private void TestRandomSetLengthOperation(FileStream targetStream, AlignedWriteStream writeStream, long length)
        {
            long position = targetStream.Position;

            targetStream.SetLength(length);
            writeStream.SetLength(length);

            Assert.AreEqual(targetStream.Position, writeStream.Position, string.Format("Position mismatch (Original: {0})", position));
            Assert.AreEqual(targetStream.Length, writeStream.Length, "Length mismatch");
        }
Пример #4
0
 private WriteSreamOperations TestRandomWriteOperation(FileStream targetStream, AlignedWriteStream writeStream, byte[] data)
 {
     int offsetUpperBound = data.Length;
     int offset = Random.Next(offsetUpperBound);
     if (0 == Random.Next(2))
     {
         offset -= (offset % writeStream.AlignmentSize);
     }
     int countUpperBound = Math.Min((int)(data.Length - targetStream.Position), data.Length - offset);
     int count = Random.Next(countUpperBound);
     if (0 == Random.Next(2))
     {
         count -= (count % writeStream.AlignmentSize);
     }
     this.TestRandomWriteOperation(targetStream, writeStream, data, offset, count);
     return WriteSreamOperations.Write;
 }
Пример #5
0
 private WriteSreamOperations TestRandomOperation(FileStream targetStream, AlignedWriteStream writeStream, byte[] data)
 {
     WriteSreamOperations operation = (WriteSreamOperations)Random.Next((int)WriteSreamOperations.Count);
     switch (operation)
     {
         case WriteSreamOperations.SetLength:
             this.TestRandomSetLengthOperation(targetStream, writeStream, data);
             break;
         case WriteSreamOperations.Write:
             this.TestRandomWriteOperation(targetStream, writeStream, data);
             break;
         case WriteSreamOperations.Seek:
             this.TestRandomSeekOperation(writeStream, targetStream, data.Length);
             break;
         case WriteSreamOperations.Flush:
             this.TestRandomFlushOperation(writeStream, targetStream);
             break;
         default:
             Assert.Fail("Invalid operation");
             break;
     }
     return operation;
 }
Пример #6
0
 private void TestRandomSetLengthOperation(FileStream targetStream, AlignedWriteStream writeStream, byte[] data)
 {
     int lengthUpperBound = (int)data.Length;
     int length = Random.Next(lengthUpperBound);
     if (0 == Random.Next(2))
     {
         length -= (length % writeStream.AlignmentSize);
     }
     this.TestRandomSetLengthOperation(targetStream, writeStream, length);
 }
Пример #7
0
 private void TestAlignedWriteStreamWritePosition(string targetFilename)
 {
     var buffer = new byte[1024];
     using (FileStream targetFileStream = new FileStream(targetFilename, FileMode.Create, FileAccess.Write, FileShare.Write, 8,
         FileOptions.WriteThrough | Win32.FileFlagNoBuffering))
     {
         using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream))
         {
             writeStream.Write(buffer, 0, 500);
             Assert.AreEqual(500, writeStream.Position, "Invlid position");
             Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(500, writeStream.Length, "Invlid length");
             Assert.AreEqual(0, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 500, 524);
             Assert.AreEqual(1024, writeStream.Position, "Invlid position");
             Assert.AreEqual(writeStream.Position, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1024, writeStream.Length, "Invlid length");
             Assert.AreEqual(writeStream.Length, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 19, 512);
             Assert.AreEqual(1536, writeStream.Position, "Invlid position");
             Assert.AreEqual(writeStream.Position, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1536, writeStream.Length, "Invlid length");
             Assert.AreEqual(writeStream.Length, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 0, 1000);
             Assert.AreEqual(2536, writeStream.Position, "Invlid position");
             Assert.AreEqual(2048, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2536, writeStream.Length, "Invlid length");
             Assert.AreEqual(2048, targetFileStream.Length, "Invlid length");
         }
         Assert.AreEqual(2560, targetFileStream.Position, "Invlid position");
         Assert.AreEqual(2560, targetFileStream.Length, "Invlid position");
     }
 }
Пример #8
0
        private void TestRandomFlushOperation(AlignedWriteStream writeStream, FileStream targetStream)
        {
            writeStream.Flush();
            targetStream.Flush();

            Assert.AreEqual(targetStream.Position, writeStream.Position, "Position mismatch");
            Assert.AreEqual(targetStream.Length, writeStream.Length, "Length mismatch");
        }
Пример #9
0
 private void TestAlignedWriteStreamSeekPosition(string targetFilename)
 {
     var buffer = new byte[1024];
     using (FileStream targetFileStream = new FileStream(targetFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.Write, 8,
         FileOptions.WriteThrough | Win32.FileFlagNoBuffering))
     {
         using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream))
         {
             writeStream.Write(buffer, 0, 500);
             Assert.AreEqual(500, writeStream.Position, "Invlid position");
             Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(500, writeStream.Length, "Invlid length");
             Assert.AreEqual(0, targetFileStream.Length, "Invlid length");
             writeStream.Seek(400, SeekOrigin.Begin);
             Assert.AreEqual(400, writeStream.Position, "Invlid position");
             Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(500, writeStream.Length, "Invlid length");
             Assert.AreEqual(0, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 0, 650);
             Assert.AreEqual(1050, writeStream.Position, "Invlid position");
             Assert.AreEqual(1024, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1024, targetFileStream.Length, "Invlid length");
             writeStream.Seek(1000, SeekOrigin.Begin);
             Assert.AreEqual(1000, writeStream.Position, "Invlid position");
             Assert.AreEqual(512, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1536, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 19, 24);
             Assert.AreEqual(1024, writeStream.Position, "Invlid position");
             Assert.AreEqual(writeStream.Position, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1536, targetFileStream.Length, "Invlid length");
             writeStream.Seek(12, SeekOrigin.Begin);
             Assert.AreEqual(12, writeStream.Position, "Invlid position");
             Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1536, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 19, 500);
             Assert.AreEqual(512, writeStream.Position, "Invlid position");
             Assert.AreEqual(writeStream.Position, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1536, targetFileStream.Length, "Invlid length");
             writeStream.Seek(-2, SeekOrigin.End);
             Assert.AreEqual(1048, writeStream.Position, "Invlid position");
             Assert.AreEqual(1024, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(1050, writeStream.Length, "Invlid length");
             Assert.AreEqual(1536, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 0, 1000);
             Assert.AreEqual(2048, writeStream.Position, "Invlid position");
             Assert.AreEqual(2048, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2048, writeStream.Length, "Invlid length");
             Assert.AreEqual(writeStream.Length, targetFileStream.Length, "Invlid length");
             writeStream.Seek(-1000, SeekOrigin.Current);
             Assert.AreEqual(1048, writeStream.Position, "Invlid position");
             Assert.AreEqual(1024, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2048, writeStream.Length, "Invlid length");
             Assert.AreEqual(writeStream.Length, targetFileStream.Length, "Invlid length");
             writeStream.Seek(0, SeekOrigin.End);
             Assert.AreEqual(2048, writeStream.Position, "Invlid position");
             Assert.AreEqual(2048, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2048, writeStream.Length, "Invlid length");
             Assert.AreEqual(writeStream.Length, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 11, 2);
             Assert.AreEqual(2050, writeStream.Position, "Invlid position");
             Assert.AreEqual(2048, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2050, writeStream.Length, "Invlid length");
             Assert.AreEqual(2048, targetFileStream.Length, "Invlid length");
             writeStream.Seek(4000, SeekOrigin.Begin);
             Assert.AreEqual(4000, writeStream.Position, "Invlid position");
             Assert.AreEqual(3584, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(2050, writeStream.Length, "Invlid length");
             Assert.AreEqual(2560, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 40, 95);
             Assert.AreEqual(4095, writeStream.Position, "Invlid position");
             Assert.AreEqual(3584, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(4095, writeStream.Length, "Invlid length");
             Assert.AreEqual(2560, targetFileStream.Length, "Invlid length");
             writeStream.Write(buffer, 40, 1);
             Assert.AreEqual(4096, writeStream.Position, "Invlid position");
             Assert.AreEqual(4096, targetFileStream.Position, "Invlid position");
             Assert.AreEqual(4096, writeStream.Length, "Invlid length");
             Assert.AreEqual(4096, targetFileStream.Length, "Invlid length");
         }
         Assert.AreEqual(4096, targetFileStream.Position, "Invlid position");
         Assert.AreEqual(4096, targetFileStream.Length, "Invlid length");
     }
 }
Пример #10
0
        private void TestAlignedWriteStreamLength(string targetFilename)
        {
            var buffer = new byte[1024];
            using (FileStream targetFileStream = new FileStream(targetFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.Write, 8,
                FileOptions.WriteThrough | Win32.FileFlagNoBuffering))
            {
                using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream))
                {
                    writeStream.Write(buffer, 0, 1000);
                    Assert.AreEqual(1000, writeStream.Position, "Invlid position");
                    Assert.AreEqual(512, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(1000, writeStream.Length, "Invlid length");
                    Assert.AreEqual(512, targetFileStream.Length, "Invlid length");
                    writeStream.SetLength(1024);
                    Assert.AreEqual(1000, writeStream.Position, "Invlid position");
                    Assert.AreEqual(512, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(1024, writeStream.Length, "Invlid length");
                    Assert.AreEqual(1024, targetFileStream.Length, "Invlid length");
                    writeStream.SetLength(512);
                    Assert.AreEqual(512, writeStream.Position, "Invlid position");
                    Assert.AreEqual(512, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(512, writeStream.Length, "Invlid length");
                    Assert.AreEqual(512, targetFileStream.Length, "Invlid length");
                    writeStream.Write(buffer, 0, 1000);
                    Assert.AreEqual(1512, writeStream.Position, "Invlid position");
                    Assert.AreEqual(1024, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(1512, writeStream.Length, "Invlid length");
                    Assert.AreEqual(1024, targetFileStream.Length, "Invlid length");
                    writeStream.SetLength(2048);
                    Assert.AreEqual(1512, writeStream.Position, "Invlid position");
                    Assert.AreEqual(1024, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(2048, writeStream.Length, "Invlid length");
                    Assert.AreEqual(2048, targetFileStream.Length, "Invlid length");
                    writeStream.Write(buffer, 345, 38);
                    Assert.AreEqual(1550, writeStream.Position, "Invlid position");
                    Assert.AreEqual(1536, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(2048, writeStream.Length, "Invlid length");
                    Assert.AreEqual(2048, targetFileStream.Length, "Invlid length");
                    writeStream.SetLength(0);
                    Assert.AreEqual(0, writeStream.Position, "Invlid position");
                    Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(0, writeStream.Length, "Invlid length");
                    Assert.AreEqual(0, targetFileStream.Length, "Invlid length");
                    writeStream.Flush();
                    Assert.AreEqual(0, writeStream.Position, "Invlid position");
                    Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
                    Assert.AreEqual(0, writeStream.Length, "Invlid length");
                    Assert.AreEqual(0, targetFileStream.Length, "Invlid length");

                }
                Assert.AreEqual(0, targetFileStream.Position, "Invlid position");
                Assert.AreEqual(0, targetFileStream.Length, "Invlid position");
            }
        }
Пример #11
0
 protected void Decompress(SevenZipExtractor extractor, FileStream targetFileStream,
     int bufferSize, int preallocationPercent)
 {
     decimal preallocationSize = (((decimal)extractor.PackedSize * preallocationPercent) / 100);
     preallocationSize -= preallocationSize % this._sectorSize;
     if (targetFileStream.Length < preallocationSize)
     {
         targetFileStream.SetLength((long)preallocationSize);
     }
     long decompressedLength = 0;
     using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream, bufferSize))
     {
         extractor.ExtractFile(0, writeStream);
         decompressedLength = writeStream.Length;
     }
     if (targetFileStream.Length != decompressedLength)
     {
         this.SetFileLength(targetFileStream.SafeFileHandle, decompressedLength);
     }
 }
Пример #12
0
 protected long Compress(SevenZipCompressor compressor, AlignedReadStream readStream, AlignedWriteStream writeStream,
     string password)
 {
     using (MaxPositionStream maxWriteStream = new MaxPositionStream(writeStream))
     {
         if (string.IsNullOrEmpty(password))
         {
             compressor.CompressStream(readStream, maxWriteStream);
         }
         else
         {
             compressor.CompressStream(readStream, maxWriteStream, password);
         }
         return maxWriteStream.MaxPosition;
     }
 }
Пример #13
0
 protected void Compress(SevenZipCompressor compressor, FileStream sourceFileStream, FileStream targetFileStream,
     string password, int preallocationPercent, bool check, int bufferSize)
 {
     using (AlignedReadStream readStream = new AlignedReadStream(sourceFileStream, bufferSize))
     {
         decimal preallocationSize = (((decimal)readStream.Length * preallocationPercent) / 100);
         preallocationSize -= preallocationSize % this._sectorSize;
         if (targetFileStream.Length < preallocationSize)
         {
             targetFileStream.SetLength((long)preallocationSize);
         }
         long compressedLength = 0;
         using (AlignedWriteStream writeStream = new AlignedWriteStream(targetFileStream, bufferSize))
         {
             compressedLength = this.Compress(compressor, readStream, writeStream, password);
         }
         if (targetFileStream.Length != compressedLength)
         {
             this.SetFileLength(targetFileStream.SafeFileHandle, compressedLength);
         }
         if (check)
         {
             this.Check(targetFileStream.SafeFileHandle, password, bufferSize);
         }
     }
 }