KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQBytesMessage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20 import org.apache.activemq.ActiveMQConnection;
21 import org.apache.activemq.util.ByteArrayInputStream;
22 import org.apache.activemq.util.ByteArrayOutputStream;
23 import org.apache.activemq.util.*;
24
25 import javax.jms.*;
26 import java.io.*;
27 import java.util.zip.Deflater JavaDoc;
28 import java.util.zip.DeflaterOutputStream JavaDoc;
29 import java.util.zip.InflaterInputStream JavaDoc;
30
31 /**
32  * A <CODE>BytesMessage</CODE> object is used to send a message containing a stream of uninterpreted bytes. It inherits
33  * from the <CODE>Message</CODE> interface and adds a bytes message body. The receiver of the message supplies the
34  * interpretation of the bytes. <P> The <CODE>BytesMessage</CODE> methods are based largely on those found in
35  * <CODE>java.io.DataInputStream</CODE> and <CODE>java.io.DataOutputStream</CODE>. <P> This message type is for client
36  * encoding of existing message formats. If possible, one of the other self-defining message types should be used
37  * instead. <P> Although the JMS API allows the use of message properties with byte messages, they are typically not
38  * used, since the inclusion of properties may affect the format. <P> The primitive types can be written explicitly
39  * using methods for each type. They may also be written generically as objects. For instance, a call to
40  * <CODE>BytesMessage.writeInt(6)</CODE> is equivalent to <CODE> BytesMessage.writeObject(new Integer(6))</CODE>. Both
41  * forms are provided, because the explicit form is convenient for static programming, and the object form is needed
42  * when types are not known at compile time. <P> When the message is first created, and when <CODE>clearBody</CODE> is
43  * called, the body of the message is in write-only mode. After the first call to <CODE>reset</CODE> has been made, the
44  * message body is in read-only mode. After a message has been sent, the client that sent it can retain and modify it
45  * without affecting the message that has been sent. The same message object can be sent multiple times. When a message
46  * has been received, the provider has called <CODE>reset</CODE> so that the message body is in read-only mode for the
47  * client. <P> If <CODE>clearBody</CODE> is called on a message in read-only mode, the message body is cleared and the
48  * message is in write-only mode. <P> If a client attempts to read a message in write-only mode, a
49  * <CODE>MessageNotReadableException</CODE> is thrown. <P> If a client attempts to write a message in read-only mode, a
50  * <CODE>MessageNotWriteableException</CODE> is thrown.
51  *
52  * @openwire:marshaller code=24
53  * @see javax.jms.Session#createBytesMessage()
54  * @see javax.jms.MapMessage
55  * @see javax.jms.Message
56  * @see javax.jms.ObjectMessage
57  * @see javax.jms.StreamMessage
58  * @see javax.jms.TextMessage
59  */

