KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyStreamMessage


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import javax.jms.JMSException JavaDoc;
31 import javax.jms.MessageEOFException JavaDoc;
32 import javax.jms.MessageFormatException JavaDoc;
33 import javax.jms.MessageNotReadableException JavaDoc;
34 import javax.jms.MessageNotWriteableException JavaDoc;
35 import javax.jms.StreamMessage JavaDoc;
36
37 import org.jboss.util.Primitives;
38
39 /**
40  * This class implements javax.jms.StreamMessage
41  *
42  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
43  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
44  * @version $Revision: 37459 $
45  */

46 public class SpyStreamMessage extends SpyMessage implements StreamMessage JavaDoc, Cloneable JavaDoc, Externalizable JavaDoc
47 {
48    // Constants -----------------------------------------------------
49

50    /** The serialVersionUID */
51    private final static long serialVersionUID = 2490910971426786841L;
52
53    // Attributes ----------------------------------------------------
54

55    /** The content */
56    Vector JavaDoc content;
57    /** The position */
58    int position;
59    /** The offset */
60    int offset;
61    /** The size */
62    int size;
63
64    // Static --------------------------------------------------------
65

66    // Constructors --------------------------------------------------
67

68    /**
69      * Create a new SpyStreamMessage
70      */

71    public SpyStreamMessage()
72    {
73       content = new Vector JavaDoc();
74       position = 0;
75       size = 0;
76       offset = 0;
77    }
78
79    // Public --------------------------------------------------------
80

81    // StreamMessage implementation ----------------------------------
82

83    public boolean readBoolean() throws JMSException JavaDoc
84    {
85       if (!header.msgReadOnly)
86          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
87
88       try
89       {
90          Object JavaDoc value = content.get(position);
91          offset = 0;
92
93          if (value == null)
94             throw new NullPointerException JavaDoc("Value is null");
95          else if (value instanceof Boolean JavaDoc)
96          {
97             position++;
98             return ((Boolean JavaDoc) value).booleanValue();
99          }
100          else if (value instanceof String JavaDoc)
101          {
102             boolean result = Boolean.valueOf((String JavaDoc) value).booleanValue();
103             position++;
104             return result;
105          }
106          else
107             throw new MessageFormatException JavaDoc("Invalid conversion");
108       }
109       catch (ArrayIndexOutOfBoundsException JavaDoc e)
110       {
111          throw new MessageEOFException JavaDoc("");
112       }
113
114    }
115
116    public byte readByte() throws JMSException JavaDoc
117    {
118       if (!header.msgReadOnly)
119          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
120
121       try
122       {
123          Object JavaDoc value = content.get(position);
124          offset = 0;
125          if (value == null)
126             throw new NullPointerException JavaDoc("Value is null");
127          else if (value instanceof Byte JavaDoc)
128          {
129             position++;
130             return ((Byte JavaDoc) value).byteValue();
131          }
132          else if (value instanceof String JavaDoc)
133          {
134             byte result = Byte.parseByte((String JavaDoc) value);
135             position++;
136             return result;
137          }
138          else
139             throw new MessageFormatException JavaDoc("Invalid conversion");
140       }
141       catch (ArrayIndexOutOfBoundsException JavaDoc e)
142       {
143          throw new MessageEOFException JavaDoc("");
144       }
145    }
146
147    public short readShort() throws JMSException JavaDoc
148    {
149       if (!header.msgReadOnly)
150          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
151       try
152       {
153          Object JavaDoc value = content.get(position);
154          offset = 0;
155
156          if (value == null)
157             throw new NullPointerException JavaDoc("Value is null");
158          else if (value instanceof Byte JavaDoc)
159          {
160             position++;
161             return ((Byte JavaDoc) value).shortValue();
162          }
163          else if (value instanceof Short JavaDoc)
164          {
165             position++;
166             return ((Short JavaDoc) value).shortValue();
167          }
168          else if (value instanceof String JavaDoc)
169          {
170             short result = Short.parseShort((String JavaDoc) value);
171             position++;
172             return result;
173          }
174          else
175             throw new MessageFormatException JavaDoc("Invalid conversion");
176       }
177       catch (ArrayIndexOutOfBoundsException JavaDoc e)
178       {
179          throw new MessageEOFException JavaDoc("");
180       }
181    }
182
183    public char readChar() throws JMSException JavaDoc
184    {
185       if (!header.msgReadOnly)
186          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
187       try
188       {
189          Object JavaDoc value = content.get(position);
190          offset = 0;
191
192          if (value == null)
193             throw new NullPointerException JavaDoc("Value is null");
194          else if (value instanceof Character JavaDoc)
195          {
196             position++;
197             return ((Character JavaDoc) value).charValue();
198          }
199          else
200             throw new MessageFormatException JavaDoc("Invalid conversion");
201       }
202       catch (ArrayIndexOutOfBoundsException JavaDoc e)
203       {
204          throw new MessageEOFException JavaDoc("");
205       }
206    }
207
208    public int readInt() throws JMSException JavaDoc
209    {
210       if (!header.msgReadOnly)
211          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
212       try
213       {
214          Object JavaDoc value = content.get(position);
215          offset = 0;
216
217          if (value == null)
218             throw new NullPointerException JavaDoc("Value is null");
219          else if (value instanceof Byte JavaDoc)
220          {
221             position++;
222             return ((Byte JavaDoc) value).intValue();
223          }
224          else if (value instanceof Short JavaDoc)
225          {
226             position++;
227             return ((Short JavaDoc) value).intValue();
228          }
229          else if (value instanceof Integer JavaDoc)
230          {
231             position++;
232             return ((Integer JavaDoc) value).intValue();
233          }
234          else if (value instanceof String JavaDoc)
235          {
236             int result = Integer.parseInt((String JavaDoc) value);
237             position++;
238             return result;
239          }
240          else
241             throw new MessageFormatException JavaDoc("Invalid conversion");
242       }
243       catch (ArrayIndexOutOfBoundsException JavaDoc e)
244       {
245          throw new MessageEOFException JavaDoc("");
246       }
247    }
248
249    public long readLong() throws JMSException JavaDoc
250    {
251       if (!header.msgReadOnly)
252          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
253       try
254       {
255          Object JavaDoc value = content.get(position);
256          offset = 0;
257
258          if (value == null)
259             throw new NullPointerException JavaDoc("Value is null");
260          else if (value instanceof Byte JavaDoc)
261          {
262             position++;
263             return ((Byte JavaDoc) value).longValue();
264          }
265          else if (value instanceof Short JavaDoc)
266          {
267             position++;
268             return ((Short JavaDoc) value).longValue();
269          }
270          else if (value instanceof Integer JavaDoc)
271          {
272             position++;
273             return ((Integer JavaDoc) value).longValue();
274          }
275          else if (value instanceof Long JavaDoc)
276          {
277             position++;
278             return ((Long JavaDoc) value).longValue();
279          }
280          else if (value instanceof String JavaDoc)
281          {
282             long result = Long.parseLong((String JavaDoc) value);
283             position++;
284             return result;
285          }
286          else
287             throw new MessageFormatException JavaDoc("Invalid conversion");
288       }
289       catch (ArrayIndexOutOfBoundsException JavaDoc e)
290       {
291          throw new MessageEOFException JavaDoc("");
292       }
293    }
294
295    public float readFloat() throws JMSException JavaDoc
296    {
297       if (!header.msgReadOnly)
298          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
299       try
300       {
301          Object JavaDoc value = content.get(position);
302          offset = 0;
303
304          if (value == null)
305             throw new NullPointerException JavaDoc("Value is null");
306          else if (value instanceof Float JavaDoc)
307          {
308             position++;
309             return ((Float JavaDoc) value).floatValue();
310          }
311          else if (value instanceof String JavaDoc)
312          {
313             float result = Float.parseFloat((String JavaDoc) value);
314             position++;
315             return result;
316          }
317          else
318             throw new MessageFormatException JavaDoc("Invalid conversion");
319       }
320       catch (ArrayIndexOutOfBoundsException JavaDoc e)
321       {
322          throw new MessageEOFException JavaDoc("");
323       }
324    }
325
326    public double readDouble() throws JMSException JavaDoc
327    {
328       if (!header.msgReadOnly)
329          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
330       try
331       {
332          Object JavaDoc value = content.get(position);
333          offset = 0;
334
335          if (value == null)
336             throw new NullPointerException JavaDoc("Value is null");
337          else if (value instanceof Float JavaDoc)
338          {
339             position++;
340             return ((Float JavaDoc) value).doubleValue();
341          }
342          else if (value instanceof Double JavaDoc)
343          {
344             position++;
345             return ((Double JavaDoc) value).doubleValue();
346          }
347          else if (value instanceof String JavaDoc)
348          {
349             double result = Double.parseDouble((String JavaDoc) value);
350             position++;
351             return result;
352          }
353          else
354             throw new MessageFormatException JavaDoc("Invalid conversion");
355       }
356       catch (ArrayIndexOutOfBoundsException JavaDoc e)
357       {
358          throw new MessageEOFException JavaDoc("");
359       }
360    }
361
362    public String JavaDoc readString() throws JMSException JavaDoc
363    {
364       if (!header.msgReadOnly)
365          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
366       try
367       {
368          Object JavaDoc value = content.get(position);
369          offset = 0;
370
371          if (value == null)
372          {
373             position++;
374             return null;
375          }
376          else if (value instanceof Boolean JavaDoc)
377          {
378             position++;
379             return ((Boolean JavaDoc) value).toString();
380          }
381          else if (value instanceof Byte JavaDoc)
382          {
383             position++;
384             return ((Byte JavaDoc) value).toString();
385          }
386          else if (value instanceof Short JavaDoc)
387          {
388             position++;
389             return ((Short JavaDoc) value).toString();
390          }
391          else if (value instanceof Character JavaDoc)
392          {
393             position++;
394             return ((Character JavaDoc) value).toString();
395          }
396          else if (value instanceof Integer JavaDoc)
397          {
398             position++;
399             return ((Integer JavaDoc) value).toString();
400          }
401          else if (value instanceof Long JavaDoc)
402          {
403             position++;
404             return ((Long JavaDoc) value).toString();
405          }
406          else if (value instanceof Float JavaDoc)
407          {
408             position++;
409             return ((Float JavaDoc) value).toString();
410          }
411          else if (value instanceof Double JavaDoc)
412          {
413             position++;
414             return ((Double JavaDoc) value).toString();
415          }
416          else if (value instanceof String JavaDoc)
417          {
418             position++;
419             return (String JavaDoc) value;
420          }
421          else
422             throw new MessageFormatException JavaDoc("Invalid conversion");
423       }
424       catch (ArrayIndexOutOfBoundsException JavaDoc e)
425       {
426          throw new MessageEOFException JavaDoc("");
427       }
428    }
429
430    public int readBytes(byte[] value) throws JMSException JavaDoc
431    {
432       if (!header.msgReadOnly)
433          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
434       try
435       {
436          Object JavaDoc myObj = content.get(position);
437          if (myObj == null)
438             throw new NullPointerException JavaDoc("Value is null");
439          else if (!(myObj instanceof byte[]))
440             throw new MessageFormatException JavaDoc("Invalid conversion");
441          byte[] obj = (byte[]) myObj;
442
443          if (obj.length == 0)
444          {
445             position++;
446             offset = 0;
447             return 0;
448          }
449
450          if (offset >= obj.length)
451          {
452             position++;
453             offset = 0;
454             return -1;
455          }
456
457          if (obj.length - offset < value.length)
458          {
459             for (int i = 0; i < obj.length; i++)
460                value[i] = obj[i + offset];
461
462             position++;
463             offset = 0;
464
465             return obj.length - offset;
466          }
467          else
468          {
469             for (int i = 0; i < value.length; i++)
470                value[i] = obj[i + offset];
471             offset += value.length;
472
473             return value.length;
474          }
475
476       }
477       catch (ArrayIndexOutOfBoundsException JavaDoc e)
478       {
479          throw new MessageEOFException JavaDoc("");
480       }
481    }
482
483    public Object JavaDoc readObject() throws JMSException JavaDoc
484    {
485       if (!header.msgReadOnly)
486          throw new MessageNotReadableException JavaDoc("The message body is writeonly");
487       try
488       {
489          Object JavaDoc value = content.get(position);
490          position++;
491          offset = 0;
492
493          return value;
494       }
495       catch (ArrayIndexOutOfBoundsException JavaDoc e)
496       {
497          throw new MessageEOFException JavaDoc("");
498       }
499    }
500
501    public void writeBoolean(boolean value) throws JMSException JavaDoc
502    {
503       if (header.msgReadOnly)
504          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
505       content.add(Primitives.valueOf(value));
506    }
507
508    public void writeByte(byte value) throws JMSException JavaDoc
509    {
510       if (header.msgReadOnly)
511          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
512       content.add(new Byte JavaDoc(value));
513    }
514
515    public void writeShort(short value) throws JMSException JavaDoc
516    {
517       if (header.msgReadOnly)
518          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
519       content.add(new Short JavaDoc(value));
520    }
521
522    public void writeChar(char value) throws JMSException JavaDoc
523    {
524       if (header.msgReadOnly)
525          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
526       content.add(new Character JavaDoc(value));
527    }
528
529    public void writeInt(int value) throws JMSException JavaDoc
530    {
531       if (header.msgReadOnly)
532          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
533       content.add(new Integer JavaDoc(value));
534    }
535
536    public void writeLong(long value) throws JMSException JavaDoc
537    {
538       if (header.msgReadOnly)
539          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
540       content.add(new Long JavaDoc(value));
541    }
542
543    public void writeFloat(float value) throws JMSException JavaDoc
544    {
545       if (header.msgReadOnly)
546          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
547       content.add(new Float JavaDoc(value));
548    }
549
550    public void writeDouble(double value) throws JMSException JavaDoc
551    {
552       if (header.msgReadOnly)
553          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
554       content.add(new Double JavaDoc(value));
555    }
556
557    public void writeString(String JavaDoc value) throws JMSException JavaDoc
558    {
559       if (header.msgReadOnly)
560          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
561       if (value == null)
562          content.add(null);
563       else
564          content.add(value);
565    }
566
567    public void writeBytes(byte[] value) throws JMSException JavaDoc
568    {
569       if (header.msgReadOnly)
570          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
571       content.add(value.clone());
572    }
573
574    public void writeBytes(byte[] value, int offset, int length) throws JMSException JavaDoc
575    {
576       if (header.msgReadOnly)
577          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
578
579       if (offset + length > value.length)
580          throw new JMSException JavaDoc("Array is too small");
581       byte[] temp = new byte[length];
582       for (int i = 0; i < length; i++)
583          temp[i] = value[i + offset];
584
585       content.add(temp);
586    }
587
588    public void writeObject(Object JavaDoc value) throws JMSException JavaDoc
589    {
590       if (header.msgReadOnly)
591          throw new MessageNotWriteableException JavaDoc("The message body is readonly");
592       if (value == null)
593          content.add(null);
594       else if (value instanceof Boolean JavaDoc)
595          content.add(value);
596       else if (value instanceof Byte JavaDoc)
597          content.add(value);
598       else if (value instanceof Short JavaDoc)
599          content.add(value);
600       else if (value instanceof Character JavaDoc)
601          content.add(value);
602       else if (value instanceof Integer JavaDoc)
603          content.add(value);
604       else if (value instanceof Long JavaDoc)
605          content.add(value);
606       else if (value instanceof Float JavaDoc)
607          content.add(value);
608       else if (value instanceof Double JavaDoc)
609          content.add(value);
610       else if (value instanceof String JavaDoc)
611          content.add(value);
612       else if (value instanceof byte[])
613          content.add(((byte[]) value).clone());
614       else
615          throw new MessageFormatException JavaDoc("Invalid object type");
616    }
617
618    public void reset() throws JMSException JavaDoc
619    {
620       header.msgReadOnly = true;
621       position = 0;
622       size = content.size();
623       offset = 0;
624    }
625
626    // SpyMessage overrides ------------------------------------------
627

628    public void clearBody() throws JMSException JavaDoc
629    {
630       content = new Vector JavaDoc();
631       position = 0;
632       offset = 0;
633       size = 0;
634
635       super.clearBody();
636    }
637
638    public SpyMessage myClone() throws JMSException JavaDoc
639    {
640       SpyStreamMessage result = MessagePool.getStreamMessage();
641       result.copyProps(this);
642       result.content = (Vector JavaDoc) this.content.clone();
643       result.position = this.position;
644       result.offset = this.offset;
645       result.size = this.size;
646       return result;
647    }
648    
649    // Externalizable implementation ---------------------------------
650

651    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
652    {
653       super.readExternal(in);
654       content = (Vector JavaDoc) in.readObject();
655       position = in.readInt();
656       offset = in.readInt();
657       size = in.readInt();
658    }
659
660    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
661    {
662       super.writeExternal(out);
663       out.writeObject(content);
664       out.writeInt(position);
665       out.writeInt(offset);
666       out.writeInt(size);
667    }
668    
669    // Package protected ---------------------------------------------
670

671    // Protected -----------------------------------------------------
672

673    // Private -------------------------------------------------------
674

675    // Inner classes -------------------------------------------------
676
}
677
Popular Tags