1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
41 // Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 // Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 // are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 // Technologies LLC, and Rabbit Technologies Ltd.
46 // Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 // Ltd. Portions created by Cohesive Financial Technologies LLC are
48 // Copyright (C) 2007-2009 Cohesive Financial Technologies
49 // LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 // (C) 2007-2009 Rabbit Technologies Ltd.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
59 using System.Collections;
61 using RabbitMQ.Client;
64 namespace RabbitMQ.Client.Content {
65 ///<summary>Framework for constructing various types of AMQP
66 ///Basic-class application messages.</summary>
67 public class BasicMessageBuilder: IMessageBuilder {
68 ///<summary>By default, new instances of BasicMessageBuilder and its
69 ///subclasses will have this much initial buffer
71 public const int DefaultAccumulatorSize = 1024;
73 protected IBasicProperties m_properties;
74 protected MemoryStream m_accumulator;
76 protected NetworkBinaryWriter m_writer = null;
78 ///<summary>Retrieve this instance's NetworkBinaryWriter writing to BodyStream.</summary>
80 /// If no NetworkBinaryWriter instance exists, one is created,
81 /// pointing at the beginning of the accumulator. If one
82 /// already exists, the existing instance is returned. The
83 /// instance is not reset.
85 public NetworkBinaryWriter Writer {
87 if (m_writer == null) {
88 m_writer = new NetworkBinaryWriter(m_accumulator);
94 ///<summary>Construct an instance ready for writing.</summary>
96 /// The DefaultAccumulatorSize is used for the initial accumulator buffer size.
98 public BasicMessageBuilder(IModel model): this(model, DefaultAccumulatorSize) {}
100 ///<summary>Construct an instance ready for writing.</summary>
101 public BasicMessageBuilder(IModel model, int initialAccumulatorSize) {
102 m_properties = model.CreateBasicProperties();
103 m_accumulator = new MemoryStream(initialAccumulatorSize);
105 string contentType = GetDefaultContentType();
106 if (contentType != null) {
107 Properties.ContentType = contentType;
111 ///<summary>Retrieve the IBasicProperties associated with this instance.</summary>
112 public IBasicProperties Properties {
118 ///<summary>Implement IMessageBuilder.Headers</summary>
119 public IDictionary Headers {
121 if (Properties.Headers == null) {
122 Properties.Headers = new Hashtable();
124 return Properties.Headers;
128 ///<summary>Implement IMessageBuilder.BodyStream</summary>
129 public Stream BodyStream {
131 return m_accumulator;
135 ///<summary>Implement
136 ///IMessageBuilder.GetDefaultContentType(). Returns null;
137 ///overridden in subclasses.</summary>
138 public virtual string GetDefaultContentType() {
142 ///<summary>Implement IMessageBuilder.RawWrite</summary>
143 public IMessageBuilder RawWrite(byte b) {
144 BodyStream.WriteByte(b);
148 ///<summary>Implement IMessageBuilder.RawWrite</summary>
149 public IMessageBuilder RawWrite(byte[] bytes) {
150 return RawWrite(bytes, 0, bytes.Length);
153 ///<summary>Implement IMessageBuilder.RawWrite</summary>
154 public IMessageBuilder RawWrite(byte[] bytes, int offset, int length) {
155 BodyStream.Write(bytes, offset, length);
159 ///<summary>Implement IMessageBuilder.GetContentHeader</summary>
160 public virtual IContentHeader GetContentHeader() {
164 ///<summary>Implement IMessageBuilder.GetContentBody</summary>
165 public virtual byte[] GetContentBody() {
166 return m_accumulator.ToArray();