/// <summary>Tries to read the DICOM file header from a stream</summary>
        public static bool TryReadHeader(BufferedStreamReader input, out DicomFileMetaInformation fileMetaInformation)
        {
            fileMetaInformation = null;

            Span <byte> header = stackalloc byte[128 + BeforeFileMetaInformationLength.Length + 4 + AfterFileMetaInformationLength.Length];

            if (!TryReadAll(input, header))
            {
                return(false);
            }

            var before = header.Slice(128, BeforeFileMetaInformationLength.Length);
            var after  = header.Slice(128 + BeforeFileMetaInformationLength.Length + 4, AfterFileMetaInformationLength.Length);

            if (!before.SequenceEqual(BeforeFileMetaInformationLength) || !after.SequenceEqual(AfterFileMetaInformationLength))
            {
                return(false);
            }

            var metaInformationLength = BitConverter.ToUInt32(header.Slice(128 + BeforeFileMetaInformationLength.Length, 4));

            DicomFileMetaInformation tmp = null;

            input.Read((int)metaInformationLength - AfterFileMetaInformationLength.Length, () =>
            {
                var metaInformationReader = DicomStreamReader.Create(input, DicomUID.TransferSyntax.ExplicitVRLittleEndian);
                tmp = FileMetaInformationSerializer.Deserialize(metaInformationReader);
            });

            fileMetaInformation = tmp;
            return(true);
        }
        /// <summary>Writes a DICOM file header to a stream</summary>
        public static void WriteHeader(BufferedStreamWriter output, DicomFileMetaInformation fileMetaInformation)
        {
            output.WriteZeros(128);
            output.WriteBytes(BeforeFileMetaInformationLength);

            var fileMetaInformationLengthPosition = output.Position;

            output.WriteZeros(4);
            output.WriteBytes(AfterFileMetaInformationLength);

            var metaInformationWriter = DicomStreamWriter.Create(output, DicomUID.TransferSyntax.ExplicitVRLittleEndian);

            FileMetaInformationSerializer.Serialize(metaInformationWriter, fileMetaInformation);

            var endOfFileMetaInformationPosition = output.Position;

            output.Flush(FlushMode.Shallow);

            var fileMetaInformationLength = endOfFileMetaInformationPosition - (fileMetaInformationLengthPosition + 4);

            output.Stream.Seek(fileMetaInformationLengthPosition, SeekOrigin.Begin);
            output.Stream.Write(BitConverter.GetBytes((uint)fileMetaInformationLength));
            output.Stream.Seek(endOfFileMetaInformationPosition, SeekOrigin.Begin);
        }