CSharpTest.Net
Create(Int64) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > FragmentedFile Class > Create Method : Create(Int64) Method

identity
A unique integer id for the block to be used with Open/Delete

Glossary Item Box

Creates a new allocation block within the file

Syntax

Visual Basic (Declaration) 
Public Overloads Function Create( _
   ByRef identity As Long _
) As Stream
C# 
public Stream Create( 
   out long identity
)

Parameters

identity
A unique integer id for the block to be used with Open/Delete

Return Value

The stream to write to the newly created block

Example

Library/Library.Test/TestFragmentedFile.cs

C#Copy Code
using (SharedMemoryStream shared = new SharedMemoryStream())
using (FragmentedFile ff = FragmentedFile.CreateNew(shared, 512, 100, 2))
{
    long id;
    using (Stream write = ff.Create(out id))
         PrimitiveSerializer.Int64.WriteTo(id, write);

    using (Stream read = ff.Open(id, FileAccess.Read))
    {
        Assert.IsTrue(read.CanRead);
        Assert.IsFalse(read.CanWrite);
        Assert.IsFalse(read.CanSeek);
        Assert.AreEqual(id, PrimitiveSerializer.Int64.ReadFrom(read));
        read.Flush();//no-op

        AssertThrows<NotSupportedException>(delegate() { read.Position = 0; });
        AssertThrows<NotSupportedException>(delegate() { GC.KeepAlive(read.Position); });
        AssertThrows<NotSupportedException>(delegate() { GC.KeepAlive(read.Length); });
        AssertThrows<NotSupportedException>(delegate() { read.SetLength(1); });
        AssertThrows<NotSupportedException>(delegate() { read.Seek(1, SeekOrigin.Begin); });
        AssertThrows<NotSupportedException>(delegate() { read.WriteByte(1); });
    }
}
VB.NETCopy Code
Using [shared] As New SharedMemoryStream()
    Using ff As FragmentedFile = FragmentedFile.CreateNew([shared], 512, 100, 2)
        Dim id As Long
        Using write As Stream = ff.Create(id)
            PrimitiveSerializer.Int64.WriteTo(id, write)
        End Using

        Using read As Stream = ff.Open(id, FileAccess.Read)
            Assert.IsTrue(read.CanRead)
            Assert.IsFalse(read.CanWrite)
            Assert.IsFalse(read.CanSeek)
            Assert.AreEqual(id, PrimitiveSerializer.Int64.ReadFrom(read))
            read.Flush()
            'no-op
            AssertThrows(Of NotSupportedException)(Function() Do
                read.Position = 0
            End Function)
            AssertThrows(Of NotSupportedException)(Function() Do
                GC.KeepAlive(read.Position)
            End Function)
            AssertThrows(Of NotSupportedException)(Function() Do
                GC.KeepAlive(read.Length)
            End Function)
            AssertThrows(Of NotSupportedException)(Function() Do
                read.SetLength(1)
            End Function)
            AssertThrows(Of NotSupportedException)(Function() Do
                read.Seek(1, SeekOrigin.Begin)
            End Function)
            AssertThrows(Of NotSupportedException)(Function() Do
                read.WriteByte(1)
            End Function)
        End Using
    End Using
End Using

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys