CreateInstance() public static method

Creates a new CodedOutputStream which write to the given stream.
public static CreateInstance ( Stream output ) : CodedOutputStream
output Stream
return CodedOutputStream
Beispiel #1
0
        public void TestSlowPathAvoidance()
        {
            using (var ms = new MemoryStream())
            {
                CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
                output.WriteField(FieldType.Bytes, 1, "bytes", ByteString.CopyFrom(new byte[100]));
                output.WriteField(FieldType.Bytes, 2, "bytes", ByteString.CopyFrom(new byte[100]));
                output.Flush();

                ms.Position = 0;
                CodedInputStream input = CodedInputStream.CreateInstance(ms, new byte[ms.Length / 2]);

                uint       tag;
                string     ignore;
                ByteString value;

                Assert.IsTrue(input.ReadTag(out tag, out ignore));
                Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
                value = ByteString.Empty;
                Assert.IsTrue(input.ReadBytes(ref value) && value.Length == 100);

                Assert.IsTrue(input.ReadTag(out tag, out ignore));
                Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
                value = ByteString.Empty;
                Assert.IsTrue(input.ReadBytes(ref value) && value.Length == 100);
            }
        }
Beispiel #2
0
        public void ReadInvalidUtf8()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = CodedOutputStream.CreateInstance(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(1);
            output.WriteRawBytes(new byte[] { 0x80 });
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = CodedInputStream.CreateInstance(ms);

            uint   testtag;
            string ignored;

            Assert.IsTrue(input.ReadTag(out testtag, out ignored));
            Assert.AreEqual(tag, testtag);
            string text = null;

            input.ReadString(ref text);
            Assert.AreEqual('\ufffd', text[0]);
        }
Beispiel #3
0
        public void TestNegativeEnumArray()
        {
            int arraySize = 1 + 1 + (11 * 5);
            int msgSize   = arraySize;

            byte[]            bytes  = new byte[msgSize];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);

            output.WriteInt32Array(8, "", new int[] { 0, -1, -2, -3, -4, -5 });

            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = CodedInputStream.CreateInstance(bytes);
            uint             tag;
            string           name;

            Assert.IsTrue(input.ReadTag(out tag, out name));

            List <TestNegEnum>   values = new List <TestNegEnum>();
            ICollection <object> unk;

            input.ReadEnumArray(tag, name, values, out unk);

            Assert.AreEqual(2, values.Count);
            Assert.AreEqual(TestNegEnum.None, values[0]);
            Assert.AreEqual(TestNegEnum.Value, values[1]);

            Assert.IsNotNull(unk);
            Assert.AreEqual(4, unk.Count);
        }
Beispiel #4
0
        public void ReadMaliciouslyLargeBlob()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = CodedOutputStream.CreateInstance(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(0x7FFFFFFF);
            output.WriteRawBytes(new byte[32]); // Pad with a few random bytes.
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = CodedInputStream.CreateInstance(ms);
            uint             testtag;
            string           ignore;

            Assert.IsTrue(input.ReadTag(out testtag, out ignore));
            Assert.AreEqual(tag, testtag);

            try
            {
                ByteString bytes = null;
                input.ReadBytes(ref bytes);
                Assert.Fail("Should have thrown an exception!");
            }
            catch (InvalidProtocolBufferException)
            {
                // success.
            }
        }
        public void TestRoundTripNegativeEnums()
        {
            NegativeEnumMessage msg = NegativeEnumMessage.CreateBuilder()
                                      .SetValue(NegativeEnum.MinusOne)         //11
                                      .AddValues(NegativeEnum.Zero)            //2
                                      .AddValues(NegativeEnum.MinusOne)        //11
                                      .AddValues(NegativeEnum.FiveBelow)       //11
                                                                               //2
                                      .AddPackedValues(NegativeEnum.Zero)      //1
                                      .AddPackedValues(NegativeEnum.MinusOne)  //10
                                      .AddPackedValues(NegativeEnum.FiveBelow) //10
                                      .Build();

            Assert.AreEqual(58, msg.SerializedSize);

            byte[]            bytes  = new byte[58];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);

            msg.WriteTo(output);
            Assert.AreEqual(0, output.SpaceLeft);

            NegativeEnumMessage copy = NegativeEnumMessage.ParseFrom(bytes);

            Assert.AreEqual(msg, copy);
        }
