KickJava   Java API By Example, From Geeks To Geeks.

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


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

84
85 import java.io.ByteArrayInputStream JavaDoc;
86 import java.io.ByteArrayOutputStream JavaDoc;
87 import java.io.DataInputStream JavaDoc;
88 import java.io.DataOutputStream JavaDoc;
89 import java.io.IOException JavaDoc;
90
91 import javax.jms.JMSException JavaDoc;
92 import javax.jms.MessageEOFException JavaDoc;
93 import javax.jms.MessageFormatException JavaDoc;
94 import javax.jms.MessageNotReadableException JavaDoc;
95 import javax.jms.MessageNotWriteableException JavaDoc;
96 import javax.jms.StreamMessage JavaDoc;
97
98 import org.mr.core.util.byteable.Byteable;
99 import org.mr.core.util.byteable.ByteableInputStream;
100 import org.mr.core.util.byteable.ByteableOutputStream;
101 import org.mr.core.util.byteable.ByteableRegistry;
102
103
104 public class MantaStreamMessage extends MantaMessage implements StreamMessage JavaDoc {
105
106     //An empty byte array for initialization.
107
private static final byte[] EMPTY = new byte[]{};
108     //The underlying byte message.
109
private byte[] message = EMPTY;
110    // private byte[] backupMessage;
111
//Streams and wrappers used for the message implementation.
112
private DataOutputStream JavaDoc outputStream = null;
113     private ByteArrayOutputStream JavaDoc byteOutputStream = null;
114     private DataInputStream JavaDoc inputStream = null;
115     private ByteArrayInputStream JavaDoc byteInputStream = null;
116     //the current offset inside the message
117
private int internalOffset = 0;
118     private boolean readingBytes = false;
119     
120     private byte[] keepForReadBytes;
121     /**
122     * only for makeCopy method use
123     */

124     MantaStreamMessage(){ }
125     
126     /**
127      * The constructor used by the MantaSession to create the message.
128      * @param sess - the session creating this message.
129      *
130      * @throws JMSException
131      */

132     public MantaStreamMessage(MantaSession sess) throws JMSException JavaDoc {
133         flags = (flags & ONLY_MSG_TYPES) | STREAM_MESSAGE;
134         creatingSession = sess;
135     } //MantaBytesMessage
136

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

146     public boolean readBoolean() throws JMSException JavaDoc {
147         
148         checkBytes();
149         boolean result = false;
150         //make sure it is readable
151
changeInput();
152         Object JavaDoc o = readNextValue();
153         if (o instanceof Boolean JavaDoc)
154            result = ((Boolean JavaDoc)o).booleanValue();
155         else if (o==null || o instanceof String JavaDoc)
156            result = Boolean.valueOf((String JavaDoc)o).booleanValue();
157         else
158             throw new MessageFormatException JavaDoc("MNJMS0008C : FAILED ON METHOD readBoolean(). CAN NOT CONVERT FROM "+o.getClass().getName());
159         return result;
160     } //readBoolean
161

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

172     public final byte readByte() throws JMSException JavaDoc {
173         
174         checkBytes();
175         
176         byte result = 0;
177         //make sure it is readable
178
changeInput();
179         Object JavaDoc o = readNextValue();
180         if (o instanceof Byte JavaDoc)
181            result = ((Byte JavaDoc) o).byteValue();
182         else if (o instanceof String JavaDoc)
183             try {
184                 result = Byte.parseByte((String JavaDoc) o);
185             }
186             catch (NumberFormatException JavaDoc nfe) {
187                 throw new MessageFormatException JavaDoc("MNJMS0008D : FAILED ON METHOD readByte(). CAN NOT CONVERT TO BYTE FROM "+o);
188             }
189         else if (o == null)
190             result = Byte.valueOf((String JavaDoc) o).byteValue();
191         else
192             throw new MessageFormatException JavaDoc("MNJMS0008E : FAILED ON METHOD readByte(). CAN NOT CONVERT TO BYTE FROM OBJECT OF TYPE "+o.getClass().getName());
193         
194         return result;
195     }//readByte
196

197
198     /**
199      * Read an Object from the stream message
200      *
201      * @return an object from the message stream.
202      *
203      * @throws JMSException if JMS fails to read message due to some internal JMS error
204      * @throws MessageEOFException if end of message stream
205      * @throws MessageNotReadableException if message is in write-only mode
206      */

207     public Object JavaDoc readObject() throws JMSException JavaDoc {
208        
209        checkBytes();
210        changeInput();
211        Object JavaDoc o = readNextValue();
212        
213        return o;
214     }
215     
216     /**
217      * Read a Short from the stream message
218      *
219      * @return the next short from the message stream
220      *
221      * @throws JMSException if JMS fails to read message due to some internal JMS error
222      * @throws MessageEOFException if end of message stream
223      * @throws MessageNotReadableException if message is in write-only mode
224      */

225     public final short readShort() throws JMSException JavaDoc {
226         
227         checkBytes();
228         
229         short result = 0;
230         //make sure it is readable
231
changeInput();
232         Object JavaDoc o = readNextValue();
233         if (o instanceof Short JavaDoc)
234             result = ((Short JavaDoc) o).shortValue();
235         else if (o instanceof Byte JavaDoc)
236             result = ((Byte JavaDoc) o).shortValue();
237         else if (o instanceof String JavaDoc)
238             try {
239                 result = Short.parseShort((String JavaDoc) o);
240             }
241             catch (NumberFormatException JavaDoc nfe) {
242                 throw new MessageFormatException JavaDoc("MNJMS0008F : FAILED ON METHOD readShort(). CAN NOT CONVERT TO SHORT FROM "+o);
243             }
244         else if (o==null)
245              result = Short.valueOf((String JavaDoc) o).shortValue();
246         else
247             throw new MessageFormatException JavaDoc("MNJMS00090 : FAILED ON METHOD readShort(). CAN NOT CONVERT TO SHORT FROM OBJECT OF TYPE " +o.getClass().getName());
248         
249         return result;
250     }//readShort
251

252
253     /**
254      * Read a Unicode character value from the message stream
255      *
256      * @return the next two bytes from the bytes message stream as a Unicode character
257      *
258      * @throws JMSException if JMS fails to read message due to some internal JMS error
259      * @throws MessageEOFException if end of message stream
260      * @throws MessageNotReadableException if message is in write-only mode
261      */

262     
263     public final char readChar() throws JMSException JavaDoc {
264         
265         checkBytes();
266         
267         char result = 0;
268         //make sure it is readable
269
changeInput();
270         Object JavaDoc o = readNextValue();
271         if (o == null)
272             throw new NullPointerException JavaDoc("MNJMS00091 : FAILED ON METHOD readChar(). A NULL VALUE WAS READ.");
273         else if (o instanceof Character JavaDoc)
274             result = ((Character JavaDoc) o).charValue();
275         else
276             throw new MessageFormatException JavaDoc("MNJMS00092 : FAILED ON METHOD readChar(). CAN NOT CONVERT TO CHAR FROM OBJECT OF TYPE "+o.getClass().getName());
277         
278         return result;
279     }//readChar
280

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

291     public final int readInt() throws JMSException JavaDoc {
292         
293         checkBytes();
294         
295         int result = 0;
296         //make sure it is readable
297
changeInput();
298         Object JavaDoc o = readNextValue();
299         if (o instanceof Integer JavaDoc)
300            result = ((Integer JavaDoc) o).intValue();
301         else if (o instanceof Short JavaDoc)
302            result = ((Short JavaDoc) o).intValue();
303         else if (o instanceof Byte JavaDoc)
304            result = ((Byte JavaDoc) o).intValue();
305         else if (o instanceof String JavaDoc)
306             try {
307                 result = Integer.parseInt((String JavaDoc)o);
308             }
309             catch (NumberFormatException JavaDoc nfe) {
310                 throw new MessageFormatException JavaDoc("MNJMS00093 : FAILED ON METHOD readInt(). CAN NOT CONVERT TO INTEGER FROM "+o);
311             }
312         else if (o==null)
313            result = Integer.valueOf((String JavaDoc) o).intValue();
314         else
315             throw new MessageFormatException JavaDoc("MNJMS00094 : FAILED ON METHOD readInt(). CAN NOT CONVERT TO INTEGER FROM OBJECT OF TYPE "+o.getClass().getName());
316         
317         return result;
318     }//readInt
319

320
321     /**
322      * Read a signed 64-bit integer from the message stream
323      *
324      * @return the next eight bytes from the message stream, interpreted as a long.
325      *
326      * @throws JMSException if JMS fails to read message due to some internal JMS error
327      * @throws MessageEOFException if end of message stream
328      * @throws MessageNotReadableException if message is in write-only mode
329      */

330     public final long readLong() throws JMSException JavaDoc {
331         
332         checkBytes();
333         
334         long result = 0;
335         //make sure it is readable
336
changeInput();
337         Object JavaDoc o = readNextValue();
338         if (o instanceof Long JavaDoc)
339            result = ((Long JavaDoc) o).longValue();
340         else if (o instanceof Integer JavaDoc)
341            result = ((Integer JavaDoc) o).longValue();
342         else if (o instanceof Short JavaDoc)
343            result = ((Short JavaDoc) o).longValue();
344         else if (o instanceof Byte JavaDoc)
345            result = ((Byte JavaDoc) o).longValue();
346         else if (o instanceof String JavaDoc)
347             try {
348                 result = Long.valueOf((String JavaDoc) o).longValue();
349             }
350             catch (NumberFormatException JavaDoc nfe) {
351                 throw new MessageFormatException JavaDoc("MNJMS00095 : FAILED ON METHOD readLong(). CAN NOT CONVERT TO LONG FROM "+o);
352             }
353         else
354             throw new MessageFormatException JavaDoc("MNJMS00096 : FAILED ON METHOD readLong(). CAN NOT CONVERT TO LONG FROM OBJECT OF TYPE "+o.getClass().getName());
355             
356         return result;
357     }//readLong
358

359
360     /**
361      * Read a float from the message stream
362      *
363      * @return the next four bytes from the message stream, interpreted as a float
364      * @throws JMSException if JMS fails to read message due to some internal JMS error
365      * @throws MessageEOFException if end of message stream
366      * @throws MessageNotReadableException if message is in write-only mode
367      */

368     public final float readFloat() throws JMSException JavaDoc {
369         
370         checkBytes();
371         
372         float result = 0;
373         //make sure it is readable
374
changeInput();
375         Object JavaDoc o = readNextValue();
376         if (o instanceof Float JavaDoc)
377            result = ((Float JavaDoc) o).floatValue();
378         else if (o instanceof String JavaDoc)
379             try {
380                 result = Float.parseFloat((String JavaDoc) o);
381             }
382             catch(NumberFormatException JavaDoc nfe) {
383                 throw new MessageFormatException JavaDoc("MNJMS00097 : FAILED ON METHOD readFloat(). CAN NOT CONVERT TO FLOAT FROM "+o);
384             }
385         else if (o == null)
386            result = Float.valueOf((String JavaDoc) o).floatValue();
387         else
388             throw new MessageFormatException JavaDoc("MNJMS00098 : FAILED ON METHOD readFloat(). CAN NOT CONVERT TO FLOAT FROM OBJECT OF TYPE "+o.getClass().getName());
389         
390         return result;
391     }//readFloat
392

393
394     /**
395      * Read a double from the message stream
396      *
397      * @return the next eight bytes from the message stream, interpreted as a double
398      *
399      * @throws JMSException if JMS fails to read message due to some internal JMS error
400      * @throws MessageEOFException if end of message stream
401      * @throws MessageNotReadableException if message is in write-only mode
402      */

403     public final double readDouble() throws JMSException JavaDoc {
404         
405         checkBytes();
406         
407         double result = 0;
408         //make sure it is readable
409
changeInput();
410         Object JavaDoc o = readNextValue();
411         if (o instanceof Double JavaDoc)
412            result = ((Double JavaDoc) o).doubleValue();
413         else if (o instanceof Float JavaDoc)
414            result = ((Float JavaDoc) o).doubleValue();
415         else if (o instanceof String JavaDoc)
416             try {
417                 result = Double.parseDouble((String JavaDoc) o);
418             }
419             catch (NumberFormatException JavaDoc nfe) {
420                 
421                 throw new MessageFormatException JavaDoc("MNJMS00099 : FAILED ON METHOD readDouble(). CAN NOT CONVERT TO DOUBLE FROM "+o);
422             }
423         else if (o == null)
424            result = Double.valueOf((String JavaDoc) o).doubleValue();
425         else
426             throw new MessageFormatException JavaDoc("MNJMS0009A : FAILED ON METHOD readDouble(). CAN NOT CONVERT TO DOUBLE FROM OBJECT OF TYPE "+o.getClass().getName());
427             
428         return result;
429     }//readDouble
430

431
432     /**
433      * Read in a string that has been encoded using a modified UTF-8 format
434      * from the bytes message stream
435      *
436      *
437      * @return a Unicode string from the message stream
438      *
439      * @throws JMSException if JMS fails to read message due to some internal JMS error
440      * @throws MessageEOFException if end of message stream
441      * @throws MessageFormatException if string has an invalid format
442      * @throws MessageNotReadableException if message is in write-only mode
443      */

444     public final String JavaDoc readString() throws JMSException JavaDoc {
445         
446         checkBytes();
447         
448         String JavaDoc result = null;
449         //make sure it is readable
450
changeInput();
451         Object JavaDoc o = readNextValue();
452         if (o instanceof byte[])
453            throw new MessageFormatException JavaDoc("MNJMS0009B : FAILED ON METHOD readString(). CAN NOT CONVERT A BYTE ARRAY TO STRING.");
454         else if (o!=null)
455             result=String.valueOf(o);
456         return result;
457     }//readUTF
458

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

481     public final int readBytes(byte[] value) throws JMSException JavaDoc {
482       
483           //if we are not in the middle - read a byte[].
484
if (!readingBytes) {
485               
486               changeInput();
487               Object JavaDoc o = readNextValue();
488               if (o==null)
489                   throw new MessageFormatException JavaDoc("MNJMS0009C : FAILED ON METHOD readBytes(). A NULL VALUE WAS SPECIFIED.");
490               else if (o instanceof byte[])
491                   keepForReadBytes = (byte[])o; //this is our byte[] feeder now.
492
else //no byte[] or null
493
throw new MessageFormatException JavaDoc("MNJMS0009D : FAILED ON METHOD readBytes(). CAN NOT CONVERT TO BYTE ARRAY FROM "+o.getClass().getName());
494                 
495           }
496         
497           if (keepForReadBytes.length<=value.length) {
498               System.arraycopy(keepForReadBytes,0,value,0,keepForReadBytes.length);
499               readingBytes = false;
500               //this is because we read a shorter array.
501
return keepForReadBytes.length;
502               //nothing is happening now - all the byte[] was consumed.
503
}
504           else {
505               //we have a smaller target then the data.
506
//so - we are still reading bytes.
507
System.arraycopy(keepForReadBytes,0,value,0,value.length);
508               readingBytes=true;
509               int remainder =keepForReadBytes.length - value.length;
510               byte[] temp = new byte[remainder];
511               System.arraycopy(keepForReadBytes,value.length,temp,0,remainder);
512               keepForReadBytes = temp;
513               return value.length;
514           }
515           
516        }//readBytes
517

518     /**
519      * Write a boolean to the message stream as a 1-byte
520      * value. The value true is written out as the value (byte)1;
521      * the value false is written out as the value (byte)0.
522      *
523      * @param value the boolean value to be written
524      *
525      * @throws JMSException if JMS fails to write message due to some internal JMS error
526      * @throws MessageNotWriteableException if message is in read-only mode
527      */

528     public final void writeBoolean(boolean value) throws JMSException JavaDoc {
529         //Check that it is allowed to write on this message.
530
checkWriteableState(false);
531         try {
532            getOutputStream().writeByte(BOOLEAN);
533             getOutputStream().writeBoolean(value);
534         }//try
535
catch (IOException JavaDoc exception) {
536             throw new JMSException JavaDoc("MNJMS0009E : FAILED ON METHOD writeBoolean(). ERROR TEXT : "+exception.getMessage());
537         }//catch
538
}//writeBoolean
539

540
541     /**
542      * Write out a byte to the message stream as a 1-byte value
543      *
544      * @param value the byte value to be written
545      *
546      * @throws JMSException if JMS fails to write message due to some internal JMS error
547      * @throws MessageNotWriteableException if message is in read-only mode
548      */

549     public final void writeByte(byte value) throws JMSException JavaDoc {
550         //Check that it is allowed to write on this message.
551
checkWriteableState(false);
552         try {
553            getOutputStream().writeByte(BYTE);
554            getOutputStream().writeByte(value);
555             
556         }//try
557
catch (IOException JavaDoc exception) {
558             throw new JMSException JavaDoc("MNJMS0009F : FAILED ON METHOD writeByte(). ERROR TEXT : "+exception.getMessage());
559         }//catch
560
}//writeByte
561

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

571     public final void writeShort(short value) throws JMSException JavaDoc {
572         //Check that it is allowed to write on this message.
573
checkWriteableState(false);
574         try {
575            getOutputStream().writeByte(SHORT);
576             getOutputStream().writeShort(value);
577         }//try
578
catch (IOException JavaDoc exception) {
579             throw new JMSException JavaDoc("MNJMS000A0 : FAILED ON METHOD writeByte(). ERROR TEXT : "+exception.getMessage());
580             
581         }//catch
582
}//writeShort
583

584     
585     /**
586      * Write a char to the message stream as a 2-byte value, high byte first.
587      *
588      * @param value the char value to be written
589      *
590      * @throws MessageNotWriteableException if message is in read-only mode
591      * @throws JMSException if JMS fails to write message due to some internal JMS error
592      */

593     public final void writeChar(char value) throws JMSException JavaDoc {
594         //Check that it is allowed to write on this message.
595
checkWriteableState(false);
596         try {
597            getOutputStream().writeByte(CHAR);
598             getOutputStream().writeChar(value);
599         }//try
600
catch (IOException JavaDoc exception) {
601             throw new JMSException JavaDoc("MNJMS000A1 : FAILED ON METHOD writeChar(). ERROR TEXT : "+exception.getMessage());
602         }//catch
603
}//writeChar
604

605     
606     /**
607      * Write an int to the message stream as four bytes, high byte first.
608      *
609      * @param value the int to be written
610      *
611      * @throws JMSException if JMS fails to write message due to some internal JMS error
612      * @throws MessageNotWriteableException if message is in read-only mode
613      */

614     public final void writeInt(int value) throws JMSException JavaDoc {
615         //Check that it is allowed to write on this message.
616
checkWriteableState(false);
617         try {
618            getOutputStream().writeByte(MantaMessage.INTEGER);
619             getOutputStream().writeInt(value);
620         }//try
621
catch (IOException JavaDoc exception) {
622             throw new JMSException JavaDoc("MNJMS000A2 : FAILED ON METHOD writeInt(). ERROR TEXT : "+exception.getMessage());
623         }//catch
624
}//writeInt
625

626     
627     /**
628      * Write a long to the message stream as eight bytes, high byte first
629      *
630      * @param value the long to be written
631      * @throws JMSException if JMS fails to write message due to some internal JMS error
632      * @throws MessageNotWriteableException if message is in read-only mode
633      */

634     public final void writeLong(long value) throws JMSException JavaDoc {
635         //Check that it is allowed to write on this message.
636
checkWriteableState(false);
637         try {
638            getOutputStream().writeByte(LONG);
639             getOutputStream().writeLong(value);
640         }//try
641
catch (IOException JavaDoc exception) {
642             throw new JMSException JavaDoc("MNJMS000A3 : FAILED ON METHOD writeLong(). ERROR TEXT : "+exception.getMessage());
643         }//catch
644
}//writeLong
645

646     
647     /**
648      * Convert the float argument to an int using the floatToIntBits
649      * method in class Float, and then writes that int
650      * value to the message stream as a 4-byte quantity, high byte first.
651      *
652      * @param value the float value to be written.
653      *
654      * @throws JMSException if JMS fails to write message due to some internal JMS error
655      * @throws MessageNotWriteableException if message is in read-only mode
656      */

657     public final void writeFloat(float value) throws JMSException JavaDoc {
658         //Check that it is allowed to write on this message.
659
checkWriteableState(false);
660         try {
661            getOutputStream().writeByte(FLOAT);
662             getOutputStream().writeFloat(value);
663         }//try
664
catch (IOException JavaDoc exception) {
665             throw new JMSException JavaDoc("MNJMS000A4 : FAILED ON METHOD writeFloat(). ERROR TEXT : "+exception.getMessage());
666         }//catch
667
}//writeFloat
668

669     
670     /**
671      * Convert the double argument to a long using the doubleToLongBits
672      * method in class Double, and then writes that long
673      * value to the message stream as an 8-byte quantity, high byte
674      * first.
675      *
676      * @param value the double value to be written.
677      *
678      * @throws JMSException if JMS fails to write message due to some internal JMS error
679      * @throws MessageNotWriteableException if message is in read-only mode
680      */

681     public final void writeDouble(double value) throws JMSException JavaDoc {
682         //Check that it is allowed to write on this message.
683
checkWriteableState(false);
684         try {
685            getOutputStream().writeByte(DOUBLE);
686             getOutputStream().writeDouble(value);
687         }//try
688
catch (IOException JavaDoc exception) {
689             throw new JMSException JavaDoc("MNJMS000A5 : FAILED ON METHOD writeDouble(). ERROR TEXT : "+exception.getMessage());
690         }//catch
691
}//writeDouble
692

693     
694     /**
695      * Write a string to the message stream using UTF-8 encoding in a
696      * machine-independent manner.
697      *
698      *
699      * @param value the String value to be written
700      * @throws MessageNotWriteableException if message is in read-only mode
701      * @throws JMSException if JMS fails to write message due to some internal JMS error
702      */

703     public final void writeString(String JavaDoc value) throws JMSException JavaDoc {
704         //Check that it is allowed to write on this message.
705
checkWriteableState(false);
706         try {
707            getOutputStream().writeByte(STRING);
708             getOutputStream().writeUTF(value);
709         }//try
710
catch (IOException JavaDoc exception) {
711             throw new JMSException JavaDoc("MNJMS000A6 : FAILED ON METHOD writeString(). ERROR TEXT : "+exception.getMessage());
712         }//catch
713
}//wrtieUTF
714

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

723     public final void writeBytes(byte[] value) throws JMSException JavaDoc {
724         //Check that it is allowed to write on this message.
725
checkWriteableState(false);
726         try {
727            getOutputStream().writeByte(BYTEARR);
728            getOutputStream().writeInt(value.length);
729             getOutputStream().write(value);
730         }//try
731
catch (IOException JavaDoc exception) {
732             throw new JMSException JavaDoc("MNJMS000A7 : FAILED ON METHOD writeBytes(). ERROR TEXT : "+exception.getMessage());
733         }//catch
734
}//writeBytes
735

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

747     public final void writeBytes(byte[] value, int offset, int length) throws JMSException JavaDoc {
748         //Check that it is allowed to write on this message.
749
checkWriteableState(false);
750         try {
751            getOutputStream().writeByte(BYTEARR);
752            getOutputStream().writeInt(length);
753            getOutputStream().write(value, offset, length);
754         }//try
755
catch (IOException JavaDoc exception) {
756             throw new JMSException JavaDoc("MNJMS000A8 : FAILED ON METHOD writeBytes(). ERROR TEXT : "+exception.getMessage());
757         }//catch
758
}//wrtieBytes
759

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

774     public final void writeObject(Object JavaDoc value) throws JMSException JavaDoc {
775         //Check that it is allowed to write on this message.
776
checkWriteableState(false);
777         
778         //See which object we're dealing with
779
if (value == null) {
780             throw new NullPointerException JavaDoc("MNJMS000A9 : FAILED ON METHOD writeObject(). NULL VALUE SPECIFIED.");
781         }
782         else if (value instanceof Boolean JavaDoc) {
783             writeBoolean(((Boolean JavaDoc) value).booleanValue());
784         }//if
785
else if (value instanceof Byte JavaDoc) {
786             writeByte(((Byte JavaDoc) value).byteValue());
787         }//else if
788
else if (value instanceof Short JavaDoc) {
789             writeShort(((Short JavaDoc) value).shortValue());
790         }//else if
791
else if (value instanceof Character JavaDoc) {
792             writeChar(((Character JavaDoc) value).charValue());
793         }//else if
794
else if (value instanceof Integer JavaDoc) {
795             writeInt(((Integer JavaDoc) value).intValue());
796         }//else if
797
else if (value instanceof Long JavaDoc) {
798             writeLong(((Long JavaDoc) value).longValue());
799         }//else if
800
else if (value instanceof Float JavaDoc) {
801             writeFloat(((Float JavaDoc) value).floatValue());
802         }//else if
803
else if (value instanceof Double JavaDoc) {
804             writeDouble(((Double JavaDoc) value).doubleValue());
805         }//else if
806
else if (value instanceof String JavaDoc) {
807             writeString((String JavaDoc) value);
808         }//else if
809
else if (value instanceof byte[]) {
810             writeBytes((byte[]) value);
811         }//else if
812
else {
813             throw new MessageFormatException JavaDoc("MNJMS000AA : FAILED ON METHOD writeObject(). TYPE "+value.getClass().getName()+" IS NOT ALLOWED.");
814         }//else
815
} //writeObject
816

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

824     public final void reset() throws JMSException JavaDoc {
825         
826         try {
827             if (this.isWriteable())
828                 this.setWriteableState(false);
829             if (outputStream != null) {
830                 outputStream.flush();
831                 message = this.byteOutputStream.toByteArray();
832                 byteOutputStream = null;
833                 outputStream.close();
834                 outputStream=null;
835             }
836             
837             else
838                if (this.inputStream!=null) {
839                     this.byteInputStream=null;
840                     this.inputStream.close();
841                     this.inputStream = null;
842                }
843                     
844         }//try
845
catch (IOException JavaDoc exception) {
846             throw new JMSException JavaDoc("MNJMS000AB : FAILED ON METHOD reset(). ERROR TEXT : "+exception.getMessage());
847         }//catch
848
}//reset
849

850     
851     /**
852      * Overide the super class method to reset the streams, and put the message
853      * body in write only mode.
854      *
855      * If clearBody is called on a message in read-only mode,
856      * the message body is cleared and the message is in write-only mode. bytes
857      * to the beginning.
858      *
859      * If clearBody is called on a message already in write-only
860      * mode, the spec does not define the outcome, so do nothing. Client must
861      * then call reset, followed by clearBody to
862      * reset the stream at the beginning for a new write.
863      *
864      * @throws JMSException if JMS fails to reset the message due to some internal JMS error
865      */

866     public final void clearBody() throws JMSException JavaDoc {
867         try {
868             if (!this.isWriteable()) {
869                 // in read-only mode
870
this.setWriteableState(true);
871                 if (inputStream != null) {
872                     byteInputStream = null;
873                     inputStream.close();
874                     inputStream = null;
875                     this.internalOffset = 0;
876                 } //if
877
}//if
878
else if (outputStream != null) {
879                 this.byteOutputStream = null;
880                 this.outputStream.close();
881                 this.outputStream = null;
882             }//else if
883
//
884
// if ((flags&ORIG_BODY_SAVED)==0) {
885
// backupMessage = new byte[message.length];
886
// System.arraycopy(message,0,backupMessage,0,message.length);
887
flags=flags|ORIG_BODY_SAVED;
888 // }
889
this.message = EMPTY;
890             this.byteOutputStream = null;
891             this.outputStream = null;
892         }//try
893
catch (IOException JavaDoc exception) {
894             throw new JMSException JavaDoc("MNJMS000AC : FAILED ON METHOD clearBody(). ERROR TEXT : "+exception.getMessage());
895         }//catch
896
}//clearBody
897

898     
899     
900     /*
901      * Makes the stream available for writing.
902      */

903     private DataOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
904         if (this.outputStream==null) {
905             this.byteOutputStream = new ByteArrayOutputStream JavaDoc();
906             this.outputStream = new DataOutputStream JavaDoc(this.byteOutputStream);
907             this.outputStream.write(this.message);
908         }
909                 
910         return this.outputStream;
911     }
912     
913     
914     // methods for byteable object-----------------------------
915
public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
916         super.toBytes(out);
917         if(message == null)
918             message = this.byteOutputStream.toByteArray();
919         out.writeInt(message.length);
920         out.write(message);
921         out.flush();
922     }
923     
924     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
925         MantaStreamMessage msm;
926         try {
927             msm = new MantaStreamMessage(null);
928             msm.byteOutputStream = new ByteArrayOutputStream JavaDoc();
929             createHeadersAndProperties(msm,in);
930             byte[] cbmInputStream = new byte[in.readInt()];
931             in.read(cbmInputStream);
932             msm.inputStream = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(cbmInputStream));
933             msm.message =cbmInputStream;
934         }
935         catch (Exception JavaDoc ee) {
936             throw new IOException JavaDoc("MNJMS00FFB : METHOD createInstance() FAILED ON MantaStreamMessage. EXCEPTION TEXT : "+ee.getMessage());
937         }
938         
939         return msm;
940     }
941     
942 // protected void retrieveOldBody() {
943
//
944
// if ((flags&ORIG_BODY_SAVED)>0) {
945
// //someone actually cleared the body here
946
//
947
// if (backupMessage==null)
948
// message=null;
949
// else {
950
// message = new byte[backupMessage.length];
951
// System.arraycopy(backupMessage,0,message,0,backupMessage.length);
952
// outputStream=null;
953
// }
954
// }
955
// //else - message was probably not touched.
956
//
957
//
958
// }
959
//
960
public void registerToByteableRegistry() {
961         ByteableRegistry.registerByteableFactory(getByteableName() , this);
962     }
963     
964     
965     public final static String JavaDoc BYTEABLENAME = "MantaStreamMsg";
966     
967     public String JavaDoc getByteableName() {
968         
969         return BYTEABLENAME;
970     }
971     
972     public static void register() throws JMSException JavaDoc{
973         MantaStreamMessage instance = new MantaStreamMessage(null);
974         instance.registerToByteableRegistry();
975     }
976
977 //--------------end of byteable methods
978

