package org.jboss.mq;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import javax.jms.JMSException;
import javax.jms.MessageNotWriteableException;
import javax.jms.TextMessage;
public class SpyTextMessage extends SpyMessage implements Cloneable, TextMessage, Externalizable
{
private final static long serialVersionUID = 235726945332013953L;
String content;
private final static int chunkSize = 16384;
public void setText(String string) throws JMSException
{
if (header.msgReadOnly)
throw new MessageNotWriteableException("Cannot set the content; message is read-only");
content = string;
}
public String getText() throws JMSException
{
return content;
}
public void clearBody() throws JMSException
{
content = null;
super.clearBody();
}
public SpyMessage myClone() throws JMSException
{
SpyTextMessage result = MessagePool.getTextMessage();
result.copyProps(this);
result.content = this.content;
return result;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
super.readExternal(in);
byte type = in.readByte();
if (type == NULL)
{
content = null;
}
else
{
int chunksToRead = in.readInt();
int bufferSize = chunkSize * chunksToRead;
if (chunksToRead == 1)
{
int inSize = in.available();
if (inSize <= 0)
{
inSize = 256;
}
bufferSize = Math.min(inSize, bufferSize);
}
StringBuffer sb = new StringBuffer(bufferSize);
for (int i = 0; i < chunksToRead; i++)
{
sb.append(in.readUTF());
}
content = sb.toString();
}
}
public void writeExternal(ObjectOutput out) throws IOException
{
super.writeExternal(out);
if (content == null)
{
out.writeByte(NULL);
}
else
{
ArrayList v = new ArrayList();
int contentLength = content.length();
while (contentLength > 0)
{
int beginCopy = (v.size()) * chunkSize;
int endCopy = contentLength <= chunkSize ? beginCopy + contentLength : beginCopy + chunkSize;
String theChunk = content.substring(beginCopy, endCopy);
v.add(theChunk);
contentLength -= chunkSize;
}
out.writeByte(OBJECT);
out.writeInt(v.size());
for (int i = 0; i < v.size(); i++)
{
out.writeUTF((String) v.get(i));
}
}
}
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append("SpyTextMessage {\n").append(header).append('\n');
buffer.append("Body {\n text :").append(content).append('\n');
buffer.append("}\n}");
return buffer.toString();
}
}