| IOLink C# 1.11.0

Loading...
Searching...
No Matches
SerializeArray.cs

This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file. The code creates a 2D array of size 4x2, fills it with float values and writes it to a file. It then reads the data back and prints the content of the array.

using IOLink;
using System;
namespace Examples
{
public class SerializeArray
{
static void SerializeArrayXf()
{
// path to a temporary file in the temp directory
string tmpFilePath = System.IO.Path.GetTempPath() + "serialization_array_net.txt";
VectorXu64 shape = new VectorXu64(4, 2);
ArrayXf arrayf = new ArrayXf(shape);
// fill the array with some values, cell by cell
arrayf.SetAt(new VectorXu64(0, 0), 0.0F);
arrayf.SetAt(new VectorXu64(1, 0), 1.0F);
arrayf.SetAt(new VectorXu64(2, 0), 2.0F);
arrayf.SetAt(new VectorXu64(3, 0), 3.0F);
arrayf.SetAt(new VectorXu64(0, 1), 10.0F);
arrayf.SetAt(new VectorXu64(1, 1), 11.0F);
arrayf.SetAt(new VectorXu64(2, 1), 12.0F);
arrayf.SetAt(new VectorXu64(3, 1), 13.0F);
Console.WriteLine("Serialize array into " + tmpFilePath);
// open a streamAccess to serialize the array to a
// file
StreamAccess dst = StreamAccessFactory.OpenFileWrite(tmpFilePath);
if (dst == null)
{
throw new Exception("Impossible to open file " + tmpFilePath);
}
// array is serialized to the file
Serialization.EncodeArrayXf(arrayf, dst);
// flush and close the streamAccess
dst.Flush();
dst = null;
// open another streamAccess to read the serialized
// array from the file
StreamAccess src = StreamAccessFactory.OpenFileRead(tmpFilePath);
if (src == null)
{
throw new Exception("Impossible to open file " + tmpFilePath);
}
// array is deserialized from the file
ArrayXf arrayRead = Serialization.DecodeArrayXf(src);
// print the content of the array
Console.WriteLine("Content of unserialized array:" + arrayRead.ToString());
}
public static void Main(string[] args)
{
SerializeArrayXf();
Console.WriteLine("SUCCESS");
}
}
}
IOLink.ArrayXf
A multi dimensionnal array.
Definition ArrayXf.cs:27
IOLink.ArrayXf.ToString
string ToString()
Return a string representation.
Definition ArrayXf.cs:193
IOLink.ArrayXf.SetAt
void SetAt(VectorXu64 index, float value)
Set a value to an element of the array.
Definition ArrayXf.cs:180
IOLink.Serialization
Serialization class which provides methods to serialize and unserialize most of IOLink objects.
Definition Serialization.cs:15
IOLink.StreamAccessFactory
A factory to create StreamAccess instances from various ressources.
Definition StreamAccessFactory.cs:14
IOLink.StreamAccessFactory.OpenFileRead
static StreamAccess OpenFileRead(string filePath)
Create a stream that can read data from a file.
Definition StreamAccessFactory.cs:106
IOLink.StreamAccessFactory.OpenFileWrite
static StreamAccess OpenFileWrite(string filePath)
Create a stream that can write data to a file.
Definition StreamAccessFactory.cs:116
IOLink.StreamAccess
Mother interface for all stream-like data accessors.
Definition StreamAccess.cs:20
IOLink.StreamAccess.Flush
virtual void Flush()
Assure that all data is written to the endpoint.
Definition StreamAccess.cs:168
IOLink.VectorXu64
A dynamically sized arithmetic vector.
Definition VectorXu64.cs:14
Examples
Definition CreateDataFrame.cs:6
IOLink
Definition AccessCapabilities.cs:11