-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource.vb
More file actions
103 lines (74 loc) · 2.53 KB
/
Copy pathsource.vb
File metadata and controls
103 lines (74 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Text
Public Class Class1:Implements Microsoft.SqlServer.Server.IBinarySerialize
Dim StringValue As String
Dim DoubleValue As Double
Shared Sub Main()
Dim fileName As String = "info.dat"
Dim temp As New Class1()
Dim fs As New FileStream(fileName, FileMode.Create)
Dim w As New BinaryWriter(fs)
temp.Write(w)
w.Close()
fs.Close()
fs = New FileStream(fileName, FileMode.Open, FileAccess.Read)
Dim r As New BinaryReader(fs)
temp.Read(r)
Console.WriteLine("String Value: " & temp.StringValue)
Console.WriteLine("Double value: " & temp.DoubleValue)
End Sub
'<Snippet1>
' The binary layout is as follows:
' Bytes 0 - 19: string text, padded to the right with null
' characters
' Bytes 20+: double value
Public Sub Read(ByVal r As System.IO.BinaryReader) _
Implements Microsoft.SqlServer.Server.IBinarySerialize.Read
Dim maxStringSize As Integer = 20
Dim chars As Char()
Dim stringEnd As Integer
Dim stringValue As String
Dim value As double
' Read the characters from the binary stream.
chars = r.ReadChars(maxStringSize)
' Find the start of the null character padding.
stringEnd = Array.IndexOf(chars, ControlChars.NullChar)
If StringEnd = 0 Then
stringValue = Nothing
Exit Sub
End If
' Build the string from the array of characters.
stringValue = new String(chars, 0, stringEnd)
' Read the double value from the binary stream.
value = r.ReadDouble()
' Set the object's properties equal to the values.
Me.StringValue = stringValue
Me.DoubleValue = value
End Sub
'</Snippet1>
'<Snippet2>
' The binary layout is as follows:
' Bytes 0 - 19: string text, padded to the right with null characters
' Bytes 20+: Double value
Public Sub Write(ByVal w As System.IO.BinaryWriter) _
Implements Microsoft.SqlServer.Server.IBinarySerialize.Write
Dim maxStringSize As Integer = 20
Dim stringValue As String = "The value of PI: "
Dim paddedString As String
Dim value As Double = 3.14159
' Pad the string from the right with null characters.
paddedString = stringValue.PadRight(maxStringSize, ControlChars.NullChar)
' Write the string value one byte at a time.
Dim i As Integer
For i = 0 To paddedString.Length - 1
w.Write(paddedString(i))
Next
' Write the double value.
w.Write(value)
End Sub
'</Snippet2>
End Class