Example #1
0
        /// <summary>
        /// Reads and returns the tag size from a stream.
        /// </summary>
        /// <param name="stream">The stream containing the tag.</param>
        /// <returns>The tag size from a stream.</returns>
        private static int ReadTagSize(Stream stream)
        {
            stream.Seek(TAG_SIZE_START, SeekOrigin.Begin);
            byte[] tagSizeBytes = new byte[TAG_SIZE_LENGTH];
            stream.Read(tagSizeBytes, 0, tagSizeBytes.Length);
            int tagSize = SynchsafeInteger.UnSynchsafe(tagSizeBytes);

            return(tagSize + TAG_HEADER_LENGTH);
        }
Example #2
0
        /// <summary>
        /// Reads and returns a frame from a stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="version">The ID3v2 version of the tag being parsed.</param>
        /// <returns>The frame read from the stream.</returns>
        public static ID3v2Frame ReadFrame(Stream stream, ID3Versions version)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            ID3v2Frame      frame      = null;
            FrameParameters parameters = new FrameParameters(version);

            byte[] header    = new byte[parameters.HeaderLength];
            char[] idChars   = new char[parameters.IDLength];
            byte[] sizeBytes = new byte[parameters.SizeLength];
            byte[] flags     = new byte[parameters.FlagsLength];
            byte[] frameData;
            string frameID;
            int    size;

            stream.Read(header, 0, header.Length);

            Array.Copy(header, 0, idChars, 0, idChars.Length);
            Array.Copy(header, parameters.IDLength, sizeBytes, 0, sizeBytes.Length);
            Array.Copy(header, parameters.IDLength + parameters.SizeLength, flags, 0, flags.Length);

            if (idChars[0] != 0x0)
            {
                frameID = new String(idChars);
                if (parameters.SizeIsSynchSafe)
                {
                    size = SynchsafeInteger.UnSynchsafe(sizeBytes);
                }
                else
                {
                    size = EncodedInteger.ToInt(sizeBytes);
                }

                frameData = new byte[size];
                stream.Read(frameData, 0, frameData.Length);

                FrameType frameType = FrameRegistry.GetFrameType(frameID, version);
                frame = FrameRegistry.GetNewFrame(frameType);

                frame.Initialize(flags, frameData, version);
            }
            else
            {
                frame = null;
            }

            return(frame);
        }