Ejemplo n.º 1
0
        public void TestSetPositionWrite()
        {
            //Test setting Position durung a write by writing the contents of the file to disk backwards one byte at a time
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionWrite");

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(filePath);

            Assert.AreEqual(0, wfio.Position);

            for (int i = FILE_CONTENT.Length - 1; i >= 0; i--)
            {
                wfio.Position = i;
                Assert.AreEqual(i, wfio.Position);

                buffer[0] = FILE_CONTENT[i];
                wfio.WriteBlocks(1);
            }
            wfio.Close();

            byte[] actual = readFile(filePath);

            CollectionAssert.AreEqual(FILE_CONTENT, actual);
        }
Ejemplo n.º 2
0
        private void TestReadFileWinAPI1()
        {
            // This function tests out the Windows API function ReadFile.  This function tests the ReadFile function
            // by reading in the entire file with one call.
            String S;
            int    FileLoop;

            try
            {
                // Get how fast it takes to read in the .683MB, 10MB, and 50MB files into memory.
                for (FileLoop = 0; FileLoop < 3; FileLoop++)
                {
                    Stopwatch stopwatch = Stopwatch.StartNew();
                    WFIO.OpenForReading(FileNames[FileLoop]);
                    WFIO.Read(BufSizeM1M);
                    WFIO.Close();
                    stopwatch.Stop();

                    S = "   Total time reading " + FileDesc[FileLoop] + " with WFIO1.Read No Parsing			= "
                        + stopwatch.Elapsed.TotalMilliseconds.ToString();
                    DispUIMsg(S);
                }
            }
            catch (System.Exception ex)
            {
                S = "TestReadFileWinAPI1: threw an exception of type " + ex.GetType().ToString();
                DispUIMsg(S);
                DispUIMsg(ex.Message);
            }
        }
Ejemplo n.º 3
0
        public void TestSetPositionReadWrite()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionReadWrite");

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReadingWriting(filePath);

            Assert.AreEqual(0, wfio.Position);

            //Write stuff, move about writing more, then come back in & read it in etc...
            buffer[0] = 1;
            wfio.WriteBlocks(1);
            buffer[0] = 3;
            wfio.WriteBlocks(1);
            buffer[0] = 3;
            wfio.WriteBlocks(1);
            buffer[0] = 7;
            wfio.WriteBlocks(1);

            wfio.Position = 0;
            wfio.ReadBlocks(1);
            Assert.AreEqual(1, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(3, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(3, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(7, buffer[0]);

            wfio.Position = 2;
            buffer[0]     = 24;
            wfio.WriteBlocks(1);
            wfio.Position = 0;
            buffer[0]     = 10;
            wfio.WriteBlocks(1);
            wfio.Position = 2;
            wfio.ReadBlocks(1);
            Assert.AreEqual(24, buffer[0]);
            wfio.Position = 0;
            wfio.ReadBlocks(1);
            Assert.AreEqual(10, buffer[0]);

            wfio.Close();

            //Now check that the final file is what we expected it to be
            byte[] expected = new byte[] { 10, 3, 24, 7 };
            byte[] actual   = readFile(filePath);

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void TestLengthRead()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestLengthRead");

            createFile(filePath);

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReading(filePath);

            Assert.AreEqual((long)FILE_CONTENT.Length, wfio.Length);

            wfio.Close();
        }
Ejemplo n.º 5
0
        public void TestInitialPositionWrite()
        {
            const string FILE_NAME = "WinFileIOTests.TestInitialPositionWrite";

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(FILE_NAME);

            Assert.AreEqual(0, wfio.Position);

            wfio.Close();

            fileNames.Add(FILE_NAME);
        }
Ejemplo n.º 6
0
        public void TestReadWholeFile()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestReadWholeFile");

            createFile(filePath);

            byte[]    buffer = new byte[FILE_CONTENT.Length];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReading(filePath);
            wfio.ReadBlocks(buffer.Length);
            wfio.Close();

            CollectionAssert.AreEqual(FILE_CONTENT, buffer);
        }
Ejemplo n.º 7
0
        public void TestLengthWrite()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestLengthWrite");

            byte[]    buffer = new byte[FILE_CONTENT.Length];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(filePath);
            Array.Copy(FILE_CONTENT, buffer, buffer.Length);
            wfio.WriteBlocks(buffer.Length);

            Assert.AreEqual((long)FILE_CONTENT.Length, wfio.Length);
            wfio.Close();

            fileNames.Add(filePath);
        }