Beispiel #6
0
        /// <summary>
        /// Serializes the message and writes it to <paramref name="output"/>. This is
        /// just a trivial wrapper around WriteTo(ICodedOutputStream).
        /// </summary>
        /// <param name="output"></param>
        public void WriteTo(Stream output)
        {
            CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);

            WriteTo(codedOutput);
            codedOutput.Flush();
        }
        public void TestNegativeEnumArray()
        {
            int arraySize = 1 + 1 + (11 * 5);
            int msgSize   = arraySize;

            byte[]            bytes  = new byte[msgSize];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);

            output.WriteEnumArray(8, "", new int[] { 0, -1, -2, -3, -4, -5 });

            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = CodedInputStream.CreateInstance(bytes);
            uint             tag;
            string           name;

            Assert.IsTrue(input.ReadTag(out tag, out name));

            List <int> values = new List <int>();

            input.ReadInt32Array(tag, name, values);

            Assert.AreEqual(6, values.Count);
            for (int i = 0; i > -6; i--)
            {
                Assert.AreEqual(i, values[Math.Abs(i)]);
            }
        }
Beispiel #8
0
        public void ReadMaliciouslyLargeBlob()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = CodedOutputStream.CreateInstance(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(0x7FFFFFFF);
            output.WriteRawBytes(new byte[32]); // Pad with a few random bytes.
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = CodedInputStream.CreateInstance(ms);
            uint             testtag;
            string           ignore;

            Assert.IsTrue(input.ReadTag(out testtag, out ignore));
            Assert.AreEqual(tag, testtag);

            ByteString bytes = null;

            // TODO(jonskeet): Should this be ArgumentNullException instead?
            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadBytes(ref bytes));
        }
Beispiel #9
0
        public void WriteDelimitedTo(Stream output)
        {
            CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);

            codedOutput.WriteRawVarint32((uint)SerializedSize);
            WriteTo(codedOutput);
            codedOutput.Flush();
        }
Beispiel #10
0
        public byte[] ToByteArray()
        {
            byte[]            array             = new byte[this.SerializedSize];
            CodedOutputStream codedOutputStream = CodedOutputStream.CreateInstance(array);

            this.WriteTo(codedOutputStream);
            codedOutputStream.CheckNoSpaceLeft();
            return(array);
        }
