KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > StreamMessageImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms;
8
9 import org.jboss.jms.util.JMSTypeConversions;
10
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.MessageNotReadableException JavaDoc;
13 import javax.jms.StreamMessage JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 /**
17  *
18  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
19  * @version $Revision: 1.2 $ $Date: 2003/08/21 10:07:04 $
20  */

21 public class StreamMessageImpl extends MessageImpl implements StreamMessage JavaDoc
22 {
23     private int index = 0;
24
25     public StreamMessageImpl()
26     {
27         this.type = MessageImpl.STREAM_MESSGE_NAME;
28         this.body = new ArrayList JavaDoc();
29     }
30
31     public final void clearBody()
32     {
33         this.getBody().clear();
34         this.setReadOnly(false);
35     }
36
37     public final boolean readBoolean() throws JMSException JavaDoc
38     {
39         this.throwExceptionIfNotReadable();
40         boolean value =
41                 JMSTypeConversions.getBoolean(this.getBody().get(this.index));
42         this.incrementPosition();
43         return value;
44     }
45
46     public final byte readByte() throws JMSException JavaDoc
47     {
48         this.throwExceptionIfNotReadable();
49         byte value = JMSTypeConversions.getByte(this.getBody().get(this.index));
50         this.incrementPosition();
51         return value;
52     }
53
54     public final int readBytes(byte[] value) throws JMSException JavaDoc
55     {
56         this.throwExceptionIfNotReadable();
57         value = JMSTypeConversions.getBytes(this.getBody().get(this.index));
58         this.incrementPosition();
59         if (value == null)
60         {
61             return -1;
62         }
63         else
64         {
65             return 0;
66         }
67     }
68
69     public final char readChar() throws JMSException JavaDoc
70     {
71         this.throwExceptionIfNotReadable();
72         char value = JMSTypeConversions.getChar(this.getBody().get(this.index));
73         this.incrementPosition();
74         return value;
75     }
76
77     public final double readDouble() throws JMSException JavaDoc
78     {
79         this.throwExceptionIfNotReadable();
80         double value =
81                 JMSTypeConversions.getDouble(this.getBody().get(this.index));
82         this.incrementPosition();
83         return value;
84     }
85
86     public final float readFloat() throws JMSException JavaDoc
87     {
88         this.throwExceptionIfNotReadable();
89         float value =
90                 JMSTypeConversions.getFloat(this.getBody().get(this.index));
91         this.incrementPosition();
92         return value;
93     }
94
95     public final int readInt() throws JMSException JavaDoc
96     {
97         this.throwExceptionIfNotReadable();
98         int value = JMSTypeConversions.getInt(this.getBody().get(this.index));
99         this.incrementPosition();
100         return value;
101     }
102
103     public final long readLong() throws JMSException JavaDoc
104     {
105         this.throwExceptionIfNotReadable();
106         long value = JMSTypeConversions.getLong(this.getBody().get(this.index));
107         this.incrementPosition();
108         return value;
109     }
110
111     public final Object JavaDoc readObject() throws JMSException JavaDoc
112     {
113         this.throwExceptionIfNotReadable();
114         Object JavaDoc value =
115                 JMSTypeConversions.getObject(this.getBody().get(this.index));
116         this.incrementPosition();
117         return value;
118     }
119
120     public final short readShort() throws JMSException JavaDoc
121     {
122         this.throwExceptionIfNotReadable();
123         short value =
124                 JMSTypeConversions.getShort(this.getBody().get(this.index));
125         this.incrementPosition();
126         return value;
127     }
128
129     public final String JavaDoc readString() throws JMSException JavaDoc
130     {
131         this.throwExceptionIfNotReadable();
132         String JavaDoc value =
133                 JMSTypeConversions.getString(this.getBody().get(this.index));
134         this.incrementPosition();
135         return value;
136     }
137
138     public final void reset()
139     {
140         this.setReadOnly(true);
141         this.index = 0;
142     }
143
144     public final void writeBoolean(boolean value) throws JMSException JavaDoc
145     {
146         this.getBody().add(new Boolean JavaDoc(value));
147         this.incrementPosition();
148     }
149
150     public final void writeByte(byte value) throws JMSException JavaDoc
151     {
152         this.getBody().add(new Byte JavaDoc(value));
153         this.incrementPosition();
154     }
155
156     public final void writeBytes(byte[] value) throws JMSException JavaDoc
157     {
158         this.getBody().add(value);
159         this.incrementPosition();
160     }
161
162     public final void writeBytes(byte[] value, int offset, int length)
163             throws JMSException JavaDoc
164     {
165         byte[] bytes = new byte[length];
166         System.arraycopy(value, offset, bytes, 0, length);
167         this.getBody().add(bytes);
168         this.incrementPosition();
169     }
170
171     public final void writeChar(char value) throws JMSException JavaDoc
172     {
173         this.getBody().add(new Character JavaDoc(value));
174         this.incrementPosition();
175     }
176
177     public final void writeDouble(double value) throws JMSException JavaDoc
178     {
179         this.getBody().add(new Double JavaDoc(value));
180         this.incrementPosition();
181     }
182
183     public final void writeFloat(float value) throws JMSException JavaDoc
184     {
185         this.getBody().add(new Float JavaDoc(value));
186         this.incrementPosition();
187     }
188
189     public final void writeInt(int value) throws JMSException JavaDoc
190     {
191         this.getBody().add(new Integer JavaDoc(value));
192         this.incrementPosition();
193     }
194
195     public final void writeLong(long value) throws JMSException JavaDoc
196     {
197         this.getBody().add(new Long JavaDoc(value));
198         this.incrementPosition();
199     }
200
201     public final void writeObject(Object JavaDoc value) throws JMSException JavaDoc
202     {
203         this.getBody().add(value);
204         this.incrementPosition();
205     }
206
207     public final void writeShort(short value) throws JMSException JavaDoc
208     {
209         this.getBody().add(new Short JavaDoc(value));
210         this.incrementPosition();
211     }
212
213     public final void writeString(String JavaDoc value) throws JMSException JavaDoc
214     {
215         this.getBody().add(value);
216         this.incrementPosition();
217     }
218
219     private ArrayList JavaDoc getBody()
220     {
221         return (ArrayList JavaDoc) super.body;
222     }
223
224     private void incrementPosition()
225     {
226         this.index = this.index + 1;
227     }
228
229     private void throwExceptionIfNotReadable()
230             throws MessageNotReadableException JavaDoc
231     {
232         if (!this.isReadOnly())
233         {
234             throw new MessageNotReadableException JavaDoc("The message is in write only mode.");
235         }
236     }
237
238 }
Popular Tags