KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > BytesMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s):ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms;
25
26 import java.io.*;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.MessageFormatException JavaDoc;
30 import javax.jms.MessageNotWriteableException JavaDoc;
31 import javax.jms.MessageNotReadableException JavaDoc;
32 import javax.jms.MessageEOFException JavaDoc;
33
34 /**
35  * Implements the <code>javax.jms.BytesMessage</code> interface.
36  */

37 public final class BytesMessage extends Message implements javax.jms.BytesMessage JavaDoc {
38   /** The array in which the written data is buffered. */
39   private transient ByteArrayOutputStream outputBuffer = null;
40   /** The stream in which body data is written. */
41   private transient DataOutputStream outputStream = null;
42   /** The stream for reading the written data. */
43   private transient DataInputStream inputStream = null;
44   /** <code>true</code> if the message has been sent since its last modif. */
45   private transient boolean prepared = false;
46
47   /**
48    * Instanciates a bright new <code>BytesMessage</code>.
49    */

50   BytesMessage() {
51     super();
52     momMsg.type = momMsg.BYTES;
53
54     outputBuffer = new ByteArrayOutputStream();
55     outputStream = new DataOutputStream(outputBuffer);
56   }
57
58   /**
59    * Instanciates a <code>BytesMessage</code> wrapping a consumed
60    * MOM message containing a bytes array.
61    *
62    * @param sess The consuming session.
63    * @param momMsg The MOM message to wrap.
64    */

65   BytesMessage(Session sess, org.objectweb.joram.shared.messages.Message momMsg) {
66     super(sess, momMsg);
67     inputStream = new DataInputStream(new ByteArrayInputStream(momMsg.body));
68   }
69
70   /**
71    * API method.
72    *
73    * @exception MessageNotReadableException If the message is WRITE-ONLY.
74    */

75   public long getBodyLength() throws JMSException JavaDoc {
76     if (! RObody)
77       throw new MessageNotReadableException JavaDoc("Can't get not readable message's"
78                                             + " size.");
79     return momMsg.body.length;
80   }
81
82   /**
83    * API method.
84    *
85    * @exception JMSException In case of an error while closing the output or
86    * input streams.
87    */

88   public void clearBody() throws JMSException JavaDoc {
89     try {
90       if (! RObody) {
91         outputStream.close();
92         outputBuffer.close();
93       } else {
94         inputStream.close();
95       }
96
97       outputBuffer = new ByteArrayOutputStream();
98       outputStream = new DataOutputStream(outputBuffer);
99
100       super.clearBody();
101
102       prepared = false;
103     } catch (IOException ioE) {
104       JMSException JavaDoc jE = new JMSException JavaDoc("Error while closing the stream"
105                                          + " facilities.");
106       jE.setLinkedException(ioE);
107       throw jE;
108     }
109   }
110
111   /**
112    * API method.
113    *
114    * @exception MessageNotWriteableException If the message body is read-only.
115    * @exception JMSException If the value could not be written on the stream.
116    */

117   public void writeBoolean(boolean value) throws JMSException JavaDoc {
118     writeObject(new Boolean JavaDoc(value));
119   }
120  
121   /**
122    * API method.
123    *
124    * @exception MessageNotWriteableException If the message body is read-only.
125    * @exception JMSException If the value could not be written on the stream.
126    */

127   public void writeByte(byte value) throws JMSException JavaDoc {
128     writeObject(new Byte JavaDoc(value));
129   }
130  
131   /**
132    * API method.
133    *
134    * @exception MessageNotWriteableException If the message body is read-only.
135    * @exception JMSException If the value could not be written on the stream.
136    */

137   public void writeBytes(byte[] value) throws JMSException JavaDoc {
138     writeObject(value);
139   }
140
141   /**
142    * API method.
143    *
144    * @exception MessageNotWriteableException If the message body is read-only.
145    * @exception JMSException If the value could not be written on the stream.
146    */

147   public void writeBytes(byte[] value, int offset, int length) throws JMSException JavaDoc {
148     if (RObody)
149       throw new MessageNotWriteableException JavaDoc("Can't write a value as the"
150                                              + " message body is read-only.");
151
152     if (prepared) {
153       prepared = false;
154       outputBuffer = new ByteArrayOutputStream();
155       outputStream = new DataOutputStream(outputBuffer);
156     }
157
158     try {
159       outputStream.write(value, offset, length);
160     } catch (IOException ioE) {
161       JMSException JavaDoc jE = new JMSException JavaDoc("Error while writing the value.");
162       jE.setLinkedException(ioE);
163       throw jE;
164     }
165   }
166  
167   /**
168    * API method.
169    *
170    * @exception MessageNotWriteableException If the message body is read-only.
171    * @exception JMSException If the value could not be written on the stream.
172    */

173   public void writeChar(char value) throws JMSException JavaDoc {
174     writeObject(new Character JavaDoc(value));
175   }
176  
177   /**
178    * API method.
179    *
180    * @exception MessageNotWriteableException If the message body is read-only.
181    * @exception JMSException If the value could not be written on the stream.
182    */

183   public void writeDouble(double value) throws JMSException JavaDoc {
184     writeObject(new Double JavaDoc(value));
185   }
186  
187   /**
188    * API method.
189    *
190    * @exception MessageNotWriteableException If the message body is read-only.
191    * @exception JMSException If the value could not be written on the stream.
192    */

193   public void writeFloat(float value) throws JMSException JavaDoc {
194     writeObject(new Float JavaDoc(value));
195   }
196  
197   /**
198    * API method.
199    *
200    * @exception MessageNotWriteableException If the message body is read-only.
201    * @exception JMSException If the value could not be written on the stream.
202    */

203   public void writeInt(int value) throws JMSException JavaDoc {
204     writeObject(new Integer JavaDoc(value));
205   }
206  
207   /**
208    * API method.
209    *
210    * @exception MessageNotWriteableException If the message body is read-only.
211    * @exception JMSException If the value could not be written on the stream.
212    */

213   public void writeLong(long value) throws JMSException JavaDoc {
214     writeObject(new Long JavaDoc(value));
215   }
216
217   /**
218    * API method.
219    *
220    * @exception MessageNotWriteableException If the message body is read-only.
221    * @exception JMSException If the value could not be written on the stream.
222    */

223   public void writeShort(short value) throws JMSException JavaDoc {
224     writeObject(new Short JavaDoc(value));
225   }
226  
227   /**
228    * API method.
229    *
230    * @exception MessageNotWriteableException If the message body is read-only.
231    * @exception JMSException If the value could not be written on the stream.
232    */

233   public void writeUTF(String JavaDoc value) throws JMSException JavaDoc {
234     writeObject(value);
235   }
236
237   /**
238    * API method.
239    *
240    * @exception MessageNotWriteableException If the message body is read-only.
241    * @exception MessageFormatException If the value type is invalid.
242    * @exception JMSException If the value could not be written on the stream.
243    */

244   public void writeObject(Object JavaDoc value) throws JMSException JavaDoc {
245     if (RObody)
246       throw new MessageNotWriteableException JavaDoc("Can't write a value as the"
247                                              + " message body is read-only.");
248
249     if (value == null)
250       throw new NullPointerException JavaDoc("Forbidden null value.");
251
252     if (prepared) {
253       prepared = false;
254       outputBuffer = new ByteArrayOutputStream();
255       outputStream = new DataOutputStream(outputBuffer);
256     }
257
258     try {
259       if (value instanceof Boolean JavaDoc)
260         outputStream.writeBoolean(((Boolean JavaDoc) value).booleanValue());
261       else if (value instanceof Character JavaDoc)
262         outputStream.writeChar(((Character JavaDoc) value).charValue());
263       else if (value instanceof Integer JavaDoc)
264         outputStream.writeInt(((Integer JavaDoc) value).intValue());
265       else if (value instanceof Short JavaDoc)
266         outputStream.writeShort(((Short JavaDoc) value).shortValue());
267       else if (value instanceof Long JavaDoc)
268         outputStream.writeLong(((Long JavaDoc) value).longValue());
269       else if (value instanceof Float JavaDoc)
270         outputStream.writeFloat(((Float JavaDoc) value).floatValue());
271       else if (value instanceof Double JavaDoc)
272         outputStream.writeDouble(((Double JavaDoc) value).doubleValue());
273       else if (value instanceof String JavaDoc)
274         outputStream.writeUTF((String JavaDoc) value);
275       else if (value instanceof Byte JavaDoc)
276       outputStream.writeByte(((Byte JavaDoc) value).intValue());
277       else if (value instanceof byte[])
278         outputStream.write((byte[]) value);
279       else
280         throw new MessageFormatException JavaDoc("Can't write non Java primitive type"
281                                          + " as a bytes array.");
282     } catch (IOException ioE) {
283       JMSException JavaDoc jE = new JMSException JavaDoc("Error while writing the value.");
284       jE.setLinkedException(ioE);
285       throw jE;
286     }
287   }
288   
289   /**
290    * API method.
291    *
292    * @exception MessageNotReadableException If the message body is write-only.
293    * @exception JMSException If an exception occurs while reading the bytes.
294    */

295   public boolean readBoolean() throws JMSException JavaDoc {
296     if (! RObody)
297       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
298                                             + " it is write-only.");
299     try {
300       return inputStream.readBoolean();
301     } catch (Exception JavaDoc e) {
302       JMSException JavaDoc jE = null;
303       if (e instanceof EOFException JavaDoc)
304         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
305       else if (e instanceof IOException)
306         jE = new JMSException JavaDoc("Could not read the bytes array.");
307       jE.setLinkedException(e);
308       throw jE;
309     }
310   }
311
312   /**
313    * API method.
314    *
315    * @exception MessageNotReadableException If the message body is write-only.
316    * @exception JMSException If an exception occurs while reading the bytes.
317    */

