KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyTextMessage


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import javax.jms.JMSException JavaDoc;
31 import javax.jms.MessageNotWriteableException JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33
34 /**
35  * This class implements javax.jms.TextMessage
36  *
37  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
38  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
39  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
40  * @version $Revision: 37459 $
41  */

42 public class SpyTextMessage extends SpyMessage implements Cloneable JavaDoc, TextMessage JavaDoc, Externalizable JavaDoc
43 {
44    // Constants -----------------------------------------------------
45

46    /** The serialVersionUID */
47    private final static long serialVersionUID = 235726945332013953L;
48    
49    // Attributes ----------------------------------------------------
50

51    /** The content */
52    String JavaDoc content;
53
54    /** The chunkSize */
55    private final static int chunkSize = 16384;
56    
57    // Static --------------------------------------------------------
58

59    // Constructors --------------------------------------------------
60

61    // Public --------------------------------------------------------
62

63    // TextMessage implementation ------------------------------------
64

65    public void setText(String JavaDoc string) throws JMSException JavaDoc
66    {
67       if (header.msgReadOnly)
68          throw new MessageNotWriteableException JavaDoc("Cannot set the content; message is read-only");
69
70       content = string;
71    }
72
73    public String JavaDoc getText() throws JMSException JavaDoc
74    {
75       return content;
76    }
77    
78    // SpyMessage overrides ------------------------------------------
79

80    public void clearBody() throws JMSException JavaDoc
81    {
82       content = null;
83       super.clearBody();
84    }
85
86    public SpyMessage myClone() throws JMSException JavaDoc
87    {
88       SpyTextMessage result = MessagePool.getTextMessage();
89       result.copyProps(this);
90       result.content = this.content;
91       return result;
92    }
93    
94    // Externalizable implementation ---------------------------------
95

96    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
97    {
98       super.readExternal(in);
99       byte type = in.readByte();
100
101       if (type == NULL)
102         {
103          content = null;
104       }
105       else
106         {
107          // apply workaround for string > 64K bug in jdk's 1.3.*
108

109          // Read the no. of chunks this message is split into, allocate
110
// a StringBuffer that can hold all chunks, read the chunks
111
// into the buffer and set 'content' accordingly
112
int chunksToRead = in.readInt();
113          int bufferSize = chunkSize * chunksToRead;
114
115          // special handling for single chunk
116
if (chunksToRead == 1)
117            {
118             // The text size is likely to be much smaller than the chunkSize
119
// so set bufferSize to the min of the input stream available
120
// and the maximum buffer size. Since the input stream
121
// available() can be <= 0 we check for that and default to
122
// a small msg size of 256 bytes.
123

124             int inSize = in.available();
125             if (inSize <= 0)
126               {
127                inSize = 256;
128             }
129
130             bufferSize = Math.min(inSize, bufferSize);
131          }
132
133          // read off all of the chunks
134
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bufferSize);
135
136          for (int i = 0; i < chunksToRead; i++)
137            {
138             sb.append(in.readUTF());
139          }
140
141          content = sb.toString();
142       }
143    }
144
145    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
146    {
147       super.writeExternal(out);
148
149       if (content == null)
150         {
151          out.writeByte(NULL);
152       }
153       else
154         {
155          // apply workaround for string > 64K bug in jdk's 1.3.*
156

157          // Split content into chunks of size 'chunkSize' and assemble
158
// the pieces into a List ...
159

160          // FIXME: could calculate the number of chunks first, then
161
// write as we chunk for efficiency
162

163          ArrayList JavaDoc v = new ArrayList JavaDoc();
164          int contentLength = content.length();
165
166          while (contentLength > 0)
167            {
168             int beginCopy = (v.size()) * chunkSize;
169             int endCopy = contentLength <= chunkSize ? beginCopy + contentLength : beginCopy + chunkSize;
170
171             String JavaDoc theChunk = content.substring(beginCopy, endCopy);
172             v.add(theChunk);
173
174             contentLength -= chunkSize;
175          }
176
177          // Write out the type (OBJECT), the no. of chunks and finally
178
// all chunks that have been assembled previously
179
out.writeByte(OBJECT);
180          out.writeInt(v.size());
181
182          for (int i = 0; i < v.size(); i++)
183            {
184             out.writeUTF((String JavaDoc) v.get(i));
185          }
186       }
187    }
188
189    // Object override -----------------------------------------------
190

191    public String JavaDoc toString()
192    {
193       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
194       buffer.append("SpyTextMessage {\n").append(header).append('\n');
195       buffer.append("Body {\n text :").append(content).append('\n');
196       buffer.append("}\n}");
197       return buffer.toString();
198    }
199    
200    // Package protected ---------------------------------------------
201

202    // Protected -----------------------------------------------------
203

204    // Private -------------------------------------------------------
205

206    // Inner classes -------------------------------------------------
207

208    // Public --------------------------------------------------------
209
}
Popular Tags