User: starksm
Date: 01/12/08 15:26:09
Modified: src/main/org/jboss/mq Tag: Branch_2_4 SpyMessage.java
SpyTextMessage.java
Log:
Integrate patch for text messages with a UTF encoding > 65535 bytes.
This adds a new message type and is compatible with earlier JBossMQ clients
as long as the text messages do not exceed the 65535 byte limitation.
Revision Changes Path
No revision
No revision
1.3.2.3 +13 -2 jbossmq/src/main/org/jboss/mq/SpyMessage.java
Index: SpyMessage.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpyMessage.java,v
retrieving revision 1.3.2.2
retrieving revision 1.3.2.3
diff -u -r1.3.2.2 -r1.3.2.3
--- SpyMessage.java 2001/11/06 22:51:27 1.3.2.2
+++ SpyMessage.java 2001/12/08 23:26:09 1.3.2.3
@@ -24,7 +24,7 @@
* @author Hiram Chirino ([EMAIL PROTECTED])
* @author David Maplesden ([EMAIL PROTECTED])
*
- * @version $Revision: 1.3.2.2 $
+ * @version $Revision: 1.3.2.3 $
*/
public class SpyMessage
implements Serializable, Message, Comparable, Externalizable
@@ -529,6 +529,7 @@
protected static final byte STREAM_MESS = 5;
protected static final byte ENCAP_MESS = 6;
protected static final byte SPY_MESS = 7;
+ protected static final byte TEXT_MESS_V2 = 8;
public static void writeMessage(SpyMessage message,ObjectOutput out)throws
IOException
{
@@ -546,7 +547,15 @@
out.writeByte(MAP_MESS);
}else if(message instanceof SpyTextMessage)
{
- out.writeByte(TEXT_MESS);
+ // Check for version 2 ...
+ SpyTextMessage tMsg = (SpyTextMessage) message;
+ if ( tMsg.isVersion2() )
+ {
+ out.writeByte(TEXT_MESS_V2);
+ } else
+ {
+ out.writeByte(TEXT_MESS);
+ }
}else if(message instanceof SpyStreamMessage)
{
out.writeByte(STREAM_MESS);
@@ -576,6 +585,7 @@
message = new SpyStreamMessage();
break;
case TEXT_MESS:
+ case TEXT_MESS_V2:
message = new SpyTextMessage();
break;
case ENCAP_MESS:
@@ -604,6 +614,7 @@
protected static final int STRING = 7;
protected static final int OBJECT = 8;
protected static final int NULL = 9;
+ protected static final int V2_MSG_INDICATOR = 10;
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
{
1.2.2.4 +116 -30 jbossmq/src/main/org/jboss/mq/SpyTextMessage.java
Index: SpyTextMessage.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpyTextMessage.java,v
retrieving revision 1.2.2.3
retrieving revision 1.2.2.4
diff -u -r1.2.2.3 -r1.2.2.4
--- SpyTextMessage.java 2001/10/01 06:31:58 1.2.2.3
+++ SpyTextMessage.java 2001/12/08 23:26:09 1.2.2.4
@@ -17,75 +17,161 @@
*
* @author Norbert Lataille ([EMAIL PROTECTED])
* @created August 16, 2001
- * @version $Revision: 1.2.2.3 $
+ * @version $Revision: 1.2.2.4 $
*/
public class SpyTextMessage
- extends SpyMessage
- implements Cloneable, TextMessage, Externalizable {
+ extends SpyMessage
+ implements Cloneable, TextMessage, Externalizable
+{
+ private final static long serialVersionUID = 235726945332013953L;
+
+ // Maximum length of a String so that it can be safely written
+ // using readUTF() / writeUTF() methods
+ public final static int MAX_UTF_LENGTH = 65535 / 3;
// Attributes ----------------------------------------------------
-
String content = null;
-
- private final static long serialVersionUID = 235726945332013953L;
-
+
// Public --------------------------------------------------------
-
+
public void setText( String string )
- throws JMSException {
- if ( msgReadOnly ) {
+ throws JMSException
+ {
+ if ( msgReadOnly )
+ {
throw new MessageNotWriteableException( "Cannot set the content" );
}
content = string;
}
-
+
public String getText()
- throws JMSException {
+ throws JMSException
+ {
return content;
}
-
+
public void clearBody()
- throws JMSException {
+ throws JMSException
+ {
content = null;
super.clearBody();
}
-
+
public SpyMessage myClone()
- throws JMSException {
+ throws JMSException
+ {
SpyTextMessage result = new SpyTextMessage();
result.copyProps( this );
result.content = this.content;
return result;
}
+
+ /** Test for version 2, i.e. if the content can be written out in
+ * one chunk of UTF data or if the data has to be split into more
+ * parts based on the DataOutput.writeUTF limitation of only allowing
+ * 65535 bytes per write.
+ */
+ final public boolean isVersion2()
+ {
+ // This is very conservative but cheap
+ int length = content.length();
+ boolean canWriteUTF = length < MAX_UTF_LENGTH;
+ // If canWriteUTF is false and length <= 65535, calculate the true length
+ if( canWriteUTF == false && length <= 65535 )
+ {
+ // Making a copy of the string is faster than calling charAt() repeatedly
+ char[] tmp = new char[length];
+ content.getChars(0, length, tmp, 0);
+ int writeUTFLength = 0;
+ for(int n = 0; n < length; n ++)
+ {
+ char c = tmp[n];
+ if( (c >= 0x0001) && (c <= 0x007F) )
+ writeUTFLength ++;
+ else if( c > 0x07FF )
+ writeUTFLength += 3;
+ else
+ writeUTFLength += 2;
+ }
+ canWriteUTF = writeUTFLength <= 65535;
+ }
+ return canWriteUTF;
+ }
public void readExternal( java.io.ObjectInput in )
- throws java.io.IOException, ClassNotFoundException {
+ throws java.io.IOException, ClassNotFoundException
+ {
super.readExternal( in );
byte type = in.readByte();
- if ( type == NULL ) {
+ if ( type == NULL )
+ {
content = null;
- } else {
+ } else if ( type == OBJECT ) // Version 1
+ {
content = in.readUTF();
+ } else // Version 2
+ {
+ int chunksToRead = in.readInt();
+ StringBuffer sb = new StringBuffer(MAX_UTF_LENGTH * chunksToRead);
+
+ for ( int i = 0; i < chunksToRead; i++ )
+ {
+ sb.append( in.readUTF() );
+ }
+ content = sb.toString();
}
}
-
+
public void writeExternal( java.io.ObjectOutput out )
- throws java.io.IOException {
+ throws java.io.IOException
+ {
super.writeExternal( out );
- if ( content == null ) {
+ if ( content == null )
+ {
out.writeByte( NULL );
- } else {
- out.writeByte( OBJECT );
- out.writeUTF( content );
+ } else
+ {
+ if ( isVersion2() ) // Version 2 ...
+ {
+ out.writeByte( V2_MSG_INDICATOR );
+
+ // Calculate number of UTF chunks to write
+ int chunks = content.length() / MAX_UTF_LENGTH;
+ if ( (content.length() % MAX_UTF_LENGTH) > 0 )
+ {
+ chunks++;
+ }
+
+ // write out # of chunks followed by the chunks ...
+ out.writeInt( chunks );
+ int beginCopy, endCopy;
+
+ for ( int i = 0; i < chunks; i++ )
+ {
+ beginCopy = i * MAX_UTF_LENGTH;
+ endCopy = beginCopy + MAX_UTF_LENGTH;
+ if ( endCopy > content.length() )
+ endCopy = content.length();
+
+ out.writeUTF( content.substring(beginCopy, endCopy) );
+ }
+
+ } else // Version 1 compatible
+ {
+ out.writeByte( OBJECT );
+ out.writeUTF( content );
+ }
}
}
-
+
// Object override -----------------------------------------------
-
- public String toString() {
- try {
+ public String toString()
+ {
+ try
+ {
return "TextMessage@" + getText();
- } catch ( JMSException e ) {
+ } catch ( JMSException e )
+ {
return "toString() failed !";
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development