KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > BytesMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 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): Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram;
25
26 import java.io.*;
27
28 import com.scalagent.kjoram.excepts.JMSException;
29 import com.scalagent.kjoram.excepts.MessageFormatException;
30 import com.scalagent.kjoram.excepts.MessageNotWriteableException;
31 import com.scalagent.kjoram.excepts.MessageNotReadableException;
32 import com.scalagent.kjoram.excepts.MessageEOFException;
33
34
35 public class BytesMessage extends Message
36 {
37   /** The array in which the written data is buffered. */
38   private ByteArrayOutputStream outputBuffer = null;
39   /** The stream in which body data is written. */
40   private DataOutputStream outputStream = null;
41   /** The stream for reading the written data. */
42   private DataInputStream inputStream = null;
43
44   /** <code>true</code> if the message body is read-only. */
45   private boolean RObody = false;
46   /** <code>true</code> if the message body is write-only. */
47   private boolean WObody = true;
48
49   /** Local bytes array. */
50   private byte[] bytes = null;
51   /** <code>true</code> if the message has been sent since its last modif. */
52   private boolean prepared = false;
53
54
55   /**
56    * Instanciates a bright new <code>BytesMessage</code>.
57    */

58   BytesMessage()
59   {
60     super();
61     outputBuffer = new ByteArrayOutputStream();
62     outputStream = new DataOutputStream(outputBuffer);
63   }
64
65   /**
66    * Instanciates a <code>BytesMessage</code> wrapping a consumed
67    * MOM message containing a bytes array.
68    *
69    * @param sess The consuming session.
70    * @param momMsg The MOM message to wrap.
71    */

72   BytesMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg)
73   {
74     super(sess, momMsg);
75     bytes = momMsg.getBytes();
76
77     inputStream = new DataInputStream(new ByteArrayInputStream(bytes));
78
79     RObody = true;
80     WObody = false;
81   }
82  
83
84   /**
85    * API method.
86    *
87    * @exception MessageNotReadableException If the message is WRITE-ONLY.
88    */

89   public long getBodyLength() throws JMSException
90   {
91     if (WObody)
92       throw new MessageNotReadableException("Can't get not readable message's"
93                                             + " size.");
94     return bytes.length;
95   }
96
97   /**
98    * API method.
99    *
100    * @exception JMSException In case of an error while closing the output or
101    * input streams.
102    */

103   public void clearBody() throws JMSException
104   {
105     super.clearBody();
106
107     try {
108       if (WObody) {
109         outputStream.close();
110         outputBuffer.close();
111       }
112       else
113         inputStream.close();
114
115       outputBuffer = new ByteArrayOutputStream();
116       outputStream = new DataOutputStream(outputBuffer);
117       bytes = null;
118       RObody = false;
119       WObody = true;
120       prepared = false;
121     }
122     catch (IOException ioE) {
123       JMSException jE = new JMSException("Error while closing the stream"
124                                          + " facilities.");
125       jE.setLinkedException(ioE);
126       throw jE;
127     }
128   }
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 writeBoolean(boolean value) throws JMSException
138   {
139     writeObject(new Boolean JavaDoc(value));
140   }
141  
142   /**
143    * API method.
144    *
145    * @exception MessageNotWriteableException If the message body is read-only.
146    * @exception JMSException If the value could not be written on the stream.
147    */

148   public void writeByte(byte value) throws JMSException
149   {
150     writeObject(new Byte JavaDoc(value));
151   }
152  
153   /**
154    * API method.
155    *
156    * @exception MessageNotWriteableException If the message body is read-only.
157    * @exception JMSException If the value could not be written on the stream.
158    */

159   public void writeBytes(byte[] value) throws JMSException
160   {
161     writeObject(value);
162   }
163
164   /**
165    * API method.
166    *
167    * @exception MessageNotWriteableException If the message body is read-only.
168    * @exception JMSException If the value could not be written on the stream.
169    */

170   public void writeBytes(byte[] value, int offset, int length)
171               throws JMSException
172   {
173     if (RObody)
174       throw new MessageNotWriteableException("Can't write a value as the"
175                                              + " message body is read-only.");
176
177     if (prepared) {
178       prepared = false;
179       outputBuffer = new ByteArrayOutputStream();
180       outputStream = new DataOutputStream(outputBuffer);
181     }
182
183     try {
184       outputStream.write(value, offset, length);
185     }
186     catch (IOException ioE) {
187       JMSException jE = new JMSException("Error while writing the value.");
188       jE.setLinkedException(ioE);
189       throw jE;
190     }
191   }
192  
193   /**
194    * API method.
195    *
196    * @exception MessageNotWriteableException If the message body is read-only.
197    * @exception JMSException If the value could not be written on the stream.
198    */

