本文整理汇总了C#中NPOI.POIFS.FileSystem.NPOIFSFileSystem.GetNextBlock方法的典型用法代码示例。如果您正苦于以下问题:C# NPOIFSFileSystem.GetNextBlock方法的具体用法?C# NPOIFSFileSystem.GetNextBlock怎么用?C# NPOIFSFileSystem.GetNextBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.POIFS.FileSystem.NPOIFSFileSystem
的用法示例。
在下文中一共展示了NPOIFSFileSystem.GetNextBlock方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: assertBATCount
protected static void assertBATCount(NPOIFSFileSystem fs, int expectedBAT, int expectedXBAT)
{
int foundBAT = 0;
int foundXBAT = 0;
int sz = (int)(fs.Size / fs.GetBigBlockSize());
for (int i = 0; i < sz; i++)
{
if (fs.GetNextBlock(i) == POIFSConstants.FAT_SECTOR_BLOCK)
{
foundBAT++;
}
if (fs.GetNextBlock(i) == POIFSConstants.DIFAT_SECTOR_BLOCK)
{
foundXBAT++;
}
}
Assert.AreEqual(expectedBAT, foundBAT, "Wrong number of BATs");
Assert.AreEqual(expectedXBAT, foundXBAT, "Wrong number of XBATs with " + expectedBAT + " BATs");
}
示例2: TestWriteStream4096
public void TestWriteStream4096()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));
// 0 -> 1 -> 2 -> end
Assert.AreEqual(1, fs.GetNextBlock(0));
Assert.AreEqual(2, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(4, fs.GetNextBlock(3));
// First free one is at 15
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(14));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(15));
// Write a 5 block file
byte[] data = new byte[4096 * 5];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
NPOIFSStream stream = new NPOIFSStream(fs, 0);
stream.UpdateContents(data);
// Check it
Assert.AreEqual(1, fs.GetNextBlock(0));
Assert.AreEqual(2, fs.GetNextBlock(1));
Assert.AreEqual(15, fs.GetNextBlock(2)); // Jumps
Assert.AreEqual(4, fs.GetNextBlock(3)); // Next stream
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(14));
Assert.AreEqual(16, fs.GetNextBlock(15)); // Continues
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(16)); // Ends
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(17)); // Free
// Check the contents too
IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
int count = 0;
while (it.MoveNext())
{
ByteBuffer b = it.Current;
data = new byte[512];
// b.get(data);
// Array.Copy(b, 0, data, 0, b.Length);
b.Read(data);
for (int i = 0; i < data.Length; i++)
{
byte exp = (byte)(i % 256);
Assert.AreEqual(exp, data[i]);
}
count++;
}
Assert.AreEqual(5, count);
}
示例3: TestWriteNewStreamExtraFATs
public void TestWriteNewStreamExtraFATs()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
// Allocate almost all the blocks
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(127));
for (int i = 100; i < 127; i++)
{
fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
}
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(127));
Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
// Write a 3 block stream
byte[] data = new byte[512 * 3];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
NPOIFSStream stream = new NPOIFSStream(fs);
stream.UpdateContents(data);
// Check we got another BAT
Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
// the BAT will be in the first spot of the new block
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(126));
Assert.AreEqual(129, fs.GetNextBlock(127));
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128));
Assert.AreEqual(130, fs.GetNextBlock(129));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(130));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(131));
}
示例4: TestWriteNewStream
public void TestWriteNewStream()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
// 100 is our first free one
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));
// Add a single block one
byte[] data = new byte[512];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
NPOIFSStream stream = new NPOIFSStream(fs);
stream.UpdateContents(data);
// Check it was allocated properly
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));
// And check the contents
IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
int count = 0;
while (it.MoveNext())
{
ByteBuffer b = it.Current;
data = new byte[512];
//b.get(data);
//Array.Copy(b, 0, data, 0, b.Length);
b.Read(data);
for (int i = 0; i < data.Length; i++)
{
byte exp = (byte)(i % 256);
Assert.AreEqual(exp, data[i]);
}
count++;
}
Assert.AreEqual(1, count);
// And a multi block one
data = new byte[512 * 3];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
stream = new NPOIFSStream(fs);
stream.UpdateContents(data);
// Check it was allocated properly
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
Assert.AreEqual(102, fs.GetNextBlock(101));
Assert.AreEqual(103, fs.GetNextBlock(102));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(103));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));
// And check the contents
it = stream.GetBlockIterator();
count = 0;
while (it.MoveNext())
{
ByteBuffer b = it.Current;
data = new byte[512];
//b.get(data);
// Array.Copy(b, 0, data, 0, b.Length);
b.Read(data);
for (int i = 0; i < data.Length; i++)
{
byte exp = (byte)(i % 256);
Assert.AreEqual(exp, data[i]);
}
count++;
}
Assert.AreEqual(3, count);
// Free it
stream.free();
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));
}
示例5: TestReplaceStreamWithMore
public void TestReplaceStreamWithMore()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
byte[] data = new byte[512 * 3];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
// 97 -> 98 -> end
Assert.AreEqual(98, fs.GetNextBlock(97));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98));
// 100 is our first free one
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
// Create a 2 block stream, will become a 3 block one
NPOIFSStream stream = new NPOIFSStream(fs, 97);
stream.UpdateContents(data);
// 97 -> 98 -> 100 -> end
Assert.AreEqual(98, fs.GetNextBlock(97));
Assert.AreEqual(100, fs.GetNextBlock(98));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
// Check the reading of blocks
IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
int count = 0;
while (it.MoveNext())
{
ByteBuffer b = it.Current;
data = new byte[512];
//b.get(data);
//Array.Copy(b, 0, data, 0, b.Length);
b.Read(data);
for (int i = 0; i < data.Length; i++)
{
byte exp = (byte)(i % 256);
Assert.AreEqual(exp, data[i]);
}
count++;
}
Assert.AreEqual(3, count);
}
示例6: TestReplaceStreamWithLess
public void TestReplaceStreamWithLess()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
byte[] data = new byte[512];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % 256);
}
// 97 -> 98 -> end
Assert.AreEqual(98, fs.GetNextBlock(97));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98));
// Create a 2 block stream, will become a 1 block one
NPOIFSStream stream = new NPOIFSStream(fs, 97);
stream.UpdateContents(data);
// 97 should now be the end, and 98 free
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(97));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(98));
// Check the reading of blocks
IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
Assert.AreEqual(true, it.MoveNext());
ByteBuffer b = it.Current;
Assert.AreEqual(false, it.MoveNext());
// Now check the contents
data = new byte[512];
// b.get(data);
//for (int i = 0; i < b.Length; i++)
// data[i] = b[i];
//Array.Copy(b, 0, data, 0, b.Length);
b.Read(data);
for (int i = 0; i < data.Length; i++)
{
byte exp = (byte)(i % 256);
Assert.AreEqual(exp, data[i]);
}
}
示例7: CreateWriteRead
public void CreateWriteRead()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem();
DocumentEntry miniDoc;
DocumentEntry normDoc;
// Initially has a BAT but not SBAT
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2));
// Check that the SBAT is empty
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
// Check that no properties table has been written yet
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.PropertyTable.StartBlock);
// Write and read it
fs = WriteOutAndReadBack(fs);
// Property table entries have been added to the blocks
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(3));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
Assert.AreEqual(2, fs.PropertyTable.StartBlock);
// Put everything within a new directory
DirectoryEntry testDir = fs.CreateDirectory("Test Directory");
// Add a new Normal Stream (Normal Streams minimum 4096 bytes)
byte[] main4096 = new byte[4096];
main4096[0] = unchecked((byte)-10);
main4096[4095] = unchecked((byte)-11);
testDir.CreateDocument("Normal4096", new MemoryStream(main4096));
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(4, fs.GetNextBlock(3));
Assert.AreEqual(5, fs.GetNextBlock(4));
Assert.AreEqual(6, fs.GetNextBlock(5));
Assert.AreEqual(7, fs.GetNextBlock(6));
Assert.AreEqual(8, fs.GetNextBlock(7));
Assert.AreEqual(9, fs.GetNextBlock(8));
Assert.AreEqual(10, fs.GetNextBlock(9));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(11));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
// Add a bigger Normal Stream
byte[] main5124 = new byte[5124];
main5124[0] = unchecked((byte)-22);
main5124[5123] = unchecked((byte)-33);
testDir.CreateDocument("Normal5124", new MemoryStream(main5124));
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(4, fs.GetNextBlock(3));
Assert.AreEqual(5, fs.GetNextBlock(4));
Assert.AreEqual(6, fs.GetNextBlock(5));
Assert.AreEqual(7, fs.GetNextBlock(6));
Assert.AreEqual(8, fs.GetNextBlock(7));
Assert.AreEqual(9, fs.GetNextBlock(8));
Assert.AreEqual(10, fs.GetNextBlock(9));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));
Assert.AreEqual(12, fs.GetNextBlock(11));
Assert.AreEqual(13, fs.GetNextBlock(12));
Assert.AreEqual(14, fs.GetNextBlock(13));
Assert.AreEqual(15, fs.GetNextBlock(14));
Assert.AreEqual(16, fs.GetNextBlock(15));
Assert.AreEqual(17, fs.GetNextBlock(16));
Assert.AreEqual(18, fs.GetNextBlock(17));
Assert.AreEqual(19, fs.GetNextBlock(18));
Assert.AreEqual(20, fs.GetNextBlock(19));
Assert.AreEqual(21, fs.GetNextBlock(20));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(22));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
// Now Add a mini stream
byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
testDir.CreateDocument("Mini", new MemoryStream(mini));
// Mini stream will Get one block for fat + one block for data
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(4, fs.GetNextBlock(3));
Assert.AreEqual(5, fs.GetNextBlock(4));
Assert.AreEqual(6, fs.GetNextBlock(5));
Assert.AreEqual(7, fs.GetNextBlock(6));
//.........这里部分代码省略.........
示例8: TestGetFreeBlockWithNoneSpare
public void TestGetFreeBlockWithNoneSpare()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
int free;
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
assertBATCount(fs, 1, 0);
for (int i = 100; i < 128; i++)
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(i));
Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
for (int i = 100; i < 128; i++)
fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
Assert.Fail("Should only be one BAT");
}
//catch (IndexOutOfRangeException)
catch (ArgumentOutOfRangeException)
{
}
assertBATCount(fs, 1, 0);
// Now ask for a free one, will need to extend the file
Assert.AreEqual(129, fs.GetFreeBlock());
Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(129));
// We now have 2 BATs, but no XBATs
assertBATCount(fs, 2, 0);
// Fill up to hold 109 BAT blocks
for (int i = 0; i < 109; i++)
{
fs.GetFreeBlock();
int startOffset = i * 128;
while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
{
free = fs.GetFreeBlock();
fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
}
}
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128).Block.HasFreeSectors);
Assert.Fail("Should only be 109 BATs");
}
// catch (IndexOutOfRangeException)
catch (ArgumentOutOfRangeException)
{
}
// We now have 109 BATs, but no XBATs
assertBATCount(fs, 109, 0);
// Ask for it to be written out, and check the header
HeaderBlock header = WriteOutAndReadHeader(fs);
Assert.AreEqual(109, header.BATCount);
Assert.AreEqual(0, header.XBATCount);
free = fs.GetFreeBlock();
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(110 * 128 - 1).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(110 * 128).Block.HasFreeSectors);
Assert.Fail("Should only be 110 BATs");
}
//catch (IndexOutOfRangeException)
catch (ArgumentOutOfRangeException)
{
}
assertBATCount(fs, 110, 1);
header = WriteOutAndReadHeader(fs);
Assert.AreEqual(110, header.BATCount);
Assert.AreEqual(1, header.XBATCount);
for (int i = 109; i < 109 + 127; i++)
{
fs.GetFreeBlock();
int startOffset = i * 128;
while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
{
free = fs.GetFreeBlock();
fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
}
//.........这里部分代码省略.........
示例9: TestGetFreeBlockWithSpare
public void TestGetFreeBlockWithSpare()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
Assert.AreEqual(100, fs.GetFreeBlock());
Assert.AreEqual(100, fs.GetFreeBlock());
fs.SetNextBlock(100, POIFSConstants.END_OF_CHAIN);
Assert.AreEqual(101, fs.GetFreeBlock());
fs.Close();
}
示例10: AddBeforeWrite
public void AddBeforeWrite()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem();
DocumentEntry miniDoc;
DocumentEntry normDoc;
HeaderBlock hdr;
// Initially has BAT + Properties but nothing else
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2));
hdr = WriteOutAndReadHeader(fs);
// No mini stream, and no xbats
// Will have fat then properties stream
Assert.AreEqual(1, hdr.BATCount);
Assert.AreEqual(0, hdr.BATArray[0]);
Assert.AreEqual(2, hdr.PropertyStart);
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.SBATStart);
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.XBATIndex);
Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 4, fs.Size);
// Get a clean filesystem to start with
fs = new NPOIFSFileSystem();
// Put our test files in a non-standard place
DirectoryEntry parentDir = fs.CreateDirectory("Parent Directory");
DirectoryEntry testDir = parentDir.CreateDirectory("Test Directory");
// Add to the mini stream
byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
testDir.CreateDocument("Mini", new MemoryStream(mini));
// Add to the main stream
byte[] main4096 = new byte[4096];
main4096[0] = unchecked((byte)-10);
main4096[4095] = unchecked((byte)-11);
testDir.CreateDocument("Normal4096", new MemoryStream(main4096));
// Check the mini stream was Added, then the main stream
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3));
Assert.AreEqual(5, fs.GetNextBlock(4));
Assert.AreEqual(6, fs.GetNextBlock(5));
Assert.AreEqual(7, fs.GetNextBlock(6));
Assert.AreEqual(8, fs.GetNextBlock(7));
Assert.AreEqual(9, fs.GetNextBlock(8));
Assert.AreEqual(10, fs.GetNextBlock(9));
Assert.AreEqual(11, fs.GetNextBlock(10));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(11));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(12));
Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 13, fs.Size);
// Check that we can read the right data pre-write
miniDoc = (DocumentEntry)testDir.GetEntry("Mini");
assertContentsMatches(mini, miniDoc);
normDoc = (DocumentEntry)testDir.GetEntry("Normal4096");
assertContentsMatches(main4096, normDoc);
// Write, Read, check
hdr = WriteOutAndReadHeader(fs);
fs = WriteOutAndReadBack(fs);
// Check the header details - will have the sbat near the start,
// then the properties at the end
Assert.AreEqual(1, hdr.BATCount);
Assert.AreEqual(0, hdr.BATArray[0]);
Assert.AreEqual(2, hdr.SBATStart);
Assert.AreEqual(12, hdr.PropertyStart);
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.XBATIndex);
// Check the block allocation is unChanged, other than
// the properties stream going in at the end
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3));
Assert.AreEqual(5, fs.GetNextBlock(4));
Assert.AreEqual(6, fs.GetNextBlock(5));
Assert.AreEqual(7, fs.GetNextBlock(6));
Assert.AreEqual(8, fs.GetNextBlock(7));
Assert.AreEqual(9, fs.GetNextBlock(8));
Assert.AreEqual(10, fs.GetNextBlock(9));
Assert.AreEqual(11, fs.GetNextBlock(10));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(11));
Assert.AreEqual(13, fs.GetNextBlock(12));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(13));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(14));
Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 15, fs.Size);
// Check the data
DirectoryEntry fsRoot = fs.Root;
Assert.AreEqual(1, fsRoot.EntryCount);
//.........这里部分代码省略.........
示例11: TestCreateWriteRead
public void TestCreateWriteRead()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem();
// Initially has a BAT but not SBAT
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2));
// Check that the SBAT is empty
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
// Write and read it
MemoryStream baos = new MemoryStream();
fs.WriteFilesystem(baos);
byte[] temp = baos.ToArray();
fs = new NPOIFSFileSystem(new MemoryStream(temp));
//fs = new NPOIFSFileSystem(new MemoryStream(baos.ToArray()));
// Check it's still like that
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2));
Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
// Now add a normal stream and a mini stream
// TODO
// TODO The rest of the test
}
示例12: TestGetFreeBlockWithNoneSpare
public void TestGetFreeBlockWithNoneSpare()
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
int free;
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
for (int i = 100; i < 128; i++)
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(i));
Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
for (int i = 100; i < 128; i++)
fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
Assert.Fail("Should only be one BAT");
}
//catch (IndexOutOfRangeException)
catch(ArgumentOutOfRangeException)
{
}
// Now ask for a free one, will need to extend the file
Assert.AreEqual(129, fs.GetFreeBlock());
Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128));
Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(129));
// Fill up to hold 109 BAT blocks
for (int i = 0; i < 109; i++)
{
fs.GetFreeBlock();
int startOffset = i * 128;
while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
{
free = fs.GetFreeBlock();
fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
}
}
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128).Block.HasFreeSectors);
Assert.Fail("Should only be 109 BATs");
}
// catch (IndexOutOfRangeException)
catch(ArgumentOutOfRangeException)
{
}
free = fs.GetFreeBlock();
Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(110 * 128 - 1).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(110 * 128).Block.HasFreeSectors);
Assert.Fail("Should only be 110 BATs");
}
//catch (IndexOutOfRangeException)
catch(ArgumentOutOfRangeException)
{
}
for (int i = 109; i < 109 + 127; i++)
{
fs.GetFreeBlock();
int startOffset = i * 128;
while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
{
free = fs.GetFreeBlock();
fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
}
}
// Should now have 109+127 = 236 BATs
Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors);
try
{
Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128).Block.HasFreeSectors);
Assert.Fail("Should only be 236 BATs");
}
catch (ArgumentOutOfRangeException)
{
}
// Ask for another, will get our 2nd XBAT
free = fs.GetFreeBlock();
Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors);
Assert.AreEqual(true, fs.GetBATBlockAndIndex(237 * 128 - 1).Block.HasFreeSectors);
try
{
//.........这里部分代码省略.........