Exemplo n.º 1
0
        public void MultipleFlushes_SameOutput(int count)
        {
            using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray))
            {
                writer.StepIn(IonType.Struct);
                writer.SetFieldName("key");
                writer.WriteString("value");
                writer.StepOut();

                writer.Flush();
                var size = _memoryStream.Length;
                for (var i = 0; i < count; i++)
                {
                    writer.Flush();
                    Assert.AreEqual(size, _memoryStream.Length);
                }

                writer.Finish();
                Assert.AreEqual(size, _memoryStream.Length);

                _memoryStream.Seek(0, SeekOrigin.Begin);
                var reader = new UserBinaryReader(_memoryStream);
                Assert.AreEqual(IonType.Struct, reader.MoveNext());
                reader.StepIn();
                Assert.AreEqual(IonType.String, reader.MoveNext());
                Assert.AreEqual("key", reader.CurrentFieldName);
                Assert.AreEqual("value", reader.StringValue());
                Assert.AreEqual(IonType.None, reader.MoveNext());
                reader.StepOut();
                Assert.AreEqual(IonType.None, reader.MoveNext());
            }
        }
Exemplo n.º 2
0
        public void FlushAfterFinish()
        {
            using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray))
            {
                writer.StepIn(IonType.Struct);
                writer.SetFieldName("key");
                writer.WriteString("value");
                writer.StepOut();

                writer.Finish();
                var size = _memoryStream.Length;

                writer.Flush();
                Assert.AreEqual(size + BinaryConstants.BinaryVersionMarkerLength, _memoryStream.Length);

                _memoryStream.Seek(0, SeekOrigin.Begin);
                var reader = new UserBinaryReader(_memoryStream);
                Assert.AreEqual(IonType.Struct, reader.MoveNext());
                reader.StepIn();
                Assert.AreEqual(IonType.String, reader.MoveNext());
                Assert.AreEqual("key", reader.CurrentFieldName);
                Assert.AreEqual("value", reader.StringValue());
                Assert.AreEqual(IonType.None, reader.MoveNext());
                reader.StepOut();
                //movenext() should skip over bvm
                Assert.AreEqual(IonType.None, reader.MoveNext());
            }
        }
Exemplo n.º 3
0
        public void WriteObjectWithAnnotations(int annotationCount)
        {
            using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray))
            {
                writer.StepIn(IonType.Struct);

                writer.SetFieldName("FieldName");
                for (var i = 0; i < annotationCount; i++)
                {
                    writer.AddTypeAnnotation($"annot_{i}");
                }

                writer.WriteString("FieldValue");

                writer.StepOut();
                writer.Flush();
            }

            var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer()));

            reader.MoveNext();
            reader.StepIn();
            reader.MoveNext();
            //load the value
            reader.StringValue();
            var annotations = reader.GetTypeAnnotations().ToList();

            for (var i = 0; i < annotationCount; i++)
            {
                Assert.IsTrue(annotations.Any(s => s.Text == $"annot_{i}"));
            }
        }
Exemplo n.º 4
0
        public void WriteStructWithSingleBlob(int blobSize)
        {
            var blob = new byte[blobSize];

            new Random().NextBytes(blob);
            using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray))
            {
                writer.StepIn(IonType.Struct);

                writer.SetFieldName("blob");
                writer.WriteBlob(blob);

                writer.StepOut();
                writer.Flush();
            }

            var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer()));

            reader.MoveNext();
            reader.StepIn();
            Assert.AreEqual(IonType.Blob, reader.MoveNext());
            var size = reader.GetLobByteSize();

            Assert.AreEqual(blobSize, size);
            var readBlob = new byte[size];

            reader.GetBytes(readBlob);

            Assert.IsTrue(blob.SequenceEqual(readBlob));
        }