199   public void writeChar(char value) throws JMSException
200   {
201     writeObject(new Character JavaDoc(value));
202   }
203  
204   /**
205    * API method.
206    *
207    * @exception MessageNotWriteableException If the message body is read-only.
208    * @exception JMSException If the value could not be written on the stream.
209    */

210 // public void writeDouble(double value) throws JMSException
211
// {
212
// writeObject(new Double(value));
213
// }
214

215 // /**
216
// * API method.
217
// *
218
// * @exception MessageNotWriteableException If the message body is read-only.
219
// * @exception JMSException If the value could not be written on the stream.
220
// */
221
// public void writeFloat(float value) throws JMSException
222
// {
223
// writeObject(new Float(value));
224
// }
225

226   /**
227    * API method.
228    *
229    * @exception MessageNotWriteableException If the message body is read-only.
230    * @exception JMSException If the value could not be written on the stream.
231    */

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

243   public void writeLong(long value) throws JMSException
244   {
245     writeObject(new Long JavaDoc(value));
246   }
247
248   /**
249    * API method.
250    *
251    * @exception MessageNotWriteableException If the message body is read-only.
252    * @exception JMSException If the value could not be written on the stream.
253    */

254   public void writeShort(short value) throws JMSException
255   {
256     writeObject(new Short JavaDoc(value));
257   }
258  
259   /**
260    * API method.
261    *
262    * @exception MessageNotWriteableException If the message body is read-only.
263    * @exception JMSException If the value could not be written on the stream.
264    */

265   public void writeUTF(String JavaDoc value) throws JMSException
266   {
267     writeObject(value);
268   }
269
270   /**
271    * API method.
272    *
273    * @exception MessageNotWriteableException If the message body is read-only.
274    * @exception MessageFormatException If the value type is invalid.
275    * @exception JMSException If the value could not be written on the stream.
276    */

277   public void writeObject(Object JavaDoc value) throws JMSException
278   {
279     if (RObody)
280       throw new MessageNotWriteableException("Can't write a value as the"
281                                              + " message body is read-only.");
282
283     if (value == null)
284       throw new NullPointerException JavaDoc("Forbidden null value.");
285
286     if (prepared) {
287       prepared = false;
288       outputBuffer = new ByteArrayOutputStream();
289       outputStream = new DataOutputStream(outputBuffer);
290     }
291
292     try {
293       if (value instanceof Boolean JavaDoc)
294         outputStream.writeBoolean(((Boolean JavaDoc) value).booleanValue());
295       else if (value instanceof Character JavaDoc)
296         outputStream.writeChar(((Character JavaDoc) value).charValue());
297       else if (value instanceof Integer JavaDoc)
298         outputStream.writeInt(((Integer JavaDoc) value).intValue());
299       else if (value instanceof Long JavaDoc)
300         outputStream.writeLong(((Long JavaDoc) value).longValue());
301 // else if (value instanceof Float)
302
// outputStream.writeFloat(((Float) value).floatValue());
303
// else if (value instanceof Double)
304
// outputStream.writeDouble(((Double) value).doubleValue());
305
else if (value instanceof String JavaDoc)
306         outputStream.writeUTF((String JavaDoc) value);
307       else if (value instanceof byte[])
308         outputStream.write((byte[]) value);
309       else
310         throw new MessageFormatException("Can't write non Java primitive type"
311                                          + " as a bytes array.");
312     }
313     catch (IOException ioE) {
314       JMSException jE = new JMSException("Error while writing the value.");
315       jE.setLinkedException(ioE);
316       throw jE;
317     }
318   }
319   
320   
321   /**
322    * API method.
323    *
324    * @exception MessageNotReadableException If the message body is write-only.
325    * @exception JMSException If an exception occurs while reading the bytes.
326    */

327   public boolean readBoolean() throws JMSException
328   {
329     if (WObody)
330       throw new MessageNotReadableException("Can't read the message body as"
331                                             + " it is write-only.");
332     try {
333       return inputStream.readBoolean();
334     }
335     catch (Exception JavaDoc e) {
336       JMSException jE = null;
337       if (e instanceof EOFException)
338         jE = new MessageEOFException("Unexpected end of bytes array.");
339       else if (e instanceof IOException)
340         jE = new JMSException("Could not read the bytes array.");
341       jE.setLinkedException(e);
342       throw jE;
343     }
344   }
345
346   /**
347    * API method.
348    *
349    * @exception MessageNotReadableException If the message body is write-only.
350    * @exception JMSException If an exception occurs while reading the bytes.
351    */

