Apache.NMS.ActiveMQ.Commands.ActiveMQTextMessage.BeforeMarshall C# (CSharp) Method

BeforeMarshall() public method

public BeforeMarshall ( OpenWireFormat wireFormat ) : void
wireFormat Apache.NMS.ActiveMQ.OpenWire.OpenWireFormat
return void
        public override void BeforeMarshall(OpenWireFormat wireFormat)
        {
            base.BeforeMarshall(wireFormat);

            if(this.Content == null && text != null)
            {
                byte[] data = null;

                // Set initial size to the size of the string the UTF-8 encode could
                // result in more if there are chars that encode to multibye values.
                MemoryStream buffer = new MemoryStream(text.Length);
                Stream target = buffer;

                if(this.Connection != null && this.Connection.UseCompression)
                {
                    target = this.Connection.CompressionPolicy.CreateCompressionStream(target);
                    this.Compressed = true;
                }

                EndianBinaryWriter writer = new EndianBinaryWriter(target);
                writer.WriteString32(text);
                target.Close();
                data = buffer.ToArray();

                this.Content = data;
                this.text = null;
            }
        }

Usage Example

 public void TestGetBytes() 
 {
     ActiveMQTextMessage msg = new ActiveMQTextMessage();
     String str = "testText";
     msg.Text = str;
     msg.BeforeMarshall(null);
     
     byte[] bytes = msg.Content;
     msg = new ActiveMQTextMessage();
     msg.Content = bytes;
     
     Assert.AreEqual(msg.Text, str);
 }