001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq;
023:
024: import java.io.Externalizable;
025: import java.io.IOException;
026: import java.io.ObjectInput;
027: import java.io.ObjectOutput;
028: import java.util.Vector;
029:
030: import javax.jms.JMSException;
031: import javax.jms.MessageEOFException;
032: import javax.jms.MessageFormatException;
033: import javax.jms.MessageNotReadableException;
034: import javax.jms.MessageNotWriteableException;
035: import javax.jms.StreamMessage;
036:
037: import org.jboss.util.Primitives;
038:
039: /**
040: * This class implements javax.jms.StreamMessage
041: *
042: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
043: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
044: * @version $Revision: 57198 $
045: */
046: public class SpyStreamMessage extends SpyMessage implements
047: StreamMessage, Cloneable, Externalizable {
048: // Constants -----------------------------------------------------
049:
050: /** The serialVersionUID */
051: private final static long serialVersionUID = 2490910971426786841L;
052:
053: // Attributes ----------------------------------------------------
054:
055: /** The content */
056: Vector content;
057: /** The position */
058: int position;
059: /** The offset */
060: int offset;
061: /** The size */
062: int size;
063:
064: // Static --------------------------------------------------------
065:
066: // Constructors --------------------------------------------------
067:
068: /**
069: * Create a new SpyStreamMessage
070: */
071: public SpyStreamMessage() {
072: content = new Vector();
073: position = 0;
074: size = 0;
075: offset = 0;
076: }
077:
078: // Public --------------------------------------------------------
079:
080: // StreamMessage implementation ----------------------------------
081:
082: public boolean readBoolean() throws JMSException {
083: if (!header.msgReadOnly)
084: throw new MessageNotReadableException(
085: "The message body is writeonly");
086:
087: try {
088: Object value = content.get(position);
089: offset = 0;
090:
091: if (value == null)
092: throw new NullPointerException("Value is null");
093: else if (value instanceof Boolean) {
094: position++;
095: return ((Boolean) value).booleanValue();
096: } else if (value instanceof String) {
097: boolean result = Boolean.valueOf((String) value)
098: .booleanValue();
099: position++;
100: return result;
101: } else
102: throw new MessageFormatException("Invalid conversion");
103: } catch (ArrayIndexOutOfBoundsException e) {
104: throw new MessageEOFException("");
105: }
106:
107: }
108:
109: public byte readByte() throws JMSException {
110: if (!header.msgReadOnly)
111: throw new MessageNotReadableException(
112: "The message body is writeonly");
113:
114: try {
115: Object value = content.get(position);
116: offset = 0;
117: if (value == null)
118: throw new NullPointerException("Value is null");
119: else if (value instanceof Byte) {
120: position++;
121: return ((Byte) value).byteValue();
122: } else if (value instanceof String) {
123: byte result = Byte.parseByte((String) value);
124: position++;
125: return result;
126: } else
127: throw new MessageFormatException("Invalid conversion");
128: } catch (ArrayIndexOutOfBoundsException e) {
129: throw new MessageEOFException("");
130: }
131: }
132:
133: public short readShort() throws JMSException {
134: if (!header.msgReadOnly)
135: throw new MessageNotReadableException(
136: "The message body is writeonly");
137: try {
138: Object value = content.get(position);
139: offset = 0;
140:
141: if (value == null)
142: throw new NullPointerException("Value is null");
143: else if (value instanceof Byte) {
144: position++;
145: return ((Byte) value).shortValue();
146: } else if (value instanceof Short) {
147: position++;
148: return ((Short) value).shortValue();
149: } else if (value instanceof String) {
150: short result = Short.parseShort((String) value);
151: position++;
152: return result;
153: } else
154: throw new MessageFormatException("Invalid conversion");
155: } catch (ArrayIndexOutOfBoundsException e) {
156: throw new MessageEOFException("");
157: }
158: }
159:
160: public char readChar() throws JMSException {
161: if (!header.msgReadOnly)
162: throw new MessageNotReadableException(
163: "The message body is writeonly");
164: try {
165: Object value = content.get(position);
166: offset = 0;
167:
168: if (value == null)
169: throw new NullPointerException("Value is null");
170: else if (value instanceof Character) {
171: position++;
172: return ((Character) value).charValue();
173: } else
174: throw new MessageFormatException("Invalid conversion");
175: } catch (ArrayIndexOutOfBoundsException e) {
176: throw new MessageEOFException("");
177: }
178: }
179:
180: public int readInt() throws JMSException {
181: if (!header.msgReadOnly)
182: throw new MessageNotReadableException(
183: "The message body is writeonly");
184: try {
185: Object value = content.get(position);
186: offset = 0;
187:
188: if (value == null)
189: throw new NullPointerException("Value is null");
190: else if (value instanceof Byte) {
191: position++;
192: return ((Byte) value).intValue();
193: } else if (value instanceof Short) {
194: position++;
195: return ((Short) value).intValue();
196: } else if (value instanceof Integer) {
197: position++;
198: return ((Integer) value).intValue();
199: } else if (value instanceof String) {
200: int result = Integer.parseInt((String) value);
201: position++;
202: return result;
203: } else
204: throw new MessageFormatException("Invalid conversion");
205: } catch (ArrayIndexOutOfBoundsException e) {
206: throw new MessageEOFException("");
207: }
208: }
209:
210: public long readLong() throws JMSException {
211: if (!header.msgReadOnly)
212: throw new MessageNotReadableException(
213: "The message body is writeonly");
214: try {
215: Object value = content.get(position);
216: offset = 0;
217:
218: if (value == null)
219: throw new NullPointerException("Value is null");
220: else if (value instanceof Byte) {
221: position++;
222: return ((Byte) value).longValue();
223: } else if (value instanceof Short) {
224: position++;
225: return ((Short) value).longValue();
226: } else if (value instanceof Integer) {
227: position++;
228: return ((Integer) value).longValue();
229: } else if (value instanceof Long) {
230: position++;
231: return ((Long) value).longValue();
232: } else if (value instanceof String) {
233: long result = Long.parseLong((String) value);
234: position++;
235: return result;
236: } else
237: throw new MessageFormatException("Invalid conversion");
238: } catch (ArrayIndexOutOfBoundsException e) {
239: throw new MessageEOFException("");
240: }
241: }
242:
243: public float readFloat() throws JMSException {
244: if (!header.msgReadOnly)
245: throw new MessageNotReadableException(
246: "The message body is writeonly");
247: try {
248: Object value = content.get(position);
249: offset = 0;
250:
251: if (value == null)
252: throw new NullPointerException("Value is null");
253: else if (value instanceof Float) {
254: position++;
255: return ((Float) value).floatValue();
256: } else if (value instanceof String) {
257: float result = Float.parseFloat((String) value);
258: position++;
259: return result;
260: } else
261: throw new MessageFormatException("Invalid conversion");
262: } catch (ArrayIndexOutOfBoundsException e) {
263: throw new MessageEOFException("");
264: }
265: }
266:
267: public double readDouble() throws JMSException {
268: if (!header.msgReadOnly)
269: throw new MessageNotReadableException(
270: "The message body is writeonly");
271: try {
272: Object value = content.get(position);
273: offset = 0;
274:
275: if (value == null)
276: throw new NullPointerException("Value is null");
277: else if (value instanceof Float) {
278: position++;
279: return ((Float) value).doubleValue();
280: } else if (value instanceof Double) {
281: position++;
282: return ((Double) value).doubleValue();
283: } else if (value instanceof String) {
284: double result = Double.parseDouble((String) value);
285: position++;
286: return result;
287: } else
288: throw new MessageFormatException("Invalid conversion");
289: } catch (ArrayIndexOutOfBoundsException e) {
290: throw new MessageEOFException("");
291: }
292: }
293:
294: public String readString() throws JMSException {
295: if (!header.msgReadOnly)
296: throw new MessageNotReadableException(
297: "The message body is writeonly");
298: try {
299: Object value = content.get(position);
300: offset = 0;
301:
302: if (value == null) {
303: position++;
304: return null;
305: } else if (value instanceof Boolean) {
306: position++;
307: return ((Boolean) value).toString();
308: } else if (value instanceof Byte) {
309: position++;
310: return ((Byte) value).toString();
311: } else if (value instanceof Short) {
312: position++;
313: return ((Short) value).toString();
314: } else if (value instanceof Character) {
315: position++;
316: return ((Character) value).toString();
317: } else if (value instanceof Integer) {
318: position++;
319: return ((Integer) value).toString();
320: } else if (value instanceof Long) {
321: position++;
322: return ((Long) value).toString();
323: } else if (value instanceof Float) {
324: position++;
325: return ((Float) value).toString();
326: } else if (value instanceof Double) {
327: position++;
328: return ((Double) value).toString();
329: } else if (value instanceof String) {
330: position++;
331: return (String) value;
332: } else
333: throw new MessageFormatException("Invalid conversion");
334: } catch (ArrayIndexOutOfBoundsException e) {
335: throw new MessageEOFException("");
336: }
337: }
338:
339: public int readBytes(byte[] value) throws JMSException {
340: if (!header.msgReadOnly)
341: throw new MessageNotReadableException(
342: "The message body is writeonly");
343: try {
344: Object myObj = content.get(position);
345: if (myObj == null)
346: throw new NullPointerException("Value is null");
347: else if (!(myObj instanceof byte[]))
348: throw new MessageFormatException("Invalid conversion");
349: byte[] obj = (byte[]) myObj;
350:
351: if (obj.length == 0) {
352: position++;
353: offset = 0;
354: return 0;
355: }
356:
357: if (offset >= obj.length) {
358: position++;
359: offset = 0;
360: return -1;
361: }
362:
363: if (obj.length - offset < value.length) {
364: for (int i = 0; i < obj.length; i++)
365: value[i] = obj[i + offset];
366:
367: position++;
368: offset = 0;
369:
370: return obj.length - offset;
371: } else {
372: for (int i = 0; i < value.length; i++)
373: value[i] = obj[i + offset];
374: offset += value.length;
375:
376: return value.length;
377: }
378:
379: } catch (ArrayIndexOutOfBoundsException e) {
380: throw new MessageEOFException("");
381: }
382: }
383:
384: public Object readObject() throws JMSException {
385: if (!header.msgReadOnly)
386: throw new MessageNotReadableException(
387: "The message body is writeonly");
388: try {
389: Object value = content.get(position);
390: position++;
391: offset = 0;
392:
393: return value;
394: } catch (ArrayIndexOutOfBoundsException e) {
395: throw new MessageEOFException("");
396: }
397: }
398:
399: public void writeBoolean(boolean value) throws JMSException {
400: if (header.msgReadOnly)
401: throw new MessageNotWriteableException(
402: "The message body is readonly");
403: content.add(Primitives.valueOf(value));
404: }
405:
406: public void writeByte(byte value) throws JMSException {
407: if (header.msgReadOnly)
408: throw new MessageNotWriteableException(
409: "The message body is readonly");
410: content.add(new Byte(value));
411: }
412:
413: public void writeShort(short value) throws JMSException {
414: if (header.msgReadOnly)
415: throw new MessageNotWriteableException(
416: "The message body is readonly");
417: content.add(new Short(value));
418: }
419:
420: public void writeChar(char value) throws JMSException {
421: if (header.msgReadOnly)
422: throw new MessageNotWriteableException(
423: "The message body is readonly");
424: content.add(new Character(value));
425: }
426:
427: public void writeInt(int value) throws JMSException {
428: if (header.msgReadOnly)
429: throw new MessageNotWriteableException(
430: "The message body is readonly");
431: content.add(new Integer(value));
432: }
433:
434: public void writeLong(long value) throws JMSException {
435: if (header.msgReadOnly)
436: throw new MessageNotWriteableException(
437: "The message body is readonly");
438: content.add(new Long(value));
439: }
440:
441: public void writeFloat(float value) throws JMSException {
442: if (header.msgReadOnly)
443: throw new MessageNotWriteableException(
444: "The message body is readonly");
445: content.add(new Float(value));
446: }
447:
448: public void writeDouble(double value) throws JMSException {
449: if (header.msgReadOnly)
450: throw new MessageNotWriteableException(
451: "The message body is readonly");
452: content.add(new Double(value));
453: }
454:
455: public void writeString(String value) throws JMSException {
456: if (header.msgReadOnly)
457: throw new MessageNotWriteableException(
458: "The message body is readonly");
459: if (value == null)
460: content.add(null);
461: else
462: content.add(value);
463: }
464:
465: public void writeBytes(byte[] value) throws JMSException {
466: if (header.msgReadOnly)
467: throw new MessageNotWriteableException(
468: "The message body is readonly");
469: content.add(value.clone());
470: }
471:
472: public void writeBytes(byte[] value, int offset, int length)
473: throws JMSException {
474: if (header.msgReadOnly)
475: throw new MessageNotWriteableException(
476: "The message body is readonly");
477:
478: if (offset + length > value.length)
479: throw new JMSException("Array is too small");
480: byte[] temp = new byte[length];
481: for (int i = 0; i < length; i++)
482: temp[i] = value[i + offset];
483:
484: content.add(temp);
485: }
486:
487: public void writeObject(Object value) throws JMSException {
488: if (header.msgReadOnly)
489: throw new MessageNotWriteableException(
490: "The message body is readonly");
491: if (value == null)
492: content.add(null);
493: else if (value instanceof Boolean)
494: content.add(value);
495: else if (value instanceof Byte)
496: content.add(value);
497: else if (value instanceof Short)
498: content.add(value);
499: else if (value instanceof Character)
500: content.add(value);
501: else if (value instanceof Integer)
502: content.add(value);
503: else if (value instanceof Long)
504: content.add(value);
505: else if (value instanceof Float)
506: content.add(value);
507: else if (value instanceof Double)
508: content.add(value);
509: else if (value instanceof String)
510: content.add(value);
511: else if (value instanceof byte[])
512: content.add(((byte[]) value).clone());
513: else
514: throw new MessageFormatException("Invalid object type");
515: }
516:
517: public void reset() throws JMSException {
518: header.msgReadOnly = true;
519: position = 0;
520: size = content.size();
521: offset = 0;
522: }
523:
524: // SpyMessage overrides ------------------------------------------
525:
526: public void clearBody() throws JMSException {
527: content = new Vector();
528: position = 0;
529: offset = 0;
530: size = 0;
531:
532: super .clearBody();
533: }
534:
535: public SpyMessage myClone() throws JMSException {
536: SpyStreamMessage result = MessagePool.getStreamMessage();
537: result.copyProps(this );
538: result.content = (Vector) this .content.clone();
539: result.position = this .position;
540: result.offset = this .offset;
541: result.size = this .size;
542: return result;
543: }
544:
545: // Externalizable implementation ---------------------------------
546:
547: public void readExternal(ObjectInput in) throws IOException,
548: ClassNotFoundException {
549: super .readExternal(in);
550: content = (Vector) in.readObject();
551: position = in.readInt();
552: offset = in.readInt();
553: size = in.readInt();
554: }
555:
556: public void writeExternal(ObjectOutput out) throws IOException {
557: super .writeExternal(out);
558: out.writeObject(content);
559: out.writeInt(position);
560: out.writeInt(offset);
561: out.writeInt(size);
562: }
563:
564: // Package protected ---------------------------------------------
565:
566: // Protected -----------------------------------------------------
567:
568: // Private -------------------------------------------------------
569:
570: // Inner classes -------------------------------------------------
571: }
|