KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > support > converter > SimpleMessageConverter102


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20
21 import javax.jms.BytesMessage JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23
24 /**
25  * A subclass of SimpleMessageConverter that uses the JMS 1.0.2 specification,
26  * rather than the JMS 1.1 methods used by 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     /**
45      * Overrides superclass method to copy bytes from the message into a
46      * ByteArrayOutputStream, using a buffer, to avoid using the
47      * <code>getBodyLength()</code> method which has been introduced in
48      * JMS 1.1 and is therefore not available on a JMS 1.0.2 provider.
49      * @see javax.jms.BytesMessage#getBodyLength()
50      */

51     protected byte[] extractByteArrayFromMessage(BytesMessage JavaDoc message) throws JMSException JavaDoc {
52         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(BUFFER_SIZE);
53         byte[] buffer = new byte[BUFFER_SIZE];
54         int bufferCount = -1;
55         while ((bufferCount = message.readBytes(buffer)) >= 0) {
56             baos.write(buffer, 0, bufferCount);
57             if (bufferCount < BUFFER_SIZE) {
58                 break;
59             }
60         }
61         return baos.toByteArray();
62     }
63
64 }
65
Popular Tags