Ejemplo n.º 1
0
        public void CreateViewAccessor_FileMappingViewHandleIsValid()
        {
            // arrange
            string path = @"../../Data/testdata.txt"; // source file;

            // act
            bool handleWasInitialized = false;
            FileMappingViewAccessor accessor;

            using (var hFileMapping = FileMappingWrapper.CreateFromFile(path))
            {
                using (accessor = hFileMapping.CreateViewAccessor(0, 0))
                {
                    if (accessor.FileMappingViewHandle != null &&
                        !accessor.FileMappingViewHandle.IsInvalid)
                    {
                        handleWasInitialized = true;
                    }
                }
            }

            // assert
            Assert.IsTrue(handleWasInitialized, "_01");
            Assert.IsTrue(accessor.FileMappingViewHandle != null, "_02");
            Assert.IsTrue(accessor.FileMappingViewHandle.IsInvalid, "_03");
        }
        static void Main(string[] args)
        {
            string path = args[1]; // source file;

            if (!File.Exists(path))
            {
                WriteMessageToUser("Can't find source file [{0}]", path);
            }

            Win32.SystemInfo info;
            Win32.GetSystemInfo(out info);

            WriteMessageToUser("NumberOfProcessors: {0}", info.NumberOfProcessors);
            WriteMessageToUser("AllocationGranularity: {0}", info.AllocationGranularity);
            WriteMessageToUser("PageSize: {0}", info.PageSize);
            WriteMessageToUser("ProcessorArchitecture: {0}", info.ProcessorArchitecture);

            using (var fileMapping = FileMappingWrapper.CreateFromFile(path))
            {
                long offset       = 0;
                long length       = new FileInfo(path).Length;
                long bytesInBlock = info.AllocationGranularity;

                while (length > 0)
                {
                    if (length < bytesInBlock)
                    {
                        bytesInBlock = length;
                    }

                    using (var accessor = fileMapping.CreateViewAccessor(offset, bytesInBlock))
                    {
                        int size = Marshal.SizeOf(typeof(byte));

                        for (long i = 0; i < bytesInBlock; i += size)
                        {
                            byte value = accessor.ReadByte(i);
                            if (value == 0xF)
                            {
                                value -= 0x1;
                            }
                            else if (value == 0xA)
                            {
                                value += 0x1;
                            }

                            //accessor.WriteByte(i, value);
                        }
                    }

                    WriteMessageToUser("offset: {0}, length: {1}", offset, length);

                    offset += bytesInBlock;
                    length -= bytesInBlock;
                }
            }

            WriteMessageToUser("{0} completed", args[0]);
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        internal SourceReader(string path, int granularity)
        {
            this.Path        = path;
            this.Granularity = granularity;

            // можно использовать лэйзи.
            fileMapping = FileMappingWrapper.CreateFromFile(Path);

            offset       = 0;
            length       = new FileInfo(Path).Length;
            bytesInBlock = Granularity;
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            string path = args[1]; // source file;

            if (!File.Exists(path))
            {
                WriteMessageToUser("Can't find source file [{0}]", path);
            }

            Win32.SystemInfo info;
            Win32.GetSystemInfo(out info);

            WriteMessageToUser("NumberOfProcessors: {0}", info.NumberOfProcessors);
            WriteMessageToUser("AllocationGranularity: {0}", info.AllocationGranularity);
            WriteMessageToUser("PageSize: {0}", info.PageSize);
            WriteMessageToUser("ProcessorArchitecture: {0}", info.ProcessorArchitecture);

            using (var fileMapping = FileMappingWrapper.CreateFromFile(path))
                using (var compressedFile = File.Create(new FileInfo(path).FullName + ".gz"))
                    using (var compressor = new GZipStream(compressedFile, CompressionMode.Compress))
                    {
                        long offset       = 0;
                        long length       = new FileInfo(path).Length;
                        int  bytesInBlock = (int)info.AllocationGranularity;

                        while (length > 0)
                        {
                            if (length < bytesInBlock)
                            {
                                bytesInBlock = (int)length;
                            }

                            using (var accessor = fileMapping.CreateViewAccessor(offset, bytesInBlock))
                            {
                                byte[] block = new byte[bytesInBlock];
                                accessor.ReadBytes(0, block, bytesInBlock);
                                compressor.Write(block, 0, bytesInBlock);
                            }

                            WriteMessageToUser("offset: {0}, length: {1}", offset, length);

                            offset += bytesInBlock;
                            length -= bytesInBlock;
                        }
                    }

            WriteMessageToUser("{0} completed", args[0]);
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        public void CreateFromFile_FileMappingHandleIsValid()
        {
            // arrange
            string path = @"../../Data/testdata.txt"; // source file;

            // act
            bool handleWasInitialized = false;
            FileMappingWrapper hFileMapping;

            using (hFileMapping = FileMappingWrapper.CreateFromFile(path))
            {
                if (hFileMapping.FileMappingHandle != null &&
                    !hFileMapping.FileMappingHandle.IsInvalid)
                {
                    handleWasInitialized = true;
                }
            }

            // assert
            Assert.IsTrue(handleWasInitialized, "_01");
            Assert.IsTrue(hFileMapping.FileMappingHandle != null, "_02");
            Assert.IsTrue(hFileMapping.FileMappingHandle.IsInvalid, "_03");
        }