コード例 #1
0
        /// <summary>
        /// Determines whether a file header has and is large enough to store a FLAC metadata block.
        /// The block type is stored in bits 33-39 of the file stream, the first is of type "STREAMINFO".
        /// This block type corresponds to a value of 0.
        /// More information: https://xiph.org/flac/format.html#metadata_block_streaminfo.
        /// </summary>
        /// <param name="stream">The flac file stream.</param>
        /// <returns>Boolean indicating whether the file has a valid metadata block.</returns>
        public static Fin <bool> HasMetadataBlock(Stream stream)
        {
            long position = stream.Seek(BlockTypeOffset, SeekOrigin.Begin);

            Debug.Assert(position == 4, $"Expected stream.Seek position to return 4, instead returned {position}");

            Span <byte> buffer    = stackalloc byte[1];
            var         bytesRead = stream.Read(buffer);

            if (bytesRead < buffer.Length)
            {
                return(FileTooShortFlac);
            }

            return(stream.Length > MetadataBlockSize && BinaryHelpers.Read7BitUnsignedBigEndianIgnoringFirstBit(buffer) == 0);
        }