Ejemplo n.º 1
0
        /// <summary>
        /// This function parses the next frame from the stream and returns it.
        /// </summary>
        /// <param name="stream">The stream to parse the Frame from.</param>
        /// <param name="version">The ID3 v2 Major version of the ID3 tag that the stream is reading.</param>
        /// <param name="parserFactory">A FrameParserFactory to use to create FrameParsers based on Frame IDs.</param>
        /// <param name="frameID">Output: outputs the frameID of the frame just parsed.</param>
        /// <returns>The parsed Frame object.</returns>
        /// <exception cref="Achamenes.ID3.NoFrameParserProvidedException">
        /// Thrown if the FrameParserFactory object passed did not recognize the Frame ID for the given version.
        /// </exception>
        public static Frame Parse(System.IO.Stream stream, ID3v2MajorVersion version, FrameParserFactory parserFactory, out string frameID)
        {
            FrameHeaderParser headerParser=FrameHeaderParser.CreateFrameHeaderParser(version);
            FrameHeader header=headerParser.Parse(stream);
            frameID="";
            if(header==null) // have reached the padding, no more frames.
            {
                return null;
            }
            frameID=header.FrameID;

            if(header.Length > 128*128*128*128)
            {
                throw new FatalException("Invalid frame length for frame with frame ID '"+frameID+"'.");
            }

            byte[] frameData=new byte[header.Length];
            stream.Read(frameData, 0, frameData.Length);
            FrameParser parser=parserFactory.CreateFrameParser(version,header.FrameID);
            if(parser==null)
            {
                throw new NoFrameParserProvidedException(frameID,version, "No frame parser object is provided to parse this type of frame in this implementation.");
            }
            return parser.ParseFrame(frameData);
        }
Ejemplo n.º 2
0
        private void TestTextFrame(TextFrame frame, EncodingScheme encoding, ID3v2MajorVersion version)
        {
            string frameId;
            FrameParserFactory factory=new FrameParserFactory();
            FrameWriter writer=frame.CreateWriter(version, encoding);
            if(writer==null)
            {
                return;
            }
            MemoryStream stream=new MemoryStream();

            writer.WriteToStream(stream);
            stream.Seek(0, SeekOrigin.Begin);

            Frame writtenFrame=FrameParser.Parse(stream, version, factory, out frameId);

            Assert.IsInstanceOfType(writtenFrame.GetType(), frame);
            TextFrame textFrame=(TextFrame)writtenFrame;

            Assert.AreEqual(textFrame.Text, frame.Text);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This function parses the next frame from the stream and returns it.
        /// </summary>
        /// <param name="stream">The stream to parse the Frame from.</param>
        /// <param name="version">The ID3 v2 Major version of the ID3 tag that the stream is reading.</param>
        /// <param name="parserFactory">A FrameParserFactory to use to create FrameParsers based on Frame IDs.</param>
        /// <param name="frameID">Output: outputs the frameID of the frame just parsed.</param>
        /// <returns>The parsed Frame object.</returns>
        /// <exception cref="Achamenes.ID3.NoFrameParserProvidedException">
        /// Thrown if the FrameParserFactory object passed did not recognize the Frame ID for the given version.
        /// </exception>
        public static Frame Parse(System.IO.Stream stream, ID3v2MajorVersion version, FrameParserFactory parserFactory, out string frameID)
        {
            FrameHeaderParser headerParser = FrameHeaderParser.CreateFrameHeaderParser(version);
            FrameHeader       header       = headerParser.Parse(stream);

            frameID = "";
            if (header == null)          // have reached the padding, no more frames.
            {
                return(null);
            }
            frameID = header.FrameID;

            if (header.Length > 128 * 128 * 128 * 128)
            {
                throw new FatalException("Invalid frame length for frame with frame ID '" + frameID + "'.");
            }

            byte[] frameData = new byte[header.Length];
            stream.Read(frameData, 0, frameData.Length);
            FrameParser parser = parserFactory.CreateFrameParser(version, header.FrameID);

            if (parser == null)
            {
                throw new NoFrameParserProvidedException(frameID, version, "No frame parser object is provided to parse this type of frame in this implementation.");
            }
            return(parser.ParseFrame(frameData));
        }
Ejemplo n.º 4
0
		private ID3v2MajorVersion Read(Stream stream, FrameParserFactory frameParserFactory)
		{
			TagHeader header=TagHeader.FromStream(stream);
			if(header==null)
			{
				throw new FatalException("No ID3 v2 tag is attached to this file.");
			}
			if(!Enum.IsDefined(typeof(ID3v2MajorVersion),header.MajorVersion) )
			{
				throw new FatalException("Reading this major version of ID3 v2 is not supported.");
			}
			if(header.Flags!=TagHeaderFlags.None)
			{
				throw new FatalException("Reading tags with with any set flags are not supported in this version.");
			}
			long startingPosition=stream.Position;
			while(stream.Position-startingPosition <header.TagSize)
			{
				long beginPosition=stream.Position;
				Frame frame=null;
				string frameID="";
				try
				{					
					frame=FrameParser.Parse(stream, (ID3v2MajorVersion)header.MajorVersion, frameParserFactory, out frameID);
					if(frame==null)
					{
						break;
					}
				}
				catch(NonFatalException ex)
				{
					RaiseReadingWarning(ex,frameID);
				}
				if(beginPosition==stream.Position) // Probably stuck in an infinite loop because of corrupt data in file. Exit the loop.
				{
					break;
				}
				if(frame!=null)
				{
					this.Frames.Add(frame);
				}				
			}
			return (ID3v2MajorVersion)header.MajorVersion;
		}
Ejemplo n.º 5
0
		public virtual ID3v2MajorVersion ReadFromFile(string fileName, FrameParserFactory frameParserFactory)
		{
			FileStream stream=File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
			try
			{
				return Read(stream,frameParserFactory);
			}
			finally
			{
				if(stream!=null)
				{
					stream.Close();
				}
			}
		}