KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaBytesMessage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Nimo 23-MAR-2004 and Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.api.jms;
48 /**
49  *
50  * @author Nimo 23-MAR-2004 and Amir Shevat
51  *
52  *
53  * A BytesMessage object is used to send a message containing a stream of
54  * uninterpreted bytes. It inherits from the Message interface and adds a
55  * bytes message body. The receiver of the message supplies the interpretation
56  * of the bytes.
57  * The BytesMessage methods are based largely on those found in java.io.DataInputStream
58  * and java.io.DataOutputStream.
59  * This message type is for client encoding of existing message formats. If
60  * possible, one of the other self-defining message types should be used instead.
61  * Although the JMS API allows the use of message properties with byte messages,
62  * they are typically not used, since the inclusion of properties may affect
63  * the format.
64  * The primitive types can be written explicitly using methods for each type.
65  * They may also be written generically as objects. For instance, a call to
66  * BytesMessage.writeInt(6) is equivalent to BytesMessage.writeObject(new Integer(6)).
67  * Both forms are provided, because the explicit form is convenient for static
68  * programming, and the object form is needed when types are not known at compile
69  * time.
70  * When the message is first created, and when clearBody is called, the body of
71  * the message is in write-only mode. After the first call to reset has been made,
72  * the message body is in read-only mode. After a message has been sent, the client
73  * that sent it can retain and modify it without affecting the message that
74  * has been sent.
75  * The same message object can be sent multiple times. When a message has been
76  * received, the provider has called reset so that the message body is in
77  * read-only mode for the client.
78  *
79  * If clearBody is called on a message in read-only mode, the message body is
80  * cleared and the message is in write-only mode.
81  * If a client attempts to read a message in write-only mode, a MessageNotReadableException
82  * is thrown.
83  * If a client attempts to write a message in read-only mode, a MessageNotWriteableException
84  * is thrown.
85  */

