BinaryFormatter.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // BinaryFormatter.cs
  2. //
  3. // Author:
  4. // Dick Porter ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Runtime.Serialization.Formatters;
  31. using System.Runtime.Serialization;
  32. using System.Reflection;
  33. using System.Collections;
  34. using System.IO;
  35. using System.Runtime.Remoting.Messaging;
  36. namespace System.Runtime.Serialization.Formatters.Binary {
  37. public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
  38. {
  39. private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Full;
  40. private SerializationBinder binder;
  41. private StreamingContext context;
  42. private ISurrogateSelector surrogate_selector;
  43. private FormatterTypeStyle type_format = FormatterTypeStyle.TypesAlways;
  44. #if NET_1_1
  45. private TypeFilterLevel filter_level;
  46. #endif
  47. public BinaryFormatter()
  48. {
  49. surrogate_selector=null;
  50. context=new StreamingContext(StreamingContextStates.All);
  51. }
  52. public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
  53. {
  54. surrogate_selector=selector;
  55. this.context=context;
  56. }
  57. public FormatterAssemblyStyle AssemblyFormat
  58. {
  59. get {
  60. return(assembly_format);
  61. }
  62. set {
  63. assembly_format=value;
  64. }
  65. }
  66. public SerializationBinder Binder
  67. {
  68. get {
  69. return(binder);
  70. }
  71. set {
  72. binder=value;
  73. }
  74. }
  75. public StreamingContext Context
  76. {
  77. get {
  78. return(context);
  79. }
  80. set {
  81. context=value;
  82. }
  83. }
  84. public ISurrogateSelector SurrogateSelector
  85. {
  86. get {
  87. return(surrogate_selector);
  88. }
  89. set {
  90. surrogate_selector=value;
  91. }
  92. }
  93. public FormatterTypeStyle TypeFormat
  94. {
  95. get {
  96. return(type_format);
  97. }
  98. set {
  99. type_format=value;
  100. }
  101. }
  102. #if NET_1_1
  103. [System.Runtime.InteropServices.ComVisible (false)]
  104. public TypeFilterLevel FilterLevel
  105. {
  106. get { return filter_level; }
  107. set { filter_level = value; }
  108. }
  109. #endif
  110. public object Deserialize(Stream serializationStream)
  111. {
  112. return Deserialize (serializationStream, null);
  113. }
  114. public object Deserialize(Stream serializationStream, HeaderHandler handler)
  115. {
  116. if(serializationStream==null)
  117. {
  118. throw new ArgumentNullException("serializationStream is null");
  119. }
  120. if(serializationStream.CanSeek &&
  121. serializationStream.Length==0)
  122. {
  123. throw new SerializationException("serializationStream supports seeking, but its length is 0");
  124. }
  125. BinaryReader reader = new BinaryReader (serializationStream);
  126. bool hasHeader;
  127. ReadBinaryHeader (reader, out hasHeader);
  128. // Messages are read using a special static method, which does not use ObjectReader
  129. // if it is not needed. This saves time and memory.
  130. BinaryElement elem = (BinaryElement) reader.PeekChar();
  131. if (elem == BinaryElement.MethodCall) {
  132. return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, this);
  133. }
  134. else if (elem == BinaryElement.MethodResponse) {
  135. return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, this);
  136. }
  137. else {
  138. ObjectReader serializer = new ObjectReader (this);
  139. object result;
  140. Header[] headers;
  141. serializer.ReadObjectGraph (reader, hasHeader, out result, out headers);
  142. if (handler != null) handler(headers);
  143. return result;
  144. }
  145. }
  146. public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
  147. {
  148. if(serializationStream==null) {
  149. throw new ArgumentNullException("serializationStream is null");
  150. }
  151. if(serializationStream.CanSeek &&
  152. serializationStream.Length==0) {
  153. throw new SerializationException("serializationStream supports seeking, but its length is 0");
  154. }
  155. BinaryReader reader = new BinaryReader (serializationStream);
  156. bool hasHeader;
  157. ReadBinaryHeader (reader, out hasHeader);
  158. return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallmessage, this);
  159. }
  160. public void Serialize(Stream serializationStream, object graph)
  161. {
  162. Serialize (serializationStream, graph, null);
  163. }
  164. public void Serialize(Stream serializationStream, object graph, Header[] headers)
  165. {
  166. if(serializationStream==null) {
  167. throw new ArgumentNullException("serializationStream is null");
  168. }
  169. BinaryWriter writer = new BinaryWriter (serializationStream);
  170. WriteBinaryHeader (writer, headers!=null);
  171. if (graph is IMethodCallMessage) {
  172. MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
  173. }
  174. else if (graph is IMethodReturnMessage) {
  175. MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
  176. }
  177. else {
  178. ObjectWriter serializer = new ObjectWriter (surrogate_selector, context, assembly_format, type_format);
  179. serializer.WriteObjectGraph (writer, graph, headers);
  180. }
  181. writer.Flush();
  182. }
  183. [MonoTODO]
  184. [System.Runtime.InteropServices.ComVisible (false)]
  185. public object UnsafeDeserialize(Stream serializationStream, HeaderHandler handler)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. [System.Runtime.InteropServices.ComVisible (false)]
  191. public object UnsafeDeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. private void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
  196. {
  197. writer.Write ((byte)BinaryElement.Header);
  198. writer.Write ((int)1);
  199. if (hasHeaders) writer.Write ((int)2);
  200. else writer.Write ((int)-1);
  201. writer.Write ((int)1);
  202. writer.Write ((int)0);
  203. }
  204. private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
  205. {
  206. reader.ReadByte();
  207. reader.ReadInt32();
  208. int val = reader.ReadInt32();
  209. hasHeaders = (val==2);
  210. reader.ReadInt32();
  211. reader.ReadInt32();
  212. }
  213. }
  214. }