
Namespace Net

    ''' <summary>
    ''' Describes the encoding used for a message envelope.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class EnvelopeEncoding

        ''' <summary>
        ''' Initializes a default EnvelopeEncoding class with the specified encoding identifier.
        ''' </summary>
        ''' <param name="encoding">The identifier of the encoding to use.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal encoding As Byte)
            Me._id = encoding
        End Sub

        ''' <summary>
        ''' Gets the envelope encoding.
        ''' </summary>
        ''' <returns>The envelope encoding.</returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Encoding As System.Text.Encoding
            Get
                Return GetEncoding(Me.Id)
            End Get
        End Property

        Private _id As Byte

        ''' <summary>
        ''' Gets the identifier for this envelope encoding.
        ''' </summary>
        ''' <returns>The identifier for this envelope encoding.</returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Id As Byte
            Get
                Return Me._id
            End Get
        End Property

        ''' <summary>
        ''' Returns the encoding associated with the specified identifier.
        ''' </summary>
        ''' <param name="encoding">The identifier of the encoding to retrieve.</param>
        ''' <returns>The encoding associated with the specified identifier or null.</returns>
        ''' <remarks></remarks>
        Protected Function GetEncoding(ByVal encoding As Byte) As System.Text.Encoding
            If encoding = &H0 Then
                Return System.Text.Encoding.UTF8
            ElseIf encoding = &H1 Then
                Return System.Text.Encoding.BigEndianUnicode
            ElseIf encoding = &H2 Then
                Return System.Text.Encoding.Unicode
            ElseIf encoding = &H3 Then
                Return Nothing
            End If
            Return Nothing
        End Function

        ''' <summary>
        ''' UTF-8 encoding.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly Utf8 As EnvelopeEncoding = New EnvelopeEncoding(&H0)

        ''' <summary>
        ''' UTF-16, big-endian, encoding.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly Utf16 As EnvelopeEncoding = New EnvelopeEncoding(&H1)

        ''' <summary>
        ''' Unicode, little-endian, encoding.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly Unicode As EnvelopeEncoding = New EnvelopeEncoding(&H2)

        ''' <summary>
        ''' Raw binary.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly Binary As EnvelopeEncoding = New EnvelopeEncoding(&H3)

    End Class

End Namespace