Example #1
0
 internal static ReadOnlyMemoryMappedViewByteBuffer Copy(MemoryMappedViewByteBuffer other, int markOfOther)
 {
     return(new ReadOnlyMemoryMappedViewByteBuffer(other.accessor, other.Capacity, other.offset)
     {
         limit = other.Limit,
         position = other.Position,
         mark = markOfOther,
         Order = other.Order
     });
 }
Example #2
0
            public void TestFlush()
            {
                long fileSize = Random.Next(3 * 1024 * 1024);
                int cut = Random.Next((int)fileSize);
                var file = new FileInfo(Path.GetTempFileName());

                using (FileStream raf = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite))
                using (var fc = MemoryMappedFile.CreateFromFile(raf, null, fileSize, MemoryMappedFileAccess.ReadWrite,
                #if FEATURE_MEMORYMAPPEDFILESECURITY
                    null,
                #endif
                    HandleInheritability.None, false))
                {
                    MemoryMappedViewByteBuffer mbb = fc.CreateViewByteBuffer(cut, fileSize - cut, MemoryMappedFileAccess.ReadWrite);
                    mbb.Flush();
                }

                // improve chance that mapped buffer will be unmapped
                GC.Collect();
                DeleteFile(file);
            }
            public void TestZeroMap()
            {
                long fileSize = Random.Next(1024 * 1024);
                int  cut      = Random.Next((int)fileSize);
                var  file     = new FileInfo(Path.GetTempFileName());

                using (FileStream raf = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite))
                    using (var fc = MemoryMappedFile.CreateFromFile(raf, null, fileSize, MemoryMappedFileAccess.ReadWrite,
#if !NETSTANDARD
                                                                    null,
#endif
                                                                    HandleInheritability.None, false))
                    {
                        MemoryMappedViewByteBuffer mbb = fc.CreateViewByteBuffer(cut, 0, MemoryMappedFileAccess.ReadWrite);
                        mbb.Flush();
                    }

                // improve chance that mapped buffer will be unmapped
                GC.Collect();
                Thread.Sleep(500);

                DeleteFile(file);
            }
Example #4
0
            public void TestZeroMap() //"Awaits fix: https://github.com/xamarin/xamarin-android/issues/5423"
            {
                long fileSize = Random.Next(1024 * 1024);
                int  cut      = Random.Next((int)fileSize);
                var  file     = new FileInfo(Path.GetTempFileName());

                using (FileStream raf = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite))
                    using (var fc = MemoryMappedFile.CreateFromFile(raf, null, fileSize, MemoryMappedFileAccess.ReadWrite,
#if FEATURE_MEMORYMAPPEDFILESECURITY
                                                                    null,
#endif
                                                                    HandleInheritability.None, false))
                    {
                        MemoryMappedViewByteBuffer mbb = fc.CreateViewByteBuffer(cut, 0, MemoryMappedFileAccess.ReadWrite);
                        mbb.Flush();
                    }

                // improve chance that mapped buffer will be unmapped
                GC.Collect();
                Thread.Sleep(500);

                DeleteFile(file);
            }
Example #5
0
        public void Test_Flush()
        {
            MemoryMappedViewByteBuffer mmbRead;

            // buffer was not mapped in read/write mode
            using (FileStream fileInputStream = new FileStream(tmpFile.FullName, FileMode.Open, FileAccess.Read))
            {
                using (var fileChannelRead = MemoryMappedFile.CreateFromFile(fileInputStream, null, 0, MemoryMappedFileAccess.Read,
            #if FEATURE_MEMORYMAPPEDFILESECURITY
                null,
            #endif
                HandleInheritability.None, true))
                {
                    //FileChannel fileChannelRead = fileInputStream.getChannel();
                    //MappedByteBuffer mmbRead = fileChannelRead.map(MapMode.READ_ONLY, 0,
                    //        fileChannelRead.size());

                    mmbRead = fileChannelRead.CreateViewByteBuffer(0, (int)fileInputStream.Length, MemoryMappedFileAccess.Read);
                    mmbRead.Flush();
                }

                //using (FileStream inputStream = new FileStream(tmpFile.FullName, FileMode.Open, FileAccess.Read))
                using (var fileChannelR = MemoryMappedFile.CreateFromFile(fileInputStream, null, 0, MemoryMappedFileAccess.Read,
            #if FEATURE_MEMORYMAPPEDFILESECURITY
                null,
            #endif
                HandleInheritability.None, false))
                {
                    //FileInputStream inputStream = new FileInputStream(tmpFile);
                    //FileChannel fileChannelR = inputStream.getChannel();
                    //MappedByteBuffer resultRead = fileChannelR.map(MapMode.READ_ONLY, 0,
                    //        fileChannelR.size());
                    MemoryMappedViewByteBuffer resultRead = fileChannelR.CreateViewByteBuffer(0, (int)fileInputStream.Length, MemoryMappedFileAccess.Read);

                    //If this buffer was not mapped in read/write mode, then invoking this method has no effect.
                    assertEquals(
                                    "Invoking force() should have no effect when this buffer was not mapped in read/write mode",
                            mmbRead, resultRead);
                }
            }

            MemoryMappedViewByteBuffer mmbReadWrite;

            // Buffer was mapped in read/write mode
            using (FileStream randomFile = new FileStream(tmpFile.FullName, FileMode.Open, FileAccess.ReadWrite))
            {
                using (var fileChannelReadWrite = MemoryMappedFile.CreateFromFile(randomFile, null, 0, MemoryMappedFileAccess.ReadWrite,
            #if FEATURE_MEMORYMAPPEDFILESECURITY
                null,
            #endif
                HandleInheritability.None, true))
                {
                    //RandomAccessFile randomFile = new RandomAccessFile(tmpFile, "rw");
                    //FileChannel fileChannelReadWrite = randomFile.getChannel();
                    //MappedByteBuffer mmbReadWrite = fileChannelReadWrite.map(
                    //        FileChannel.MapMode.READ_WRITE, 0, fileChannelReadWrite.size());
                    mmbReadWrite = fileChannelReadWrite.CreateViewByteBuffer(0, (int)randomFile.Length, MemoryMappedFileAccess.ReadWrite);
                    mmbReadWrite.Put((byte)'o');
                    mmbReadWrite.Flush();
                }

                //using (FileStream random = new FileStream(tmpFile.FullName, FileMode.Open, FileAccess.ReadWrite))
                using (var fileChannelRW = MemoryMappedFile.CreateFromFile(randomFile, null, 0, MemoryMappedFileAccess.ReadWrite,
            #if FEATURE_MEMORYMAPPEDFILESECURITY
                null,
            #endif
                HandleInheritability.None, false))
                {

                    //RandomAccessFile random = new RandomAccessFile(tmpFile, "rw");
                    //FileChannel fileChannelRW = random.getChannel();
                    //MappedByteBuffer resultReadWrite = fileChannelRW.map(
                    //        FileChannel.MapMode.READ_WRITE, 0, fileChannelRW.size());

                    MemoryMappedViewByteBuffer resultReadWrite = fileChannelRW.CreateViewByteBuffer(0, (int)randomFile.Length, MemoryMappedFileAccess.ReadWrite);

                    // Invoking force() will change the buffer
                    assertFalse(mmbReadWrite.Equals(resultReadWrite));
                }
            }

                //fileChannelRead.close();
                //fileChannelR.close();
                //fileChannelReadWrite.close();
                //fileChannelRW.close();
            //}
        }