KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.BytesMessage JavaDoc;
10 import javax.jms.JMSException JavaDoc;
11 import javax.jms.MessageNotReadableException JavaDoc;
12 import javax.jms.MessageNotWriteableException JavaDoc;
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.DataInputStream JavaDoc;
16 import java.io.DataOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
21  * @version $Revision: 1.2 $ $Date: 2003/08/21 10:07:04 $
22  */

23 public class BytesMessageImpl extends MessageImpl implements BytesMessage JavaDoc
24 {
25
26     private ByteArrayOutputStream JavaDoc outputStream = null;
27
28     public BytesMessageImpl()
29     {
30         super.type = MessageImpl.BYTES_MESSAGE_NAME;
31         this.outputStream = new ByteArrayOutputStream JavaDoc();
32         super.body = new DataOutputStream JavaDoc(this.outputStream);
33     }
34
35     public long getBodyLength() throws JMSException JavaDoc
36     {
37         return 0;
38     }
39
40     private DataInputStream JavaDoc getReadableBody()
41             throws MessageNotReadableException JavaDoc
42     {
43         if (!this.isReadOnly())
44         {
45             throw new MessageNotReadableException JavaDoc(
46                     "Unable to read message body. The message body"
47                     + "is currently marked as write only.");
48         }
49         return (DataInputStream JavaDoc) super.body;
50     }
51
52     private DataOutputStream JavaDoc getWritableBody()
53             throws MessageNotWriteableException JavaDoc
54     {
55         super.throwExceptionIfReadOnly();
56         return (DataOutputStream JavaDoc) super.body;
57     }
58
59     public boolean readBoolean() throws JMSException JavaDoc
60     {
61         try
62         {
63             return this.getReadableBody().readBoolean();
64         }
65         catch (IOException JavaDoc exception)
66         {
67             throw new JMSException JavaDoc("");
68             //TOD: Decide what exception should be thrown here.
69
}
70     }
71
72     public byte readByte() throws JMSException JavaDoc
73     {
74         try
75         {
76             return this.getReadableBody().readByte();
77         }
78         catch (IOException JavaDoc exception)
79         {
80             throw new JMSException JavaDoc("");
81             //TOD: Decide what exception should be thrown here.
82
}
83     }
84
85     public int readBytes(byte[] value) throws JMSException JavaDoc
86     {
87         try
88         {
89             return this.getReadableBody().read(value);
90         }
91         catch (IOException JavaDoc exception)
92         {
93             throw new JMSException JavaDoc("");
94             //TOD: Decide what exception should be thrown here.
95
}
96     }
97
98     public int readBytes(byte[] value, int length) throws JMSException JavaDoc
99     {
100         try
101         {
102             return this.getReadableBody().read(value, 0, length);
103             //TOD: check this.
104
}
105         catch (IOException JavaDoc exception)
106         {
107             throw new JMSException JavaDoc("");
108             //TOD: Decide what exception should be thrown here.
109
}
110     }
111
112     public char readChar() throws JMSException JavaDoc
113     {
114         try
115         {
116             return this.getReadableBody().readChar();
117         }
118         catch (IOException JavaDoc exception)
119         {
120             throw new JMSException JavaDoc("");
121             //TOD: Decide what exception should be thrown here.
122
}
123     }
124
125     public double readDouble() throws JMSException JavaDoc
126     {
127         try
128         {
129             return this.getReadableBody().readDouble();
130         }
131         catch (IOException JavaDoc exception)
132         {
133             throw new JMSException JavaDoc("");
134             //TOD: Decide what exception should be thrown here.
135
}
136     }
137
138     public float readFloat() throws JMSException JavaDoc
139     {
140         try
141         {
142             return this.getReadableBody().readFloat();
143         }
144         catch (IOException JavaDoc exception)
145         {
146             throw new JMSException JavaDoc("");
147             //TOD: Decide what exception should be thrown here.
148
}
149     }
150
151     public int readInt() throws JMSException JavaDoc
152     {
153         try
154         {
155             return this.getReadableBody().readInt();
156         }
157         catch (IOException JavaDoc exception)
158         {
159             throw new JMSException JavaDoc("");
160             //TOD: Decide what exception should be thrown here.
161
}
162     }
163
164     public long readLong() throws JMSException JavaDoc
165     {
166         try
167         {
168             return this.getReadableBody().readLong();
169         }
170         catch (IOException JavaDoc exception)
171         {
172             throw new JMSException JavaDoc("");
173             //TOD: Decide what exception should be thrown here.
174
}
175     }
176
177     public short readShort() throws JMSException JavaDoc
178     {
179         try
180         {
181             return this.getReadableBody().readShort();
182         }
183         catch (IOException JavaDoc exception)
184         {
185             throw new JMSException JavaDoc("");
186             //TOD: Decide what exception should be thrown here.
187
}
188     }
189
190     public int readUnsignedByte() throws JMSException JavaDoc
191     {
192         try
193         {
194             return this.getReadableBody().readUnsignedByte();
195         }
196         catch (IOException JavaDoc exception)
197         {
198             throw new JMSException JavaDoc("");
199             //TOD: Decide what exception should be thrown here.
200
}
201     }
202
203     public int readUnsignedShort() throws JMSException JavaDoc
204     {
205         try
206         {
207             return this.getReadableBody().readUnsignedShort();
208         }
209         catch (IOException JavaDoc exception)
210         {
211             throw new JMSException JavaDoc("");
212             //TOD: Decide what exception should be thrown here.
213
}
214     }
215
216     public String JavaDoc readUTF() throws JMSException JavaDoc
217     {
218         try
219         {
220             return this.getReadableBody().readUTF();
221         }
222         catch (IOException JavaDoc exception)
223         {
224             throw new JMSException JavaDoc("");
225             //TOD: Decide what exception should be thrown here.
226
}
227     }
228
229     public void reset() throws JMSException JavaDoc
230     {
231         this.setReadOnly(true);
232         super.body =
233                 new DataInputStream JavaDoc(
234                         new ByteArrayInputStream JavaDoc(this.outputStream.toByteArray()));
235         try
236         {
237             this.outputStream.close();
238         }
239         catch (IOException JavaDoc exception)
240         {
241             //TOD: Decide what exception should be thrown here.
242
}
243         finally
244         {
245             this.outputStream = null;
246         }
247     }
248
249     public void writeBoolean(boolean value) throws JMSException JavaDoc
250     {
251         try
252         {
253             this.getWritableBody().writeBoolean(value);
254         }
255         catch (IOException JavaDoc exception)
256         {
257             //TOD: Decide what exception should be thrown here.
258
}
259     }
260
261     public void writeByte(byte value) throws JMSException JavaDoc
262     {
263         try
264         {
265             this.getWritableBody().writeByte(value);
266         }
267         catch (IOException JavaDoc exception)
268         {
269             //TOD: Decide what exception should be thrown here.
270
}
271     }
272
273     public void writeBytes(byte[] value) throws JMSException JavaDoc
274     {
275         try
276         {
277             this.getWritableBody().write(value);
278         }
279         catch (IOException JavaDoc exception)
280         {
281             //TOD: Decide what exception should be thrown here.
282
}
283     }
284
285     public void writeBytes(byte[] value, int offset, int length)
286             throws JMSException JavaDoc
287     {
288         try
289         {
290             this.getWritableBody().write(value, offset, length);
291         }
292         catch (IOException JavaDoc exception)
293         {
294             //TOD: Decide what exception should be thrown here.
295
}
296     }
297
298     public void writeChar(char value) throws JMSException JavaDoc
299     {
300         try
301         {
302             this.getWritableBody().writeChar(value);
303         }
304         catch (IOException JavaDoc exception)
305         {
306             //TOD: Decide what exception should be thrown here.
307
}
308     }
309
310     public void writeDouble(double value) throws JMSException JavaDoc
311     {
312         try
313         {
314             this.getWritableBody().writeDouble(value);
315         }
316         catch (IOException JavaDoc exception)
317         {
318             //TOD: Decide what exception should be thrown here.
319
}
320     }
321
322     public void writeFloat(float value) throws JMSException JavaDoc
323     {
324         try
325         {
326             this.getWritableBody().writeFloat(value);
327         }
328         catch (IOException JavaDoc exception)
329         {
330             //TOD: Decide what exception should be thrown here.
331
}
332     }
333
334     public void writeInt(int value) throws JMSException JavaDoc
335     {
336         try
337         {
338             this.getWritableBody().writeInt(value);
339         }
340         catch (IOException JavaDoc exception)
341         {
342             //TOD: Decide what exception should be thrown here.
343
}
344     }
345
346     public void writeLong(long value) throws JMSException JavaDoc
347     {
348         try
349         {
350             this.getWritableBody().writeLong(value);
351         }
352         catch (IOException JavaDoc exception)
353         {
354             //TOD: Decide what exception should be thrown here.
355
}
356     }
357
358     public void writeObject(Object JavaDoc value) throws JMSException JavaDoc
359     {
360         if (value instanceof Boolean JavaDoc)
361         {
362             this.writeBoolean(((Boolean JavaDoc) value).booleanValue());
363         }
364         else if (value instanceof Byte JavaDoc)
365         {
366             this.writeByte(((Byte JavaDoc) value).byteValue());
367         }
368         else if (value instanceof Byte JavaDoc[])
369         {
370             this.writeBytes((byte[]) value);
371         }
372         else if (value instanceof Character JavaDoc)
373         {
374             this.writeChar(((Character JavaDoc) value).charValue());
375         }
376         else if (value instanceof Double JavaDoc)
377         {
378             this.writeDouble(((Double JavaDoc) value).doubleValue());
379         }
380         else if (value instanceof Float JavaDoc)
381         {
382             this.writeFloat(((Float JavaDoc) value).floatValue());
383         }
384         else if (value instanceof Integer JavaDoc)
385         {
386             this.writeInt(((Integer JavaDoc) value).intValue());
387         }
388         else if (value instanceof Long JavaDoc)
389         {
390             this.writeLong(((Long JavaDoc) value).longValue());
391         }
392         else if (value instanceof Short JavaDoc)
393         {
394             this.writeShort(((Short JavaDoc) value).shortValue());
395         }
396         else if (value instanceof String JavaDoc)
397         {
398             this.writeUTF((String JavaDoc) value);
399         }
400         else
401         {
402             // TOD: throw excption here...
403
}
404     }
405
406     public void writeShort(short value) throws JMSException JavaDoc
407     {
408         try
409         {
410             this.getWritableBody().writeShort(value);
411         }
412         catch (IOException JavaDoc exception)
413         {
414             //TOD: Decide what exception should be thrown here.
415
}
416     }
417
418     public void writeUTF(String JavaDoc value) throws JMSException JavaDoc
419     {
420         try
421         {
422             this.getWritableBody().writeUTF(value);
423         }
424         catch (IOException JavaDoc exception)
425         {
426             //TOD: Decide what exception should be thrown here.
427
}
428     }
429
430     public void clearBody()
431     {
432         this.setReadOnly(false);
433         this.outputStream = new ByteArrayOutputStream JavaDoc();
434         this.body = super.body = new DataOutputStream JavaDoc(this.outputStream);
435     }
436
437 }
Popular Tags