318   public byte readByte() throws JMSException JavaDoc {
319     if (! RObody)
320       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
321                                             + " it is write-only.");
322     try {
323       return inputStream.readByte();
324     } catch (Exception JavaDoc e) {
325       JMSException JavaDoc jE = null;
326       if (e instanceof EOFException JavaDoc)
327         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
328       else if (e instanceof IOException)
329         jE = new JMSException JavaDoc("Could not read the bytes array.");
330       jE.setLinkedException(e);
331       throw jE;
332     }
333   }
334
335   /**
336    * API method.
337    *
338    * @exception MessageNotReadableException If the message body is write-only.
339    * @exception JMSException If an exception occurs while reading the bytes.
340    */

341   public int readUnsignedByte() throws JMSException JavaDoc {
342     if (! RObody)
343       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
344                                             + " it is write-only.");
345     try {
346       return inputStream.readUnsignedByte();
347     } catch (Exception JavaDoc e) {
348       JMSException JavaDoc jE = null;
349       if (e instanceof EOFException JavaDoc)
350         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
351       else if (e instanceof IOException)
352         jE = new JMSException JavaDoc("Could not read the bytes array.");
353       jE.setLinkedException(e);
354       throw jE;
355     }
356   }
357
358   /**
359    * API method.
360    *
361    * @exception MessageNotReadableException If the message body is write-only.
362    * @exception JMSException If an exception occurs while reading the bytes.
363    */

