2009-01-06 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Messaging / System.Messaging / BinaryMessageFormatter.cs
1
2 //
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:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
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.
21 //
22
23 //
24 // System.Messaging
25 //
26 // Authors:
27 //      Peter Van Isacker (sclytrack@planetinternet.be)
28 //      Rafael Teixeira   (rafaelteixeirabr@hotmail.com)
29 //      Michael Barker    (mike@middlesoft.co.uk)
30 //
31 // (C) 2003 Peter Van Isacker
32 //
33
34 using System;
35 using System.ComponentModel;
36 using System.IO;
37 using System.Runtime.Serialization.Formatters;
38 using System.Runtime.Serialization.Formatters.Binary;
39
40 namespace System.Messaging
41 {
42         public class BinaryMessageFormatter : IMessageFormatter, ICloneable
43         {
44                 private BinaryFormatter _formatter;
45
46                 public BinaryMessageFormatter ()
47                 {
48                         _formatter = new BinaryFormatter ();
49                 }
50
51                 public BinaryMessageFormatter (FormatterAssemblyStyle topObjectFormat, FormatterTypeStyle typeFormat)
52                 {
53                         _formatter = new BinaryFormatter ();
54                         _formatter.AssemblyFormat = topObjectFormat;
55                         _formatter.TypeFormat = typeFormat;
56                 }
57
58                 [DefaultValue (0)]
59                 [MessagingDescription ("MsgTopObjectFormat")]
60                 public FormatterAssemblyStyle TopObjectFormat {
61                         get {
62                                 return _formatter.AssemblyFormat;
63                         }
64                         set {
65                                 _formatter.AssemblyFormat = value;
66                         }
67                 }
68
69                 [DefaultValue (0)]
70                 [MessagingDescription ("MsgTypeFormat")]
71                 public FormatterTypeStyle TypeFormat {
72                         get {
73                                 return _formatter.TypeFormat;
74                         }
75                         set {
76                                 _formatter.TypeFormat = value;
77                         }
78                 }
79
80                 [MonoTODO ("only return true if body type is binary")]
81                 public bool CanRead (Message message)
82                 {
83                         if (message == null)
84                                 throw new ArgumentNullException ();
85                                 
86                         return message.BodyStream.CanRead;
87                 }
88
89                 [MonoTODO ("throw InvalidOperationException if message body is not binary")]
90                 public object Read (Message message)
91                 {
92                         if (message == null)
93                                 throw new ArgumentNullException ();
94                         
95                         message.BodyStream.Seek (0, SeekOrigin.Begin);
96                         return _formatter.Deserialize (message.BodyStream);
97                 }
98
99                 [MonoTODO ("throw InvalidOperationException if message body is not binary")]
100                 public void Write (Message message, object obj)
101                 {
102                         if (message == null)
103                                 throw new ArgumentNullException ();
104                                 
105                         Stream stream = message.BodyStream;
106                         if (stream == null) {
107                                 stream = new MemoryStream ();
108                                 message.BodyStream = stream;
109                         }
110
111                         message.BodyType = 768;
112                         _formatter.Serialize (stream, obj);
113                 }
114
115                 public object Clone ()
116                 {
117                         return new BinaryMessageFormatter (TopObjectFormat, TypeFormat);
118                 }
119         }
120 }