Exemplo n.º 5
0
        public void WriteObjectWithAnnotations(int annotationCount)
        {
            using (var stream = new MemoryStream())
            {
                IIonWriter writer;
                using (writer = new ManagedBinaryWriter(BinaryConstants.EmptySymbolTablesArray))
                {
                    writer.StepIn(IonType.Struct);

                    writer.SetFieldName("FieldName");
                    for (var i = 0; i < annotationCount; i++)
                    {
                        writer.AddTypeAnnotation($"annot_{i}");
                    }

                    writer.WriteString("FieldValue");

                    writer.StepOut();
                    writer.Flush(stream);
                }

                var annotReader = new SaveAnnotationsReaderRoutine();
                var reader      = new UserBinaryReader(new MemoryStream(stream.ToArray()), annotReader);
                reader.MoveNext();
                reader.StepIn();
                reader.MoveNext();
                //load the value
                reader.StringValue();
                for (var i = 0; i < annotationCount; i++)
                {
                    Assert.IsTrue(annotReader.Symbols.Contains($"annot_{i}"));
                }
            }
        }
Exemplo n.º 6
0
        public void WriteLayersDeep(int depth)
        {
            List <(string key, object value)> kvps;

            using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray))
            {
                writer.StepIn(IonType.Struct);
                for (var i = 0; i < depth - 1; i++)
                {
                    writer.SetFieldName($"layer{i}");
                    writer.StepIn(IonType.Struct);
                }

                kvps = WriteFlat(writer);

                for (var i = 0; i < depth; i++)
                {
                    writer.StepOut();
                }

                writer.Flush();
            }

            var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer()));

            for (var i = 0; i < depth - 1; i++)
            {
                Console.WriteLine(i);
                Assert.AreEqual(IonType.Struct, reader.MoveNext());
                Console.WriteLine(reader.CurrentFieldName);
                reader.StepIn();
            }

            ReadUtils.AssertFlatStruct(reader, kvps);
        }
