static public FlexReadBuffer Deserialize(ILogReader S,
                                                 FlexReadBuffer flexBuf)
        {
            var messageSize = S.ReadInt(flexBuf._sizeBuf);

            flexBuf._curSize     = messageSize.Item1 + messageSize.Item2;
            flexBuf._curSizeSize = messageSize.Item2;
            if (flexBuf.Buffer != null)
            {
                throw new Exception("Flexbuffer should have been null in Deserialize");
            }
            flexBuf.GetBuffer();
            var bytesRead = S.ReadAllRequiredBytes(flexBuf._buffer, messageSize.Item2, messageSize.Item1);

            if (bytesRead < messageSize.Item1)
            {
                throw new Exception("Error deserializing buffer in stream");
            }
            return(flexBuf);
        }
        // Deserializes a byte array from a .net stream assuming that the first 4 bytes contains the length of the byte array to be
        // subsequently copied. If the specified number of bytes can't be read before hitting end of stream, an exception is thrown.
        static public async Task <FlexReadBuffer> DeserializeAsync(Stream S,
                                                                   FlexReadBuffer flexBuf)
        {
            var intReaderTask = S.ReadIntAsync(flexBuf._sizeBuf);
            var messageSize   = await intReaderTask;

            flexBuf._curSize     = messageSize.Item1 + messageSize.Item2;
            flexBuf._curSizeSize = messageSize.Item2;
            if (flexBuf.Buffer != null)
            {
                throw new Exception("Flexbuffer should have been null in Deserialize");
            }
            flexBuf.GetBuffer();
            var bytesReaderTask = S.ReadAllRequiredBytesAsync(flexBuf._buffer, messageSize.Item2, messageSize.Item1);
            var bytesRead       = await bytesReaderTask;

            if (bytesRead < messageSize.Item1)
            {
                throw new Exception("Error deserializing buffer in stream");
            }
            return(flexBuf);
        }