當前位置: 首頁>>代碼示例>>C#>>正文


C# Block.Write方法代碼示例

本文整理匯總了C#中Block.Write方法的典型用法代碼示例。如果您正苦於以下問題:C# Block.Write方法的具體用法?C# Block.Write怎麽用?C# Block.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Block的用法示例。


在下文中一共展示了Block.Write方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Resize

        /// <summary>
        /// 修改並釋放數據占用空間,刪除第 newSize 字節開始直到原文件大小的數據塊
        /// </summary>
        /// <param name="newSize"></param>
        public void Resize(UInt32 newSize)
        {
            try
            {
                // 寫入時動態擴充,因此向更大大小擴展不做處理
                if (newSize >= data.sizeByte)
                {
                    return;
                }

                Console.WriteLine("Resizing inode {0} size from {1} to {2}...", index, data.sizeByte, newSize);

                UInt32 position = newSize;

                // 縮小占用空間
                UInt32 blockSize = vfs.GetSuperBlock().data.blockSize;
                UInt32 IndexPerBlock = blockSize / sizeof(UInt32);

                // 檢查是否需要整理二級間接索引
                Int32 lv0_index = 13;
                Int32 lv1_index = -1;
                Int32 lv2_index = -1;
                if (position > BoundLv1)
                {
                    lv1_index = (int)(((position - BoundLv1) / blockSize) / IndexPerBlock);
                    lv2_index = (int)(((position - BoundLv1) / blockSize) % IndexPerBlock);
                }
                if (data.dataBlockId[lv0_index] != UInt32.MaxValue)
                {
                    //Console.WriteLine("Level 2 block recycling...");

                    // 讀取其中所有項
                    Block lv0_block = new Block(vfs, data.dataBlockId[lv0_index]);
                    UInt32[] lv1_block_id = lv0_block.ReadArray<UInt32>(0, (int)IndexPerBlock);

                    // 依次釋放 lv1_index 之後所有的項
                    for (var i = lv1_index + 1; i < IndexPerBlock; ++i)
                    {
                        if (lv1_block_id[i] != UInt32.MaxValue)
                        {
                            Block lv1_block = new Block(vfs, lv1_block_id[i]);
                            UInt32[] lv2_block_id = lv1_block.ReadArray<UInt32>(0, (int)IndexPerBlock);

                            for (var j = 0; j < IndexPerBlock; ++j)
                            {
                                //Console.WriteLine(j);
                                //Console.WriteLine("{0}, {1}", j, lv2_block_id[j]);
                                if (lv2_block_id[j] != UInt32.MaxValue)
                                {
                                    //Console.WriteLine("Dealloc lv2 block: inode->{0}->{1}->{2}->{3}", lv0_index, lv0_block.index, lv1_block.index, lv2_block_id[j]);
                                    vfs.DeAllocateBlock(lv2_block_id[j]);
                                    data.blockPreserved--;
                                }
                            }

                            //Console.WriteLine("Dealloc lv1 block: inode->{0}->{1}->{2}", lv0_index, lv0_block.index, lv1_block.index);
                            vfs.DeAllocateBlock(lv1_block.index);
                            data.blockPreserved--;
                            lv0_block.Write((uint)(i * sizeof(UInt32)), UInt32.MaxValue);
                        }
                    }

                    // 釋放 lv1_index 中 lv2_index 之後的所有項
                    if (lv1_index != -1 && lv1_block_id[lv1_index] != UInt32.MaxValue)
                    {
                        Block lv1_block = new Block(vfs, lv1_block_id[lv1_index]);
                        UInt32[] lv2_block_id = lv1_block.ReadArray<UInt32>(0, (int)IndexPerBlock);

                        for (var j = lv2_index; j < IndexPerBlock; ++j)
                        {
                            if (lv2_block_id[j] != UInt32.MaxValue)
                            {
                                //Console.WriteLine("Dealloc lv2 block: inode->{0}->{1}->{2}->{3}", lv0_index, lv0_block.index, lv1_block.index, lv2_block_id[j]);
                                vfs.DeAllocateBlock(lv2_block_id[j]);
                                data.blockPreserved--;
                                lv1_block.Write((uint)(j * sizeof(UInt32)), UInt32.MaxValue);
                            }
                        }
                    }

                    // 回收 inode.dataBlockId[13]
                    if (lv1_index == -1)
                    {
                        //Console.WriteLine("Dealloc lv0 block: inode->{0}->{1}", lv0_index, data.dataBlockId[lv0_index]);
                        vfs.DeAllocateBlock(data.dataBlockId[lv0_index]);
                        data.blockPreserved--;
                        data.dataBlockId[lv0_index] = UInt32.MaxValue;
                    }
                }

                // 是否需要整理一級索引
                lv0_index = 12;
                lv1_index = -1;
                if (position < BoundLv1)
                {
                    if (position > BoundLv0)
//.........這裏部分代碼省略.........
開發者ID:SummerWish,項目名稱:vfs,代碼行數:101,代碼來源:INode.cs

示例2: PrepareBlockAtPosition

        /// <summary>
        /// 查找指定偏移所在的塊,若塊不存在則創建
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public Block PrepareBlockAtPosition(UInt32 position)
        {
            UInt32 blockSize = vfs.GetSuperBlock().data.blockSize;
            UInt32 IndexPerBlock = blockSize / sizeof(UInt32);

            // 直接索引
            if (position < BoundLv0)
            {
                UInt32 totalBlocks = 0;
                UInt32 lv0_index = position / blockSize;

                totalBlocks += lv0_index + 1;
                UInt32 preserve = GetBlocksToPreserve(totalBlocks);
                //Console.WriteLine("preserve block: {0} (+{1})", totalBlocks, preserve);

                if (preserve > 0)
                {
                    vfs.PreserveBlock(preserve);
                    data.blockPreserved += preserve;
                    //Console.WriteLine("data.blockPreserved = {0}", data.blockPreserved);
                    Save();
                }

                Block lv0_block = new Block(vfs, data.dataBlockId[lv0_index]);
                if (lv0_block.index == UInt32.MaxValue)
                {
                    lv0_block = vfs.AllocateBlock();
                    data.dataBlockId[lv0_index] = lv0_block.index;
                    Save();
                }

                return lv0_block;
            }
            else if (position < BoundLv1)
            {
                // 一級間接索引
                position -= BoundLv0;

                UInt32 totalBlocks = 0;
                UInt32 lv0_index = 12;
                UInt32 lv1_index = position / blockSize;

                totalBlocks += lv0_index + 1;
                totalBlocks += lv1_index + 1;
                UInt32 preserve = GetBlocksToPreserve(totalBlocks);
                //Console.WriteLine("preserve block: {0} (+{1})", totalBlocks, preserve);

                if (preserve > 0)
                {
                    vfs.PreserveBlock(preserve);
                    data.blockPreserved += preserve;
                    //Console.WriteLine("data.blockPreserved = {0}", data.blockPreserved);
                    Save();
                }

                Block lv0_block = new Block(vfs, data.dataBlockId[lv0_index]);
                if (lv0_block.index == UInt32.MaxValue)
                {
                    lv0_block = vfs.AllocateBlock(0xFF);
                    data.dataBlockId[lv0_index] = lv0_block.index;
                    Save();
                }

                Block lv1_block = new Block(vfs, lv0_block.Read<UInt32>(lv1_index * sizeof(UInt32)));
                if (lv1_block.index == UInt32.MaxValue)
                {
                    lv1_block = vfs.AllocateBlock();
                    lv0_block.Write(lv1_index * sizeof(UInt32), lv1_block.index);
                }

                return lv1_block;
            }
            else
            {
                // 二級間接索引
                position -= BoundLv1;

                UInt32 totalBlocks = 0;
                UInt32 lv0_index = 13;
                UInt32 lv1_index = (position / blockSize) / IndexPerBlock;
                UInt32 lv2_index = (position / blockSize) % IndexPerBlock;

                //Console.WriteLine("position = {0}, lv0_index = {1}, lv1_index = {2}, lv2_index = {3}", position, lv0_index, lv1_index, lv2_index);

                totalBlocks += lv0_index + 1;
                totalBlocks += IndexPerBlock + 1;
                totalBlocks += (IndexPerBlock + 1) * lv1_index;
                totalBlocks += lv2_index + 1;
                UInt32 preserve = GetBlocksToPreserve(totalBlocks);
                //Console.WriteLine("preserve block: {0} (+{1})", totalBlocks, preserve);

                if (preserve > 0)
                {
                    vfs.PreserveBlock(preserve);
                    data.blockPreserved += preserve;
//.........這裏部分代碼省略.........
開發者ID:SummerWish,項目名稱:vfs,代碼行數:101,代碼來源:INode.cs


注:本文中的Block.Write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。