4 // Dick Porter (dick@ximian.com)
5 // Lluis Sanchez Gual (lluis@ideary.com)
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 using System.Runtime.Serialization.Formatters;
10 using System.Runtime.Serialization;
11 using System.Reflection;
12 using System.Collections;
14 using System.Runtime.Remoting.Messaging;
16 namespace System.Runtime.Serialization.Formatters.Binary {
17 public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
19 private FormatterAssemblyStyle assembly_format; // TODO: Do something with this
20 private SerializationBinder binder;
21 private StreamingContext context;
22 private ISurrogateSelector surrogate_selector;
23 private FormatterTypeStyle type_format; // TODO: Do something with this
25 public BinaryFormatter()
27 surrogate_selector=null;
28 context=new StreamingContext(StreamingContextStates.All);
31 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
33 surrogate_selector=selector;
37 public FormatterAssemblyStyle AssemblyFormat
40 return(assembly_format);
43 assembly_format=value;
47 public SerializationBinder Binder
57 public StreamingContext Context
67 public ISurrogateSelector SurrogateSelector
70 return(surrogate_selector);
73 surrogate_selector=value;
77 public FormatterTypeStyle TypeFormat
87 public object Deserialize(Stream serializationStream)
89 return Deserialize (serializationStream, null);
92 public object Deserialize(Stream serializationStream, HeaderHandler handler)
94 if(serializationStream==null)
\r
96 throw new ArgumentNullException("serializationStream is null");
98 if(serializationStream.CanSeek &&
99 serializationStream.Length==0)
\r
101 throw new SerializationException("serializationStream supports seeking, but its length is 0");
104 BinaryReader reader = new BinaryReader (serializationStream);
107 ReadBinaryHeader (reader, out hasHeader);
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.
112 BinaryElement elem = (BinaryElement) reader.PeekChar();
114 if (elem == BinaryElement.MethodCall) {
115 return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, surrogate_selector, context, binder);
117 else if (elem == BinaryElement.MethodResponse) {
118 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, surrogate_selector, context, binder);
121 ObjectReader serializer = new ObjectReader (surrogate_selector, context, binder);
122 return serializer.ReadObjectGraph (reader, hasHeader, handler);
126 public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
128 if(serializationStream==null) {
129 throw new ArgumentNullException("serializationStream is null");
131 if(serializationStream.CanSeek &&
132 serializationStream.Length==0) {
133 throw new SerializationException("serializationStream supports seeking, but its length is 0");
136 BinaryReader reader = new BinaryReader (serializationStream);
139 ReadBinaryHeader (reader, out hasHeader);
140 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallmessage, surrogate_selector, context, binder);
143 public void Serialize(Stream serializationStream, object graph)
145 Serialize (serializationStream, graph, null);
148 public void Serialize(Stream serializationStream, object graph, Header[] headers)
150 if(serializationStream==null) {
151 throw new ArgumentNullException("serializationStream is null");
154 BinaryWriter writer = new BinaryWriter (serializationStream);
155 WriteBinaryHeader (writer, headers!=null);
\r
157 if (graph is IMethodCallMessage) {
\r
158 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context);
\r
160 else if (graph is IMethodReturnMessage) {
\r
161 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context);
\r
164 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context);
165 serializer.WriteObjectGraph (writer, graph, headers);
170 public void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
\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
180 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
\r
183 reader.ReadInt32();
\r
184 int val = reader.ReadInt32();
\r
185 hasHeaders = (val==2);
\r
186 reader.ReadInt32();
\r
187 reader.ReadInt32();
\r