364   public short readShort() throws JMSException JavaDoc {
365     if (! RObody)
366       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
367                                             + " it is write-only.");
368     try {
369       return inputStream.readShort();
370     } catch (Exception JavaDoc e) {
371       JMSException JavaDoc jE = null;
372       if (e instanceof EOFException JavaDoc)
373         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
374       else if (e instanceof IOException)
375         jE = new JMSException JavaDoc("Could not read the bytes array.");
376       jE.setLinkedException(e);
377       throw jE;
378     }
379   }
380   
381   /**
382    * API method.
383    *
384    * @exception MessageNotReadableException If the message body is write-only.
385    * @exception JMSException If an exception occurs while reading the bytes.
386    */

387   public int readUnsignedShort() throws JMSException JavaDoc {
388     if (! RObody)
389       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
390                                             + " it is write-only.");
391     try {
392       return inputStream.readUnsignedShort();
393     } catch (Exception JavaDoc e) {
394       JMSException JavaDoc jE = null;
395       if (e instanceof EOFException JavaDoc)
396         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
397       else if (e instanceof IOException)
398         jE = new JMSException JavaDoc("Could not read the bytes array.");
399       jE.setLinkedException(e);
400       throw jE;
401     }
402   }
403
404   /**
405    * API method.
406    *
407    * @exception MessageNotReadableException If the message body is write-only.
408    * @exception JMSException If an exception occurs while reading the bytes.
409    */