352   public byte readByte() throws JMSException
353   {
354     if (WObody)
355       throw new MessageNotReadableException("Can't read the message body as"
356                                             + " it is write-only.");
357     try {
358       return inputStream.readByte();
359     }
360     catch (Exception JavaDoc e) {
361       JMSException jE = null;
362       if (e instanceof EOFException)
363         jE = new MessageEOFException("Unexpected end of bytes array.");
364       else if (e instanceof IOException)
365         jE = new JMSException("Could not read the bytes array.");
366       jE.setLinkedException(e);
367       throw jE;
368     }
369   }
370
371   /**
372    * API method.
373    *
374    * @exception MessageNotReadableException If the message body is write-only.
375    * @exception JMSException If an exception occurs while reading the bytes.
376    */

377   public int readUnsignedByte() throws JMSException
378   {
379     if (WObody)
380       throw new MessageNotReadableException("Can't read the message body as"
381                                             + " it is write-only.");
382     try {
383       return inputStream.readUnsignedByte();
384     }
385     catch (Exception JavaDoc e) {
386       JMSException jE = null;
387       if (e instanceof EOFException)
388         jE = new MessageEOFException("Unexpected end of bytes array.");
389       else if (e instanceof IOException)
390         jE = new JMSException("Could not read the bytes array.");
391       jE.setLinkedException(e);
392       throw jE;
393     }
394   }
395
396   /**
397    * API method.
398    *
399    * @exception MessageNotReadableException If the message body is write-only.
400    * @exception JMSException If an exception occurs while reading the bytes.
401    */

402   public short readShort() throws JMSException
403   {
404     if (WObody)
405       throw new MessageNotReadableException("Can't read the message body as"
406                                             + " it is write-only.");
407     try {
408       return inputStream.readShort();
409     }
410     catch (Exception JavaDoc e) {
411       JMSException jE = null;
412       if (e instanceof EOFException)
413         jE = new MessageEOFException("Unexpected end of bytes array.");
414       else if (e instanceof IOException)
415         jE = new JMSException("Could not read the bytes array.");
416       jE.setLinkedException(e);
417       throw jE;
418     }
419   }
420   
421   /**
422    * API method.
423    *
424    * @exception MessageNotReadableException If the message body is write-only.
425    * @exception JMSException If an exception occurs while reading the bytes.
426    */

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

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

