using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.Globalization;
using System.Runtime.Serialization;
public struct struct_realTime
public int indexNum { get; set; }
public string currentTime { get; set; }
public string currentType { get; set; }
public static class MessagePackExtensions
const byte Array32 = 0xdd;
const int Array32HeaderLength = 5;
public static void AppendToFile<T>(Stream stream, T item)
throw new ArgumentNullException(nameof(stream));
throw new ArgumentException("!stream.CanSeek");
var buffer = new byte[Array32HeaderLength];
var read = stream.Read(buffer, 0, Array32HeaderLength);
FormatArray32Header(buffer, 1);
stream.Write(buffer, 0, Array32HeaderLength);
var count = ParseArray32Header(buffer, read);
FormatArray32Header(buffer, count + 1);
stream.Write(buffer, 0, Array32HeaderLength);
stream.Position = stream.Length;
MessagePackSerializer.Serialize(stream, item);
static void FormatArray32Header(byte [] buffer, uint value)
buffer[1] = unchecked((byte)(value >> 24));
buffer[2] = unchecked((byte)(value >> 16));
buffer[3] = unchecked((byte)(value >> 8));
buffer[4] = unchecked((byte)value);
static uint ParseArray32Header(byte [] buffer, int readCount)
if (readCount < 5 || buffer[0] != Array32)
throw new ArgumentException("Stream was not positioned on an Array32 header.");
var value = unchecked((uint)((buffer[i++] << 24) | (buffer[i++] << 16) | (buffer[i++] << 8) | buffer[i++]));
public static void Test()
var path = "Question58780225.bin";
foreach (var count in new [] { 1, 2, 14, 15, 16, 17, ushort.MaxValue - 4, ushort.MaxValue + 4 })
static void Test(string filename, int expectedNumberOfRootItemsInFile)
List<struct_realTime> list_temp = new List<struct_realTime>(innerCount);
for (int num=0; num < innerCount; num++)
list_temp.Add(new struct_realTime
Console.WriteLine("Testing: {0} repetitions of {1}", expectedNumberOfRootItemsInFile, MessagePackSerializer.ToJson(list_temp));
for (int i = 0; i < expectedNumberOfRootItemsInFile; i++)
using (var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
MessagePackExtensions.AppendToFile(fileStream, list_temp);
List<List<struct_realTime>> allItemsInFile;
using (var fileStream = File.OpenRead(filename))
allItemsInFile = MessagePackSerializer.Deserialize<List<List<struct_realTime>>>(fileStream);
Assert.IsTrue(allItemsInFile.Count == expectedNumberOfRootItemsInFile);
Assert.IsTrue(allItemsInFile.All(i => i.SequenceEqual(list_temp)));
Console.WriteLine(" Tests passed.");
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("MessagePack version: " + typeof(MessagePackSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];