* ObjectWriter.cs, ObjectReader.cs: Added suport for headers.
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / BinaryFormatter.cs
1 // BinaryFormatter.cs
2 //
3 // Author:
4 //      Dick Porter (dick@ximian.com)
5 //  Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // (C) 2002 Ximian, Inc.  http://www.ximian.com
8
9 using System.Runtime.Serialization.Formatters;
10 using System.Runtime.Serialization;
11 using System.Reflection;
12 using System.Collections;
13 using System.IO;
14 using System.Runtime.Remoting.Messaging;
15
16 namespace System.Runtime.Serialization.Formatters.Binary {
17         public sealed class BinaryFormatter : IRemotingFormatter, IFormatter 
18         {
19                 private FormatterAssemblyStyle assembly_format;
20                 private SerializationBinder binder;
21                 private StreamingContext context;
22                 private ISurrogateSelector surrogate_selector;
23                 private FormatterTypeStyle type_format;
24                 
25                 public BinaryFormatter()
26                 {
27                         surrogate_selector=null;
28                         context=new StreamingContext(StreamingContextStates.All);
29                 }
30                 
31                 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
32                 {
33                         surrogate_selector=selector;
34                         this.context=context;
35                 }
36
37                 public FormatterAssemblyStyle AssemblyFormat
38                 {
39                         get {
40                                 return(assembly_format);
41                         }
42                         set {
43                                 assembly_format=value;
44                         }
45                 }
46
47                 public SerializationBinder Binder
48                 {
49                         get {
50                                 return(binder);
51                         }
52                         set {
53                                 binder=value;
54                         }
55                 }
56
57                 public StreamingContext Context 
58                 {
59                         get {
60                                 return(context);
61                         }
62                         set {
63                                 context=value;
64                         }
65                 }
66                 
67                 public ISurrogateSelector SurrogateSelector 
68                 {
69                         get {
70                                 return(surrogate_selector);
71                         }
72                         set {
73                                 surrogate_selector=value;
74                         }
75                 }
76                 
77                 public FormatterTypeStyle TypeFormat 
78                 {
79                         get {
80                                 return(type_format);
81                         }
82                         set {
83                                 type_format=value;
84                         }
85                 }
86
87                 public object Deserialize(Stream serializationStream)
88                 {
89                         return Deserialize (serializationStream, null);
90                 }
91
92                 public object Deserialize(Stream serializationStream, HeaderHandler handler) 
93                 {
94                         if(serializationStream==null) \r
95                         {
96                                 throw new ArgumentNullException("serializationStream is null");
97                         }
98                         if(serializationStream.CanSeek &&
99                                 serializationStream.Length==0) \r
100                         {
101                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
102                         }
103
104                         BinaryReader reader = new BinaryReader (serializationStream);
105
106                         bool hasHeader;
107                         ReadBinaryHeader (reader, out hasHeader);
108
109                         // Messages are read using a special static method, which does not use ObjectReader
110                         // if it is not needed. This saves time and memory.
111
112                         BinaryElement elem = (BinaryElement) reader.PeekChar();
113
114                         if (elem == BinaryElement.MethodCall) {
115                                 return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, surrogate_selector, context);
116                         }
117                         else if (elem == BinaryElement.MethodResponse) {
118                                 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, surrogate_selector, context);
119                         }
120                         else {
121                                 ObjectReader serializer = new ObjectReader (surrogate_selector, context);
122                                 return serializer.ReadObjectGraph (reader, hasHeader, handler);
123                         }
124                 }
125                 
126                 public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
127                 {
128                         if(serializationStream==null) {
129                                 throw new ArgumentNullException("serializationStream is null");
130                         }
131                         if(serializationStream.CanSeek &&
132                            serializationStream.Length==0) {
133                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
134                         }
135
136                         BinaryReader reader = new BinaryReader (serializationStream);
137
138                         bool hasHeader;
139                         ReadBinaryHeader (reader, out hasHeader);
140                         return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallmessage, surrogate_selector, context);
141                 }
142
143                 public void Serialize(Stream serializationStream, object graph)
144                 {
145                         Serialize (serializationStream, graph, null);
146                 }
147
148                 public void Serialize(Stream serializationStream, object graph, Header[] headers)
149                 {
150                         if(serializationStream==null) {
151                                 throw new ArgumentNullException("serializationStream is null");
152                         }
153
154                         BinaryWriter writer = new BinaryWriter (serializationStream);
155                         WriteBinaryHeader (writer, headers!=null);\r
156 \r
157                         if (graph is IMethodCallMessage) {\r
158                                 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context);\r
159                         }\r
160                         else if (graph is IMethodReturnMessage)  {\r
161                                 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context);\r
162                         }\r
163                         else {\r
164                                 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context);
165                                 serializer.WriteObjectGraph (writer, graph, headers);
166                         }
167                         writer.Flush();
168                 }
169 \r
170                 public void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)\r
171                 {\r
172                         writer.Write ((byte)BinaryElement.Header);\r
173                         writer.Write ((int)1);\r
174                         if (hasHeaders) writer.Write ((int)2);\r
175                         else writer.Write ((int)-1);\r
176                         writer.Write ((int)1);\r
177                         writer.Write ((int)0);\r
178                 }\r
179 \r
180                 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)\r
181                 {\r
182                         reader.ReadByte();\r
183                         reader.ReadInt32();\r
184                         int val = reader.ReadInt32();\r
185                         hasHeaders = (val==2);\r
186                         reader.ReadInt32();\r
187                         reader.ReadInt32();\r
188                 }\r
189         }
190 }