Beispiel #11
0
        /// <summary>
        /// Serializes the message to a byte array and returns it. This is
        /// just a trivial wrapper around WriteTo(ICodedOutputStream).
        /// </summary>
        /// <returns></returns>
        public byte[] ToByteArray()
        {
            byte[]            data   = new byte[SerializedSize];
            CodedOutputStream output = CodedOutputStream.CreateInstance(data);

            WriteTo(output);
            output.CheckNoSpaceLeft();
            return(data);
        }
        public void TestNegativeEnumNoTag()
        {
            Assert.AreEqual(10, CodedOutputStream.ComputeInt32SizeNoTag(-2));
            Assert.AreEqual(10, CodedOutputStream.ComputeEnumSizeNoTag(-2));

            byte[]            bytes  = new byte[10];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);

            output.WriteEnumNoTag(-2);

            Assert.AreEqual(0, output.SpaceLeft);
            Assert.AreEqual("FE-FF-FF-FF-FF-FF-FF-FF-FF-01", BitConverter.ToString(bytes));
        }
        public void TestNegativeEnumWithTag()
        {
            Assert.AreEqual(11, CodedOutputStream.ComputeInt32Size(8, -2));
            Assert.AreEqual(11, CodedOutputStream.ComputeEnumSize(8, -2));

            byte[]            bytes  = new byte[11];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);

            output.WriteEnum(8, "", -2, -2);

            Assert.AreEqual(0, output.SpaceLeft);
            //fyi, 0x40 == 0x08 << 3 + 0, field num + wire format shift
            Assert.AreEqual("40-FE-FF-FF-FF-FF-FF-FF-FF-FF-01", BitConverter.ToString(bytes));
        }
        /// <summary>
        /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
        /// checks that the result matches the given bytes
        /// </summary>
        private static void AssertWriteVarint(byte[] data, ulong value)
        {
            // Only do 32-bit write if the value fits in 32 bits.
            if ((value >> 32) == 0)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);
                output.WriteRawVarint32((uint)value);
                output.Flush();
                Assert.AreEqual(data, rawOutput.ToArray());
                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint)value));
            }

            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);
                output.WriteRawVarint64(value);
                output.Flush();
                Assert.AreEqual(data, rawOutput.ToArray());

                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
            }

            // Try different buffer sizes.
            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
            {
                // Only do 32-bit write if the value fits in 32 bits.
                if ((value >> 32) == 0)
                {
                    MemoryStream      rawOutput = new MemoryStream();
                    CodedOutputStream output    =
                        CodedOutputStream.CreateInstance(rawOutput, bufferSize);
                    output.WriteRawVarint32((uint)value);
                    output.Flush();
                    Assert.AreEqual(data, rawOutput.ToArray());
                }

                {
                    MemoryStream      rawOutput = new MemoryStream();
                    CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
                    output.WriteRawVarint64(value);
                    output.Flush();
                    Assert.AreEqual(data, rawOutput.ToArray());
                }
            }
        }
        public void WriteWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    =
                    CodedOutputStream.CreateInstance(rawOutput, blockSize);
                message.WriteTo(output);
                output.Flush();
                TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray());
            }
        }
        /// <summary>
        /// Parses the given bytes using WriteRawLittleEndian32() and checks
        /// that the result matches the given value.
        /// </summary>
        private static void AssertWriteLittleEndian32(byte[] data, uint value)
        {
            MemoryStream      rawOutput = new MemoryStream();
            CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);

            output.WriteRawLittleEndian32(value);
            output.Flush();
            TestUtil.AssertBytesEqual(data, rawOutput.ToArray());

            // Try different buffer sizes.
            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
            {
                rawOutput = new MemoryStream();
                output    = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
                output.WriteRawLittleEndian32(value);
                output.Flush();
                TestUtil.AssertBytesEqual(data, rawOutput.ToArray());
            }
        }
        /// <summary>
        /// Parses the given bytes using WriteRawLittleEndian64() and checks
        /// that the result matches the given value.
        /// </summary>
        private static void AssertWriteLittleEndian64(byte[] data, ulong value)
        {
            MemoryStream      rawOutput = new MemoryStream();
            CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);

            output.WriteRawLittleEndian64(value);
            output.Flush();
            Assert.AreEqual(data, rawOutput.ToArray());

            // Try different block sizes.
            for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
            {
                rawOutput = new MemoryStream();
                output    = CodedOutputStream.CreateInstance(rawOutput, blockSize);
                output.WriteRawLittleEndian64(value);
                output.Flush();
                Assert.AreEqual(data, rawOutput.ToArray());
            }
        }
        public void TestCodedInputOutputPosition()
        {
            byte[] content = new byte[110];
            for (int i = 0; i < content.Length; i++)
            {
                content[i] = (byte)i;
            }

            byte[] child = new byte[120];
            {
                MemoryStream      ms   = new MemoryStream(child);
                CodedOutputStream cout = CodedOutputStream.CreateInstance(ms, 20);
                // Field 11: numeric value: 500
                cout.WriteTag(11, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32NoTag(500);
                Assert.AreEqual(3, cout.Position);
                //Field 12: length delimited 120 bytes
                cout.WriteTag(12, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytesNoTag(ByteString.CopyFrom(content));
                Assert.AreEqual(115, cout.Position);
                // Field 13: fixed numeric value: 501
                cout.WriteTag(13, WireFormat.WireType.Fixed32);
                Assert.AreEqual(116, cout.Position);
                cout.WriteSFixed32NoTag(501);
                Assert.AreEqual(120, cout.Position);
                cout.Flush();
            }

            byte[] bytes = new byte[130];
            {
                CodedOutputStream cout = CodedOutputStream.CreateInstance(bytes);
                // Field 1: numeric value: 500
                cout.WriteTag(1, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32NoTag(500);
                Assert.AreEqual(3, cout.Position);
                //Field 2: length delimited 120 bytes
                cout.WriteTag(2, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytesNoTag(ByteString.CopyFrom(child));
                Assert.AreEqual(125, cout.Position);
                // Field 3: fixed numeric value: 500
                cout.WriteTag(3, WireFormat.WireType.Fixed32);
                Assert.AreEqual(126, cout.Position);
                cout.WriteSFixed32NoTag(501);
                Assert.AreEqual(130, cout.Position);
                cout.Flush();
            }
            //Now test Input stream:
            {
                CodedInputStream cin = CodedInputStream.CreateInstance(new MemoryStream(bytes), new byte[50]);
                uint             tag;
                int    intValue = 0;
                string ignore;
                Assert.AreEqual(0, cin.Position);
                // Field 1:
                Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 1);
                Assert.AreEqual(1, cin.Position);
                Assert.IsTrue(cin.ReadInt32(ref intValue) && intValue == 500);
                Assert.AreEqual(3, cin.Position);
                //Field 2:
                Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 2);
                Assert.AreEqual(4, cin.Position);
                uint childlen = cin.ReadRawVarint32();
                Assert.AreEqual(120u, childlen);
                Assert.AreEqual(5, cin.Position);
                int oldlimit = cin.PushLimit((int)childlen);
                Assert.AreEqual(5, cin.Position);
                // Now we are reading child message
                {
                    // Field 11: numeric value: 500
                    Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 11);
                    Assert.AreEqual(6, cin.Position);
                    Assert.IsTrue(cin.ReadInt32(ref intValue) && intValue == 500);
                    Assert.AreEqual(8, cin.Position);
                    //Field 12: length delimited 120 bytes
                    Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 12);
                    Assert.AreEqual(9, cin.Position);
                    ByteString bstr = null;
                    Assert.IsTrue(cin.ReadBytes(ref bstr) && bstr.Length == 110 && bstr.ToByteArray()[109] == 109);
                    Assert.AreEqual(120, cin.Position);
                    // Field 13: fixed numeric value: 501
                    Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 13);
                    // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit
                    Assert.AreEqual(121, cin.Position);
                    Assert.IsTrue(cin.ReadSFixed32(ref intValue) && intValue == 501);
                    Assert.AreEqual(125, cin.Position);
                    Assert.IsTrue(cin.IsAtEnd);
                }
                cin.PopLimit(oldlimit);
                Assert.AreEqual(125, cin.Position);
                // Field 3: fixed numeric value: 501
                Assert.IsTrue(cin.ReadTag(out tag, out ignore) && tag >> 3 == 3);
                Assert.AreEqual(126, cin.Position);
                Assert.IsTrue(cin.ReadSFixed32(ref intValue) && intValue == 501);
                Assert.AreEqual(130, cin.Position);
                Assert.IsTrue(cin.IsAtEnd);
            }
        }
Beispiel #19
0
 internal CodedBuilder(int size)
 {
     buffer = new byte[size];
     output = CodedOutputStream.CreateInstance(buffer);
 }
Beispiel #20
0
 /// <summary>
 /// Creates an instance which writes to the given stream.
 /// </summary>
 /// <param name="output">Stream to write messages to.</param>
 public MessageStreamWriter(Stream output)
 {
     codedOutput = CodedOutputStream.CreateInstance(output);
 }