Ejemplo n.º 8
0
        public void TestReadFirst8Bytes()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestReadFirst8Bytes");

            createFile(filePath);

            byte[]    buffer = new byte[8];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReading(filePath);
            wfio.ReadBlocks(buffer.Length);
            wfio.Close();

            byte[] expected = FILE_CONTENT.Take(buffer.Length).ToArray();
            CollectionAssert.AreEqual(expected, buffer);
        }
Ejemplo n.º 9
0
        public void TestPositionIncrementsWrite()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestPositionIncrementsWrite");

            byte[]    buffer = new byte[] { 7, 3 };
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(filePath);
            wfio.WriteBlocks(buffer.Length);

            Assert.AreEqual(2, wfio.Position);

            wfio.Close();

            fileNames.Add(filePath);
        }
Ejemplo n.º 10
0
        public void TestPositionIncrementsRead()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestPositionIncrementsRead");

            createFile(filePath);

            byte[]    buffer = new byte[5];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReading(filePath);
            wfio.ReadBlocks(buffer.Length);

            Assert.AreEqual(5, wfio.Position);

            wfio.Close();
        }
Ejemplo n.º 11
0
        public void WritePointArray(byte[] points_buffer)
        {
            if (File.Exists(_FullName))
            {
                Close();
                File.Delete(_FullName);
            }
            _WFIO = new WinFileIO();
            _WFIO.OpenForWriting(_FullName);
            byte[] header_bytes = _Header.ToByteArray();
            _WFIO.PinBuffer(header_bytes);
            _WFIO.WriteBlocks(header_bytes.Length);

            _WFIO.PinBuffer(points_buffer);
            _WFIO.WriteBlocks(points_buffer.Length);

            _WFIO.Close();
        }
Ejemplo n.º 12
0
        public void TestWriteFirstByte()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestWriteFirstByte");

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(filePath);

            buffer[0] = FILE_CONTENT[0];
            wfio.WriteBlocks(buffer.Length);
            wfio.Close();

            byte[] expected = FILE_CONTENT.Take(1).ToArray();
            byte[] actual   = readFile(filePath);

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 13
0
        public void TestWriteWholeFile()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestWriteWholeFile");

            byte[]    buffer = new byte[FILE_CONTENT.Length];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForWriting(filePath);

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = FILE_CONTENT[i];
            }
            wfio.WriteBlocks(buffer.Length);
            wfio.Close();

            byte[] expected = FILE_CONTENT;
            byte[] actual   = readFile(filePath);

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 14
0
        public void TestSetPositionRead()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionRead");

            createFile(filePath);

            byte[]    buffer = new byte[4];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReading(filePath);

            Assert.AreEqual(0, wfio.Position);
            wfio.Position = 5;
            Assert.AreEqual(5, wfio.Position);
            wfio.ReadBlocks(buffer.Length);
            Assert.AreEqual(5 + buffer.Length, wfio.Position);

            byte[] expected = FILE_CONTENT.Skip(5).Take(buffer.Length).ToArray();
            CollectionAssert.AreEqual(expected, buffer);

            wfio.Close();
        }
Ejemplo n.º 15
0
        private void TestRead()
        {
            // This function tests the Read function.
            int    BytesRead, Loop;
            String S;

            try
            {
                WFIO.OpenForReading(TestFileName);
                BytesRead = WFIO.Read(BufSize);
                WFIO.Close();
                // Check to see if the data read in matches the verification buffer.
                if (BytesRead != VerBytesRead)
                {
                    S = "  TestRead: test failed - bytes read != VerBytesRead";
                    DispUIMsg(S);
                    return;
                }
                // Compare the 2 arrays.  If there are any differences, then report an error.
                for (Loop = 0; Loop < BytesRead; Loop++)
                {
                    if (ByteBuf[Loop] != ByteBufVer[Loop])
                    {
                        S = "  TestRead: test failed - the " + Loop.ToString() + " element does not match the verification buffer.";
                        DispUIMsg(S);
                        return;
                    }
                }
                S = "  TestRead: Passed";
                DispUIMsg(S);
            }
            catch (System.Exception ex)
            {
                S = "  TestRead: threw an exception of type " + ex.GetType().ToString();
                DispUIMsg(S);
                S = "  " + ex.Message;
                DispUIMsg(S);
            }
        }
 public void TestReadFileWinApi1(string pathToFile)
 {
     _wfio.OpenForReading(pathToFile);
     _wfio.Read(BufSizeM1M);
     _wfio.Close();
 }
