summaryrefslogtreecommitdiff
path: root/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/StreamMessageImpl.java
blob: 9aededca5ef630ea50ecafed1ab800e0b334ead1 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * 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.
 */

package org.apache.qpid.amqp_1_0.jms.impl;

import org.apache.qpid.amqp_1_0.jms.StreamMessage;
import org.apache.qpid.amqp_1_0.type.Binary;
import org.apache.qpid.amqp_1_0.type.Section;
import org.apache.qpid.amqp_1_0.type.messaging.*;
import org.apache.qpid.amqp_1_0.type.messaging.Properties;

import javax.jms.JMSException;
import javax.jms.MessageEOFException;
import javax.jms.MessageFormatException;
import java.io.EOFException;
import java.util.*;

public class StreamMessageImpl extends MessageImpl implements StreamMessage
{
    private List _list;
    private boolean _readOnly;
    private int _position = -1;
    private int _offset = -1;



    protected StreamMessageImpl(Header header, MessageAnnotations messageAnnotations, Properties properties, ApplicationProperties appProperties, List list,
                                Footer footer, SessionImpl session)
    {
        super(header, messageAnnotations, properties, appProperties, footer, session);
        _list = list;
    }

    StreamMessageImpl(final SessionImpl session)
    {
        super(new Header(), new MessageAnnotations(new HashMap()), new Properties(),
              new ApplicationProperties(new HashMap()), new Footer(Collections.EMPTY_MAP),
              session);
        _list = new ArrayList();
    }

    public StreamMessageImpl(final Header header,
                             final MessageAnnotations messageAnnotations,
                             final Properties properties,
                             final ApplicationProperties appProperties,
                             final List amqpListSection, final Footer footer)
    {
        super(header, messageAnnotations, properties, appProperties, footer, null);
        _list = amqpListSection;
    }

