/// <summary> /// Does the actual serialization of the given data structures. /// </summary> private static void Serialize(int begin, Stream stream, List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > > tagIndex, ObjectTable <string> stringTable) { RuntimeTypeModel typeModel = TagIndexSerializer.CreateTypeModel(); // move until after the index (index contains two int's, startoftagindex, endoffile). stream.Seek(begin + 8, SeekOrigin.Begin); // serialize string table. List <string> strings = new List <string>(); for (uint id = 0; id < stringTable.Count; id++) { strings.Add(stringTable.Get(id)); } stringTable.Clear(); stringTable = null; typeModel.Serialize(stream, strings); long startOfTagsIndex = stream.Position - begin; // serialize tagindex. typeModel.Serialize(stream, tagIndex); long endOfFile = stream.Position - begin; // write index. stream.Seek(begin, SeekOrigin.Begin); stream.Write(BitConverter.GetBytes((int)startOfTagsIndex), 0, 4); // write start position of tagindex. stream.Write(BitConverter.GetBytes((int)endOfFile), 0, 4); // write size of complete file. stream.Seek(begin + endOfFile, SeekOrigin.Begin); }
public static void Save() { if (_lockFile == null) { TraceLog.WriteLine("Lock file has not been acquired. Work document collection cannot be saved. "); return; } lock (Instance) { CleanEmptyBindVariables(); _allowBackupOverwrite &= File.Exists(_fileName); if (_allowBackupOverwrite) { File.Copy(_fileName, _backupFileName, true); } using (var file = File.Create(_fileName)) { Serializer.Serialize(file, Instance); } if (_allowBackupOverwrite) { File.Delete(_backupFileName); } } }
/// <summary> /// Given a serialized estimator (<paramref name="estimatorStream"/>), determine the size of the difference, create a Bloom filter for the difference and return that Bloom filter /// </summary> /// <param name="estimatorStream">The estimator</param> /// <param name="otherActor"></param> /// <returns></returns> public MemoryStream RequestFilter(MemoryStream estimatorStream, PrecalculatedActor <TCount> otherActor) { Console.WriteLine($"Estimator size: {estimatorStream.Length} "); var otherEstimator = (IHybridEstimatorData <int, TCount>) _protobufModel.Deserialize(estimatorStream, null, typeof(HybridEstimatorData <int, TCount>)); var estimate = _estimator.Decode(otherEstimator); if (estimate == null) { //additional communication step needed to create a new estimator. byte failedDecodeCount = 0; while (estimate == null && failedDecodeCount < 5) { var estimator = _hybridEstimatorFactory.Extract(_configuration, _estimator, failedDecodeCount); using (var stream = new MemoryStream()) { _protobufModel.Serialize(stream, estimator); stream.Position = 0; estimate = otherActor.GetEstimate(stream); } failedDecodeCount++; } if (estimate == null) { throw new NullReferenceException("Did not negotiate a good estimate"); } } var result = new MemoryStream(); _protobufModel.Serialize(result, _configuration.DataFactory.Extract(_configuration, _filter, estimate.Value)); result.Position = 0; Console.WriteLine($"Filter size: {result.Length} "); return(result); }
public static void TestModel(RuntimeTypeModel model, object value, string hex) { byte[] raw; using (MemoryStream ms = new MemoryStream()) { model.Serialize(ms, value); raw = ms.ToArray(); } Assert.Equal(hex, GetHex(raw)); model.CompileInPlace(); using (MemoryStream ms = new MemoryStream()) { model.Serialize(ms, value); raw = ms.ToArray(); } Assert.Equal(hex, GetHex(raw)); TypeModel compiled = model.Compile("compiled", "compiled.dll"); PEVerify.Verify("compiled.dll"); using (MemoryStream ms = new MemoryStream()) { compiled.Serialize(ms, value); raw = ms.ToArray(); } Assert.Equal(hex, GetHex(raw)); }
/// <summary> /// 使用protobuf序列化对象为二进制文件 /// </summary> /// <param name="obj"></param> /// <param name="serializeFilePath"></param> public static void Serialize(object obj, string serializeFilePath) { using (var fs = new FileStream(serializeFilePath, FileMode.Create)) { typeModel.Serialize(fs, obj); } }
public void WriteTo(Stream output) { var req = OriginalData == null ? new Proto.PaymentRequest() : (Proto.PaymentRequest)Serializer.DeepClone(OriginalData); req.pki_type = ToPKITypeString(PKIType); var certs = new Proto.X509Certificates(); if (this.MerchantCertificate != null) { certs.certificate.Add(MerchantCertificate.Export(X509ContentType.Cert)); } foreach (var cert in AdditionalCertificates) { certs.certificate.Add(cert.Export(X509ContentType.Cert)); } MemoryStream ms = new MemoryStream(); Serializer.Serialize(ms, certs); req.pki_data = ms.ToArray(); req.serialized_payment_details = Details.ToBytes(); req.signature = Signature; if (Details.Version != 1) { req.payment_details_version = Details.Version; } Serializer.Serialize(output, req); }
public void Serialize<T>(Stream destination, T instance) { if (instance != null) { protobufTypeModel.Serialize(destination, instance); } }
/// <summary> /// Initializes this target. /// </summary> public override void Initialize() { _currentEntities.Clear(); // write the mandatory header. _buffer.Seek(0, SeekOrigin.Begin); // create header block. var blockHeader = new HeaderBlock(); blockHeader.required_features.Add("OsmSchema-V0.6"); blockHeader.required_features.Add("DenseNodes"); _runtimeTypeModel.Serialize(_buffer, blockHeader); var blockHeaderData = _buffer.ToArray(); _buffer.SetLength(0); // create blob. var blob = new Blob(); blob.raw = blockHeaderData; _runtimeTypeModel.Serialize(_buffer, blob); // create blobheader. var blobHeader = new BlobHeader(); blobHeader.datasize = (int)_buffer.Length; blobHeader.indexdata = null; blobHeader.type = Encoder.OSMHeader; _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0); // flush to stream. _buffer.Seek(0, SeekOrigin.Begin); _buffer.CopyTo(_stream); }
public override byte[] SerializeEvent(object @event) { using (var stream = new MemoryStream()) { _typeModel.Serialize(stream, @event); return(stream.ToArray()); } }
public MemoryStream Serialize <T>(T value) { var ms = new MemoryStream(); Model.Serialize(ms, value); ms.Position = 0; return(ms); }
public override string Serialize(object serializable) { Initialize(); var ms = new MemoryStream(); _Model.Serialize(ms, serializable); ms.Flush(); ms.Position = 0; return(Convert.ToBase64String(ms.ToArray())); }
/// <summary> /// Initializes this target. /// </summary> public override void Initialize() { _currentEntities.Clear(); // write the mandatory header. _buffer.Seek(0, SeekOrigin.Begin); // create header block. var blockHeader = new HeaderBlock(); blockHeader.required_features.Add("OsmSchema-V0.6"); blockHeader.required_features.Add("DenseNodes"); _runtimeTypeModel.Serialize(_buffer, blockHeader); var blockHeaderData = _buffer.ToArray(); _buffer.SetLength(0); // create blob. var blob = new Blob(); if (_compress) { using (var target = new MemoryStream()) { using (var source = new MemoryStream(blockHeaderData)) using (var deflate = new DeflaterOutputStream(target)) { source.CopyTo(deflate); } blob.zlib_data = target.ToArray(); } } else { blob.raw = blockHeaderData; } _runtimeTypeModel.Serialize(_buffer, blob); // create blobheader. var blobHeader = new BlobHeader(); blobHeader.datasize = (int)_buffer.Length; blobHeader.indexdata = null; blobHeader.type = Encoder.OSMHeader; _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0); // flush to stream. _buffer.Seek(0, SeekOrigin.Begin); _buffer.CopyTo(_stream); }
/// <summary> /// Given a serialized estimator (<paramref name="estimatorStream"/>), determine the size of the difference, create a Bloom filter for the difference and return that Bloom filter /// </summary> /// <param name="estimatorStream">The estimator</param> /// <param name="otherActor">The other party involved</param> /// <returns></returns> public MemoryStream RequestFilter(MemoryStream estimatorStream, Actor <TCount> otherActor) { Console.WriteLine($"Estimator size: {estimatorStream.Length} "); var otherEstimator = (IHybridEstimatorData <int, TCount>) _protobufModel.Deserialize(estimatorStream, null, typeof(HybridEstimatorData <int, TCount>)); var estimator = _hybridEstimatorFactory.CreateMatchingEstimator(otherEstimator, _configuration, _dataSet.LongCount()); foreach (var item in _dataSet) { estimator.Add(item); } var estimate = estimator.Decode(otherEstimator); if (estimate == null) { //additional communication step needed to create a new estimator. byte failedDecodeCount = 0; while (estimate == null && failedDecodeCount < 5) { estimator = _hybridEstimatorFactory.Create(_configuration, _dataSet.Count(), ++failedDecodeCount); foreach (var item in _dataSet) { estimator.Add(item); } using (var stream = new MemoryStream()) { _protobufModel.Serialize(stream, estimator.Extract()); stream.Position = 0; estimate = otherActor.GetEstimate(stream); } } if (estimate == null) { throw new NullReferenceException("Did not negotiate a good estimate"); } } var filter = _bloomFilterFactory.Create(_configuration, estimate.Value, 0.001F, true); foreach (var item in _dataSet) { filter.Add(item); } var result = new MemoryStream(); _protobufModel.Serialize(result, filter.Extract()); result.Position = 0; Console.WriteLine($"Filter size: {result.Length} "); return(result); }
static void Main() { var obj = new MyObject { Name = "abc", Value = 123, Items = new List <MyOtherObject> { new MyOtherObject(), new MyOtherObject(), new MyOtherObject(), new MyOtherObject(), } }; using (var file = File.Create("foo.bin")) { fatModel.Serialize(file, obj); } MyObject lite; using (var file = File.OpenRead("foo.bin")) { lite = (MyObject)liteModel.Deserialize( file, null, typeof(MyObject)); } }
/// <summary> /// Serializes a stream with a style index. /// </summary> /// <param name="stream"></param> /// <param name="index"></param> public void Serialize(Stream stream, RTreeMemoryIndex <Scene2DEntry> index) { Scene2DStyledIndex styleIndex = new Scene2DStyledIndex(); Scene2DRTreeSerializer serializer = new Scene2DRTreeSerializer(true, styleIndex); // serialize the tree and fill the styleindex. MemoryStream rTreeStream = new MemoryStream(); serializer.Serialize(rTreeStream, index); // serialize the index. MemoryStream indexStream = new MemoryStream(); RuntimeTypeModel typeModel = TypeModel.Create(); typeModel.Add(typeof(Scene2DStyledIndex), true); typeModel.Add(typeof(Icon2DStyle), true); typeModel.Add(typeof(Image2DStyle), true); typeModel.Add(typeof(Line2DStyle), true); typeModel.Add(typeof(Point2DStyle), true); typeModel.Add(typeof(Polygon2DStyle), true); typeModel.Add(typeof(Text2DStyle), true); typeModel.Add(typeof(LineText2DStyle), true); typeModel.Serialize(indexStream, styleIndex); // write to the final stream. byte[] indexSizeBytes = BitConverter.GetBytes((int)indexStream.Length); stream.Write(indexSizeBytes, 0, indexSizeBytes.Length); indexStream.Seek(0, SeekOrigin.Begin); indexStream.WriteTo(stream); rTreeStream.WriteTo(stream); indexStream.Dispose(); rTreeStream.Dispose(); }
public void RuntimeTypeModelSerializeInt() { using (var stream = new MemoryStream()) { _runtimeTypeModel.Serialize(stream, 42); } }
private void Write <T>(string path, T content, RuntimeTypeModel model) { using (var fileStream = File.Open(path, FileMode.Create)) { model.Serialize(fileStream, content); } }
public static byte[] Serialize <T>(T data, RuntimeTypeModel typeModel) { using (var stream = new MemoryStream()) { typeModel.Serialize(stream, data); return(stream.ToArray()); } }
public void Serialise <T>(T instance, string fileName) { using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { RuntimeTypeModel model = GetProtocolBufferModel(typeof(T)); model.Serialize(file, instance); } }
public static byte[] Serialize <T>(T obj) { using (MemoryStream ms = new MemoryStream()) { m_RuntimeTypeModel.Serialize(ms, obj); return(ms.ToArray()); } }
/// <summary> /// Serializes an object to a payload /// </summary> protected override byte[] Serialize <T>(T value) { using (var ms = new MemoryStream()) { _model.Serialize(ms, value, context: null); return(ms.ToArray()); } }
private static T Roundtrip <T>(RuntimeTypeModel model, T obj) { var stream = new MemoryStream(); model.Serialize(stream, obj); stream.Seek(0, SeekOrigin.Begin); return(model.Deserialize <T>(stream)); }
public byte[] Serialize(Object obj) { using (MemoryStream ms = new MemoryStream()) { _model.Serialize(ms, obj); return(ms.ToArray()); } }
public byte[] Serialize <T>(T obj) { using (var ms = new MemoryStream()) { _protobufModel.Serialize(ms, obj); return(ms.ToArray()); } }
public static byte[] SerializeProtocalBuffer(Object user, RuntimeTypeModel model) { using (MemoryStream ms = new System.IO.MemoryStream()) { model.Serialize(ms, user); return(ms.ToArray()); } }
public void Serialize(Stream destination, object message) { if (message == null) { return; } _model.Serialize(destination, message); }
internal static void Serialize <T>(T value, SerializationContext context, RuntimeTypeModel runtimeTypeModel) { using (var buffer = context.AsStream()) { runtimeTypeModel.Serialize(buffer, value); } context.Complete(); }
private static Byte[] Serialize(object obj) { using (var memory = new MemoryStream()) { typeModel.Serialize(memory, obj); Byte[] bytes = memory.ToArray(); return(bytes); } }
/// <summary> /// Serializes the object to string. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static string Serialize <T>(T data) { using (MemoryStream outputStream = new MemoryStream()) { TypeModel.Serialize(outputStream, data); return(Convert.ToBase64String(outputStream.GetBuffer(), 0, (int)outputStream.Length)); } }
public void Serialization_ValidNotification() { INotification example = new UserNotification { Email = "*****@*****.**", Name = "Test", }; using (var ms = new MemoryStream()) { _model.Serialize(ms, example); ms.Seek(0, SeekOrigin.Begin); var result = (INotification)_model.Deserialize(ms, null, typeof(INotification)); Assert.IsInstanceOf <UserNotification>(result); Assert.IsInstanceOf <EmailEnvironment>(result.Environment); Assert.AreEqual(example.Email, result.Email); Assert.AreEqual(example.Name, result.Name); } }