KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > message > StreamMessageImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.message;
30
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.MessageEOFException JavaDoc;
33 import javax.jms.StreamMessage JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 /**
37  * A stream message.
38  */

39 public class StreamMessageImpl extends MessageImpl implements StreamMessage JavaDoc {
40   private ArrayList JavaDoc<Object JavaDoc> _values = new ArrayList JavaDoc<Object JavaDoc>();
41   private int _index;
42   private byte []_bytes;
43   private int _bytesOffset;
44
45   /**
46    * Sets the body for reading.
47    */

48   public void setReceive()
49     throws JMSException JavaDoc
50   {
51     super.setReceive();
52     
53     reset();
54   }
55
56   /**
57    * Set the stream for reading.
58    */

59   public void reset()
60     throws JMSException JavaDoc
61   {
62     setBodyReadOnly();
63     
64     _index = 0;
65     _bytes = null;
66     _bytesOffset = 0;
67   }
68
69   /**
70    * Read a boolean from the stream.
71    */

72   public boolean readBoolean()
73     throws JMSException JavaDoc
74   {
75     boolean value = ObjectConverter.toBoolean(readObjectImpl());
76
77     _index++;
78     
79     return value;
80   }
81
82   /**
83    * Read a byte from the stream.
84    */

85   public byte readByte()
86     throws JMSException JavaDoc
87   {
88     byte value = ObjectConverter.toByte(readObjectImpl());
89
90     _index++;
91
92     return value;
93   }
94
95   /**
96    * Read a short from the stream.
97    */

98   public short readShort()
99     throws JMSException JavaDoc
100   {
101     short value = ObjectConverter.toShort(readObjectImpl());
102
103     _index++;
104
105     return value;
106   }
107
108   /**
109    * Read an integer from the stream.
110    */

111   public int readInt()
112     throws JMSException JavaDoc
113   {
114     int value = ObjectConverter.toInt(readObjectImpl());
115
116     _index++;
117
118     return value;
119   }
120
121   /**
122    * Read a long from the stream.
123    */

124   public long readLong()
125     throws JMSException JavaDoc
126   {
127     long value = ObjectConverter.toLong(readObjectImpl());
128
129     _index++;
130
131     return value;
132   }
133
134   /**
135    * Read a float from the stream.
136    */

137   public float readFloat()
138     throws JMSException JavaDoc
139   {
140     float value = ObjectConverter.toFloat(readObjectImpl());
141
142     _index++;
143
144     return value;
145   }
146
147   /**
148    * Read a double from the stream.
149    */

150   public double readDouble()
151     throws JMSException JavaDoc
152   {
153     double value = ObjectConverter.toDouble(readObjectImpl());
154
155     _index++;
156
157     return value;
158   }
159
160   /**
161    * Read a character object from the stream.
162    */

163   public char readChar()
164     throws JMSException JavaDoc
165   {
166     char value = ObjectConverter.toChar(readObjectImpl());
167
168     _index++;
169
170     return value;
171   }
172
173   /**
174    * Read a string from the stream.
175    */

176   public String JavaDoc readString()
177     throws JMSException JavaDoc
178   {
179     String JavaDoc value = ObjectConverter.toString(readObjectImpl());
180
181     _index++;
182
183     return value;
184   }
185
186   /**
187    * Read a byte array object from the stream.
188    */

189   public int readBytes(byte []value)
190     throws JMSException JavaDoc
191   {
192     byte []bytes;
193     
194     if (_bytes != null) {
195       if (_bytesOffset == _bytes.length) {
196     _bytes = null;
197     _bytesOffset = 0;
198     return -1;
199       }
200     }
201     else {
202       _bytes = ObjectConverter.toBytes(readObjectImpl());
203       _index++;
204     }
205
206     if (_bytes == null)
207       return -1;
208
209     int sublen = _bytes.length - _bytesOffset;
210     if (value.length < sublen)
211       sublen = value.length;
212
213     for (int i = 0; i < sublen; i++)
214       value[i] = _bytes[_bytesOffset++];
215
216     return sublen;
217   }
218
219   /**
220    * Reads the next object.
221    */

222   public Object JavaDoc readObject()
223     throws JMSException JavaDoc
224   {
225     Object JavaDoc value = readObjectImpl();
226
227     _index++;
228
229     return value;
230   }
231
232   /**
233    * Reads the next object.
234    */

235   private Object JavaDoc readObjectImpl()
236     throws JMSException JavaDoc
237   {
238     checkBodyReadable();
239
240     if (_values.size() <= _index)
241       throw new MessageEOFException JavaDoc(L.l("end of message in stream"));
242
243     _bytes = null;
244     _bytesOffset = 0;
245
246     return _values.get(_index);
247   }
248
249   /**
250    * Clears the message and puts it into write mode.
251    */

252   public void clearBody()
253     throws JMSException JavaDoc
254   {
255     super.clearBody();
256     
257     _values.clear();
258     _index = 0;
259     _bytes = null;
260     _bytesOffset = 0;
261   }
262
263   /**
264    * Writes a boolean to the stream.
265    */

266   public void writeBoolean(boolean b)
267     throws JMSException JavaDoc
268   {
269     writeObject(new Boolean JavaDoc(b));
270   }
271
272   /**
273    * Writes a byte to the stream.
274    */

275   public void writeByte(byte b)
276     throws JMSException JavaDoc
277   {
278     writeObject(new Byte JavaDoc(b));
279   }
280
281   /**
282    * Writes a short to the stream.
283    */

284   public void writeShort(short s)
285     throws JMSException JavaDoc
286   {
287     writeObject(new Short JavaDoc(s));
288   }
289
290   /**
291    * Writes an integer to the stream.
292    */

293   public void writeInt(int i)
294     throws JMSException JavaDoc
295   {
296     writeObject(new Integer JavaDoc(i));
297   }
298
299   /**
300    * Writes a long to the stream.
301    */

302   public void writeLong(long l)
303     throws JMSException JavaDoc
304   {
305     writeObject(new Long JavaDoc(l));
306   }
307
308   /**
309    * Writes a float to the stream.
310    */

311   public void writeFloat(float f)
312     throws JMSException JavaDoc
313   {
314     writeObject(new Float JavaDoc(f));
315   }
316
317   /**
318    * Writes a double to the stream.
319    */

320   public void writeDouble(double d)
321     throws JMSException JavaDoc
322   {
323     writeObject(new Double JavaDoc(d));
324   }
325
326   /**
327    * Writes a string to the stream.
328    */

329   public void writeString(String JavaDoc s)
330     throws JMSException JavaDoc
331   {
332     writeObject(s);
333   }
334
335   /**
336    * Writes a character to the stream.
337    */

338   public void writeChar(char ch)
339     throws JMSException JavaDoc
340   {
341     writeObject(new Character JavaDoc(ch));
342   }
343
344   /**
345    * Writes a byte array to the stream.
346    */

347   public void writeBytes(byte []buf)
348     throws JMSException JavaDoc
349   {
350     writeBytes(buf, 0, buf.length);
351   }
352
353   /**
354    * Writes a byte array to the stream.
355    */

356   public void writeBytes(byte []buf, int offset, int length)
357     throws JMSException JavaDoc
358   {
359     byte []newBuf = new byte[length];
360
361     System.arraycopy(buf, offset, newBuf, 0, length);
362     
363     writeObject(newBuf);
364   }
365
366   /**
367    * Writes the next object.
368    */

369   public void writeObject(Object JavaDoc obj)
370     throws JMSException JavaDoc
371   {
372     checkBodyWriteable();
373     
374     _values.add(obj);
375   }
376
377   public MessageImpl copy()
378   {
379     StreamMessageImpl msg = new StreamMessageImpl();
380
381     copy(msg);
382
383     return msg;
384   }
385
386   protected void copy(StreamMessageImpl newMsg)
387   {
388     super.copy(newMsg);
389
390     newMsg._values = new ArrayList JavaDoc(_values);
391     newMsg._index = 0;
392   }
393 }
394
395
Popular Tags