Esempio n. 1
0
        public static IEnumerable <T> ReadCollection <T>(IBinarySerialization <T> serialization, long offset = 0)
        {
            List <T> collection = new List <T>();
            int      count      = ReadInt32(offset);

            T   obj    = default(T);
            int length = 0;

            byte[] bytes = null;

            ReadAction <T> readAction = (binaryReader) =>
            {
                obj    = default(T);
                length = binaryReader.ReadInt32();
                bytes  = binaryReader.ReadBytes(length);
                obj    = serialization.Deserialize(bytes);
                return(obj);
            };

            long currentOffset = offset + TypeSizes.SIZE_INT;

            for (int k = 0; k < count; k++)
            {
                ExecuteRead <T>(readAction, currentOffset);
                collection.Add(obj);
                currentOffset = currentOffset + TypeSizes.SIZE_INT + length;
            }

            return(collection);
        }
Esempio n. 2
0
        public static T Read <T>(IBinarySerialization <T> serialization, long offset = 0)
        {
            ReadAction <T> readAction = (binaryReader) =>
            {
                T      obj    = default(T);
                int    length = binaryReader.ReadInt32();
                byte[] bytes  = binaryReader.ReadBytes(length);
                obj = serialization.Deserialize(bytes);
                return(obj);
            };

            return(ExecuteRead <T>(readAction, offset));
        }
        public static int Write <T>(T obj, IBinarySerialization <T> serialization, long offset = 0)
        {
            byte[] bytes  = serialization.Serialize(obj);
            int    length = bytes.Length;

            WriteAction writeAction = (binaryWriter) =>
            {
                binaryWriter.Write(length);
                binaryWriter.Write(bytes);

                return(TypeSizes.SIZE_INT + length);
            };

            return(ExecuteWrite(writeAction, offset));
        }
        public static int WriteCollection <T>(IEnumerable <T> collection, IBinarySerialization <T> serialization, long offset = 0)
        {
            int  count         = collection.Count();
            int  zc            = Write(count, offset);
            long currentOffset = offset + zc;
            int  totalSize     = zc;

            foreach (T obj in collection)
            {
                int size = Write <T>(obj, serialization, currentOffset);
                currentOffset = currentOffset + size;
                totalSize     = totalSize + size;
            }

            return(totalSize);
        }