979
980     private void checkBytes() throws JMSException JavaDoc {
981         if (readingBytes)
982             throw new MessageFormatException JavaDoc("MNJMS000AD : FAILED ON METHOD checkBytes(). STREAM MESSAGE IS IN A readBytes() SEQUENCE.");
983             
984     }
985     
986     /*
987     * Used to change the input stream in order to allow for a Data input stream.
988     */

989
990     private void changeInput() throws JMSException JavaDoc {
991         if ((flags&IS_WRITEABLE)>0)
992             throw new MessageNotReadableException JavaDoc("MNJMS000AE : FAILED ON METHOD changeInput(). MESSAGE IS IN WRITEABLE STATE.");
993                   
994         if (this.inputStream==null) {
995             this.byteInputStream = new ByteArrayInputStream JavaDoc(message,this.internalOffset,
996                     message.length-internalOffset);
997             inputStream = new DataInputStream JavaDoc(byteInputStream);
998         }
999         try {
1000            inputStream.mark(this.message.length - inputStream.available());
1001        }
1002        catch (IOException JavaDoc ioe) {
1003            throw new JMSException JavaDoc("MNJMS000AF : FAILED ON METHOD changeInput(). ERROR TEXT : "+ioe.getMessage());
1004        }
1005    } //changeInput
1006

