WriteString32() публичный Метод

Method WriteString32, writes a string to the output using the Openwire standard modified UTF-8 encoding which an int value written first to indicate the length of the encoded data, the int is read as an signed value so the max amount of data this method can write is 2^31 encoded bytes. In the case of a null value being passed this method writes a -1 to the stream to indicate that the string is null. Because modified UTF-8 encding can result in a number of bytes greater that the size of the String this method must first check that the encoding proces will not result in a value that cannot be written becuase it is greater than the max value of an int.
public WriteString32 ( String text ) : void
text String A string
Результат void
Пример #1
0
 protected void SetContent(Message message, String text)
 {
     MemoryStream mstream = new MemoryStream();
     EndianBinaryWriter dataOut = new EndianBinaryWriter(mstream);
     dataOut.WriteString32(text);
     dataOut.Close();
     message.Content = mstream.ToArray();
 }
Пример #2
0
        public void testWriteString32_nullstring()
        {
            // test that a null strings writes a -1
            MemoryStream stream = new MemoryStream();
            EndianBinaryWriter writer = new EndianBinaryWriter(stream);
            writer.WriteString32(null);

            stream.Seek(0, SeekOrigin.Begin);
            EndianBinaryReader reader = new EndianBinaryReader(stream);
            Assert.AreEqual(-1, reader.ReadInt32());
        }
Пример #3
0
        void writeString32TestHelper(char[] input, byte[] expect)
        {
            MemoryStream stream = new MemoryStream();
            EndianBinaryWriter writer = new EndianBinaryWriter(stream);

            String str = new String(input);

            writer.WriteString32(str);

            byte[] result = stream.GetBuffer();

            Assert.AreEqual(result[0], 0x00);
            Assert.AreEqual(result[1], 0x00);
            Assert.AreEqual(result[2], 0x00);
            Assert.AreEqual(result[3], expect.Length);

            for(int i = 4; i < expect.Length; ++i)
            {
                Assert.AreEqual(result[i], expect[i - 4]);
            }
        }
Пример #4
0
        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;
            }
        }