01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.jms.support.converter;
18:
19: import java.io.ByteArrayOutputStream;
20:
21: import javax.jms.BytesMessage;
22: import javax.jms.JMSException;
23:
24: /**
25: * A subclass of {@link SimpleMessageConverter} for the JMS 1.0.2 specification,
26: * not relying on JMS 1.1 methods like SimpleMessageConverter itself.
27: * This class can be used for JMS 1.0.2 providers, offering the same functionality
28: * as SimpleMessageConverter does for JMS 1.1 providers.
29: *
30: * <p>The only difference to the default SimpleMessageConverter is that BytesMessage
31: * is handled differently: namely, without using the <code>getBodyLength()</code>
32: * method which has been introduced in JMS 1.1 and is therefore not available on a
33: * JMS 1.0.2 provider.
34: *
35: * @author Juergen Hoeller
36: * @since 1.1.1
37: * @see javax.jms.BytesMessage#getBodyLength()
38: */
39: public class SimpleMessageConverter102 extends SimpleMessageConverter {
40:
41: public static final int BUFFER_SIZE = 4096;
42:
43: /**
44: * Overrides superclass method to copy bytes from the message into a
45: * ByteArrayOutputStream, using a buffer, to avoid using the
46: * <code>getBodyLength()</code> method which has been introduced in
47: * JMS 1.1 and is therefore not available on a JMS 1.0.2 provider.
48: * @see javax.jms.BytesMessage#getBodyLength()
49: */
50: protected byte[] extractByteArrayFromMessage(BytesMessage message)
51: throws JMSException {
52: ByteArrayOutputStream baos = new ByteArrayOutputStream(
53: BUFFER_SIZE);
54: byte[] buffer = new byte[BUFFER_SIZE];
55: int bufferCount = -1;
56: while ((bufferCount = message.readBytes(buffer)) >= 0) {
57: baos.write(buffer, 0, bufferCount);
58: if (bufferCount < BUFFER_SIZE) {
59: break;
60: }
61: }
62: return baos.toByteArray();
63: }
64:
65: }
|