本文整理汇总了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)
//.........这里部分代码省略.........
示例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;
//.........这里部分代码省略.........