Ejemplo n.º 17
0
        public static void Compress(string Folder, Action <int, int> reportProgress, bool is64 = false, int compression = 1)
        {
            string[] files = Directory.EnumerateFiles(Folder, "*", SearchOption.AllDirectories).ToArray();

            MemoryStream fileTableEntriesStream = new MemoryStream();
            MemoryStream fileTableStream        = new MemoryStream();

            for (var i = 0; i < files.Length; i++)
            {
                reportProgress(i + 1, files.Length);

                string path = files[i].Replace(Folder, "").TrimStart('\\');

                byte[] unpackedFileBuffer;
                using (FileStream fis = new FileStream(files[i], FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 2 << 15))
                    using (MemoryStream buf = new MemoryStream())
                    {
                        if (path.EndsWith(".xml") || path.EndsWith(".x16"))
                        {
                            // encode bxml
                            Convert(fis, BXML.DetectType(fis), buf, BXML_TYPE.BXML_BINARY);
                        }
                        else
                        {
                            // compress raw
                            fis.CopyTo(buf);
                        }

                        unpackedFileBuffer = buf.ToArray();
                    }

                BPKG_FTE entry = new BPKG_FTE
                {
                    FilePathLength       = path.Length,
                    FilePath             = path,
                    IsCompressed         = true,
                    IsEncrypted          = true,
                    FileDataOffset       = (int)fileTableStream.Position,
                    FileDataSizeUnpacked = unpackedFileBuffer.Length
                };

                byte[] packedFileBuffer = Pack(unpackedFileBuffer, out entry.FileDataSizeSheared, out entry.FileDataSizeStored, entry.IsEncrypted, entry.IsCompressed, compression);
                fileTableStream.Write(packedFileBuffer, 0, packedFileBuffer.Length);

                entry.WriteTo(fileTableEntriesStream, is64);
            }

            MemoryStream packageStream = new MemoryStream();
            BinaryWriter package       = new BinaryWriter(packageStream);

            package.Write(new[]
            {
                (byte)'U', (byte)'O', (byte)'S', (byte)'E', (byte)'D', (byte)'A', (byte)'L', (byte)'B'
            });                                          // Signature
            package.Write(2);                            // Version
            package.Write(new byte[] { 0, 0, 0, 0, 0 }); // Unknown 1

            if (is64)
            {
                package.Write(fileTableStream.Length);
                package.Write((long)files.Length);
            }
            else
            {
                package.Write((int)fileTableStream.Length);
                package.Write(files.Length);
            }

            package.Write(true);         // Is compressed
            package.Write(true);         // Is encrypted
            package.Write(new byte[62]); // Unknown 2

            int FTESizePacked;

            byte[] packedFTEBuffer = Pack(fileTableEntriesStream.ToArray(), out _, out FTESizePacked, true, true, compression);

            if (is64)
            {
                package.Write((long)FTESizePacked);
                package.Write(fileTableEntriesStream.Length);
            }
            else
            {
                package.Write(FTESizePacked);
                package.Write((int)fileTableEntriesStream.Length);
            }

            package.Write(packedFTEBuffer);

            int globalOffset = (int)packageStream.Position + (is64 ? 8 : 4);

            if (is64)
            {
                package.Write((long)globalOffset);
            }
            else
            {
                package.Write(globalOffset);
            }

            // Write the packed file data
            package.Write(fileTableStream.ToArray());

            WinFileIO writer = new WinFileIO(packageStream.ToArray());

            writer.OpenForWriting(Folder.Replace(".files", ""));
            writer.WriteBlocks((int)packageStream.Length);
            writer.Close();
        }