Exemplo n.º 7
0
        public void TestCurrentType()
        {
            var reader = new UserBinaryReader(new MemoryStream(new byte[] { 0xE0, 0x01, 0x00, 0xEA, 0x11 }));
            Assert.AreEqual(IonType.None, reader.CurrentType);

            reader.MoveNext();
            Assert.AreEqual(IonType.Bool, reader.CurrentType);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Deserialize a binary format to object type T
 /// </summary>
 /// <param name="binary">Binary input</param>
 /// <param name="scalarConverter"></param>
 /// <typeparam name="T">Type of object to deserialize to</typeparam>
 /// <returns>Deserialized object</returns>
 public T Deserialize <T>(byte[] binary, IScalarConverter scalarConverter = null)
 {
     using (var stream = new MemoryStream(binary))
     {
         var reader = new UserBinaryReader(stream, scalarConverter);
         reader.MoveNext();
         return((T)IonSerializationPrivate.Deserialize(reader, typeof(T), scalarConverter));
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Deserialize a binary format to object type T
 /// </summary>
 /// <param name="binary">Binary input</param>
 /// <typeparam name="T">Type of object to deserialize to</typeparam>
 /// <returns>Deserialized object</returns>
 public T Deserialize <T>(byte[] binary)
 {
     using (var stream = new MemoryStream(binary))
     {
         var reader = new UserBinaryReader(stream);
         reader.MoveNext();
         return((T)IonSerializationPrivate.Deserialize(reader, typeof(T)));
     }
 }
Exemplo n.º 10
0
        public void WriteNulls()
        {
            using (var stream = new MemoryStream())
            {
                IIonWriter writer;
                using (writer = new ManagedBinaryWriter(BinaryConstants.EmptySymbolTablesArray))
                {
                    writer.StepIn(IonType.Struct);

                    foreach (var iType in Enum.GetValues(typeof(IonType)))
                    {
                        if ((IonType)iType == IonType.Datagram || (IonType)iType == IonType.None)
                        {
                            continue;
                        }

                        var name = Enum.GetName(typeof(IonType), iType);
                        writer.SetFieldName($"null_{name}");
                        writer.WriteNull((IonType)iType);
                    }

                    writer.StepOut();
                    writer.Flush(stream);
                }

                var reader = new UserBinaryReader(new MemoryStream(stream.ToArray()));
                reader.MoveNext();
                reader.StepIn();

                foreach (var iType in Enum.GetValues(typeof(IonType)))
                {
                    if ((IonType)iType == IonType.Datagram || (IonType)iType == IonType.None)
                    {
                        continue;
                    }
                    var name = Enum.GetName(typeof(IonType), iType);
                    Assert.AreEqual((IonType)iType, reader.MoveNext());
                    Assert.AreEqual($"null_{name}", reader.CurrentFieldName);
                    Assert.IsTrue(reader.CurrentIsNull);
                }

                reader.StepOut();
            }
        }
Exemplo n.º 11
0
        public void Date_2000_11_20_8_20_15_Unknown()
        {
            var        data   = DirStructure.ReadDataFile("binary/ts_2000_11_20_8_20_15_unknown.bindat");
            IIonReader reader = new UserBinaryReader(new MemoryStream(data));

            Assert.AreEqual(IonType.Timestamp, reader.MoveNext());
            var timeStamp = reader.TimestampValue();
            var datetime  = new DateTime(2000, 11, 20, 8, 20, 15, DateTimeKind.Unspecified);

            Assert.AreEqual(datetime, timeStamp.DateTime);
        }
Exemplo n.º 12
0
        public void WriteValuesWithAnnotation()
        {
            var ionValues    = new List <IIonValue>();
            var valueFactory = new ValueFactory();

            ionValues.Add(valueFactory.NewBool(true));
            ionValues.Add(valueFactory.NewString("string"));
            ionValues.Add(valueFactory.NewFloat(22.22f));
            ionValues.Add(valueFactory.NewDecimal(3.4m));
            ionValues.Add(valueFactory.NewInt(3));
            ionValues.Add(valueFactory.NewNull());
            ionValues.Add(valueFactory.NewSymbol("symbol"));
            ionValues.Add(valueFactory.NewTimestamp(new Timestamp(DateTime.Now)));
            var arrayOfbytes          = Enumerable.Repeat <byte>(1, 10).ToArray();
            ReadOnlySpan <byte> bytes = new ReadOnlySpan <byte>(arrayOfbytes);

            ionValues.Add(valueFactory.NewClob(bytes));
            ionValues.Add(valueFactory.NewBlob(bytes));

            foreach (var ionValue in ionValues)
            {
                ionValue.AddTypeAnnotation("annotation");
            }

            var ms     = new MemoryStream();
            var writer = IonBinaryWriterBuilder.Build(ms);

            using (writer)
            {
                foreach (var ionValue in ionValues)
                {
                    ionValue.WriteTo(writer);
                }
                writer.Finish();
            }

            var reader = new UserBinaryReader(new MemoryStream(ms.GetWrittenBuffer()));

            while (reader.MoveNext() != IonType.None)
            {
                var annotation = reader.GetTypeAnnotations();
                Assert.AreEqual("annotation", annotation[0]);
            }
        }
Exemplo n.º 13
0
        public void Run(string[] args)
        {
            var outputStream = new MemoryStream();

            var writer = new ManagedBinaryWriter(new ISymbolTable[0]);

            writer.StepIn(IonType.Struct);

            writer.SetFieldName("yes");
            writer.WriteBool(true);

            writer.SetFieldName("strings");
            writer.WriteString("abcd def adsd dasdas tiếng việt  😂");

            writer.SetFieldName("number_struct");
            writer.StepIn(IonType.Struct);
            writer.SetFieldName("number");
            writer.WriteInt(int.MaxValue / 2);
            writer.StepOut();

            writer.SetFieldName("float_min");
            writer.WriteFloat(float.MinValue);

            writer.SetFieldName("float_rand");
            writer.WriteFloat(float.MaxValue / 2);

            writer.SetFieldName("double_max");
            writer.WriteFloat(double.MaxValue);

            writer.SetFieldName("double_rand");
            writer.WriteFloat(3.12345678901);

            writer.StepOut();
            writer.WriteInt(int.MaxValue);
            writer.Flush(outputStream);
            writer.Dispose();

            var bytes = outputStream.ToArray();

            Console.WriteLine(bytes.Length);

            Console.WriteLine(string.Join(" ", bytes.Select(b => $"{b:X2}")));

            var reader = new UserBinaryReader(new MemoryStream(bytes));

            reader.MoveNext();
            Console.WriteLine(reader.CurrentType);
            reader.StepIn();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.BoolValue());
            Console.WriteLine();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.StringValue());
            Console.WriteLine();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            reader.StepIn();
            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.IntValue());
            reader.StepOut();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.DoubleValue());
            Console.WriteLine();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.DoubleValue());
            Console.WriteLine();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.DoubleValue());
            Console.WriteLine();

            reader.MoveNext();
            Console.WriteLine(reader.CurrentFieldName);
            Console.WriteLine(reader.CurrentType);
            Console.WriteLine(reader.DoubleValue());
            Console.WriteLine();
        }
