梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Ticket is converted into a binary stream�察�the programmer does not need to do anything other
than pass an instance to the data stream。 The data stream libraries handle all of the other details。
In the declaration of Ticket�察�the parameterless constructor has been bolded to emphasize
that this type of constructor is necessary when converting a data stream into an object instance。
When binary streams restore types�察�they instantiate an empty object and then assign the data
members。 Thus�察�when an object is created�察�a parameterless constructor is needed。
Converting a Text Stream into a Binary Stream
To convert the stream from text to binary involves breaking apart the text stream�察�instantiating
a Ticket instance�察�assigning the data members�察�and saving the instance to the binary stream。
All of these steps are performed in the following source code ��you¨ll need a reference to
LottoLibrary in Text2Binary��。
Imports System。IO
Imports System。Runtime。Serialization。Formatters。Binary
Imports System。Text
Public Class LottoTicketProcessor �此�Implements IText2BinaryProcessor
Public Sub Process��ByVal reader As TextReader�察�ByVal writer As Stream�� _
Implements IText2BinaryProcessor。Process
Dim retval As StringBuilder = New StringBuilder�┌�
Do While reader。Peek�┌� ´1
Dim lineOfText As String = reader。ReadLine�┌�
Dim splitUpText As String�┌�= lineOfText。Split��New Char�┌� ��; ;c����
Dim dateSplit As String�┌� = splitUpText��0��。Split��New Char�┌� ��;。;c����
Dim ticket As LottoLibrary。Ticket = _
New LottoLibrary。Ticket�─�_
New DateTime�─�_
Integer。Parse��dateSplit��0�����察�_
Integer。Parse��dateSplit��1�����察�_
Integer。Parse��dateSplit��2�������察�_
´´´´´´´´´´´´´´´´´´´´´´Page 297´´´´´´´´´´´´´´´´´´´´´´´
CH A PT E R 1 0 * L E A R N I N G A B O U T P E R S IS T E N CE 275
New Integer�┌� �� _
Integer。Parse��splitUpText��1�����察�_
Integer。Parse��splitUpText��2�����察�_
Integer。Parse��splitUpText��3�����察�_
Integer。Parse��splitUpText��4�����察�_
Integer。Parse��splitUpText��5�����察�_
Integer。Parse��splitUpText��6���� ���察�_
Integer。Parse��splitUpText��7������
Dim formatter As BinaryFormatter = New BinaryFormatter�┌�
formatter。Serialize��writer�察�ticket��
Loop
End Sub
End Class
The code splits the text stream by reading a line of text and then splitting apart the fields。
The split´apart fields are then converted into numbers by using the Integer。Parse�┌� method。
This process of splitting and conversion is called marshaling data。 Marshaling is a technical
term that means to convert a type from one medium to another。
We manage the text marshaling�察�but manages the binary marshaling�察�which is still there
behind the scenes。 The System。Runtime。Serialization。Formatters。Binary。BinaryFormatter type
manages marshaling of the Ticket instance to the binary stream。 The Serializable attribute
is used by the BinaryFormatter as an indicator that the Ticket type is allowed to be serialized and
deserialized。 In essence�察�converting from a text stream to a binary stream means to marshal a
text´defined ticket into a ´defined ticket�察�and that is then marshaled into a binary´defined
ticket。
Converting a Binary Stream into a Text Stream
Converting a binary stream into a text stream involves using the ´provided formatter to
create a Ticket instance that is then converted into text。 The following is the plete source
code ��you¨ll need a reference to LottoLibrary in Binary2Text��。
Imports System。IO
Imports System。Runtime。Serialization。Formatters。Binary
Public Class LottoTicketProcessor �此�Implements IBinary2TextProcessor
Public Sub Process��ByVal input As Stream�察�ByVal output As TextWriter�� _
Implements IBinary2TextProcessor。Process
Dim builder As StringBuilder = New StringBuilder�┌�
Try
Do While True
Dim formatter As BinaryFormatter = New BinaryFormatter�┌�
Dim ticket As LottoLibrary。Ticket = _
DirectCast��formatter。Deserialize��input���察�
LottoLibrary。Ticket��
´´´´´´´´´´´´´´´´´´´´´´Page 298´´´´´´´´´´´´´´´´´´´´´´´
276 CH AP T E R 1 0 * L E A R N I N G A B OU T P E R S IS TE N CE
builder。AppendFormat�─�_
;��0��。��1��。��2�� ��3�� ��4�� ��5�� ��6�� ��7�� ��8�� ��9�� ��10��;�察�
ticket。DrawDate。Year�察�
ticket。DrawDate。Month�察�
ticket。DrawDate。Day�察�
ticket。Numbers��0���察�
ticket。Numbers��1���察�
ticket。Numbers��2���察�
ticket。Numbers��3���察�
ticket。Numbers��4���察�
ticket。Numbers��5���察�
ticket。Bonus�察�
ControlChars。NewLine��
Loop
Catch e As Exception
End Try
output。Write��builder。ToString�┌���
End Sub
End Class
In the code�察�a binary stream is read using the BinaryFormatter class�察�which reads the data
for an object from the data stream。 Notice how the method Deserialize�┌� does not ask which
type to read。 This is because all of that information is saved in the stream。 Deserialize�┌� will
read data from the stream�察�associate the data with a type�察�instantiate it�察�and populate the data
members。
The bolded code in the preceding listing points out where deserialization bees tricky�察 �
which centers around when to read what type。 A binary stream when processed by the
BinaryFormatter will read and write objects。 BinaryFormatter will instantiate whatever it encoun
ters and assumes that the caller of BinaryFormatter knows which type is being manipulated。 If
the caller does not know this�察�an exception will occur because the type cast to the specific type
will fail。
The exception block is necessary because you don¨t know how many Ticket objects have
been saved�察�as the count has not been saved to the stream。 provides the Position and
Length properties to determine if any instances are left to be read�察�but those properties work
only with files。 If the binary stream being read is a console data stream�察�there is no length or
position。 Thus�察�the only real solution is to keep reading until you can¨t do so anymore and
assume processing is plete。
INTENTION AND IMPLEMENTATION
The fact that Position and Length have different behaviors depending on the implementation might seem
to break the contract of being able to separate intention from implementation。 It seems to break the po
nent software paradigm because�察�as a developer�察�you do need to know about the stream implementation。 The