1 22 package org.jboss.mq; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.util.ArrayList ; 29 30 import javax.jms.JMSException ; 31 import javax.jms.MessageNotWriteableException ; 32 import javax.jms.TextMessage ; 33 34 42 public class SpyTextMessage extends SpyMessage implements Cloneable , TextMessage , Externalizable 43 { 44 46 47 private final static long serialVersionUID = 235726945332013953L; 48 49 51 52 String content; 53 54 55 private final static int chunkSize = 16384; 56 57 59 61 63 65 public void setText(String string) throws JMSException 66 { 67 if (header.msgReadOnly) 68 throw new MessageNotWriteableException ("Cannot set the content; message is read-only"); 69 70 content = string; 71 } 72 73 public String getText() throws JMSException 74 { 75 return content; 76 } 77 78 80 public void clearBody() throws JMSException 81 { 82 content = null; 83 super.clearBody(); 84 } 85 86 public SpyMessage myClone() throws JMSException 87 { 88 SpyTextMessage result = MessagePool.getTextMessage(); 89 result.copyProps(this); 90 result.content = this.content; 91 return result; 92 } 93 94 96 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 97 { 98 super.readExternal(in); 99 byte type = in.readByte(); 100 101 if (type == NULL) 102 { 103 content = null; 104 } 105 else 106 { 107 109 int chunksToRead = in.readInt(); 113 int bufferSize = chunkSize * chunksToRead; 114 115 if (chunksToRead == 1) 117 { 118 124 int inSize = in.available(); 125 if (inSize <= 0) 126 { 127 inSize = 256; 128 } 129 130 bufferSize = Math.min(inSize, bufferSize); 131 } 132 133 StringBuffer sb = new StringBuffer (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 out) throws IOException 146 { 147 super.writeExternal(out); 148 149 if (content == null) 150 { 151 out.writeByte(NULL); 152 } 153 else 154 { 155 157 160 163 ArrayList v = new ArrayList (); 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 theChunk = content.substring(beginCopy, endCopy); 172 v.add(theChunk); 173 174 contentLength -= chunkSize; 175 } 176 177 out.writeByte(OBJECT); 180 out.writeInt(v.size()); 181 182 for (int i = 0; i < v.size(); i++) 183 { 184 out.writeUTF((String ) v.get(i)); 185 } 186 } 187 } 188 189 191 public String toString() 192 { 193 StringBuffer buffer = new StringBuffer (); 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 202 204 206 208 } | Popular Tags |