1007
1008   /**
1009    * Gets the length of the message's body.
1010    *
1011    * @return the length of the message
1012    *
1013    * @throws JMSException
1014    */

1015   public long getBodyLength() throws JMSException JavaDoc {
1016      
1017      return this.message.length;
1018   }
1019
1020   
1021   //constants for all the types.
1022

1023   
1024   /*
1025    * reads a value from the stream.
1026    *
1027    * @return an object, which is the next value from the stream.
1028    */

1029   private Object JavaDoc readNextValue() throws JMSException JavaDoc {
1030      
1031      Object JavaDoc ret = null;
1032      byte objectType=0;
1033      try {
1034         objectType = inputStream.readByte();
1035      
1036         switch (objectType) {
1037         case STRING:
1038            ret = inputStream.readUTF();
1039            break;
1040         case LONG:
1041            ret = new Long JavaDoc(inputStream.readLong());
1042            break;
1043         case FLOAT:
1044            ret = new Float JavaDoc(inputStream.readFloat());
1045            break;
1046         case DOUBLE:
1047            ret = new Double JavaDoc(inputStream.readDouble());
1048            break;
1049         case CHAR:
1050            ret = new Character JavaDoc(inputStream.readChar());
1051            break;
1052         case BYTE:
1053            ret = new Byte JavaDoc(inputStream.readByte());
1054            break;
1055         case BYTEARR:
1056            int len = inputStream.readInt();
1057            byte[] retBytes = new byte[len];
1058            inputStream.readFully(retBytes);
1059            ret = retBytes;
1060            break;
1061         case INTEGER:
1062            ret = new Integer JavaDoc(inputStream.readInt());
1063            break;
1064         case SHORT:
1065            ret = new Short JavaDoc(inputStream.readShort());
1066            break;
1067         case BOOLEAN:
1068            ret = new Boolean JavaDoc(inputStream.readBoolean());
1069            break;
1070         }
1071      }
1072      catch (Exception JavaDoc ig) {
1073         throw new MessageEOFException JavaDoc("MNJMS000B0 : FAILED ON METHOD readNextValue(). STREAM END REACHED.");
1074      }
1075      
1076      return ret;
1077            
1078   }
1079   
1080   public MantaMessage makeCopy() throws JMSException JavaDoc{
1081        MantaStreamMessage copy = new MantaStreamMessage();
1082        initCopy(copy);
1083        // The underlying byte message.
1084
// The underlying byte message.
1085
if(message == null ||message == EMPTY ){
1086            if (byteOutputStream==null)
1087                copy.message = EMPTY;
1088            else
1089                copy.message=byteOutputStream.toByteArray();
1090        }else{
1091            copy.message = message;
1092        }
1093        
1094        copy.internalOffset = internalOffset;
1095        
1096        return copy;
1097   }//makeCopy
1098

1099}
1100   
1101
Popular Tags