Beispiel #1
0
        public override bool ReadFromStream(FileStream fStream, bool throwExceptions)
        {
            /*

             +-----------------------------+
             |      Header (10 bytes)      |
             +-----------------------------+
             |       Extended Header       |
             | (variable length, OPTIONAL) |
             +-----------------------------+
             |   Frames (variable length)  |
             +-----------------------------+
             |           Padding           |
             | (variable length, OPTIONAL) |
             +-----------------------------+
             | Footer (10 bytes, OPTIONAL) |
             +-----------------------------+

                ID3v2/file identifier      "ID3"
                     ID3v2 version              $04 00
                     ID3v2 flags                %abcd0000
                     ID3v2 size             4 * %0xxxxxxx

             */

            try
            {
                Clear();

                Loaded = false;
                Active = false;
                Changed = false;

                Logger.Logger.WriteToLog(String.Format("Reading TAG v2 ..."));

                if (fStream.Length<HeaderByteLength)
                {
                    throw new Exception(String.Format("File length < {0}",HeaderByteLength));
                }

                OriginalHeader = new byte[HeaderByteLength];

                fStream.Seek(0,0);
                fStream.Read(OriginalHeader,0,HeaderByteLength);

                byte[] id3tag2Indication = new byte[] {OriginalHeader[0],OriginalHeader[1],OriginalHeader[2]};

                VersionMajor = OriginalHeader[3];
                VersionRevision = OriginalHeader[4];

                var totalTagSize = new byte[] {0,0,0,0};
                for (var i=0;i<4;i++) totalTagSize[i] = OriginalHeader[6+i];

                if (!IsValid)
                {
                    Logger.Logger.WriteToLog("TAG v2 not found (or invalid header)");
                    return false;
                }

                TotalByteLength =  Convert.ToInt32(TAG2Frame.MakeID3v2Size(totalTagSize,7));

                if (FlagUnsynchronisation)
                {
                    throw new Exception("TAG v2 Unsynchronisation not supported yet");
                }

                if (FlagExtendedHeader)
                {
                    throw new Exception("Extended header not supported yet");
                }

                if (FlagFooter)
                {
                    throw new Exception("Usage of footer - not supported yet");
                }

                    long actSize = 0;
                    while(actSize<TotalByteLength-HeaderByteLength)
                    {
                        var frame = new TAG2Frame();
                        var ok = frame.ReadFromOpenedStream(fStream,VersionMajor,false);
                        if (!ok)
                        {
                            //if (throwExceptions) throw new Exception("Error reading frame");
                            if (Frames.Count == 0) Logger.Logger.WriteToLog("First Invalid frame found");
                            break;
                        }

                        actSize+=TAG2Frame.HeaderByteLength+frame.Size;

                        /*
                        Logger.Logger.WriteToLog(String.Format("Adding frame {0}",frame.Name));
                        Logger.Logger.WriteToLog("Name:"+frame.Name);
                        Logger.Logger.WriteToLog("Size:"+frame.Size);
                        Logger.Logger.WriteToLog("Value:"+frame.Value);
                        if (frame.Name != "APIC")
                        Logger.Logger.WriteToLog("Data:"+TAG2Frame.ByteArrayToDecimalStrings(frame.OriginalData));
                        Logger.Logger.WriteToLog("-------------");
                        */

                        AddFrame(frame);
                    }

                ParseBaseValues();

                Logger.Logger.WriteToLog(String.Format("TAG found: (Title:{0}, Artist:{1}, ...)",Title,Artist));

                Active = true;
                Loaded = true;
                Changed = false;

                return true;

            } catch (Exception ex)
            {
                Logger.Logger.WriteToLog(String.Format("Error while reading TAG v2"),ex);
                if (throwExceptions) throw;
                return false;
            }
        }