477   public int readInt() throws JMSException
478   {
479     if (WObody)
480       throw new MessageNotReadableException("Can't read the message body as"
481                                             + " it is write-only.");
482     try {
483       return inputStream.readInt();
484     }
485     catch (Exception JavaDoc e) {
486       JMSException jE = null;
487       if (e instanceof EOFException)
488         jE = new MessageEOFException("Unexpected end of bytes array.");
489       else if (e instanceof IOException)
490         jE = new JMSException("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 long readLong() throws JMSException
503   {
504     if (WObody)
505       throw new MessageNotReadableException("Can't read the message body as"
506                                             + " it is write-only.");
507     try {
508       return inputStream.readLong();
509     }
510     catch (Exception JavaDoc e) {
511       JMSException jE = null;
512       if (e instanceof EOFException)
513         jE = new MessageEOFException("Unexpected end of bytes array.");
514       else if (e instanceof IOException)
515         jE = new JMSException("Could not read the bytes array.");
516       jE.setLinkedException(e);
517       throw jE;
518     }
519   }
520
521   /**
522    * API method.
523    *
524    * @exception MessageNotReadableException If the message body is write-only.
525    * @exception JMSException If an exception occurs while reading the bytes.
526    */

527 // public float readFloat() throws JMSException
528
// {
529
// if (WObody)
530
// throw new MessageNotReadableException("Can't read the message body as"
531
// + " it is write-only.");
532
// try {
533
// return inputStream.readFloat();
534
// }
535
// catch (Exception e) {
536
// JMSException jE = null;
537
// if (e instanceof EOFException)
538
// jE = new MessageEOFException("Unexpected end of bytes array.");
539
// else if (e instanceof IOException)
540
// jE = new JMSException("Could not read the bytes array.");
541
// jE.setLinkedException(e);
542
// throw jE;
543
// }
544
// }
545

546   /**
547    * API method.
548    *
549    * @exception MessageNotReadableException If the message body is write-only.
550    * @exception JMSException If an exception occurs while reading the bytes.
551    */

552 // public double readDouble() throws JMSException
553
// {
554
// if (WObody)
555
// throw new MessageNotReadableException("Can't read the message body as"
556
// + " it is write-only.");
557
// try {
558
// return inputStream.readDouble();
559
// }
560
// catch (Exception e) {
561
// JMSException jE = null;
562
// if (e instanceof EOFException)
563
// jE = new MessageEOFException("Unexpected end of bytes array.");
564
// else if (e instanceof IOException)
565
// jE = new JMSException("Could not read the bytes array.");
566
// jE.setLinkedException(e);
567
// throw jE;
568
// }
569
// }
570

571   /**
572    * API method.
573    *
574    * @exception MessageNotReadableException If the message body is write-only.
575    * @exception JMSException If an exception occurs while reading the bytes.
576    */

577   public int readBytes(byte[] value) throws JMSException
578   {
579     if (WObody)
580       throw new MessageNotReadableException("Can't read the message body as"
581                                             + " it is write-only.");
582     int counter = 0;
583
584     try {
585       for (int i = 0; i < value.length; i ++) {
586         value[i] = inputStream.readByte();
587         counter++;
588       }
589     }
590     // End of array has been reached:
591
catch (EOFException eofE) {}
592     // An error has occured!
593
catch (IOException ioE) {
594       JMSException jE = null;
595       jE = new JMSException("Could not read the bytes array.");
596       jE.setLinkedException(ioE);
597       throw jE;
598     }
599     if (counter == 0)
600       return -1;
601     return counter;
602   }
603
604   /**
605    * API method.
606    *
607    * @exception MessageNotReadableException If the message body is write-only.
608    * @exception JMSException If an exception occurs while reading the bytes.
609    */

610   public int readBytes(byte[] value, int length) throws JMSException
611   {
612     if (WObody)
613       throw new MessageNotReadableException("Can't read the message body as"
614                                             + " it is write-only.");
615     if (length > value.length || length < 0)
616       throw new IndexOutOfBoundsException JavaDoc("Invalid length parameter: "
617                                           + length);
618     int counter = 0;
619
620     try {
621       for (int i = 0; i < length; i ++) {
622         value[i] = inputStream.readByte();
623         counter++;
624       }
625     }
626     // End of array has been reached:
627
catch (EOFException eofE) {}
628     // An error has occured!
629
catch (IOException ioE) {
630       JMSException jE = null;
631       jE = new JMSException("Could not read the bytes array.");
632       jE.setLinkedException(ioE);
633       throw jE;
634     }
635     if (counter == 0)
636       return -1;
637     return counter;
638   }
639
640   /**
641    * API method.
642    *
643    * @exception MessageNotReadableException If the message body is write-only.
644    * @exception JMSException If an exception occurs while reading the bytes.
645    */

646   public String JavaDoc readUTF() throws JMSException
647   {
648     if (WObody)
649       throw new MessageNotReadableException("Can't read the message body as"
650                                             + " it is write-only.");
651     try {
652       return inputStream.readUTF();
653     }
654     catch (Exception JavaDoc e) {
655       JMSException jE = null;
656       if (e instanceof EOFException)
657         jE = new MessageEOFException("Unexpected end of bytes array.");
658       else if (e instanceof IOException)
659         jE = new JMSException("Could not read the bytes array.");
660       jE.setLinkedException(e);
661       throw jE;
662     }
663   }
664
665   
666   /**
667    * API method.
668    *
669    * @exception JMSException If an error occurs while closing the output
670    * stream.
671    */

672   public void reset() throws JMSException
673   {
674     try {
675       if (WObody) {
676         outputStream.flush();
677         bytes = outputBuffer.toByteArray();
678       }
679       else
680         inputStream.close();
681       
682       inputStream = new DataInputStream(new ByteArrayInputStream(bytes));
683
684       RObody = true;
685       WObody = false;
686     }
687     catch (IOException iE) {
688       JMSException jE =
689         new JMSException("Error while manipulating the stream facilities.");
690       jE.setLinkedException(iE);
691       throw jE;
692     }
693   }
694
695   /**
696    * Method actually preparing the message for sending by transfering the
697    * local body into the wrapped MOM message.
698    *
699    * @exception Exception If an error occurs while serializing.
700    */

701   protected void prepare() throws Exception JavaDoc
702   {
703     super.prepare();
704
705     if (WObody) {
706       outputStream.flush();
707       bytes = outputBuffer.toByteArray();
708       prepared = true;
709     }
710
711     momMsg.clearBody();
712     momMsg.setBytes(bytes);
713   }
714 }
715
Popular Tags