    public boolean readBoolean() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Boolean)
        {
            return (Boolean) obj;
        }
        if(obj instanceof String || obj == null)
        {
            return Boolean.valueOf((String)obj);
        }
        else
        {
            throw new MessageFormatException("Cannot read " + obj.getClass().getName() + " as boolean");
        }
    }

    @Override
    public void clearBody() throws JMSException
    {
        super.clearBody();
        _list.clear();
        _position = -1;
        _offset = -1;
    }

    public byte readByte() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Byte)
        {
            return (Byte) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Byte.valueOf((String)obj);
            }
            catch(RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
    }

    private void backup()
    {
        _position--;
    }

    public short readShort() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Short)
        {
            return (Short) obj;
        }
        else if(obj instanceof Byte)
        {
            return (Byte) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Short.valueOf((String)obj);
            }
            catch(RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }

    }

    public char readChar() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Character)
        {
            return (Character) obj;
        }
        if(obj == null)
        {
            backup();
            throw new NullPointerException();
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot read " + obj.getClass().getName() + " as boolean");
        }

    }

    public int readInt() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Integer)
        {
            return (Integer) obj;
        }
        else if(obj instanceof Short)
        {
            return (Short) obj;
        }
        else if(obj instanceof Byte)
        {
            return (Byte) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Integer.valueOf((String)obj);
            }
            catch (RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
    }

    public long readLong() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Long)
        {
            return (Long) obj;
        }
        else if(obj instanceof Integer)
        {
            return (Integer) obj;
        }
        else if(obj instanceof Short)
        {
            return (Short) obj;
        }
        else if(obj instanceof Byte)
        {
            return (Byte) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Long.valueOf((String)obj);
            }
            catch (RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
    }

    public float readFloat() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Float)
        {
            return (Float) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Float.valueOf((String)obj);
            }
            catch (RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
    }

    public double readDouble() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Double)
        {
            return (Double) obj;
        }
        else if(obj instanceof Float)
        {
            return (Float) obj;
        }
        else if(obj instanceof String || obj == null)
        {
            try
            {
                return Double.valueOf((String)obj);
            }
            catch (RuntimeException e)
            {
                backup();
                throw e;
            }
        }
        else
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
    }

    public String readString() throws JMSException
    {
        Object obj = readObject();
        if(obj instanceof Binary)
        {
            backup();
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
        return String.valueOf(obj);
    }

    public int readBytes(final byte[] bytes) throws JMSException
    {
        Object obj = readObject();
        if(!(obj instanceof Binary))
        {
            backup();
            if(_position > -1 && _list.get(_position) instanceof Binary)
            {
                return -1;
            }
            throw new MessageFormatException("Cannot convert value of type " + obj.getClass().getName());
        }
        Binary binary = (Binary) obj;
        if(bytes.length >= binary.getLength())
        {
            System.arraycopy(binary.getArray(),binary.getArrayOffset(),bytes,0,binary.getLength());
            return binary.getLength();
        }
        return -1;
    }

    public Object readObject() throws JMSException
    {
        checkReadable();
        if(_offset == -1)
        {
            try
            {
                return _list.get(++_position);
            }
            catch (IndexOutOfBoundsException e)
            {
                throw new MessageEOFException("No more data in message stream");
            }
        }
        else
        {
            return null;  //TODO
        }
    }

    public void writeBoolean(final boolean b) throws JMSException
    {
        checkWritable();
        _list.add(b);
    }

    public void writeByte(final byte b) throws JMSException
    {
        checkWritable();
        _list.add(b);
    }

    public void writeShort(final short i) throws JMSException
    {
        checkWritable();
        _list.add(i);
    }

    public void writeChar(final char c) throws JMSException
    {
        checkWritable();
        _list.add(c);
    }

    public void writeInt(final int i) throws JMSException
    {
        checkWritable();
        _list.add(i);
    }

    public void writeLong(final long l) throws JMSException
    {
        checkWritable();
        _list.add(l);
    }

    public void writeFloat(final float v) throws JMSException
    {
        checkWritable();
        _list.add(v);
    }

    public void writeDouble(final double v) throws JMSException
    {
        checkWritable();
        _list.add(v);
    }

    public void writeString(final String s) throws JMSException
    {
        checkWritable();
        _list.add(s);
    }

    public void writeBytes(final byte[] bytes) throws JMSException
    {
        checkWritable();
        writeBytes(bytes, 0, bytes.length);
    }

    public void writeBytes(final byte[] bytes, final int offset, final int size) throws JMSException
    {
        checkWritable();
        
        if(!_list.isEmpty() && _list.get(_list.size()-1) instanceof byte[])
        {
            Binary oldVal = (Binary) _list.get(_list.size()-1);
            byte[] allBytes = new byte[oldVal.getLength() + size];
            System.arraycopy(oldVal.getArray(),oldVal.getArrayOffset(),allBytes,0,oldVal.getLength());
            System.arraycopy(bytes, offset, allBytes, oldVal.getLength(), size);
            _list.set(_list.size()-1, allBytes);
        }
        else
        {
            byte[] dup = new byte[size];
            System.arraycopy(bytes,offset,dup,0,size);
            _list.add(new Binary(dup));
        }
    }

    public void writeObject(final Object o) throws JMSException
    {
        checkWritable();
        if(o == null || _supportedClasses.contains(o.getClass()))
        {
            _list.add(o);
        }
    }

    public void reset() throws JMSException
    {
        super.reset();
        _position = -1;
        _offset = -1;
    }

    @Override Collection<Section> getSections()
    {
        List<Section> sections = new ArrayList<Section>();
        sections.add(getHeader());
        if(getMessageAnnotations() != null && getMessageAnnotations().getValue() != null && !getMessageAnnotations().getValue().isEmpty())
        {
            sections.add(getMessageAnnotations());
        }
        sections.add(getProperties());
        sections.add(getApplicationProperties());
        sections.add(new AmqpValue(_list));
        sections.add(getFooter());
        return sections;
    }
}