本文整理汇总了C#中ChunkedMemoryStream.SetLength方法的典型用法代码示例。如果您正苦于以下问题:C# ChunkedMemoryStream.SetLength方法的具体用法?C# ChunkedMemoryStream.SetLength怎么用?C# ChunkedMemoryStream.SetLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ChunkedMemoryStream
的用法示例。
在下文中一共展示了ChunkedMemoryStream.SetLength方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Write_64k_bytes_Truncate_17k_Read_64k
public void Write_64k_bytes_Truncate_17k_Read_64k()
{
byte[] bytes = GetBytes(64 * 1024);
var stream = new ChunkedMemoryStream();
stream.Write(bytes, 0, bytes.Length);
stream.SetLength(17 * 1024);
stream.Position = 0;
byte[] buffer = new byte[bytes.Length];
int read = stream.Read(buffer, 0, buffer.Length);
Assert.AreEqual(17 * 1024, read);
Assert.AreEqual(bytes.Take(17 * 1024).ToArray(), buffer.Take(17 * 1024).ToArray());
Assert.AreEqual(0, stream.Read(buffer, 0, buffer.Length));
}
示例2: TestSetLength
public void TestSetLength()
{
using (var stream = new ChunkedMemoryStream(4)) {
for (var len = 0L; len < 12L; len++) {
stream.SetLength(len);
Assert.AreEqual(len, stream.Length);
Assert.AreEqual(0L, stream.Position);
}
stream.Position = stream.Length;
for (var len = 12L; len <= 0L; len--) {
stream.SetLength(len);
Assert.AreEqual(len, stream.Length);
Assert.AreEqual(len, stream.Position);
}
stream.SetLength(22L);
Assert.AreEqual(22L, stream.Length);
stream.Position = 22L;
stream.SetLength(14L);
Assert.AreEqual(14L, stream.Length);
Assert.AreEqual(14L, stream.Position);
Assert.AreEqual(-1, stream.ReadByte());
Assert.AreEqual(14L, stream.Position);
stream.SetLength(3L);
Assert.AreEqual(3L, stream.Length);
Assert.AreEqual(3L, stream.Position);
Assert.AreEqual(-1, stream.ReadByte());
Assert.AreEqual(3L, stream.Position);
stream.SetLength(13L);
Assert.AreEqual(13L, stream.Length);
Assert.AreEqual(3L, stream.Position);
Assert.AreEqual(0, stream.ReadByte());
Assert.AreEqual(4L, stream.Position);
}
}
示例3: TestSetLengthNegative
public void TestSetLengthNegative()
{
using (var stream = new ChunkedMemoryStream(4)) {
stream.SetLength(-1);
}
}
示例4: TestAllocateDisposeChunk
public void TestAllocateDisposeChunk()
{
var allocated = new List<TestChunk>();
using (var stream = new ChunkedMemoryStream(4, delegate(int size) {
Assert.AreEqual(4, size);
return new TestChunk(size, allocated);
})) {
Assert.AreEqual(1, allocated.Count, "first chunk");
var writer = new System.IO.BinaryWriter(stream);
writer.Write(new byte[] {0x00, 0x01, 0x02});
writer.Flush();
Assert.AreEqual(3L, stream.Length);
Assert.AreEqual(1, allocated.Count);
writer.Write(new byte[] {0x03, 0x04, 0x05});
writer.Flush();
Assert.AreEqual(6L, stream.Length);
Assert.AreEqual(2, allocated.Count, "extended by Write 1");
writer.Write(new byte[] {0x06, 0x07, 0x08});
writer.Flush();
Assert.AreEqual(9L, stream.Length);
Assert.AreEqual(3, allocated.Count, "extended by Write 2");
stream.SetLength(stream.Length);
Assert.AreEqual(9L, stream.Length);
Assert.AreEqual(3, allocated.Count, "SetLength(stream.Length)");
Console.WriteLine("set length 12");
stream.SetLength(12L);
Assert.AreEqual(12L, stream.Length);
Assert.AreEqual(4, allocated.Count, "extended by SetLength 1");
Console.WriteLine("set length 8");
stream.SetLength(8L);
Assert.AreEqual(8L, stream.Length);
Assert.AreEqual(3, allocated.Count, "shorten by SetLength 1");
Console.WriteLine("set length 7");
stream.SetLength(7L);
Assert.AreEqual(7L, stream.Length);
Assert.AreEqual(2, allocated.Count, "shorten by SetLength 2");
Console.WriteLine("set length 3");
stream.SetLength(3L);
Assert.AreEqual(3L, stream.Length);
Assert.AreEqual(1, allocated.Count, "shorten by SetLength 3");
Console.WriteLine("set length 0");
stream.SetLength(0L);
Assert.AreEqual(0L, stream.Length);
Assert.AreEqual(1, allocated.Count, "shorten by SetLength 4");
Console.WriteLine("set length 12");
stream.SetLength(12L);
Assert.AreEqual(12L, stream.Length);
Assert.AreEqual(4, allocated.Count, "extended by SetLength 2");
Console.WriteLine("set length 0");
stream.SetLength(0L);
Assert.AreEqual(0L, stream.Length);
Assert.AreEqual(1, allocated.Count, "shorten by SetLength 5");
}
Assert.AreEqual(0, allocated.Count, "closed");
}