#Region "Copyright notice and license"
' Protocol Buffers - Google's data interchange format
' Copyright 2008 Google Inc. All rights reserved.
' https://developers.google.com/protocol-buffers/
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are
' met:
'
' * Redistributions of source code must retain the above copyright
' notice, this list of conditions and the following disclaimer.
' * Redistributions in binary form must reproduce the above
' copyright notice, this list of conditions and the following disclaimer
' in the documentation and/or other materials provided with the
' distribution.
' * Neither the name of Google Inc. nor the names of its
' contributors may be used to endorse or promote products derived from
' this software without specific prior written permission.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
' A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
' OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
' LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
' DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
' THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
' OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#End Region
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports System.Text
Namespace Google.Protobuf
'''
''' Immutable array of bytes.
'''
Public NotInheritable Class ByteString
Implements IEnumerable(Of Byte), IEquatable(Of ByteString)
Private Shared ReadOnly emptyField As ByteString = New ByteString(New Byte(-1) {})
Private ReadOnly bytes As Byte()
'''
''' Unsafe operations that can cause IO Failure and/or other catestrophic side-effects.
'''
Friend NotInheritable Class Unsafe
'''
''' Constructs a new ByteString from the given byte array. The array is
''' *not* copied, and must not be modified after this constructor is called.
'''
Friend Shared Function FromBytes(bytes As Byte()) As ByteString
Return New ByteString(bytes)
End Function
'''
''' Provides direct, unrestricted access to the bytes contained in this instance.
''' You must not modify or resize the byte array returned by this method.
'''
Friend Shared Function GetBuffer(bytes As ByteString) As Byte()
Return bytes.bytes
End Function
End Class
'''
''' Internal use only. Ensure that the provided array is not mutated and belongs to this instance.
'''
Friend Shared Function AttachBytes(bytes As Byte()) As ByteString
Return New ByteString(bytes)
End Function
'''
''' Constructs a new ByteString from the given byte array. The array is
''' *not* copied, and must not be modified after this constructor is called.
'''
Private Sub New(bytes As Byte())
Me.bytes = bytes
End Sub
'''
''' Returns an empty ByteString.
'''
Public Shared ReadOnly Property Empty As ByteString
Get
Return emptyField
End Get
End Property
'''
''' Returns the length of this ByteString in bytes.
'''
Public ReadOnly Property Length As Integer
Get
Return bytes.Length
End Get
End Property
'''
''' Returns true if this byte string is empty, false otherwise.
'''
Public ReadOnly Property IsEmpty As Boolean
Get
Return Length = 0
End Get
End Property
'''
''' Converts this into a byte array.
'''
''' The data is copied - changes to the returned array will not be reflected in this ByteString.
''' A byte array with the same data as this ByteString.
Public Function ToByteArray() As Byte()
Return CType(bytes.Clone(), Byte())
End Function
'''
''' Converts this into a standard base64 representation.
'''
''' A base64 representation of this ByteString.
Public Function ToBase64() As String
Return Convert.ToBase64String(bytes)
End Function
'''
''' Constructs a from the Base64 Encoded String.
'''
Public Shared Function FromBase64(bytes As String) As ByteString
' By handling the empty string explicitly, we not only optimize but we fix a
' problem on CF 2.0. See issue 61 for details.
Return If(Equals(bytes, ""), Empty, New ByteString(Convert.FromBase64String(bytes)))
End Function
'''
''' Constructs a from the given array. The contents
''' are copied, so further modifications to the array will not
''' be reflected in the returned ByteString.
''' This method can also be invoked in ByteString.CopyFrom(0xaa, 0xbb, ...) form
''' which is primarily useful for testing.
'''
Public Shared Function CopyFrom(ParamArray bytes As Byte()) As ByteString
Return New ByteString(CType(bytes.Clone(), Byte()))
End Function
'''
''' Constructs a from a portion of a byte array.
'''
Public Shared Function CopyFrom(bytes As Byte(), offset As Integer, count As Integer) As ByteString
Dim portion = New Byte(count - 1) {}
Copy(bytes, offset, portion, 0, count)
Return New ByteString(portion)
End Function
'''
''' Creates a new by encoding the specified text with
''' the given encoding.
'''
Public Shared Function CopyFrom(text As String, encoding As Encoding) As ByteString
Return New ByteString(encoding.GetBytes(text))
End Function
'''
''' Creates a new by encoding the specified text in UTF-8.
'''
Public Shared Function CopyFromUtf8(text As String) As ByteString
Return CopyFrom(text, Encoding.UTF8)
End Function
'''
''' Retuns the byte at the given index.
'''
Default Public ReadOnly Property Item(index As Integer) As Byte
Get
Return bytes(index)
End Get
End Property
'''
''' Converts this into a string by applying the given encoding.
'''
'''
''' This method should only be used to convert binary data which was the result of encoding
''' text with the given encoding.
'''
''' The encoding to use to decode the binary data into text.
''' The result of decoding the binary data with the given decoding.
Public Overloads Function ToString(encoding As Encoding) As String
Return encoding.GetString(bytes, 0, bytes.Length)
End Function
'''
''' Converts this into a string by applying the UTF-8 encoding.
'''
'''
''' This method should only be used to convert binary data which was the result of encoding
''' text with UTF-8.
'''
''' The result of decoding the binary data with the given decoding.
Public Function ToStringUtf8() As String
Return ToString(Encoding.UTF8)
End Function
'''
''' Returns an iterator over the bytes in this .
'''
''' An iterator over the bytes in this object.
Public Function GetEnumerator() As IEnumerator(Of Byte) Implements IEnumerable(Of Byte).GetEnumerator
Return CType(bytes, IEnumerable(Of Byte)).GetEnumerator()
End Function
'''
''' Returns an iterator over the bytes in this .
'''
''' An iterator over the bytes in this object.
Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
'''
''' Creates a CodedInputStream from this ByteString's data.
'''
Public Function CreateCodedInput() As CodedInputStream
' We trust CodedInputStream not to reveal the provided byte array or modify it
Return New CodedInputStream(bytes)
End Function
'''
''' Compares two byte strings for equality.
'''
''' The first byte string to compare.
''' The second byte string to compare.
''' true if the byte strings are equal; false otherwise.
Public Shared Operator =(lhs As ByteString, rhs As ByteString) As Boolean
If ReferenceEquals(lhs, rhs) Then
Return True
End If
If ReferenceEquals(lhs, Nothing) OrElse ReferenceEquals(rhs, Nothing) Then
Return False
End If
If lhs.bytes.Length <> rhs.bytes.Length Then
Return False
End If
For i = 0 To lhs.Length - 1
If rhs.bytes(i) <> lhs.bytes(i) Then
Return False
End If
Next
Return True
End Operator
'''
''' Compares two byte strings for inequality.
'''
''' The first byte string to compare.
''' The second byte string to compare.
''' false if the byte strings are equal; true otherwise.
Public Shared Operator <>(lhs As ByteString, rhs As ByteString) As Boolean
Return Not lhs Is rhs
End Operator
'''
''' Compares this byte string with another object.
'''
''' The object to compare this with.
''' true if refers to an equal ; false otherwise.
Public Overrides Function Equals(obj As Object) As Boolean
Return Me Is TryCast(obj, ByteString)
End Function
'''
''' Returns a hash code for this object. Two equal byte strings
''' will return the same hash code.
'''
''' A hash code for this object.
Public Overrides Function GetHashCode() As Integer
Dim ret = 23
For Each b In bytes
ret = ret << 8 Or b
Next
Return ret
End Function
'''
''' Compares this byte string with another.
'''
''' The to compare this with.
''' true if refers to an equal byte string; false otherwise.
Public Overloads Function Equals(other As ByteString) As Boolean Implements IEquatable(Of ByteString).Equals
Return Me Is other
End Function
'''
''' Used internally by CodedOutputStream to avoid creating a copy for the write
'''
Friend Sub WriteRawBytesTo(outputStream As CodedOutputStream)
outputStream.WriteRawBytes(bytes, 0, bytes.Length)
End Sub
'''
''' Copies the entire byte array to the destination array provided at the offset specified.
'''
Public Sub CopyTo(array As Byte(), position As Integer)
Copy(bytes, 0, array, position, bytes.Length)
End Sub
'''
''' Writes the entire byte array to the provided stream
'''
Public Sub WriteTo(outputStream As Stream)
outputStream.Write(bytes, 0, bytes.Length)
End Sub
End Class
End Namespace