410   public char readChar() throws JMSException JavaDoc {
411     if (! RObody)
412       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
413                                             + " it is write-only.");
414     try {
415       return inputStream.readChar();
416     } catch (Exception JavaDoc e) {
417       JMSException JavaDoc jE = null;
418       if (e instanceof EOFException JavaDoc)
419         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
420       else if (e instanceof IOException)
421         jE = new JMSException JavaDoc("Could not read the bytes array.");
422       jE.setLinkedException(e);
423       throw jE;
424     }
425   }
426
427   /**
428    * API method.
429    *
430    * @exception MessageNotReadableException If the message body is write-only.
431    * @exception JMSException If an exception occurs while reading the bytes.
432    */

433   public int readInt() throws JMSException JavaDoc {
434     if (! RObody)
435       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
436                                             + " it is write-only.");
437     try {
438       return inputStream.readInt();
439     } catch (Exception JavaDoc e) {
440       JMSException JavaDoc jE = null;
441       if (e instanceof EOFException JavaDoc)
442         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
443       else if (e instanceof IOException)
444         jE = new JMSException JavaDoc("Could not read the bytes array.");
445       jE.setLinkedException(e);
446       throw jE;
447     }
448   }
449
450   /**
451    * API method.
452    *
453    * @exception MessageNotReadableException If the message body is write-only.
454    * @exception JMSException If an exception occurs while reading the bytes.
455    */

456   public long readLong() throws JMSException JavaDoc {
457     if (! RObody)
458       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
459                                             + " it is write-only.");
460     try {
461       return inputStream.readLong();
462     } catch (Exception JavaDoc e) {
463       JMSException JavaDoc jE = null;
464       if (e instanceof EOFException JavaDoc)
465         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
466       else if (e instanceof IOException)
467         jE = new JMSException JavaDoc("Could not read the bytes array.");
468       jE.setLinkedException(e);
469       throw jE;
470     }
471   }
472
473   /**
474    * API method.
475    *
476    * @exception MessageNotReadableException If the message body is write-only.
477    * @exception JMSException If an exception occurs while reading the bytes.
478    */

479   public float readFloat() throws JMSException JavaDoc {
480     if (! RObody)
481       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
482                                             + " it is write-only.");
483     try {
484       return inputStream.readFloat();
485     } catch (Exception JavaDoc e) {
486       JMSException JavaDoc jE = null;
487       if (e instanceof EOFException JavaDoc)
488         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
489       else if (e instanceof IOException)
490         jE = new JMSException JavaDoc("Could not read the bytes array.");
491       jE.setLinkedException(e);
492       throw jE;
493     }
494   }
495
496   /**
497    * API method.
498    *
499    * @exception MessageNotReadableException If the message body is write-only.
500    * @exception JMSException If an exception occurs while reading the bytes.
501    */

502   public double readDouble() throws JMSException JavaDoc {
503     if (! RObody)
504       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
505                                             + " it is write-only.");
506     try {
507       return inputStream.readDouble();
508     } catch (Exception JavaDoc e) {
509       JMSException JavaDoc jE = null;
510       if (e instanceof EOFException JavaDoc)
511         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
512       else if (e instanceof IOException)
513         jE = new JMSException JavaDoc("Could not read the bytes array.");
514       jE.setLinkedException(e);
515       throw jE;
516     }
517   }
518
519   /**
520    * API method.
521    *
522    * @exception MessageNotReadableException If the message body is write-only.
523    * @exception JMSException If an exception occurs while reading the bytes.
524    */

