001: package dalma.endpoints.jms.impl;
002:
003: import javax.jms.BytesMessage;
004: import javax.jms.JMSException;
005: import javax.jms.MessageEOFException;
006: import javax.jms.MessageFormatException;
007: import javax.jms.Message;
008: import java.io.DataInputStream;
009: import java.io.DataOutputStream;
010: import java.io.EOFException;
011: import java.io.IOException;
012: import java.io.Serializable;
013:
014: /**
015: * {@link Serializable} {@link BytesMessage}.
016: *
017: * @author Kohsuke Kawaguchi
018: * @ThirdParty this class contains code released under ASL.
019: */
020: public class BytesMessageImpl extends MessageImpl<BytesMessage>
021: implements BytesMessage {
022: private DataOutputStream dataOut;
023: private Buffer buffer;
024: private DataInputStream dataIn;
025: private transient long bodyLength = 0;
026:
027: public BytesMessageImpl() {
028: }
029:
030: public BytesMessageImpl wrap(BytesMessage s) throws JMSException {
031: super .wrap(s);
032: long len = s.getBodyLength();
033: if (len > 0) {
034: byte[] data = new byte[(int) len];
035: s.readBytes(data);
036: buffer = new Buffer(data);
037: }
038: return this ;
039: }
040:
041: public void writeTo(BytesMessage d) throws JMSException {
042: super .writeTo(d);
043: if (dataOut != null)
044: try {
045: dataOut.flush();
046: } catch (IOException e) {
047: throw new Error(e); // impossible
048: }
049: d.writeBytes(buffer.getBuffer(), 0, buffer.size());
050: }
051:
052: public void clearBody() throws JMSException {
053: this .dataOut = null;
054: this .dataIn = null;
055: this .buffer = null;
056: }
057:
058: public long getBodyLength() throws JMSException {
059: preRead();
060: return bodyLength;
061: }
062:
063: public boolean readBoolean() throws JMSException {
064: preRead();
065: try {
066: return this .dataIn.readBoolean();
067: } catch (EOFException eof) {
068: JMSException jmsEx = new MessageEOFException(eof
069: .getMessage());
070: jmsEx.setLinkedException(eof);
071: throw jmsEx;
072: } catch (IOException ioe) {
073: JMSException jmsEx = new JMSException(
074: "Format error occured" + ioe.getMessage());
075: jmsEx.setLinkedException(ioe);
076: throw jmsEx;
077: }
078: }
079:
080: public byte readByte() throws JMSException {
081: preRead();
082: try {
083: return this .dataIn.readByte();
084: } catch (EOFException eof) {
085: JMSException jmsEx = new MessageEOFException(eof
086: .getMessage());
087: jmsEx.setLinkedException(eof);
088: throw jmsEx;
089: } catch (IOException ioe) {
090: JMSException jmsEx = new JMSException(
091: "Format error occured" + ioe.getMessage());
092: jmsEx.setLinkedException(ioe);
093: throw jmsEx;
094: }
095: }
096:
097: public int readUnsignedByte() throws JMSException {
098: preRead();
099: try {
100: return this .dataIn.readUnsignedByte();
101: } catch (EOFException eof) {
102: JMSException jmsEx = new MessageEOFException(eof
103: .getMessage());
104: jmsEx.setLinkedException(eof);
105: throw jmsEx;
106: } catch (IOException ioe) {
107: JMSException jmsEx = new JMSException(
108: "Format error occured" + ioe.getMessage());
109: jmsEx.setLinkedException(ioe);
110: throw jmsEx;
111: }
112: }
113:
114: public short readShort() throws JMSException {
115: preRead();
116: try {
117: return this .dataIn.readShort();
118: } catch (EOFException eof) {
119: JMSException jmsEx = new MessageEOFException(eof
120: .getMessage());
121: jmsEx.setLinkedException(eof);
122: throw jmsEx;
123: } catch (IOException ioe) {
124: JMSException jmsEx = new JMSException(
125: "Format error occured" + ioe.getMessage());
126: jmsEx.setLinkedException(ioe);
127: throw jmsEx;
128: }
129: }
130:
131: public int readUnsignedShort() throws JMSException {
132: preRead();
133: try {
134: return this .dataIn.readUnsignedShort();
135: } catch (EOFException eof) {
136: JMSException jmsEx = new MessageEOFException(eof
137: .getMessage());
138: jmsEx.setLinkedException(eof);
139: throw jmsEx;
140: } catch (IOException ioe) {
141: JMSException jmsEx = new JMSException(
142: "Format error occured" + ioe.getMessage());
143: jmsEx.setLinkedException(ioe);
144: throw jmsEx;
145: }
146: }
147:
148: public char readChar() throws JMSException {
149: preRead();
150: try {
151: return this .dataIn.readChar();
152: } catch (EOFException eof) {
153: JMSException jmsEx = new MessageEOFException(eof
154: .getMessage());
155: jmsEx.setLinkedException(eof);
156: throw jmsEx;
157: } catch (IOException ioe) {
158: JMSException jmsEx = new JMSException(
159: "Format error occured" + ioe.getMessage());
160: jmsEx.setLinkedException(ioe);
161: throw jmsEx;
162: }
163: }
164:
165: public int readInt() throws JMSException {
166: preRead();
167: try {
168: return this .dataIn.readInt();
169: } catch (EOFException eof) {
170: JMSException jmsEx = new MessageEOFException(eof
171: .getMessage());
172: jmsEx.setLinkedException(eof);
173: throw jmsEx;
174: } catch (IOException ioe) {
175: JMSException jmsEx = new JMSException(
176: "Format error occured" + ioe.getMessage());
177: jmsEx.setLinkedException(ioe);
178: throw jmsEx;
179: }
180: }
181:
182: public long readLong() throws JMSException {
183: preRead();
184: try {
185: return this .dataIn.readLong();
186: } catch (EOFException eof) {
187: JMSException jmsEx = new MessageEOFException(eof
188: .getMessage());
189: jmsEx.setLinkedException(eof);
190: throw jmsEx;
191: } catch (IOException ioe) {
192: JMSException jmsEx = new JMSException(
193: "Format error occured" + ioe.getMessage());
194: jmsEx.setLinkedException(ioe);
195: throw jmsEx;
196: }
197: }
198:
199: public float readFloat() throws JMSException {
200: preRead();
201: try {
202: return this .dataIn.readFloat();
203: } catch (EOFException eof) {
204: JMSException jmsEx = new MessageEOFException(eof
205: .getMessage());
206: jmsEx.setLinkedException(eof);
207: throw jmsEx;
208: } catch (IOException ioe) {
209: JMSException jmsEx = new JMSException(
210: "Format error occured" + ioe.getMessage());
211: jmsEx.setLinkedException(ioe);
212: throw jmsEx;
213: }
214: }
215:
216: public double readDouble() throws JMSException {
217: preRead();
218: try {
219: return this .dataIn.readDouble();
220: } catch (EOFException eof) {
221: JMSException jmsEx = new MessageEOFException(eof
222: .getMessage());
223: jmsEx.setLinkedException(eof);
224: throw jmsEx;
225: } catch (IOException ioe) {
226: JMSException jmsEx = new JMSException(
227: "Format error occured" + ioe.getMessage());
228: jmsEx.setLinkedException(ioe);
229: throw jmsEx;
230: }
231: }
232:
233: public String readUTF() throws JMSException {
234: preRead();
235: try {
236: return this .dataIn.readUTF();
237: } catch (EOFException eof) {
238: JMSException jmsEx = new MessageEOFException(eof
239: .getMessage());
240: jmsEx.setLinkedException(eof);
241: throw jmsEx;
242: } catch (IOException ioe) {
243: JMSException jmsEx = new JMSException(
244: "Format error occured" + ioe.getMessage());
245: jmsEx.setLinkedException(ioe);
246: throw jmsEx;
247: }
248: }
249:
250: public int readBytes(byte[] value) throws JMSException {
251: return readBytes(value, value.length);
252: }
253:
254: public int readBytes(byte[] value, int length) throws JMSException {
255: preRead();
256: try {
257: int n = 0;
258: while (n < length) {
259: int count = this .dataIn.read(value, n, length - n);
260: if (count < 0) {
261: break;
262: }
263: n += count;
264: }
265: if (n == 0 && length > 0) {
266: n = -1;
267: }
268: return n;
269: } catch (EOFException eof) {
270: JMSException jmsEx = new MessageEOFException(eof
271: .getMessage());
272: jmsEx.setLinkedException(eof);
273: throw jmsEx;
274: } catch (IOException ioe) {
275: JMSException jmsEx = new JMSException(
276: "Format error occured" + ioe.getMessage());
277: jmsEx.setLinkedException(ioe);
278: throw jmsEx;
279: }
280: }
281:
282: public void writeBoolean(boolean value) throws JMSException {
283: preWrite();
284: try {
285: this .dataOut.writeBoolean(value);
286: } catch (IOException ioe) {
287: JMSException jmsEx = new JMSException(
288: "Could not write data:" + ioe.getMessage());
289: jmsEx.setLinkedException(ioe);
290: throw jmsEx;
291: }
292: }
293:
294: public void writeByte(byte value) throws JMSException {
295: preWrite();
296: try {
297: this .dataOut.writeByte(value);
298: } catch (IOException ioe) {
299: JMSException jmsEx = new JMSException(
300: "Could not write data:" + ioe.getMessage());
301: jmsEx.setLinkedException(ioe);
302: throw jmsEx;
303: }
304: }
305:
306: public void writeShort(short value) throws JMSException {
307: preWrite();
308: try {
309: this .dataOut.writeShort(value);
310: } catch (IOException ioe) {
311: JMSException jmsEx = new JMSException(
312: "Could not write data:" + ioe.getMessage());
313: jmsEx.setLinkedException(ioe);
314: throw jmsEx;
315: }
316: }
317:
318: public void writeChar(char value) throws JMSException {
319: preWrite();
320: try {
321: this .dataOut.writeChar(value);
322: } catch (IOException ioe) {
323: JMSException jmsEx = new JMSException(
324: "Could not write data:" + ioe.getMessage());
325: jmsEx.setLinkedException(ioe);
326: throw jmsEx;
327: }
328: }
329:
330: public void writeInt(int value) throws JMSException {
331: preWrite();
332: try {
333: this .dataOut.writeInt(value);
334: } catch (IOException ioe) {
335: JMSException jmsEx = new JMSException(
336: "Could not write data:" + ioe.getMessage());
337: jmsEx.setLinkedException(ioe);
338: throw jmsEx;
339: }
340: }
341:
342: public void writeLong(long value) throws JMSException {
343: preWrite();
344: try {
345: this .dataOut.writeLong(value);
346: } catch (IOException ioe) {
347: JMSException jmsEx = new JMSException(
348: "Could not write data:" + ioe.getMessage());
349: jmsEx.setLinkedException(ioe);
350: throw jmsEx;
351: }
352: }
353:
354: public void writeFloat(float value) throws JMSException {
355: preWrite();
356: try {
357: this .dataOut.writeFloat(value);
358: } catch (IOException ioe) {
359: JMSException jmsEx = new JMSException(
360: "Could not write data:" + ioe.getMessage());
361: jmsEx.setLinkedException(ioe);
362: throw jmsEx;
363: }
364: }
365:
366: public void writeDouble(double value) throws JMSException {
367: preWrite();
368: try {
369: this .dataOut.writeDouble(value);
370: } catch (IOException ioe) {
371: JMSException jmsEx = new JMSException(
372: "Could not write data:" + ioe.getMessage());
373: jmsEx.setLinkedException(ioe);
374: throw jmsEx;
375: }
376: }
377:
378: public void writeUTF(String value) throws JMSException {
379: preWrite();
380: try {
381: this .dataOut.writeUTF(value);
382: } catch (IOException ioe) {
383: JMSException jmsEx = new JMSException(
384: "Could not write data:" + ioe.getMessage());
385: jmsEx.setLinkedException(ioe);
386: throw jmsEx;
387: }
388: }
389:
390: public void writeBytes(byte[] value) throws JMSException {
391: preWrite();
392: try {
393: this .dataOut.write(value);
394: } catch (IOException ioe) {
395: JMSException jmsEx = new JMSException(
396: "Could not write data:" + ioe.getMessage());
397: jmsEx.setLinkedException(ioe);
398: throw jmsEx;
399: }
400: }
401:
402: public void writeBytes(byte[] value, int offset, int length)
403: throws JMSException {
404: preWrite();
405: try {
406: this .dataOut.write(value, offset, length);
407: } catch (IOException ioe) {
408: JMSException jmsEx = new JMSException(
409: "Could not write data:" + ioe.getMessage());
410: jmsEx.setLinkedException(ioe);
411: throw jmsEx;
412: }
413: }
414:
415: public void writeObject(Object value) throws JMSException {
416: if (value == null) {
417: throw new NullPointerException();
418: }
419: preWrite();
420: if (value instanceof Boolean) {
421: writeBoolean((Boolean) value);
422: } else if (value instanceof Character) {
423: writeChar((Character) value);
424: } else if (value instanceof Byte) {
425: writeByte((Byte) value);
426: } else if (value instanceof Short) {
427: writeShort((Short) value);
428: } else if (value instanceof Integer) {
429: writeInt((Integer) value);
430: } else if (value instanceof Double) {
431: writeDouble((Double) value);
432: } else if (value instanceof Long) {
433: writeLong((Long) value);
434: } else if (value instanceof Float) {
435: writeFloat((Float) value);
436: } else if (value instanceof String) {
437: writeUTF(value.toString());
438: } else if (value instanceof byte[]) {
439: writeBytes((byte[]) value);
440: } else {
441: throw new MessageFormatException(
442: "Cannot write non-primitive type:"
443: + value.getClass());
444: }
445: }
446:
447: public void reset() throws JMSException {
448: // super.readOnlyMessage = true;
449: if (this .dataOut != null) {
450: try {
451: this .dataOut.flush();
452: dataOut.close();
453: } catch (IOException ioe) {
454: JMSException jmsEx = new JMSException("reset failed: "
455: + ioe.getMessage());
456: jmsEx.setLinkedException(ioe);
457: throw jmsEx;
458: }
459: }
460: this .dataIn = null;
461: this .dataOut = null;
462: }
463:
464: private void preWrite() {
465: // if (super.readOnlyMessage) {
466: // throw new MessageNotWriteableException("This message is in read-only mode");
467: // }
468: if (this .dataOut == null) {
469: this .buffer = new Buffer();
470: this .dataOut = new DataOutputStream(this .buffer);
471: }
472: }
473:
474: private void preRead() {
475: // if (!super.readOnlyMessage) {
476: // throw new MessageNotReadableException("This message is in write-only mode");
477: // }
478: if (this .dataIn == null) {
479: this .dataIn = new DataInputStream(buffer.newInputStream());
480: }
481: }
482:
483: private static final long serialVersionUID = 1L;
484: }
|