3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Peter Van Isacker (sclytrack@planetinternet.be)
28 // Rafael Teixeira (rafaelteixeirabr@hotmail.com)
29 // Michael Barker (mike@middlesoft.co.uk)
31 // (C) 2003 Peter Van Isacker
35 using System.ComponentModel;
37 using System.Runtime.Serialization.Formatters;
38 using System.Runtime.Serialization.Formatters.Binary;
40 namespace System.Messaging
42 public class BinaryMessageFormatter : IMessageFormatter, ICloneable
44 private BinaryFormatter _formatter;
46 public BinaryMessageFormatter ()
48 _formatter = new BinaryFormatter ();
51 public BinaryMessageFormatter (FormatterAssemblyStyle topObjectFormat, FormatterTypeStyle typeFormat)
53 _formatter = new BinaryFormatter ();
54 _formatter.AssemblyFormat = topObjectFormat;
55 _formatter.TypeFormat = typeFormat;
59 [MessagingDescription ("MsgTopObjectFormat")]
60 public FormatterAssemblyStyle TopObjectFormat {
62 return _formatter.AssemblyFormat;
65 _formatter.AssemblyFormat = value;
70 [MessagingDescription ("MsgTypeFormat")]
71 public FormatterTypeStyle TypeFormat {
73 return _formatter.TypeFormat;
76 _formatter.TypeFormat = value;
80 [MonoTODO ("only return true if body type is binary")]
81 public bool CanRead (Message message)
84 throw new ArgumentNullException ();
86 return message.BodyStream.CanRead;
89 [MonoTODO ("throw InvalidOperationException if message body is not binary")]
90 public object Read (Message message)
93 throw new ArgumentNullException ();
95 message.BodyStream.Seek (0, SeekOrigin.Begin);
96 return _formatter.Deserialize (message.BodyStream);
99 [MonoTODO ("throw InvalidOperationException if message body is not binary")]
100 public void Write (Message message, object obj)
103 throw new ArgumentNullException ();
105 Stream stream = message.BodyStream;
106 if (stream == null) {
107 stream = new MemoryStream ();
108 message.BodyStream = stream;
111 message.BodyType = 768;
112 _formatter.Serialize (stream, obj);
115 public object Clone ()
117 return new BinaryMessageFormatter (TopObjectFormat, TypeFormat);