Exemplo n.º 14
0
        public void WriterReader()
        {
            var catalog = new SimpleCatalog();
            var table1  = SharedSymbolTable.NewSharedSymbolTable("table1", 1, null, new[] { "s1", "s2" });
            var table2  = SharedSymbolTable.NewSharedSymbolTable("table2", 1, null, new[] { "s3", "s4" });

            catalog.PutTable(table1);
            catalog.PutTable(table2);

            var stream = new MemoryStream();

            byte[] output;
            using (var binWriter = new ManagedBinaryWriter(stream, new[] { table1, table2 }))
            {
                binWriter.StepIn(IonType.Struct);
                binWriter.SetFieldName("s1");
                binWriter.WriteSymbol("s2");
                binWriter.SetFieldName("s3");
                binWriter.WriteSymbol("s4");
                binWriter.StepOut();
                binWriter.Finish();
                Assert.AreEqual(binWriter.SymbolTable.GetImportedMaxId(), binWriter.SymbolTable.MaxId);

                output = stream.ToArray();
            }

            var reader = IonReaderBuilder.Build(new MemoryStream(output));

            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            reader.StepIn();
            Assert.AreEqual(IonType.Symbol, reader.MoveNext());
            var fns = reader.GetFieldNameSymbol();

            Assert.AreEqual(10, fns.Sid);
            Assert.IsNull(fns.Text);
            Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName);
            Assert.AreEqual(11, reader.SymbolValue().Sid);

            Assert.AreEqual(IonType.Symbol, reader.MoveNext());
            fns = reader.GetFieldNameSymbol();
            Assert.AreEqual(12, fns.Sid);
            Assert.IsNull(fns.Text);
            Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName);
            Assert.AreEqual(13, reader.SymbolValue().Sid);

            //make sure that a reader with the correct imports can read it
            var reader2 = new UserBinaryReader(new MemoryStream(output), catalog);

            Assert.AreEqual(IonType.Struct, reader2.MoveNext());
            var localTable = reader2.GetSymbolTable();

            Console.WriteLine(localTable.Find("s1").ToString());
            reader2.StepIn();
            Assert.AreEqual(IonType.Symbol, reader2.MoveNext());
            Assert.AreEqual("s1", reader2.CurrentFieldName);
            Assert.AreEqual("s2", reader2.SymbolValue().Text);

            Assert.AreEqual(IonType.Symbol, reader2.MoveNext());
            Assert.AreEqual("s3", reader2.CurrentFieldName);
            Assert.AreEqual("s4", reader2.SymbolValue().Text);
        }