Example #1
0
 /// <summary>
 /// Writes a String value to the stream.
 /// </summary>
 public void WriteString(string value)
 {
     if (value == null)
     {
         writer.Write((byte)DataKind.Null);
     }
     else
     {
         int id;
         if (dataMap.TryGetId(value, out id))
         {
             if (id <= byte.MaxValue)
             {
                 writer.Write((byte)DataKind.StringRef_B);
                 writer.Write((byte)id);
             }
             else if (id <= ushort.MaxValue)
             {
                 writer.Write((byte)DataKind.StringRef_S);
                 writer.Write((ushort)id);
             }
             else
             {
                 writer.Write((byte)DataKind.StringRef);
                 writer.Write(id);
             }
         }
         else
         {
             dataMap.Add(value);
             writer.Write((byte)DataKind.String);
             writer.Write(value);
         }
     }
 }
Example #2
0
        /// <summary>
        /// Writes a String value to the stream.
        /// </summary>
        public unsafe void WriteString(string value)
        {
            if (value == null)
            {
                writer.Write((byte)DataKind.Null);
            }
            else
            {
                int id;
                if (dataMap.TryGetId(value, out id))
                {
                    Debug.Assert(id >= 0);
                    if (id <= byte.MaxValue)
                    {
                        writer.Write((byte)DataKind.StringRef_B);
                        writer.Write((byte)id);
                    }
                    else if (id <= ushort.MaxValue)
                    {
                        writer.Write((byte)DataKind.StringRef_S);
                        writer.Write((ushort)id);
                    }
                    else
                    {
                        writer.Write((byte)DataKind.StringRef);
                        writer.Write(id);
                    }
                }
                else
                {
                    dataMap.Add(value);

                    if (value.IsValidUnicodeString())
                    {
                        // Usual case - the string can be encoded as UTF8:
                        // We can use the UTF8 encoding of the binary writer.

                        writer.Write((byte)DataKind.StringUtf8);
                        writer.Write(value);
                    }
                    else
                    {
                        writer.Write((byte)DataKind.StringUtf16);

                        // This is rare, just allocate UTF16 bytes for simplicity.

                        byte[] bytes = new byte[(uint)value.Length * sizeof(char)];
                        fixed(char *valuePtr = value)
                        {
                            Marshal.Copy((IntPtr)valuePtr, bytes, 0, bytes.Length);
                        }

                        WriteCompressedUInt((uint)value.Length);
                        writer.Write(bytes);
                    }
                }
            }
        }