KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > message > BytesMessageImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.message;
30
31 import com.caucho.jms.JMSExceptionWrapper;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.TempStream;
35 import com.caucho.vfs.WriteStream;
36
37 import javax.jms.BytesMessage JavaDoc;
38 import javax.jms.JMSException JavaDoc;
39 import javax.jms.MessageEOFException JavaDoc;
40 import javax.jms.MessageFormatException JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.logging.Level JavaDoc;
43
44 /**
45  * A byte-stream message.
46  */

47 public class BytesMessageImpl extends MessageImpl implements BytesMessage JavaDoc {
48   private TempStream _tempStream;
49   private ReadStream _rs;
50   private WriteStream _ws;
51
52   /**
53    * Sets the body for reading.
54    */

55   public void setReceive()
56     throws JMSException JavaDoc
57   {
58     super.setReceive();
59     
60     reset();
61   }
62
63   /**
64    * Set the stream for reading.
65    */

66   public void reset()
67     throws JMSException JavaDoc
68   {
69     setBodyReadOnly();
70     
71     try {
72       // XXX: test for null
73
if (_ws != null)
74     _ws.close();
75
76       if (_tempStream != null) {
77     if (_rs != null)
78       _rs.close();
79     
80     _rs = _tempStream.openRead();
81       }
82     } catch (IOException JavaDoc e) {
83       throw new JMSExceptionWrapper(e);
84     }
85   }
86
87   /**
88    * Read a boolean from the stream.
89    */

90   public boolean readBoolean()
91     throws JMSException JavaDoc
92   {
93     ReadStream is = getReadStream();
94
95     try {
96       return is.read() == 1;
97     } catch (IOException JavaDoc e) {
98       throw new JMSExceptionWrapper(e);
99     }
100   }
101
102   /**
103    * Read a byte from the stream.
104    */

105   public byte readByte()
106     throws JMSException JavaDoc
107   {
108     ReadStream is = getReadStream();
109
110     try {
111       return (byte) is.read();
112     } catch (IOException JavaDoc e) {
113       throw new JMSExceptionWrapper(e);
114     }
115   }
116
117   /**
118    * Read an unsigned byte from the stream.
119    */

120   public int readUnsignedByte()
121     throws JMSException JavaDoc
122   {
123     ReadStream is = getReadStream();
124
125     if (is == null)
126       return -1;
127
128     try {
129       return is.read();
130     } catch (IOException JavaDoc e) {
131       throw new JMSExceptionWrapper(e);
132     }
133   }
134
135   /**
136    * Read a short from the stream.
137    */

138   public short readShort()
139     throws JMSException JavaDoc
140   {
141     ReadStream is = getReadStream();
142
143     try {
144       int d1 = is.read();
145       int d2 = is.read();
146     
147       return (short) ((d1 << 8) + d2);
148     } catch (IOException JavaDoc e) {
149       throw new JMSExceptionWrapper(e);
150     }
151   }
152
153   /**
154    * Read an unsigned short from the stream.
155    */

156   public int readUnsignedShort()
157     throws JMSException JavaDoc
158   {
159     ReadStream is = getReadStream();
160
161     try {
162       int d1 = is.read();
163       int d2 = is.read();
164     
165       return ((d1 << 8) + d2);
166     } catch (IOException JavaDoc e) {
167       throw new JMSExceptionWrapper(e);
168     }
169   }
170
171   /**
172    * Read an integer from the stream.
173    */

174   public int readInt()
175     throws JMSException JavaDoc
176   {
177     ReadStream is = getReadStream();
178
179     try {
180       int d1 = is.read();
181       int d2 = is.read();
182       int d3 = is.read();
183       int d4 = is.read();
184     
185       return (d1 << 24) + (d2 << 16) + (d3 << 8) + d4;
186     } catch (IOException JavaDoc e) {
187       throw new JMSExceptionWrapper(e);
188     }
189   }
190
191   /**
192    * Read a long from the stream.
193    */

194   public long readLong()
195     throws JMSException JavaDoc
196   {
197     ReadStream is = getReadStream();
198
199     try {
200       long d1 = is.read();
201       long d2 = is.read();
202       long d3 = is.read();
203       long d4 = is.read();
204       long d5 = is.read();
205       long d6 = is.read();
206       long d7 = is.read();
207       long d8 = is.read();
208     
209       return ((d1 << 56) +
210               (d2 << 48) +
211               (d3 << 40) +
212               (d4 << 32) +
213               (d5 << 24) +
214               (d6 << 16) +
215               (d7 << 8) +
216               (d8));
217     } catch (IOException JavaDoc e) {
218       throw new JMSExceptionWrapper(e);
219     }
220   }
221
222   /**
223    * Read a float from the stream.
224    */

225   public float readFloat()
226     throws JMSException JavaDoc
227   {
228     return Float.intBitsToFloat(readInt());
229   }
230
231   /**
232    * Read a double from the stream.
233    */

234   public double readDouble()
235     throws JMSException JavaDoc
236   {
237     return Double.longBitsToDouble(readLong());
238   }
239
240   /**
241    * Read a character object from the stream.
242    */

243   public char readChar()
244     throws JMSException JavaDoc
245   {
246     ReadStream is = getReadStream();
247
248     try {
249       int d1 = is.read();
250       int d2 = is.read();
251
252       return (char) ((d1 << 8) + d2);
253     } catch (IOException JavaDoc e) {
254       throw new JMSExceptionWrapper(e);
255     }
256   }
257
258   /**
259    * Read a string from the stream.
260    */

261   public String JavaDoc readUTF()
262     throws JMSException JavaDoc
263   {
264     ReadStream is = getReadStream();
265     CharBuffer cb = new CharBuffer();
266
267     try {
268       int d1;
269       
270       while ((d1 = is.read()) > 0) {
271         if (d1 < 0x80)
272           cb.append((char) d1);
273         else if ((d1 & 0xe0) == 0xc0) {
274           int d2 = is.read();
275
276           cb.append((char) (((d1 & 0x1f) << 6) + (d2 & 0x3f)));
277         }
278         else if ((d1 & 0xf0) == 0xe0) {
279           int d2 = is.read();
280           int d3 = is.read();
281
282           cb.append((char) (((d1 & 0xf) << 12) +
283                             ((d2 & 0x3f) << 6) +
284                             (d3 & 0x3f)));
285         }
286     else
287       throw new MessageFormatException JavaDoc(L.l("invalid UTF-8 in bytes message"));
288       }
289
290       if (d1 < 0)
291     throw new MessageEOFException JavaDoc("end of message in byte stream");
292     } catch (JMSException JavaDoc e) {
293       throw e;
294     } catch (RuntimeException JavaDoc e) {
295       throw e;
296     } catch (Throwable JavaDoc e) {
297       throw new JMSExceptionWrapper(e);
298     }
299
300     return cb.toString();
301   }
302
303   /**
304    * Read a byte array object from the stream.
305    */

306   public int readBytes(byte []value)
307     throws JMSException JavaDoc
308   {
309     ReadStream is = getReadStream();
310
311     try {
312       return is.read(value);
313     } catch (IOException JavaDoc e) {
314       throw new JMSExceptionWrapper(e);
315     }
316   }
317
318   /**
319    * Read a byte array object from the stream.
320    */

321   public int readBytes(byte []value, int length)
322     throws JMSException JavaDoc
323   {
324     ReadStream is = getReadStream();
325
326     try {
327       return is.read(value, 0, length);
328     } catch (IOException JavaDoc e) {
329       throw new JMSExceptionWrapper(e);
330     }
331   }
332
333   protected ReadStream getReadStream()
334     throws JMSException JavaDoc
335   {
336     checkBodyReadable();
337
338     /* ejb/6a87
339     if (_rs == null)
340       throw new MessageEOFException(L.l("bytes message may not be read"));
341     */

342       
343     return _rs;
344   }
345
346   /**
347    * Clears the message and puts it into write mode.
348    */

349   public void clearBody()
350     throws JMSException JavaDoc
351   {
352     super.clearBody();
353     
354     _ws = null;
355     _tempStream = null;
356     _rs = null;
357   }
358
359   /**
360    * Writes a boolean to the stream.
361    */

362   public void writeBoolean(boolean b)
363     throws JMSException JavaDoc
364   {
365     try {
366       getWriteStream().write(b ? 1 : 0);
367     } catch (IOException JavaDoc e) {
368       throw new JMSExceptionWrapper(e);
369     }
370   }
371
372   /**
373    * Writes a byte to the stream.
374    */

375   public void writeByte(byte b)
376     throws JMSException JavaDoc
377   {
378     try {
379       getWriteStream().write(b);
380     } catch (IOException JavaDoc e) {
381       throw new JMSExceptionWrapper(e);
382     }
383   }
384
385   /**
386    * Writes a short to the stream.
387    */

388   public void writeShort(short s)
389     throws JMSException JavaDoc
390   {
391     try {
392       WriteStream ws = getWriteStream();
393     
394       ws.write(s >> 8);
395       ws.write(s);
396     } catch (IOException JavaDoc e) {
397       throw new JMSExceptionWrapper(e);
398     }
399   }
400
401   /**
402    * Writes an integer to the stream.
403    */

404   public void writeInt(int i)
405     throws JMSException JavaDoc
406   {
407     try {
408       WriteStream ws = getWriteStream();
409     
410       ws.write(i >> 24);
411       ws.write(i >> 16);
412       ws.write(i >> 8);
413       ws.write(i);
414     } catch (IOException JavaDoc e) {
415       throw new JMSExceptionWrapper(e);
416     }
417   }
418
419   /**
420    * Writes a long to the stream.
421    */

422   public void writeLong(long l)
423     throws JMSException JavaDoc
424   {
425     try {
426       WriteStream ws = getWriteStream();
427     
428       ws.write((int) (l >> 56));
429       ws.write((int) (l >> 48));
430       ws.write((int) (l >> 40));
431       ws.write((int) (l >> 32));
432       ws.write((int) (l >> 24));
433       ws.write((int) (l >> 16));
434       ws.write((int) (l >> 8));
435       ws.write((int) l);
436     } catch (IOException JavaDoc e) {
437       throw new JMSExceptionWrapper(e);
438     }
439   }
440
441   /**
442    * Writes a float to the stream.
443    */

444   public void writeFloat(float f)
445     throws JMSException JavaDoc
446   {
447     int i = Float.floatToIntBits(f);
448     writeInt(i);
449   }
450
451   /**
452    * Writes a double to the stream.
453    */

454   public void writeDouble(double d)
455     throws JMSException JavaDoc
456   {
457     long l = Double.doubleToLongBits(d);
458     writeLong(l);
459   }
460
461   /**
462    * Writes a string to the stream.
463    */

464   public void writeUTF(String JavaDoc s)
465     throws JMSException JavaDoc
466   {
467     try {
468       WriteStream ws = getWriteStream();
469
470       int len = s.length();
471       for (int i = 0; i < len; i++) {
472         int ch = s.charAt(i);
473
474         if (ch == 0) {
475           ws.write(0xc0);
476           ws.write(0x80);
477         }
478         else if (ch < 0x80)
479           ws.write(ch);
480         else if (ch < 0x800) {
481           ws.write(0xc0 + ((ch >> 6) & 0x1f));
482           ws.write(0x80 + (ch & 0x3f));
483         }
484         else if (ch < 0x8000) {
485           ws.write(0xe0 + ((ch >> 12) & 0x0f));
486           ws.write(0x80 + ((ch >> 6) & 0x3f));
487           ws.write(0x80 + (ch & 0x3f));
488         }
489       }
490       
491       ws.write(0);
492     } catch (IOException JavaDoc e) {
493       throw new JMSExceptionWrapper(e);
494     }
495   }
496
497   /**
498    * Writes a character to the stream.
499    */

500   public void writeChar(char ch)
501     throws JMSException JavaDoc
502   {
503     try {
504       WriteStream ws = getWriteStream();
505
506       ws.write(ch >> 8);
507       ws.write(ch);
508     } catch (IOException JavaDoc e) {
509       throw new JMSExceptionWrapper(e);
510     }
511   }
512
513   /**
514    * Writes a byte array to the stream.
515    */

516   public void writeBytes(byte []buf)
517     throws JMSException JavaDoc
518   {
519     writeBytes(buf, 0, buf.length);
520   }
521
522   /**
523    * Writes a byte array to the stream.
524    */

525   public void writeBytes(byte []buf, int offset, int length)
526     throws JMSException JavaDoc
527   {
528     try {
529       WriteStream ws = getWriteStream();
530
531       ws.write(buf, offset, length);
532     } catch (IOException JavaDoc e) {
533       throw new JMSExceptionWrapper(e);
534     }
535   }
536
537   /**
538    * Writes the next object.
539    */

540   public void writeObject(Object JavaDoc obj)
541     throws JMSException JavaDoc
542   {
543     if (obj instanceof Boolean JavaDoc)
544       writeBoolean(((Boolean JavaDoc) obj).booleanValue());
545     else if (obj instanceof Byte JavaDoc)
546       writeByte(((Byte JavaDoc) obj).byteValue());
547     else if (obj instanceof Short JavaDoc)
548       writeShort(((Short JavaDoc) obj).shortValue());
549     else if (obj instanceof Character JavaDoc)
550       writeChar(((Character JavaDoc) obj).charValue());
551     else if (obj instanceof Integer JavaDoc)
552       writeInt(((Integer JavaDoc) obj).intValue());
553     else if (obj instanceof Long JavaDoc)
554       writeLong(((Long JavaDoc) obj).longValue());
555     else if (obj instanceof Float JavaDoc)
556       writeFloat(((Float JavaDoc) obj).floatValue());
557     else if (obj instanceof Double JavaDoc)
558       writeDouble(((Double JavaDoc) obj).doubleValue());
559     else if (obj instanceof String JavaDoc)
560       writeUTF((String JavaDoc) obj);
561     else if (obj instanceof byte[])
562       writeBytes((byte[]) obj);
563     else
564       throw new JMSException JavaDoc("jms");
565   }
566
567   public long getBodyLength()
568     throws JMSException JavaDoc
569   {
570     if (_tempStream == null)
571       return 0;
572     else
573       return _tempStream.getLength();
574   }
575
576   protected WriteStream getWriteStream()
577     throws JMSException JavaDoc
578   {
579     checkBodyWriteable();
580
581     if (_tempStream == null)
582       _tempStream = new TempStream(null);
583     
584     if (_ws == null)
585       _ws = new WriteStream(_tempStream);
586
587     return _ws;
588   }
589
590   public MessageImpl copy()
591   {
592     BytesMessageImpl msg = new BytesMessageImpl();
593
594     copy(msg);
595
596     return msg;
597   }
598
599   protected void copy(BytesMessageImpl newMsg)
600   {
601     super.copy(newMsg);
602
603     try {
604       if (_ws != null)
605     _ws.flush();
606
607       if (_tempStream != null)
608     newMsg._tempStream = _tempStream.copy();
609     } catch (Exception JavaDoc e) {
610       log.log(Level.WARNING, e.toString(), e);
611     }
612   }
613 }
614
615
Popular Tags