525   public int readBytes(byte[] value) throws JMSException JavaDoc {
526     if (! RObody)
527       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
528                                             + " it is write-only.");
529     int counter = 0;
530
531     try {
532       for (int i = 0; i < value.length; i ++) {
533         value[i] = inputStream.readByte();
534         counter++;
535       }
536     } catch (EOFException JavaDoc eofE) {
537       // End of array has been reached:
538
} catch (IOException ioE) {
539       // An error has occured!
540
JMSException JavaDoc jE = null;
541       jE = new JMSException JavaDoc("Could not read the bytes array.");
542       jE.setLinkedException(ioE);
543       throw jE;
544     }
545     if (counter == 0)
546       return -1;
547     return counter;
548   }
549
550   /**
551    * API method.
552    *
553    * @exception MessageNotReadableException If the message body is write-only.
554    * @exception JMSException If an exception occurs while reading the bytes.
555    */

556   public int readBytes(byte[] value, int length) throws JMSException JavaDoc {
557     if (! RObody)
558       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
559                                             + " it is write-only.");
560     if (length > value.length || length < 0)
561       throw new IndexOutOfBoundsException JavaDoc("Invalid length parameter: "
562                                           + length);
563     int counter = 0;
564
565     try {
566       for (int i = 0; i < length; i ++) {
567         value[i] = inputStream.readByte();
568         counter++;
569       }
570     } catch (EOFException JavaDoc eofE) {
571       // End of array has been reached:
572
} catch (IOException ioE) {
573       // An error has occured!
574
JMSException JavaDoc jE = null;
575       jE = new JMSException JavaDoc("Could not read the bytes array.");
576       jE.setLinkedException(ioE);
577       throw jE;
578     }
579     if (counter == 0) return -1;
580
581     return counter;
582   }
583
584   /**
585    * API method.
586    *
587    * @exception MessageNotReadableException If the message body is write-only.
588    * @exception JMSException If an exception occurs while reading the bytes.
589    */

590   public String JavaDoc readUTF() throws JMSException JavaDoc {
591     if (! RObody)
592       throw new MessageNotReadableException JavaDoc("Can't read the message body as"
593                                             + " it is write-only.");
594     try {
595       return inputStream.readUTF();
596     } catch (Exception JavaDoc e) {
597       JMSException JavaDoc jE = null;
598       if (e instanceof EOFException JavaDoc)
599         jE = new MessageEOFException JavaDoc("Unexpected end of bytes array.");
600       else if (e instanceof IOException)
601         jE = new JMSException JavaDoc("Could not read the bytes array.");
602       jE.setLinkedException(e);
603       throw jE;
604     }
605   }
606
607   /**
608    * API method.
609    *
610    * @exception JMSException If an error occurs while closing the output
611    * stream.
612    */

613   public void reset() throws JMSException JavaDoc {
614     try {
615       if (! RObody) {
616         outputStream.flush();
617         momMsg.body = outputBuffer.toByteArray();
618       } else {
619         inputStream.close();
620       }
621       inputStream = new DataInputStream(new ByteArrayInputStream(momMsg.body));
622
623       RObody = true;
624     } catch (IOException iE) {
625       JMSException JavaDoc jE =
626         new JMSException JavaDoc("Error while manipulating the stream facilities.");
627       jE.setLinkedException(iE);
628       throw jE;
629     }
630   }
631
632   /**
633    * Method actually preparing the message for sending by transfering the
634    * local body into the wrapped MOM message.
635    *
636    * @exception MessageFormatException If an error occurs while serializing.
637    */

638   protected void prepare() throws JMSException JavaDoc {
639     super.prepare();
640
641     try {
642       if (! RObody) {
643         outputStream.flush();
644         momMsg.body = outputBuffer.toByteArray();
645         prepared = true;
646       }
647     } catch (IOException exc) {
648       MessageFormatException JavaDoc jExc =
649         new MessageFormatException JavaDoc("The message body could not be serialized.");
650       jExc.setLinkedException(exc);
651       throw jExc;
652     }
653   }
654 }
655
Popular Tags