60 public class ActiveMQBytesMessage extends ActiveMQMessage implements BytesMessage {
61
62     public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_BYTES_MESSAGE;
63
64     transient protected DataOutputStream dataOut;
65     transient protected ByteArrayOutputStream bytesOut;
66     transient protected DataInputStream dataIn;
67     transient protected int length;
68
69     public Message copy() {
70         ActiveMQBytesMessage copy = new ActiveMQBytesMessage();
71         copy(copy);
72         return copy;
73     }
74
75     private void copy(ActiveMQBytesMessage copy) {
76         storeContent();
77         super.copy(copy);
78         copy.dataOut = null;
79         copy.bytesOut = null;
80         copy.dataIn = null;
81     }
82     
83     public void onSend() throws JMSException {
84         super.onSend();
85         storeContent();
86     }
87
88     private void storeContent() {
89         try {
90             if (dataOut != null) {
91                 dataOut.close();
92                 ByteSequence bs = bytesOut.toByteSequence();
93                 if( compressed ) {
94                     int pos = bs.offset;
95                     ByteSequenceData.writeIntBig(bs, length);
96                     bs.offset = pos;
97                 }
98                 setContent(bs);
99                 bytesOut = null;
100                 dataOut = null;
101             }
102         } catch (IOException ioe) {
103             throw new RuntimeException JavaDoc(ioe.getMessage(), ioe); //TODO verify RuntimeException
104
}
105     }
106
107     public byte getDataStructureType() {
108         return DATA_STRUCTURE_TYPE;
109     }
110     
111     public String JavaDoc getJMSXMimeType() {
112         return "jms/bytes-message";
113     }
114
115
116     /**
117      * Clears out the message body. Clearing a message's body does not clear its header values or property entries. <P>
118      * If this message body was read-only, calling this method leaves the message body in the same state as an empty
119      * body in a newly created message.
120      *
121      * @throws JMSException if the JMS provider fails to clear the message body due to some internal error.
122      */

123     public void clearBody() throws JMSException {
124         super.clearBody();
125         this.dataOut = null;
126         this.dataIn = null;
127         this.bytesOut = null;
128     }
129
130     /**
131      * Gets the number of bytes of the message body when the message is in read-only mode. The value returned can be
132      * used to allocate a byte array. The value returned is the entire length of the message body, regardless of where
133      * the pointer for reading the message is currently located.
134      *
135      * @return number of bytes in the message
136      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
137      * @throws MessageNotReadableException if the message is in write-only mode.
138      * @since 1.1
139      */

140
141     public long getBodyLength() throws JMSException {
142         initializeReading();
143         return length;
144     }
145
146     /**
147      * Reads a <code>boolean</code> from the bytes message stream.
148      *
149      * @return the <code>boolean</code> value read
150      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
151      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
152      * @throws MessageNotReadableException if the message is in write-only mode.
153      */

154     public boolean readBoolean() throws JMSException {
155         initializeReading();
156         try {
157             return this.dataIn.readBoolean();
158         } catch (EOFException e) {
159             throw JMSExceptionSupport.createMessageEOFException(e);
160         } catch (IOException e) {
161             throw JMSExceptionSupport.createMessageFormatException(e);
162         }
163     }
164
165     /**
166      * Reads a signed 8-bit value from the bytes message stream.
167      *
168      * @return the next byte from the bytes message stream as a signed 8-bit <code>byte</code>
169      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
170      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
171      * @throws MessageNotReadableException if the message is in write-only mode.
172      */

173     public byte readByte() throws JMSException {
174         initializeReading();
175         try {
176             return this.dataIn.readByte();
177         } catch (EOFException e) {
178             throw JMSExceptionSupport.createMessageEOFException(e);
179         } catch (IOException e) {
180             throw JMSExceptionSupport.createMessageFormatException(e);
181         }
182     }
183
184     /**
185      * Reads an unsigned 8-bit number from the bytes message stream.
186      *
187      * @return the next byte from the bytes message stream, interpreted as an unsigned 8-bit number
188      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
189      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
190      * @throws MessageNotReadableException if the message is in write-only mode.
191      */

192     public int readUnsignedByte() throws JMSException {
193         initializeReading();
194         try {
195             return this.dataIn.readUnsignedByte();
196         } catch (EOFException e) {
197             throw JMSExceptionSupport.createMessageEOFException(e);
198         } catch (IOException e) {
199             throw JMSExceptionSupport.createMessageFormatException(e);
200         }
201     }
202
203     /**
204      * Reads a signed 16-bit number from the bytes message stream.
205      *
206      * @return the next two bytes from the bytes message stream, interpreted as a signed 16-bit number
207      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
208      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
209      * @throws MessageNotReadableException if the message is in write-only mode.
210      */

211     public short readShort() throws JMSException {
212         initializeReading();
213         try {
214             return this.dataIn.readShort();
215         } catch (EOFException e) {
216             throw JMSExceptionSupport.createMessageEOFException(e);
217         } catch (IOException e) {
218             throw JMSExceptionSupport.createMessageFormatException(e);
219         }
220     }
221
222     /**
223      * Reads an unsigned 16-bit number from the bytes message stream.
224      *
225      * @return the next two bytes from the bytes message stream, interpreted as an unsigned 16-bit integer
226      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
227      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
228      * @throws MessageNotReadableException if the message is in write-only mode.
229      */

230     public int readUnsignedShort() throws JMSException {
231         initializeReading();
232         try {
233             return this.dataIn.readUnsignedShort();
234         } catch (EOFException e) {
235             throw JMSExceptionSupport.createMessageEOFException(e);
236         } catch (IOException e) {
237             throw JMSExceptionSupport.createMessageFormatException(e);
238         }
239     }
240
241     /**
242      * Reads a Unicode character value from the bytes message stream.
243      *
244      * @return the next two bytes from the bytes message stream as a Unicode character
245      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
246      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
247      * @throws MessageNotReadableException if the message is in write-only mode.
248      */

249     public char readChar() throws JMSException {
250         initializeReading();
251         try {
252             return this.dataIn.readChar();
253         } catch (EOFException e) {
254             throw JMSExceptionSupport.createMessageEOFException(e);
255         } catch (IOException e) {
256             throw JMSExceptionSupport.createMessageFormatException(e);
257         }
258     }
259
260     /**
261      * Reads a signed 32-bit integer from the bytes message stream.
262      *
263      * @return the next four bytes from the bytes message stream, interpreted as an <code>int</code>
264      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
265      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
266      * @throws MessageNotReadableException if the message is in write-only mode.
267      */

268     public int readInt() throws JMSException {
269         initializeReading();
270         try {
271             return this.dataIn.readInt();
272         } catch (EOFException e) {
273             throw JMSExceptionSupport.createMessageEOFException(e);
274         } catch (IOException e) {
275             throw JMSExceptionSupport.createMessageFormatException(e);
276         }
277     }
278
279     /**
280      * Reads a signed 64-bit integer from the bytes message stream.
281      *
282      * @return the next eight bytes from the bytes message stream, interpreted as a <code>long</code>
283      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
284      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
285      * @throws MessageNotReadableException if the message is in write-only mode.
286      */

287     public long readLong() throws JMSException {
288         initializeReading();
289         try {
290             return this.dataIn.readLong();
291         } catch (EOFException e) {
292             throw JMSExceptionSupport.createMessageEOFException(e);
293         } catch (IOException e) {
294             throw JMSExceptionSupport.createMessageFormatException(e);
295         }
296     }
297
298     /**
299      * Reads a <code>float</code> from the bytes message stream.
300      *
301      * @return the next four bytes from the bytes message stream, interpreted as a <code>float</code>
302      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
303      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
304      * @throws MessageNotReadableException if the message is in write-only mode.
305      */

306     public float readFloat() throws JMSException {
307         initializeReading();
308         try {
309             return this.dataIn.readFloat();
310         } catch (EOFException e) {
311             throw JMSExceptionSupport.createMessageEOFException(e);
312         } catch (IOException e) {
313             throw JMSExceptionSupport.createMessageFormatException(e);
314         }
315     }
316
317     /**
318      * Reads a <code>double</code> from the bytes message stream.
319      *
320      * @return the next eight bytes from the bytes message stream, interpreted as a <code>double</code>
321      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
322      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
323      * @throws MessageNotReadableException if the message is in write-only mode.
324      */

325     public double readDouble() throws JMSException {
326         initializeReading();
327         try {
328             return this.dataIn.readDouble();
329         } catch (EOFException e) {
330             throw JMSExceptionSupport.createMessageEOFException(e);
331         } catch (IOException e) {
332             throw JMSExceptionSupport.createMessageFormatException(e);
333         }
334     }
335
336     /**
337      * Reads a string that has been encoded using a modified UTF-8 format from the bytes message stream. <P> For more
338      * information on the UTF-8 format, see "File System Safe UCS Transformation Format (FSS_UTF)", X/Open Preliminary
339      * Specification, X/Open Company Ltd., Document Number: P316. This information also appears in ISO/IEC 10646, Annex
340      * P.
341      *
342      * @return a Unicode string from the bytes message stream
343      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
344      * @throws MessageEOFException if unexpected end of bytes stream has been reached.
345      * @throws MessageNotReadableException if the message is in write-only mode.
346      */

347     public String JavaDoc readUTF() throws JMSException {
348         initializeReading();
349         try {
350             return this.dataIn.readUTF();
351         } catch (EOFException e) {
352             throw JMSExceptionSupport.createMessageEOFException(e);
353         } catch (IOException e) {
354             throw JMSExceptionSupport.createMessageFormatException(e);
355         }
356     }
357
358     /**
359      * Reads a byte array from the bytes message stream. <P> If the length of array <code>value</code> is less than the
360      * number of bytes remaining to be read from the stream, the array should be filled. A subsequent call reads the
361      * next increment, and so on. <P> If the number of bytes remaining in the stream is less than the length of array
362      * <code>value</code>, the bytes should be read into the array. The return value of the total number of bytes read
363      * will be less than the length of the array, indicating that there are no more bytes left to be read from the
364      * stream. The next read of the stream returns -1.
365      *
366      * @param value the buffer into which the data is read
367      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
368      * stream has been reached
369      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
370      * @throws MessageNotReadableException if the message is in write-only mode.
371      */

372     public int readBytes(byte[] value) throws JMSException {
373         return readBytes(value, value.length);
374     }
375
376     /**
377      * Reads a portion of the bytes message stream. <P> If the length of array <code>value</code> is less than the
378      * number of bytes remaining to be read from the stream, the array should be filled. A subsequent call reads the
379      * next increment, and so on. <P> If the number of bytes remaining in the stream is less than the length of array
380      * <code>value</code>, the bytes should be read into the array. The return value of the total number of bytes read
381      * will be less than the length of the array, indicating that there are no more bytes left to be read from the
382      * stream. The next read of the stream returns -1.
383      * <p/>
384      * If <code>length</code> is negative, or <code>length</code> is greater than the length of the array
385      * <code>value</code>, then an <code>IndexOutOfBoundsException</code> is thrown. No bytes will be read from the
386      * stream for this exception case.
387      *
388      * @param value the buffer into which the data is read
389      * @param length the number of bytes to read; must be less than or equal to <code>value.length</code>
390      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
391      * stream has been reached
392      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
393      * @throws MessageNotReadableException if the message is in write-only mode.
394      */

395     public int readBytes(byte[] value, int length) throws JMSException {
396         initializeReading();
397         try {
398             int n = 0;
399             while (n < length) {
400                 int count = this.dataIn.read(value, n, length - n);
401                 if (count < 0) {
402                     break;
403                 }
404                 n += count;
405             }
406             if (n == 0 && length > 0) {
407                 n = -1;
408             }
409             return n;
410         } catch (EOFException e) {
411             throw JMSExceptionSupport.createMessageEOFException(e);
412         } catch (IOException e) {
413             throw JMSExceptionSupport.createMessageFormatException(e);
414         }
415     }
416
417     /**
418      * Writes a <code>boolean</code> to the bytes message stream as a 1-byte value. The value <code>true</code> is
419      * written as the value <code>(byte)1</code>; the value <code>false</code> is written as the value
420      * <code>(byte)0</code>.
421      *
422      * @param value the <code>boolean</code> value to be written
423      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
424      * @throws MessageNotWriteableException if the message is in read-only mode.
425      */

426     public void writeBoolean(boolean value) throws JMSException {
427         initializeWriting();
428         try {
429             this.dataOut.writeBoolean(value);
430         } catch (IOException ioe) {
431             throw JMSExceptionSupport.create(ioe);
432         }
433     }
434
435     /**
436      * Writes a <code>byte</code> to the bytes message stream as a 1-byte value.
437      *
438      * @param value the <code>byte</code> value to be written
439      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
440      * @throws MessageNotWriteableException if the message is in read-only mode.
441      */

442     public void writeByte(byte value) throws JMSException {
443         initializeWriting();
444         try {
445             this.dataOut.writeByte(value);
446         } catch (IOException ioe) {
447             throw JMSExceptionSupport.create(ioe);
448         }
449     }
450
451     /**
452      * Writes a <code>short</code> to the bytes message stream as two bytes, high byte first.
453      *
454      * @param value the <code>short</code> to be written
455      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
456      * @throws MessageNotWriteableException if the message is in read-only mode.
457      */

458     public void writeShort(short value) throws JMSException {
459         initializeWriting();
460         try {
461             this.dataOut.writeShort(value);
462         } catch (IOException ioe) {
463             throw JMSExceptionSupport.create(ioe);
464         }
465     }
466
467     /**
468      * Writes a <code>char</code> to the bytes message stream as a 2-byte value, high byte first.
469      *
470      * @param value the <code>char</code> value to be written
471      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
472      * @throws MessageNotWriteableException if the message is in read-only mode.
473      */

474     public void writeChar(char value) throws JMSException {
475         initializeWriting();
476         try {
477             this.dataOut.writeChar(value);
478         } catch (IOException ioe) {
479             throw JMSExceptionSupport.create(ioe);
480         }
481     }
482
483     /**
484      * Writes an <code>int</code> to the bytes message stream as four bytes, high byte first.
485      *
486      * @param value the <code>int</code> to be written
487      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
488      * @throws MessageNotWriteableException if the message is in read-only mode.
489      */

490     public void writeInt(int value) throws JMSException {
491         initializeWriting();
492         try {
493             this.dataOut.writeInt(value);
494         } catch (IOException ioe) {
495             throw JMSExceptionSupport.create(ioe);
496         }
497     }
498
499     /**
500      * Writes a <code>long</code> to the bytes message stream as eight bytes, high byte first.
501      *
502      * @param value the <code>long</code> to be written
503      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
504      * @throws MessageNotWriteableException if the message is in read-only mode.
505      */

506     public void writeLong(long value) throws JMSException {
507         initializeWriting();
508         try {
509             this.dataOut.writeLong(value);
510         } catch (IOException ioe) {
511             throw JMSExceptionSupport.create(ioe);
512         }
513     }
514
515     /**
516      * Converts the <code>float</code> argument to an <code>int</code> using the <code>floatToIntBits</code> method in
517      * class <code>Float</code>, and then writes that <code>int</code> value to the bytes message stream as a 4-byte
518      * quantity, high byte first.
519      *
520      * @param value the <code>float</code> value to be written
521      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
522      * @throws MessageNotWriteableException if the message is in read-only mode.
523      */

524     public void writeFloat(float value) throws JMSException {
525         initializeWriting();
526         try {
527             this.dataOut.writeFloat(value);
528         } catch (IOException ioe) {
529             throw JMSExceptionSupport.create(ioe);
530         }
531     }
532
533     /**
534      * Converts the <code>double</code> argument to a <code>long</code> using the <code>doubleToLongBits</code> method
535      * in class <code>Double</code>, and then writes that <code>long</code> value to the bytes message stream as an
536      * 8-byte quantity, high byte first.
537      *
538      * @param value the <code>double</code> value to be written
539      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
540      * @throws MessageNotWriteableException if the message is in read-only mode.
541      */

542     public void writeDouble(double value) throws JMSException {
543         initializeWriting();
544         try {
545             this.dataOut.writeDouble(value);
546         } catch (IOException ioe) {
547             throw JMSExceptionSupport.create(ioe);
548         }
549     }
550
551     /**
552      * Writes a string to the bytes message stream using UTF-8 encoding in a machine-independent manner. <P> For more
553      * information on the UTF-8 format, see "File System Safe UCS Transformation Format (FSS_UTF)", X/Open Preliminary
554      * Specification, X/Open Company Ltd., Document Number: P316. This information also appears in ISO/IEC 10646, Annex
555      * P.
556      *
557      * @param value the <code>String</code> value to be written
558      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
559      * @throws MessageNotWriteableException if the message is in read-only mode.
560      */

561     public void writeUTF(String JavaDoc value) throws JMSException {
562         initializeWriting();
563         try {
564             this.dataOut.writeUTF(value);
565         } catch (IOException ioe) {
566             throw JMSExceptionSupport.create(ioe);
567         }
568     }
569
570     /**
571      * Writes a byte array to the bytes message stream.
572      *
573      * @param value the byte array to be written
574      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
575      * @throws MessageNotWriteableException if the message is in read-only mode.
576      */

577     public void writeBytes(byte[] value) throws JMSException {
578         initializeWriting();
579         try {
580             this.dataOut.write(value);
581         } catch (IOException ioe) {
582             throw JMSExceptionSupport.create(ioe);
583         }
584     }
585
586     /**
587      * Writes a portion of a byte array to the bytes message stream.
588      *
589      * @param value the byte array value to be written
590      * @param offset the initial offset within the byte array
591      * @param length the number of bytes to use
592      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
593      * @throws MessageNotWriteableException if the message is in read-only mode.
594      */

595     public void writeBytes(byte[] value, int offset, int length) throws JMSException {
596         initializeWriting();
597         try {
598             this.dataOut.write(value, offset, length);
599         } catch (IOException ioe) {
600             throw JMSExceptionSupport.create(ioe);
601         }
602     }
603
604     /**
605      * Writes an object to the bytes message stream. <P> This method works only for the objectified primitive object
606      * types (<code>Integer</code>,<code>Double</code>, <code>Long</code> &nbsp;...), <code>String</code> objects, and
607      * byte arrays.
608      *
609      * @param value the object in the Java programming language ("Java object") to be written; it must not be null
610      * @throws JMSException if the JMS provider fails to write the message due to some internal
611      * error.
612      * @throws MessageFormatException if the object is of an invalid type.
613      * @throws MessageNotWriteableException if the message is in read-only mode.
614      * @throws java.lang.NullPointerException if the parameter <code>value</code> is null.
615      */

616     public void writeObject(Object JavaDoc value) throws JMSException {
617         if (value == null) {
618             throw new NullPointerException JavaDoc();
619         }
620         initializeWriting();
621         if (value instanceof Boolean JavaDoc) {
622             writeBoolean(((Boolean JavaDoc) value).booleanValue());
623         } else if (value instanceof Character JavaDoc) {
624             writeChar(((Character JavaDoc) value).charValue());
625         } else if (value instanceof Byte JavaDoc) {
626             writeByte(((Byte JavaDoc) value).byteValue());
627         } else if (value instanceof Short JavaDoc) {
628             writeShort(((Short JavaDoc) value).shortValue());
629         } else if (value instanceof Integer JavaDoc) {
630             writeInt(((Integer JavaDoc) value).intValue());
631         } else if (value instanceof Long JavaDoc) {
632             writeLong(((Long JavaDoc) value).longValue());
633         } else if (value instanceof Float JavaDoc) {
634             writeFloat(((Float JavaDoc) value).floatValue());
635         } else if (value instanceof Double JavaDoc) {
636             writeDouble(((Double JavaDoc) value).doubleValue());
637         } else if (value instanceof String JavaDoc) {
638             writeUTF(value.toString());
639         } else if (value instanceof byte[]) {
640             writeBytes((byte[]) value);
641         } else {
642             throw new MessageFormatException("Cannot write non-primitive type:" + value.getClass());
643         }
644     }
645
646     /**
647      * Puts the message body in read-only mode and repositions the stream of bytes to the beginning.
648      *
649      * @throws JMSException if an internal error occurs
650      */

651     public void reset() throws JMSException {
652         storeContent();
653         this.bytesOut = null;
654         this.dataIn = null;
655         this.dataOut = null;
656         setReadOnlyBody(true);
657     }
658
659     private void initializeWriting() throws JMSException {
660         checkReadOnlyBody();
661         if (this.dataOut == null) {
662             this.bytesOut = new ByteArrayOutputStream();
663             OutputStream os = bytesOut;
664             ActiveMQConnection connection = getConnection();
665             if( connection!=null && connection.isUseCompression() ) {
666                 // keep track of the real length of the content if
667
// we are compressed.
668
try {
669                     os.write(new byte[4]);
670                 } catch (IOException e) {
671                     throw JMSExceptionSupport.create(e);
672                 }
673                 length=0;
674                 compressed = true;
675                 Deflater JavaDoc deflater = new Deflater JavaDoc(Deflater.BEST_SPEED);
676                 os = new FilterOutputStream(new DeflaterOutputStream JavaDoc(os,deflater)) {
677                     public void write(byte[] arg0) throws IOException {
678                         length+=arg0.length;
679                         out.write(arg0);
680                     }
681                     public void write(byte[] arg0, int arg1, int arg2) throws IOException {
682                         length+=arg2;
683                         out.write(arg0, arg1, arg2);
684                     }
685                     public void write(int arg0) throws IOException {
686                         length++;
687                         out.write(arg0);
688                     }
689                 };
690             }
691             this.dataOut = new DataOutputStream(os);
692         }
693     }
694     
695     protected void checkWriteOnlyBody() throws MessageNotReadableException {
696         if (!readOnlyBody) {
697             throw new MessageNotReadableException("Message body is write-only");
698         }
699     }
700
701     private void initializeReading() throws JMSException {
702         checkWriteOnlyBody();
703         if (dataIn == null ) {
704             ByteSequence data = getContent();
705             if( data==null )
706                 data = new ByteSequence(new byte[]{}, 0, 0);
707             InputStream is = new ByteArrayInputStream(data);
708             if( isCompressed() ) {
709                 // keep track of the real length of the content if
710
// we are compressed.
711
try {
712                     DataInputStream dis = new DataInputStream(is);
713                     length = dis.readInt();
714                     dis.close();
715                 } catch (IOException e) {
716                     throw JMSExceptionSupport.create(e);
717                 }
718                 is = new InflaterInputStream JavaDoc(is);
719             } else {
720                 length = data.getLength();
721             }
722             dataIn = new DataInputStream(is);
723         }
724     }
725     
726     public void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws JMSException {
727         initializeWriting();
728         super.setObjectProperty(name, value);
729     }
730
731     public String JavaDoc toString() {
732         return super.toString() + " ActiveMQBytesMessage{ " +
733                 "bytesOut = " + bytesOut +
734                 ", dataOut = " + dataOut +
735                 ", dataIn = " + dataIn +
736                 " }";
737     }
738 }
739
Popular Tags