summaryrefslogtreecommitdiff
path: root/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessage.cs
blob: fb3efb1b0f0bea1453903fd8f7b936b0bc20330c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using Apache.Qpid.Framing;
using Apache.Qpid.Messaging;
using Apache.Qpid.Buffer;

namespace Apache.Qpid.Client.Message
{
    [Serializable]
    class MessageEOFException : QpidException
    {
        public MessageEOFException(string message) : base(message)
        {
        }

        protected MessageEOFException(SerializationInfo info, StreamingContext ctxt)
           : base(info, ctxt)
        {
        }
    }

    public class QpidBytesMessage : AbstractQmsMessage, IBytesMessage
    {
        private const int DEFAULT_BUFFER_INITIAL_SIZE = 1024;

        public QpidBytesMessage() : this(null)
        {            
        }

        /// <summary>
        /// Construct a bytes message with existing data.
        /// </summary>
        /// <param name="data">if data is not null, the message is immediately in read only mode. if data is null, it is in
        /// write-only mode</param>        
        QpidBytesMessage(ByteBuffer data) : base(data)
        {
            // superclass constructor has instantiated a content header at this point
            if (data == null)
            {
                _data = ByteBuffer.Allocate(DEFAULT_BUFFER_INITIAL_SIZE);
                _data.IsAutoExpand = true;
            }
        }

        internal QpidBytesMessage(long messageNbr, ContentHeaderBody contentHeader, ByteBuffer data)
            // TODO: this casting is ugly. Need to review whole ContentHeaderBody idea
            : base(messageNbr, (BasicContentHeaderProperties)contentHeader.Properties, data)
        {
        }

        public override void ClearBodyImpl()
        {
            _data.Clear();
        }

        public override string ToBodyString()
        {
            CheckReadable();
            try
            {
                return GetText();
            }
            catch (IOException e)
            {
                throw new QpidException(e.ToString());
            }
        }

        private String GetText()
        {
            // this will use the default platform encoding
            if (_data == null)
            {
                return null;
            }
            int pos = _data.Position;
            _data.Rewind();
            // one byte left is for the end of frame marker
            if (_data.Remaining == 0)
            {
                // this is really redundant since pos must be zero
                _data.Position = pos;
                return null;
            }
            else
            {
                byte[] data = new byte[_data.Remaining];
                _data.GetBytes(data);
                return Encoding.UTF8.GetString(data);
            }
        }

        public long BodyLength
        {
            get
            {
                CheckReadable();
                return _data.Limit;
            }
        }

        private void CheckWritable()
        {
            if (_readableMessage)
            {
                throw new MessageNotWriteableException("You need to call clearBody() to make the message writable");
            }
        }

        public bool ReadBoolean()
        {
            CheckReadable();
            CheckAvailable(1);
            return _data.GetByte() != 0;
        }

        public byte ReadByte()
        {
            CheckReadable();
            CheckAvailable(1);
            return _data.GetByte();
        }

        public short ReadSignedByte()
        {
            CheckReadable();
            CheckAvailable(1);
            return _data.GetSByte();
        }

        public short ReadShort()
        {
            CheckReadable();
            CheckAvailable(2);
            return _data.GetInt16();
        }

        public char ReadChar()
        {
            CheckReadable();
            CheckAvailable(2);
            return _data.GetChar();
        }

        public int ReadInt()
        {
            CheckReadable();
            CheckAvailable(4);
            return _data.GetInt32();
        }

        public long ReadLong()
        {
            CheckReadable();
            CheckAvailable(8);
            return _data.GetInt64();
        }

        public float ReadFloat()
        {
            CheckReadable();
            CheckAvailable(4);
            return _data.GetFloat();
        }

        public double ReadDouble()
        {
            CheckReadable();
            CheckAvailable(8);
            return _data.GetDouble();
        }

        public string ReadUTF()
        {
            CheckReadable();
            // we check only for one byte since theoretically the string could be only a
            // single byte when using UTF-8 encoding
            CheckAvailable(1);
            try
            {
                byte[] data = new byte[_data.Remaining];
                _data.GetBytes(data);
                return Encoding.UTF8.GetString(data);                
            }
            catch (IOException e)
            {
                throw new QpidException(e.ToString(), e);
            }
        }

        public int ReadBytes(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            CheckReadable();
            int count = (_data.Remaining >= bytes.Length ? bytes.Length : _data.Remaining);
            if (count == 0)
            {
                return -1;
            }
            else
            {
                _data.GetBytes(bytes, 0, count);
                return count;
            }
        }

        public int ReadBytes(byte[] bytes, int maxLength)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (maxLength > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("maxLength must be >= 0");
            }
            CheckReadable();
            int count = (_data.Remaining >= maxLength ? maxLength : _data.Remaining);
            if (count == 0)
            {
                return -1;
            }
            else
            {
                _data.GetBytes(bytes, 0, count);
                return count;
            }
        }

        public void WriteBoolean(bool b)
        {
            CheckWritable();
            _data.Put(b ? (byte)1 : (byte)0);
        }

        public void WriteByte(byte b)
        {
            CheckWritable();
            _data.Put(b);
        }

        public void WriteShort(short i)
        {
            CheckWritable();
            _data.Put(i);
        }

        public void WriteChar(char c)
        {
            CheckWritable();
            _data.Put(c);
        }

        public void WriteSignedByte(short value)
        {
            CheckWritable();
            _data.Put(value);
        }

        public void WriteDouble(double value)
        {
            CheckWritable();
            _data.Put(value);
        }

        public void WriteFloat(float value)
        {
            CheckWritable();
            _data.Put(value);
        }

        public void WriteInt(int value)
        {
            CheckWritable();
            _data.Put(value);
        }

        public void WriteLong(long value)
        {
            CheckWritable();
            _data.Put(value);
        }        

        public void WriteUTF(string value)
        {
            CheckWritable();
            byte[] encodedData = Encoding.UTF8.GetBytes(value);
            _data.Put(encodedData);
        }

        public void WriteBytes(byte[] bytes)
        {
            CheckWritable();
            _data.Put(bytes);
        }

        public void WriteBytes(byte[] bytes, int offset, int length)
        {
            CheckWritable();
            _data.Put(bytes, offset, length);
        }

        protected override void Reset()
        {
            base.Reset();
            _data.Flip();
        }

        void IBytesMessage.Reset()
        {
            Reset();
        }

        /**
         * Check that there is at least a certain number of bytes available to read
         *
         * @param len the number of bytes
         * @throws MessageEOFException if there are less than len bytes available to read
         */
        private void CheckAvailable(int len)
        {
            if (_data.Remaining < len)
            {
                throw new MessageEOFException("Unable to read " + len + " bytes");
            }
        }
    }
}