86
87
88 import java.io.ByteArrayOutputStream JavaDoc;
89 import java.io.DataOutputStream JavaDoc;
90 import java.io.EOFException JavaDoc;
91 import java.io.IOException JavaDoc;
92 import java.io.UTFDataFormatException JavaDoc;
93
94 import javax.jms.BytesMessage JavaDoc;
95 import javax.jms.JMSException JavaDoc;
96 import javax.jms.MessageEOFException JavaDoc;
97 import javax.jms.MessageFormatException JavaDoc;
98 import javax.jms.MessageNotReadableException JavaDoc;
99 import javax.jms.MessageNotWriteableException JavaDoc;
100
101 import org.mr.core.util.byteable.Byteable;
102 import org.mr.core.util.byteable.ByteableInputStream;
103 import org.mr.core.util.byteable.ByteableOutputStream;
104 import org.mr.core.util.byteable.ByteableRegistry;
105
106
107 public class MantaBytesMessage extends MantaMessage implements BytesMessage JavaDoc {
108
109     /**
110      * generated <code>serialVersionUID</code>
111      */

112     private static final long serialVersionUID = 3088174266194469819L;
113     
114     protected static final byte[] EMPTY = new byte[]{}; //initialization.
115
protected byte[] message = EMPTY;
116 // protected byte[] backupMessage;
117
protected DataOutputStream JavaDoc outputStream = null;
118     protected ByteArrayOutputStream JavaDoc byteOutputStream = null;
119     protected int internalOffset = 0;
120     
121     
122     /**
123      * only for makeCopy method use
124      *
125      */

126     public MantaBytesMessage(){
127         
128     }
129     
130     /**
131      * The constructor used by the MantaSession to create the message.
132      * @param sess - the session creating this message.
133      *
134      * @throws JMSException - actually, never thrown.
135      */

136     public MantaBytesMessage(MantaSession sess) throws JMSException JavaDoc {
137         flags = (flags&ONLY_MSG_TYPES) | BYTES_MESSAGE;
138         creatingSession = sess;
139     } //MantaBytesMessage
140

141     /**
142      * Read a <code>boolean</code> value from the bytes message stream
143      *
144      * @return the boolean value read
145      *
146      * @throws JMSException if JMS fails to read message due to some internal JMS error
147      * @throws MessageEOFException if end of bytes stream
148      * @throws MessageNotReadableException if message is in write-only mode
149      */

150     public final boolean readBoolean() throws JMSException JavaDoc {
151         int result = 0;
152         //make sure it is readable
153
changeInput();
154         try {
155             result = read();
156         } //try
157
catch (IOException JavaDoc exception) {
158             rollback(exception);
159         } //catch
160
return (result != 0);
161     } //readBoolean
162

163
164     /**
165      * Read a signed 8-bit value from the bytes message stream
166      *
167      * @return the next byte from the bytes message stream as a signed 8-bit byte
168      *
169      * @throws JMSException if JMS fails to read message due to some internal JMS error
170      * @throws MessageEOFException if end of message stream
171      * @throws MessageNotReadableException if message is in write-only mode
172      */

173     public final byte readByte() throws JMSException JavaDoc {
174         int result = 0;
175         //make sure it is readable
176
changeInput();
177         try {
178             result = read();
179         }//try
180
catch (IOException JavaDoc exception) {
181             rollback(exception);
182         }//catch
183
return (byte)(result);
184     }//readByte
185

186
187     /**
188      * Read an unsigned 8-bit number from the bytes message stream
189      *
190      * @return the next byte from the bytes message stream, interpreted as an unsigned 8-bit number
191      *
192      * @throws JMSException if JMS fails to read message due to some internal JMS error
193      * @throws MessageNotReadableException if message is in write-only mode
194      * @throws MessageEOFException if end of message stream
195      */

196     public final int readUnsignedByte() throws JMSException JavaDoc {
197         int result = 0;
198         //make sure it is readable
199
changeInput();
200         try {
201             result = read();
202         }//try
203
catch (IOException JavaDoc exception) {
204             rollback(exception);
205         }//catch
206
return result;
207     }//readUnsignedByte
208

209
210     /**
211      * Read a signed 16-bit number from the bytes message stream
212      *
213      * @return the next two bytes from the bytes message stream, interpreted as a signed 16-bit number
214      *
215      * @throws JMSException if JMS fails to read message due to some internal JMS error
216      * @throws MessageEOFException if end of message stream
217      * @throws MessageNotReadableException if message is in write-only mode
218      */

219     public final short readShort() throws JMSException JavaDoc {
220         short result = 0;
221         //make sure it is readable
222
changeInput();
223         try {
224             int ch1 = read();
225             int ch2 = read();
226             
227             result= (short)((ch1 << 8) + (ch2 << 0));
228         }//try
229
catch (IOException JavaDoc exception) {
230             rollback(exception);
231         }//catch
232
return result;
233     }//readShort
234

235
236     /**
237      * Read an unsigned 16-bit number from the bytes message stream
238      *
239      * @return the next two bytes from the bytes message stream, interpreted as an unsigned 16-bit integer
240      *
241      * @throws JMSException if JMS fails to read message due to some internal JMS error
242      * @throws MessageEOFException if end of message stream
243      * @throws MessageNotReadableException if message is in write-only mode
244      */

245     public final int readUnsignedShort() throws JMSException JavaDoc {
246         int result = 0;
247         //make sure it is readable
248
changeInput();
249         try {
250             int ch1 = read();
251             int ch2 = read();
252             result = (ch1 << 8) + (ch2 << 0);
253         }//try
254
catch (IOException JavaDoc exception) {
255             rollback(exception);
256         }//catch
257
return result;
258     }//readUnsignedShort
259

260
261     /**
262      * Read a Unicode character value from the bytes message stream
263      *
264      * @return the next two bytes from the bytes message stream as a Unicode character
265      *
266      * @throws JMSException if JMS fails to read message due to some internal JMS error
267      * @throws MessageEOFException if end of message stream
268      * @throws MessageNotReadableException if message is in write-only mode
269      */

270     public final char readChar() throws JMSException JavaDoc {
271         char result = 0;
272         //make sure it is readable
273
changeInput();
274         try {
275             int ch1 = read();
276             int ch2 = read();
277             result= (char)((ch1 << 8) + (ch2 << 0));
278         }//try
279
catch (IOException JavaDoc exception) {
280             rollback(exception);
281         } //catch
282
return result;
283     }//readChar
284

285
286     /**
287      * Read a signed 32-bit integer from the bytes message stream
288      *
289      * @return the next four bytes from the bytes message stream, interpreted as an int
290      *
291      * @throws JMSException if JMS fails to read message due to some internal JMS error
292      * @throws MessageEOFException if end of message stream
293      * @throws MessageNotReadableException if message is in write-only mode
294      */

295     public final int readInt() throws JMSException JavaDoc {
296         int result = 0;
297         //make sure it is readable
298
changeInput();
299         try {
300             int ch1 = read();
301             int ch2 = read();
302             int ch3 = read();
303             int ch4 = read();
304            
305             result= ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
306         }//try
307
catch (IOException JavaDoc exception) {
308             rollback(exception);
309         }//catch
310
return result;
311     }//readInt
312

313
314     /**
315      * Read a signed 64-bit integer from the bytes message stream
316      *
317      * @return the next eight bytes from the bytes message stream, interpreted as a long.
318      *
319      * @throws JMSException if JMS fails to read message due to some internal JMS error
320      * @throws MessageEOFException if end of message stream
321      * @throws MessageNotReadableException if message is in write-only mode
322      */

323     public final long readLong() throws JMSException JavaDoc {
324         long result = 0;
325         //make sure it is readable
326
changeInput();
327         
328         byte b1= readByte();
329         byte b2= readByte();
330         byte b3= readByte();
331         byte b4= readByte();
332         byte b5= readByte();
333         byte b6= readByte();
334         byte b7= readByte();
335         byte b8= readByte();
336             
337             result= (((long)b1 << 56) +
338                     ((long)(b2 & 255) << 48) +
339                     ((long)(b3 & 255) << 40) +
340                     ((long)(b4 & 255) << 32) +
341                     ((long)(b5 & 255) << 24) +
342                     ((b6 & 255) << 16) +
343                     ((b7 & 255) << 8) +
344                     ((b8 & 255) << 0));
345             
346
347         return result;
348     }//readLong
349

350
351     /**
352      * Read a float from the bytes message stream
353      *
354      * @return the next four bytes from the bytes message stream, interpreted as a float
355      * @throws JMSException if JMS fails to read message due to some internal JMS error
356      * @throws MessageEOFException if end of message stream
357      * @throws MessageNotReadableException if message is in write-only mode
358      */

359     public final float readFloat() throws JMSException JavaDoc {
360         return Float.intBitsToFloat(readInt());
361     }//readFloat
362

363
364     /**
365      * Read a double from the bytes message stream
366      *
367      * @return the next eight bytes from the bytes message stream, interpreted as a double
368      *
369      * @throws JMSException if JMS fails to read message due to some internal JMS error
370      * @throws MessageEOFException if end of message stream
371      * @throws MessageNotReadableException if message is in write-only mode
372      */

373     public final double readDouble() throws JMSException JavaDoc {
374         
375         return Double.longBitsToDouble(readLong());
376     }//readDouble
377

378
379     /**
380      * Read in a string that has been encoded using a modified UTF-8 format
381      * from the bytes message stream
382      *
383      *
384      * @return a Unicode string from the bytes message stream
385      *
386      * @throws JMSException if JMS fails to read message due to some internal JMS error
387      * @throws MessageEOFException if end of message stream
388      * @throws MessageFormatException if string has an invalid format
389      * @throws MessageNotReadableException if message is in write-only mode
390      */

391     public final String JavaDoc readUTF() throws JMSException JavaDoc {
392         String JavaDoc result = null;
393         //make sure it is readable
394
changeInput();
395         try {
396             int utflen = readShort();
397             if(utflen ==ByteableOutputStream.NULL_PLACE_HOLDER){
398                 return null;
399             }
400                 
401             StringBuffer JavaDoc str = new StringBuffer JavaDoc(utflen);
402             byte bytearr [] = new byte[utflen];
403             int c, char2, char3;
404             int count = 0;
405
406             readFully(bytearr, 0, utflen);
407
408             while (count < utflen) {
409                 c = bytearr[count] & 0xff;
410                 switch (c >> 4) {
411                     case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
412                         /* 0xxxxxxx*/
413                         count++;
414                         str.append((char)c);
415                         break;
416                     case 12: case 13:
417                         /* 110x xxxx 10xx xxxx*/
418                         count += 2;
419                         if (count > utflen)
420                             throw new UTFDataFormatException JavaDoc();
421                         char2 = bytearr[count-1];
422                         if ((char2 & 0xC0) != 0x80)
423                             throw new UTFDataFormatException JavaDoc(
424                                     "MNJMS00014 : CORRUPTED UTF FORMAT. FAILED IN METHOD readUTF().");
425                         str.append((char)(((c & 0x1F) << 6) | (char2 & 0x3F)));
426                         break;
427                     case 14:
428                         /* 1110 xxxx 10xx xxxx 10xx xxxx */
429                         count += 3;
430                         if (count > utflen)
431                             throw new UTFDataFormatException JavaDoc("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF().");
432                         char2 = bytearr[count-2];
433                         char3 = bytearr[count-1];
434                         if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
435                             throw new UTFDataFormatException JavaDoc("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF().");
436                         str.append((char)(((c & 0x0F) << 12) |
437                                 ((char2 & 0x3F) << 6) |
438                                 ((char3 & 0x3F) << 0)));
439                         break;
440                     default:
441                         /* 10xx xxxx, 1111 xxxx */
442                         throw new UTFDataFormatException JavaDoc("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF().");
443                 }
444                 // The number of chars produced may be less than utflen
445
result = new String JavaDoc(str);
446             }
447             
448         }//try
449
catch (IOException JavaDoc exception) {
450             rollback(exception);
451         }//catch
452
return result;
453     }//readUTF
454

455
456     /**
457      * Read a byte array from the bytes message stream
458      *
459      * If the length of array value is less than the bytes
460      * remaining to be read from the stream, the array should be filled. A
461      * subsequent call reads the next increment, etc.
462      *
463      * If the bytes remaining in the stream is less than the length of array
464      * value, the bytes should be read into the array. The
465      * return value of the total number of bytes read will be less than the
466      * length of the array, indicating that there are no more bytes left to be
467      * read from the stream. The next read of the stream returns -1.
468      *
469      * @param value the buffer into which the data is read
470      *
471      * @return the total number of bytes read into the buffer, or -1 if there
472      * is no more data because the end of the stream has been reached
473      *
474      * @throws JMSException if JMS fails to read message due to some internal JMS error
475      * @throws MessageNotReadableException if message is in write-only mode
476      */

477     public final int readBytes(byte[] value) throws JMSException JavaDoc {
478         return readBytes(value, value.length);
479     }//readBytes
480

481     
482     /**
483      * Read a portion of the bytes message stream.
484      *
485      * If the length of array value is less than the bytes
486      * remaining to be read from the stream, the array should be filled. A
487      * subsequent call reads the next increment, etc.
488      *
489      * If the bytes remaining in the stream is less than the length of array
490      * value, the bytes should be read into the array. The
491      * return value of the total number of bytes read will be less than the
492      * length of the array, indicating that there are no more bytes left to be
493      * read from the stream. The next read of the stream returns -1.
494      *
495      * If length is negative, or length is
496      * greater than the length of the array value, then an
497      * IndexOutOfBoundsException is thrown. No bytes will be
498      * read from the stream for this exception case.
499      *
500      * @param value the buffer into which the data is read.
501      * @param length the number of bytes to read. Must be less than or equal to value.length.
502      *
503      * @return the total number of bytes read into the buffer, or -1 if there
504      * is no more data because the end of the stream has been reached.
505      *
506      * @throws IndexOutOfBoundsException if length is invalid
507      * @throws JMSException if JMS fails to read message due to some internal JMS error
508      * @throws MessageNotReadableException if message is in write-only mode
509      */

510     public final int readBytes(byte[] value, int length) throws JMSException JavaDoc {
511         int read = -1;
512         //make sure it is readable
513
changeInput();
514
515         //See that this is a valid length.
516
if (length < 0 || length > value.length) {
517             throw new IndexOutOfBoundsException JavaDoc("MNJMS00001 : INVALID LENGTH SUPPLIED FOR READ OPERATION.");
518         }
519         //read the bytes from the input stream.
520
try {
521             
522             if (internalOffset < message.length) {
523                 
524                 if(length <= message.length-internalOffset){
525                     read = length;
526                 }else{
527                     read = length-internalOffset;
528                 }
529                 readFully(value, 0, read);
530                 
531             }//else
532
}//try
533
catch (EOFException JavaDoc doNothing) {
534         }//catch
535
catch (IOException JavaDoc exception) {
536             rollback(exception);
537         }//catch
538
return read;
539     }//readBytes
540

541
542     /**
543      * Write a boolean to the bytes message stream as a 1-byte
544      * value. The value true is written out as the value (byte)1;
545      * the value false is written out as the value (byte)0.
546      *
547      * @param value the boolean value to be written
548      *
549      * @throws JMSException if JMS fails to write message due to some internal JMS error
550      * @throws MessageNotWriteableException if message is in read-only mode
551      */

552     public final void writeBoolean(boolean value) throws JMSException JavaDoc {
553         checkWriteableState(false);
554         try {
555             getOutputStream().writeBoolean(value);
556         }//try
557
catch (IOException JavaDoc exception) {
558             throw new JMSException JavaDoc("MNJMS00005 : AN IO EXCEPTION OCCURED IN writeBoolean() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
559         }//catch
560
}//writeBoolean
561

562
563     /**
564      * Write out a byte to the bytes message stream as a 1-byte value
565      *
566      * @param value the byte value to be written
567      *
568      * @throws JMSException if JMS fails to write message due to some internal JMS error
569      * @throws MessageNotWritea bleException if message is in read-only mode
570      */

571     public final void writeByte(byte value) throws JMSException JavaDoc {
572         checkWriteableState(false);
573         try {
574             getOutputStream().writeByte(value);
575         }//try
576
catch (IOException JavaDoc exception) {
577             throw new JMSException JavaDoc("MNJMS00006 : AN IO EXCEPTION OCCURED IN writeByte() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
578         }//catch
579
}//writeByte
580

581     
582     /**
583      * Write a short to the bytes message stream as two bytes, high byte first
584      *
585      * @param value the short to be written
586      *
587      * @throws JMSException if JMS fails to write message due to some internal JMS error
588      * @throws MessageNotWriteableException if message is in read-only mode
589      */

590     public final void writeShort(short value) throws JMSException JavaDoc {
591         checkWriteableState(false);
592         try {
593             getOutputStream().writeShort(value);
594         }//try
595
catch (IOException JavaDoc exception) {
596             throw new JMSException JavaDoc("MNJMS00007 : AN IO EXCEPTION OCCURED IN writeShort() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
597         }//catch
598
}//writeShort
599

600     
601     /**
602      * Write a char to the bytes message stream as a 2-byte value, high byte first.
603      *
604      * @param value the char value to be written
605      *
606      * @throws MessageNotWriteableException if message is in read-only mode
607      * @throws JMSException if JMS fails to write message due to some internal JMS error
608      */

609     public final void writeChar(char value) throws JMSException JavaDoc {
610         checkWriteableState(false);
611         try {
612             getOutputStream().writeChar(value);
613         }//try
614
catch (IOException JavaDoc exception) {
615             throw new JMSException JavaDoc("MNJMS00008 : AN IO EXCEPTION OCCURED IN writeChar() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
616         }//catch
617
}//writeChar
618

619     
620     /**
621      * Write an int to the bytes message stream as four bytes, high byte first.
622      *
623      * @param value the int to be written
624      *
625      * @throws JMSException if JMS fails to write message due to some internal JMS error
626      * @throws MessageNotWriteableException if message is in read-only mode
627      */

628     public final void writeInt(int value) throws JMSException JavaDoc {
629         checkWriteableState(false);
630         try {
631             getOutputStream().writeInt(value);
632         }//try
633
catch (IOException JavaDoc exception) {
634             throw new JMSException JavaDoc("MNJMS00009 : AN IO EXCEPTION OCCURED IN writeInt() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
635         }//catch
636
}//writeInt
637

638     
639     /**
640      * Write a long to the bytes message stream as eight bytes, high byte first
641      *
642      * @param value the long to be written
643      * @throws JMSException if JMS fails to write message due to some internal JMS error
644      * @throws MessageNotWriteableException if message is in read-only mode
645      */

646     public final void writeLong(long value) throws JMSException JavaDoc {
647         checkWriteableState(false);
648         try {
649             getOutputStream().writeLong(value);
650         }//try
651
catch (IOException JavaDoc exception) {
652             throw new JMSException JavaDoc("MNJMS0000A : AN IO EXCEPTION OCCURED IN writeLong() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
653         }//catch
654
}//writeLong
655

656     
657     /**
658      * Convert the float argument to an int using the floatToIntBits
659      * method in class Float, and then writes that int
660      * value to the bytes message stream as a 4-byte quantity, high byte first.
661      *
662      * @param value the float value to be written.
663      *
664      * @throws JMSException if JMS fails to write message due to some internal JMS error
665      * @throws MessageNotWriteableException if message is in read-only mode
666      */

667     public final void writeFloat(float value) throws JMSException JavaDoc {
668         checkWriteableState(false);
669         try {
670             getOutputStream().writeFloat(value);
671         }//try
672
catch (IOException JavaDoc exception) {
673             throw new JMSException JavaDoc("MNJMS0000B : AN IO EXCEPTION OCCURED IN writeFloat() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
674         }//catch
675
}//writeFloat
676

677     
678     /**
679      * Convert the double argument to a long using the doubleToLongBits
680      * method in class Double, and then writes that long
681      * value to the bytes message stream as an 8-byte quantity, high byte
682      * first.
683      *
684      * @param value the double value to be written.
685      *
686      * @throws JMSException if JMS fails to write message due to some internal JMS error
687      * @throws MessageNotWriteableException if message is in read-only mode
688      */

689     public final void writeDouble(double value) throws JMSException JavaDoc {
690         checkWriteableState(false);
691         try {
692             getOutputStream().writeDouble(value);
693         }//try
694
catch (IOException JavaDoc exception) {
695             throw new JMSException JavaDoc("MNJMS0000C : AN IO EXCEPTION OCCURED IN writeDouble() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
696         }//catch
697
}//writeDouble
698

699     
700     /**
701      * Write a string to the bytes message stream using UTF-8 encoding in a
702      * machine-independent manner.
703      *
704      *
705      * @param value the String value to be written
706      * @throws MessageNotWriteableException if message is in read-only mode
707      * @throws JMSException if JMS fails to write message due to some internal JMS error
708      */

709     public final void writeUTF(String JavaDoc value) throws JMSException JavaDoc {
710         checkWriteableState(false);
711         try {
712             getOutputStream().writeUTF(value);
713         }//try
714
catch (IOException JavaDoc exception) {
715             throw new JMSException JavaDoc("MNJMS0000D : AN IO EXCEPTION OCCURED IN writeUTF() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
716         }//catch
717
}//wrtieUTF
718

719     
720     /**
721      * Write a byte array to the bytes message stream
722      *
723      * @param value the byte array to be written.
724      * @throws JMSException if JMS fails to write message due to some internal JMS error
725      * @throws MessageNotWriteableException if message is in read-only mode
726      */

727     public final void writeBytes(byte[] value) throws JMSException JavaDoc {
728         checkWriteableState(false);
729         try {
730             getOutputStream().write(value);
731         }//try
732
catch (IOException JavaDoc exception) {
733             throw new JMSException JavaDoc("MNJMS0000E : AN IO EXCEPTION OCCURED IN writeBytes() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
734         }//catch
735
}//writeBytes
736

737     
738     /**
739      * Write a portion of a byte array to the bytes message stream
740      *
741      * @param value the byte array value to be written.
742      * @param offset the initial offset within the byte array.
743      * @param length the number of bytes to use.
744      *
745      * @throws JMSException if JMS fails to write message due to some internal JMS error
746      * @throws MessageNotWriteableException if message is in read-only mode
747      */

748     public final void writeBytes(byte[] value, int offset, int length) throws JMSException JavaDoc {
749         checkWriteableState(false);
750         try {
751             getOutputStream().write(value, offset, length);
752         }//try
753
catch (IOException JavaDoc exception) {
754             throw new JMSException JavaDoc("MNJMS0000E : AN IO EXCEPTION OCCURED IN writeBytes() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
755         }//catch
756
}//wrtieBytes
757

758     
759     /**
760      * Write a Java object to the bytes message stream.
761      *
762      * Note that this method only works for the objectified primitive object
763      * types (Integer, Double, Long ...), String's and byte arrays.
764      *
765      * @param value the Java object to be written. Must not be null.
766      *
767      * @throws JMSException if JMS fails to write message due to some internal JMS error
768      * @throws MessageFormatException if object is invalid type
769      * @throws MessageNotWriteableException if message in read-only mode
770      * @throws NullPointerException if parameter value is null
771      */

772     public final void writeObject(Object JavaDoc value) throws JMSException JavaDoc {
773         checkWriteableState(false);
774         //See which object we're dealing with
775
if (value == null) {
776             throw new NullPointerException JavaDoc("MNJMS00002 : BYTES MESSAGE DOES NOT SUPPORT NULL VALUES.");
777         }
778         else if (value instanceof Boolean JavaDoc) {
779             writeBoolean(((Boolean JavaDoc) value).booleanValue());
780         }//if
781
else if (value instanceof Byte JavaDoc) {
782             writeByte(((Byte JavaDoc) value).byteValue());
783         }//else if
784
else if (value instanceof Short JavaDoc) {
785             writeShort(((Short JavaDoc) value).shortValue());
786         }//else if
787
else if (value instanceof Character JavaDoc) {
788             writeChar(((Character JavaDoc) value).charValue());
789         }//else if
790
else if (value instanceof Integer JavaDoc) {
791             writeInt(((Integer JavaDoc) value).intValue());
792         }//else if
793
else if (value instanceof Long JavaDoc) {
794             writeLong(((Long JavaDoc) value).longValue());
795         }//else if
796
else if (value instanceof Float JavaDoc) {
797             writeFloat(((Float JavaDoc) value).floatValue());
798         }//else if
799
else if (value instanceof Double JavaDoc) {
800             writeDouble(((Double JavaDoc) value).doubleValue());
801         }//else if
802
else if (value instanceof String JavaDoc) {
803             writeUTF((String JavaDoc) value);
804         }//else if
805
else if (value instanceof byte[]) {
806             writeBytes((byte[]) value);
807         }//else if
808
else {
809             throw new MessageFormatException JavaDoc("MNJMS00003 : OBJECT OF TYPE "+value.getClass().getName()+
810                     " IS UNALLOWED IN BYTES MESSAGE.");
811             
812         }//else
813
} //writeObject
814

815     
816     /**
817      * Put the message body in read-only mode, and repositions the stream of
818      * bytes to the beginning
819      *
820      * @throws JMSException if JMS fails to reset the message due to some internal JMS error
821      */

822     public final void reset() throws JMSException JavaDoc {
823         
824         try {
825            
826             if (this.isWriteable())
827                 this.setWriteableState(false);
828             
829             if (outputStream != null) {
830                 outputStream.flush();
831                 this.message = this.byteOutputStream.toByteArray();
832                 this.byteOutputStream = null;
833                 this.outputStream.close();
834                 this.outputStream=null;
835             }
836             internalOffset=0;
837         }//try
838
catch (IOException JavaDoc exception) {
839             throw new JMSException JavaDoc("MNJMS0000F : AN IO EXCEPTION OCCURED IN reset() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
840         }//catch
841
}//reset
842

843     
844     /**
845      * Overide the super class method to reset the streams, and put the message
846      * body in write only mode.
847      *
848      * If clearBody is called on a message in read-only mode,
849      * the message body is cleared and the message is in write-only mode. bytes
850      * to the beginning.
851      *
852      * If clearBody is called on a message already in write-only
853      * mode, the spec does not define the outcome, we clear the message body for new use
854      *
855      * @throws JMSException if JMS fails to reset the message due to some internal JMS error
856      */

857     public final void clearBody() throws JMSException JavaDoc {
858         try {
859             
860             setWriteableState(true);
861             
862             if (outputStream != null) {
863                 //write-only
864
this.byteOutputStream = null;
865                 this.outputStream.close();
866                 this.outputStream = null;
867             }//if
868
// if ((flags&ORIG_BODY_SAVED)==0) {
869
// backupMessage=new byte[message.length];
870
// System.arraycopy(message,0,backupMessage,0,message.length);
871
// flags = flags | ORIG_BODY_SAVED;
872
// }
873
this.message = EMPTY;
874             this.internalOffset = 0;
875             this.byteOutputStream = null;
876             this.outputStream = null;
877         }//try
878
catch (IOException JavaDoc exception) {
879             throw new JMSException JavaDoc("MNJMS00010 : AN IO EXCEPTION OCCURED IN clearBody() METHOD. EXCEPTION TEXT : "+ exception.getMessage());
880         }//catch
881
}//clearBody
882

883     
884     /**
885      * Reverts the stream to its prior position if an I/O exception is thrown,
886      * and propagates the exception as a JMSException
887      *
888      * @param exception the exception that caused the reset
889      *
890      * @throws JMSException for general IOException errors
891      * @throws MessageEOFException if end-of-file was reached
892      */

893     private final void rollback(IOException JavaDoc exception) throws JMSException JavaDoc {
894         
895         internalOffset = 0;
896         
897         JMSException JavaDoc error = null;
898         String JavaDoc throwText = "MNJMS000BB : AN EXCEPTION OCCURED IN METHOD rollback(). EXCEPTION TEXT : "+exception.getMessage();
899         if (exception instanceof EOFException JavaDoc) {
900             error = new MessageEOFException JavaDoc(throwText);
901         }//if
902
else if (exception instanceof UTFDataFormatException JavaDoc) {
903             error = new MessageFormatException JavaDoc(throwText);
904         }//else if
905
else {
906             error = new JMSException JavaDoc(throwText);
907         }//else
908
error.setLinkedException(exception);
909         throw error;
910     }//rollback
911

912     
913     /*
914      * Some methods inside the implementation need to get the output stream
915      * to use it.
916      */

917     private DataOutputStream JavaDoc getOutputStream() throws IOException JavaDoc, JMSException JavaDoc {
918         
919         if (outputStream==null) {
920             byteOutputStream = new ByteArrayOutputStream JavaDoc();
921             outputStream = new DataOutputStream JavaDoc(this.byteOutputStream);
922             outputStream.write(this.message);
923         }
924                 
925         return outputStream;
926     }
927     
928     // methods for byteable object-----------------------------
929

930     /**
931      * Write this message to the network stream.
932      */

933     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
934         super.toBytes(out);
935         
936         if (this.byteOutputStream!=null || message==null) {
937             outputStream.flush();
938             message =byteOutputStream.toByteArray();
939             internalOffset=0;
940             byteOutputStream = null;
941         }
942         out.writeInt(message.length);
943         out.write(message);
944         // write the offset
945
out.writeInt(internalOffset);
946         
947     }
948     
949     /**
950      * Create this message from the network stream.
951      *
952      * @return A BytesMessage
953      */

954     
955     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
956         MantaBytesMessage cbm;
957         try {
958             cbm = new MantaBytesMessage(null);
959             
960             createHeadersAndProperties(cbm,in);
961             
962             byte[] msg = new byte[in.readInt()];
963             in.read(msg);
964             cbm.message = msg;
965             cbm.internalOffset = in.readInt();
966         }
967         catch (Exception JavaDoc ee) {
968             throw new IOException JavaDoc("MNJMS00FFF : METHOD createInstance() FAILED ON MantaBytesMessage. EXCEPTION TEXT : "+ee.getMessage());
969         }
970         
971         return cbm;
972     }
973     
974     /**
975      * Register this type in the MantaRay's byteable registry.
976      */

977     public void registerToByteableRegistry() {
978         ByteableRegistry.registerByteableFactory(getByteableName() , this);
979     }
980     
981     
982     /**
983      * Return the name for this Byteable object
984      */

985     public final static String JavaDoc BYTEABLENAME = "MantaBytesMsg";
986     public String JavaDoc getByteableName() {
987         
988         return BYTEABLENAME;
989     }
990     
991     /**
992      * Register this type in the MantaRay's byteable registry.
993      */

994     public static void register() throws JMSException JavaDoc{
995         MantaBytesMessage instance = new MantaBytesMessage(null);
996         instance.registerToByteableRegistry();
997     }
998
999 //--------------end of byteable methods
1000

1001
1002    /*
1003    * Used to change the input stream in order to allow for a Data input stream.
1004    */

1005
1006    private void changeInput() throws JMSException JavaDoc {
1007        if ((flags&IS_WRITEABLE)>0)
1008            throw new MessageNotReadableException JavaDoc("MNJMS00004 : INVALID OPERATION. MESSAGE IS MARKED AS WRITE-ONLY.");
1009            
1010        if(message == null && outputStream != null){
1011            try {
1012                outputStream.flush();
1013                message = byteOutputStream.toByteArray();
1014            } catch (IOException JavaDoc e) {
1015                throw new JMSException JavaDoc("MNJMS00011 : IO ERROR OCCURED ON METHOD changeInput(). ERROR TEXT : "+e.getMessage());
1016                
1017            }
1018            
1019        }
1020    } //changeInput
1021

1022    /**
1023     * Reads the next byte of data from this input stream. The value
1024     * byte is returned as an <code>int</code> in the range
1025     * <code>0</code> to <code>255</code>. If no byte is available
1026     * an exception is thrown.
1027     *
1028     * @return the next byte of data.
1029     *
1030     * @throws EOFException
1031     */

1032    private synchronized int read() throws EOFException JavaDoc {
1033        if(internalOffset < message.length)
1034            return (message[internalOffset++] & 0xff);
1035        else
1036            throw new EOFException JavaDoc("MNJMS00012 : END OF FILE ERROR IN METHOD read()");
1037    }
1038    
1039    
1040    /**
1041     * See the general contract of the <code>readFully</code>
1042     * method of <code>DataInput</code>.
1043     * <p>
1044     * Bytes
1045     * for this operation are read from the contained
1046     * input stream.
1047     *
1048     * @param b the buffer into which the data is read.
1049     * @param off the start offset of the data.
1050     * @param len the number of bytes to read.
1051     * @exception EOFException if this input stream reaches the end before
1052     * reading all the bytes.
1053     * @exception IOException if an I/O error occurs.
1054     * @see java.io.FilterInputStream#in
1055     */

1056    private synchronized final void readFully(byte b[], int off, int len) throws IOException JavaDoc {
1057        int count =0;
1058        if(off + len > b.length ||len+ internalOffset >message.length){
1059            throw new IOException JavaDoc("MNJMS00013 : BUFFER TOO SHORT FOR READING "+
1060                    b.length+" BYTES IN METHOD readFully().");
1061            
1062        }
1063        while(count != len){
1064            b[off+count]= message[internalOffset++];
1065            count++;
1066        }
1067    }
1068   /**
1069    * Returns the length of the message.
1070    */

1071   public long getBodyLength() throws JMSException JavaDoc {
1072      if ((flags & MantaMessage.IS_WRITEABLE) > 0)
1073          throw new MessageNotReadableException JavaDoc("MNJMS00004 : INVALID OPERATION. MESSAGE IS MARKED AS WRITE-ONLY.");
1074      
1075      return message.length;
1076   }
1077   
1078// protected void retrieveOldBody() {
1079
//
1080
// if ((flags&ORIG_BODY_SAVED) > 0) {
1081
// //this means that someone actually stored the original body.
1082
//
1083
//
1084
// if (backupMessage==null)
1085
// message=null;
1086
// else {
1087
// message=new byte[backupMessage.length];
1088
// System.arraycopy(backupMessage,0,message,0,backupMessage.length);
1089
// outputStream=null;
1090
// }
1091
// }
1092
// //else - the message was not touched, so it should still be alright.
1093
// }
1094

1095   public MantaMessage makeCopy() throws JMSException JavaDoc{
1096        MantaBytesMessage copy = new MantaBytesMessage();
1097        initCopy(copy);
1098        // The underlying byte message.
1099
if(message == null ||message == EMPTY ){
1100            if (byteOutputStream==null)
1101                copy.message = EMPTY;
1102            else
1103                copy.message=byteOutputStream.toByteArray();
1104            
1105        }else{
1106            copy.message = message;
1107        }
1108        //the current offset inside the message
1109
copy.internalOffset = internalOffset;
1110        return copy;
1111   }
1112   
1113}
1114
Popular Tags