Imports System.Runtime.CompilerServices

Namespace Net

    ''' <summary>
    ''' A set of extension methods for a byte type.
    ''' </summary>
    ''' <remarks></remarks>
    Public Module ByteExtensions

        ''' <summary>
        ''' Appends the specified bytes to the tail of the current byte array.
        ''' </summary>
        ''' <param name="destination">The destination byte array.</param>
        ''' <param name="bytes">The byte array to append.</param>
        ''' <returns>The specified bytes appended to the tail of the destination byte array.</returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function Append(ByVal destination() As Byte, ByVal ParamArray bytes() As Byte) As Byte()
            If bytes Is Nothing OrElse bytes.Length = 0 Then _
                Return destination
            If destination Is Nothing Then _
                Throw New ArgumentNullException("current")
            Dim newBytes = New Byte(destination.Length + bytes.Length - 1) {}
            Array.Copy(destination, 0, newBytes, 0, destination.Length)
            Array.Copy(bytes, 0, newBytes, destination.Length, bytes.Length)

            ' Return combined
            Return newBytes
        End Function

        ''' <summary>
        ''' Merges one byte array into another.
        ''' </summary>
        ''' <param name="destination">The destination byte array.</param>
        ''' <param name="mergeBytes">The byte array to merge.</param>
        ''' <param name="offset">The offset on the destination array at which to start merging.</param>
        ''' <param name="length">The number of bytes to merge.</param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub Merge(ByVal destination() As Byte, ByVal mergeBytes() As Byte, ByVal offset As Integer, ByVal length As Integer)
            If (offset >= destination.Length) Then _
                Throw New ArgumentOutOfRangeException("offset")
            If (offset + length > destination.Length) Then _
                Throw New ArgumentException("The offset and length must not exceed the length of the current byte array.")
            Array.Copy(mergeBytes, 0, destination, offset, length)
        End Sub

        ''' <summary>
        ''' Merges one byte array into another and increments the offset by the specified length.
        ''' </summary>
        ''' <param name="destination">The destination byte array.</param>
        ''' <param name="mergeBytes">The byte array to merge.</param>
        ''' <param name="offset">The offset on the destination array at which to start merging.</param>
        ''' <param name="length">The number of bytes to merge.</param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub OffsetMerge(ByVal destination() As Byte, ByVal mergeBytes() As Byte, ByRef offset As Integer, ByVal length As Integer)
            Merge(destination, mergeBytes, offset, length)
            offset += length
        End Sub

    End Module

End Namespace