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.ArrayList;
029:
030: import javax.jms.JMSException;
031: import javax.jms.MessageNotWriteableException;
032: import javax.jms.TextMessage;
033:
034: /**
035: * This class implements javax.jms.TextMessage
036: *
037: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
038: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
039: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
040: * @version $Revision: 57198 $
041: */
042: public class SpyTextMessage extends SpyMessage implements Cloneable,
043: TextMessage, Externalizable {
044: // Constants -----------------------------------------------------
045:
046: /** The serialVersionUID */
047: private final static long serialVersionUID = 235726945332013953L;
048:
049: // Attributes ----------------------------------------------------
050:
051: /** The content */
052: String content;
053:
054: /** The chunkSize */
055: private final static int chunkSize = 16384;
056:
057: // Static --------------------------------------------------------
058:
059: // Constructors --------------------------------------------------
060:
061: // Public --------------------------------------------------------
062:
063: // TextMessage implementation ------------------------------------
064:
065: public void setText(String string) throws JMSException {
066: if (header.msgReadOnly)
067: throw new MessageNotWriteableException(
068: "Cannot set the content; message is read-only");
069:
070: content = string;
071: }
072:
073: public String getText() throws JMSException {
074: return content;
075: }
076:
077: // SpyMessage overrides ------------------------------------------
078:
079: public void clearBody() throws JMSException {
080: content = null;
081: super .clearBody();
082: }
083:
084: public SpyMessage myClone() throws JMSException {
085: SpyTextMessage result = MessagePool.getTextMessage();
086: result.copyProps(this );
087: result.content = this .content;
088: return result;
089: }
090:
091: // Externalizable implementation ---------------------------------
092:
093: public void readExternal(ObjectInput in) throws IOException,
094: ClassNotFoundException {
095: super .readExternal(in);
096: byte type = in.readByte();
097:
098: if (type == NULL) {
099: content = null;
100: } else {
101: // apply workaround for string > 64K bug in jdk's 1.3.*
102:
103: // Read the no. of chunks this message is split into, allocate
104: // a StringBuffer that can hold all chunks, read the chunks
105: // into the buffer and set 'content' accordingly
106: int chunksToRead = in.readInt();
107: int bufferSize = chunkSize * chunksToRead;
108:
109: // special handling for single chunk
110: if (chunksToRead == 1) {
111: // The text size is likely to be much smaller than the chunkSize
112: // so set bufferSize to the min of the input stream available
113: // and the maximum buffer size. Since the input stream
114: // available() can be <= 0 we check for that and default to
115: // a small msg size of 256 bytes.
116:
117: int inSize = in.available();
118: if (inSize <= 0) {
119: inSize = 256;
120: }
121:
122: bufferSize = Math.min(inSize, bufferSize);
123: }
124:
125: // read off all of the chunks
126: StringBuffer sb = new StringBuffer(bufferSize);
127:
128: for (int i = 0; i < chunksToRead; i++) {
129: sb.append(in.readUTF());
130: }
131:
132: content = sb.toString();
133: }
134: }
135:
136: public void writeExternal(ObjectOutput out) throws IOException {
137: super .writeExternal(out);
138:
139: if (content == null) {
140: out.writeByte(NULL);
141: } else {
142: // apply workaround for string > 64K bug in jdk's 1.3.*
143:
144: // Split content into chunks of size 'chunkSize' and assemble
145: // the pieces into a List ...
146:
147: // FIXME: could calculate the number of chunks first, then
148: // write as we chunk for efficiency
149:
150: ArrayList v = new ArrayList();
151: int contentLength = content.length();
152:
153: while (contentLength > 0) {
154: int beginCopy = (v.size()) * chunkSize;
155: int endCopy = contentLength <= chunkSize ? beginCopy
156: + contentLength : beginCopy + chunkSize;
157:
158: String theChunk = content.substring(beginCopy, endCopy);
159: v.add(theChunk);
160:
161: contentLength -= chunkSize;
162: }
163:
164: // Write out the type (OBJECT), the no. of chunks and finally
165: // all chunks that have been assembled previously
166: out.writeByte(OBJECT);
167: out.writeInt(v.size());
168:
169: for (int i = 0; i < v.size(); i++) {
170: out.writeUTF((String) v.get(i));
171: }
172: }
173: }
174:
175: // Object override -----------------------------------------------
176:
177: public String toString() {
178: StringBuffer buffer = new StringBuffer();
179: buffer.append("SpyTextMessage {\n").append(header).append('\n');
180: buffer.append("Body {\n text :").append(content)
181: .append('\n');
182: buffer.append("}\n}");
183: return buffer.toString();
184: }
185:
186: // Package protected ---------------------------------------------
187:
188: // Protected -----------------------------------------------------
189:
190: // Private -------------------------------------------------------
191:
192: // Inner classes -------------------------------------------------
193:
194: // Public --------------------------------------------------------
195: }
|