Inheritance: FileStream
Exemplo n.º 1
0
        /// <summary>
        /// Create new price class
        /// </summary>
        /// <param name="Data">Data to read price from</param>
        /// <param name="Length">maximum length of data</param>
        internal Price(FileStreamEx Data, int Length)
        {
            string temp = Data.ReadText(Length, TextEncodings.Ascii);
            if (temp.Length < 4)
                return;

            _Currency = temp.Substring(0, 3);
            _Value = temp.Substring(3, temp.Length - 3);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return file crc of the specified file
        /// </summary>
        public static string GetFileCRC(string filename)
        {
            if (!FileEx.Exists(filename))
            {
                return("");
            }

            Fesersoft.Hashing.crc32 crc32 = new Fesersoft.Hashing.crc32();
            using (FileStreamEx stmcheck = FileEx.OpenRead(filename))
            {
                uint crchash = (uint)crc32.CRC(stmcheck);
                stmcheck.Close();
                return(ConvertToHex(crchash));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create new LinkFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        /// <param name="Length"></param>
        internal LinkFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _FrameIdentifier = Data.ReadText(4, TextEncodings.Ascii);
            if (!ValidatingFrameID(_FrameIdentifier, ValidatingErrorTypes.ID3Error))
                return;
            Length -= 4;
            // There is 3 byte in article that i think it's not true
            // because frame identifier is 4 character

            // use Text variable as URL
            URL = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _AdditionalData = Data.ReadText(Length, TextEncodings.Ascii);
        }
Exemplo n.º 4
0
        public static string GetFileMD5(string filename)
        {
            if (!FileEx.Exists(filename))
            {
                return("");
            }
            MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();

            using (FileStreamEx stmcheck = FileEx.OpenRead(filename))
            {
                byte[] hash = csp.ComputeHash(stmcheck);
                stmcheck.Close();

                return(BitConverter.ToString(hash).Replace("-", "").ToLower());
            }
        }
Exemplo n.º 5
0
        public void When_reading_file_with_large_Lines_and_small_buffer_matches_result_of_streamreader()
        {
            var filename = $"{this.GetDataDirectory()}\\data\\LargeLineFile.txt";
            var sw = new Stopwatch();
            sw.Start();
            using (var fileStreamEx = new FileStreamEx(filename, 1000))
            {
                fileStreamEx.ReadLine();
                fileStreamEx.ReadLine();
                Action a = () => fileStreamEx.ReadLine();

                a.ShouldThrow<ArgumentException>();
            }
            sw.Stop();
            Console.WriteLine("Time: " + sw.ElapsedMilliseconds);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create new AttachedPictureFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        /// <param name="Length">MaxLength of frame</param>
        internal AttachedPictureFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _TextEncoding = (TextEncodings)Data.ReadByte();
            Length--;
            if (!IsValidEnumValue(_TextEncoding, ValidatingErrorTypes.ID3Error))
            {
                _MustDrop = true;
                return;
            }

            _MIMEType = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _PictureType = (PictureTypes)Data.ReadByte();
            Length--;

            _Description = Data.ReadText(Length, _TextEncoding, ref Length, true);

            _Data = Data.ReadData(Length);
        }
Exemplo n.º 7
0
 public void When_reading_file_with_medium_Lines_and_large_buffer_matches_result_of_streamreader()
 {
     var filename = $"{this.GetDataDirectory()}\\data\\MediumLineFile.txt";
     var sw = new Stopwatch();
     sw.Start();
     using (var fileStreamEx = new FileStreamEx(filename))
     {
         using (var streamEx = new StreamReader(filename))
         {
             Line line;
             while ((line = fileStreamEx.ReadLine()) != null)
             {
                 var streamText = streamEx.ReadLine();
                 var fileStreamText = line.Text;
                 streamText.ShouldBeEquivalentTo(fileStreamText);
             }
         }
     }
     sw.Stop();
     Console.WriteLine("Time: " + sw.ElapsedMilliseconds);
 }
Exemplo n.º 8
0
 public static bool FileCompare(string first, string second)
 {
     FileInfo F1 = new FileInfo(first);
     FileInfo F2 = new FileInfo(second);
     if(F1.Length != F2.Length)
     {
         return false;
     }
     using(FileStreamEx fs1 = new FileStreamEx(first, FileMode.Open))
     {
         using(FileStreamEx fs2 = new FileStreamEx(second, FileMode.Open))
         {
             int Blocksize = 65536;
             long Progress = 0;
             byte[] buffer = new byte[Blocksize];
             byte[] buffer2 = new byte[Blocksize];
             while (Progress < fs1.Length)
             {
                 if ((Progress + Blocksize) <= fs1.Length)
                 {
                     buffer = fs1.ReadBytes(Blocksize);
                     buffer2 = fs2.ReadBytes(Blocksize);
                     if (!CompareArray(buffer, buffer2))
                         return false;
                     Progress += Blocksize;
                 }
                 else
                 {
                     int Remaining = (int)(fs1.Length - Progress);
                     byte[] Rem1 = fs1.ReadBytes(Remaining);
                     byte[] Rem2 = fs2.ReadBytes(Remaining);
                     if (!CompareArray(Rem1, Rem2))
                         return false;
                     Progress += Remaining;
                 }
             }
         }
     }
     return true;
 }
Exemplo n.º 9
0
        /// <summary>
        /// New SynchronisedText
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Frame Flag</param>
        /// <param name="Data">FileStream contain current frame data</param>
        internal SynchronisedText(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _Syllables = new FramesCollection<Syllable>();

            TextEncoding = (TextEncodings)Data.ReadByte();
            if (!IsValidEnumValue(TextEncoding, ValidatingErrorTypes.ID3Error))
                return;

            Length--;

            _Language = new Language(Data);
            Length -= 3;

            _TimeStamp = (TimeStamps)Data.ReadByte();
            if (!IsValidEnumValue(_TimeStamp, ValidatingErrorTypes.ID3Error))
                return;

            Length--;

            _ContentType = (ContentTypes)Data.ReadByte();
            if (!IsValidEnumValue(_ContentType, ValidatingErrorTypes.Nothing))
                _ContentType = ContentTypes.Other;
            Length--;

            // use Text variable for descriptor property
            ContentDescriptor = Data.ReadText(Length, TextEncoding, ref Length, true);

            string tempText;
            uint tempTime;
            while (Length > 5)
            {
                tempText = Data.ReadText(Length, TextEncoding, ref Length, true);
                tempTime = Data.ReadUInt(4);

                _Syllables.Add(new Syllable(tempTime, tempText));

                Length -= 4;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Copy directory or file, take full path of source and dest as parameter.
 /// </summary>
 public static void CopyFile(string source, string dest, FileCancelDelegate cancel)
 {
     using (FileStreamEx srcStream = FileEx.OpenRead(source))
     {
         byte[] buffer = new byte[Math.Min(1024 * 1024 * 32, srcStream.Length)];  //32MB
         int    readCount;
         ushort completePercent = 0;
         long   completeCount   = 0;
         using (FileStreamEx destStream = FileEx.Create(dest))
         {
             while ((readCount = srcStream.Read(buffer, 0, buffer.Length)) > 0 && !IsCancelTriggered(cancel, completePercent))
             {
                 completeCount += readCount;
                 destStream.Write(buffer, 0, readCount);
                 completePercent = srcStream.Length == 0 ? (ushort)100 : (ushort)((float)completeCount / (float)srcStream.Length * 100.0);
             }
             destStream.Flush();
             destStream.Close();
         }
         srcStream.Close();
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Create new reveb frame
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Flags of frame</param>
        /// <param name="Data">Data for frame to read from</param>
        /// <param name="Length">Maximum length of frame</param>
        internal ReverbFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            if (Length != 12)
            {
                //RaiseErrorEvent(new ID3Error(208, ID3Versions.ID3v2, _FrameID,
                //"Reveb frame is not in correct length. it will drop", ErrorType.Error));
                _MustDrop = true;
                return;
            }

            _ReverbLeft = Convert.ToInt32(Data.ReadUInt(2));
            _ReverbRight = Convert.ToInt32(Data.ReadUInt(2));
            _ReverbBouncesLeft = Data.ReadByte();
            _ReverbBouncesRight = Data.ReadByte();
            _ReverbFeedbackLeftToLeft = Data.ReadByte();
            _ReverbFeedbackLeftToRight = Data.ReadByte();
            _ReverbFeedbackRightToRight = Data.ReadByte();
            _ReverbFeedbackRightToLeft = Data.ReadByte();
            _PremixLeftToRight = Data.ReadByte();
            _PremixRightToLeft = Data.ReadByte();
        }
Exemplo n.º 12
0
        protected uint _MilisecondBetweenRef; // 3 Bytes

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Create new MpegLocationLookupTable
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Flags for this frame</param>
        /// <param name="Data">FileStream to read data from</param>
        internal MpegLocationLookupTable(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _FrameBetweenRef = Data.ReadUInt(2);
            _ByteBetweenRef = Data.ReadUInt(3);
            _ByteBetweenRef = Data.ReadUInt(3);
            _BitsForByteDeviation = Data.ReadByte();
            _BitsForMilisecondDeviation = Data.ReadByte();
            Length -= 10;
            int Sum = _BitsForByteDeviation + _BitsForMilisecondDeviation;

            ////BitForByteDev + BitForMilisecondDev must be multiple of four
            //if (Sum % 4 != 0)
            //{
            //    RaiseErrorEvent(new ID3Error(208, ID3Versions.ID3v2, _FrameID,
            //        "Error in MpegLocationLookupTable, it's not standard. it will drop", ErrorType.Error));
            //    _MustDrop = true;
            //    return;
            //}

            //if (Sum > 32 || Sum % 8 != 0)
            //{
            //    RaiseErrorEvent(new ID3Error(208, ID3Versions.ID3v2, _FrameID,
            //        "this program can't process MpegLocation Table", ErrorType.Error));
            //    _MustDrop = true;
            //    return;
            //}

            uint Temp;
            Sum /= 8;
            while (Length >= Sum)
            {
                Temp = Data.ReadUInt(Sum);

                Length -= Sum;
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Load ID3v1 information from file
 /// </summary>
 public void Load()
 {
     FileStreamEx FS = new FileStreamEx(_FilePath, FileMode.Open);
     if (!FS.HaveID3v1())
     {
         FS.Close();
         _HaveTag = false;
         return;
     }
     _Title = FS.ReadText(30, TextEncodings.Ascii);
     FS.Seek(-95, SeekOrigin.End);
     _Artist = FS.ReadText(30, TextEncodings.Ascii);
     FS.Seek(-65, SeekOrigin.End);
     _Album = FS.ReadText(30, TextEncodings.Ascii);
     FS.Seek(-35, SeekOrigin.End);
     _Year = FS.ReadText(4, TextEncodings.Ascii);
     FS.Seek(-31, SeekOrigin.End);
     _Comment = FS.ReadText(28, TextEncodings.Ascii);
     FS.Seek(-2, SeekOrigin.End);
     _TrackNumber = FS.ReadByte();
     _Genre = FS.ReadByte();
     FS.Close();
     _HaveTag = true;
 }
Exemplo n.º 14
0
        /// <summary>
        /// Add Frame information to where it must store
        /// </summary>
        /// <param name="Data">FileStream contain Frame</param>
        /// <param name="FrameID">FrameID of frame</param>
        /// <param name="Length">Maximum available length to read</param>
        /// <param name="Flags">Flags of frame</param>
        private bool AddFrame(FileStreamEx Data, string FrameID, int Length, FrameFlags Flags)
        {
            // NOTE: All FrameIDs must be capital letters
            if (!FramesInfo.IsValidFrameID(FrameID))
            {
                AddError(new ID3Error("nonValid Frame found and dropped", FrameID));
                return false;
            }

            int IsText = FramesInfo.IsTextFrame(FrameID, _ver.Minor);
            if (IsText == 1)
            {
                TextFrame TempTextFrame = new TextFrame(FrameID, Flags, Data, Length);
                if (TempTextFrame.IsReadableFrame)
                {
                    _TextFrames.Add(TempTextFrame);
                    return true;
                }
                return false;
            }

            if (IsText == 2)
            {
                UserTextFrame TempUserTextFrame = new UserTextFrame(FrameID, Flags, Data, Length);
                if (TempUserTextFrame.IsReadableFrame)
                {
                    _UserTextFrames.Add(TempUserTextFrame);
                    return true;
                }
                return false;
            }

            switch (FrameID)
            {
                case "UFID":
                case "PRIV":
                    PrivateFrame TempPrivateFrame = new PrivateFrame(FrameID, Flags, Data, Length);
                    if (TempPrivateFrame.IsReadableFrame)
                    {
                        _PrivateFrames.Add(TempPrivateFrame); return true;
                    }
                    else
                        AddError(new ID3Error(TempPrivateFrame.ErrorMessage, FrameID));
                    break;
                case "USLT":
                case "COMM":
                    TextWithLanguageFrame TempTextWithLangFrame = new TextWithLanguageFrame(FrameID, Flags, Data, Length);
                    if (TempTextWithLangFrame.IsReadableFrame)
                    { _TextWithLangFrames.Add(TempTextWithLangFrame); return true; }
                    else
                        AddError(new ID3Error(TempTextWithLangFrame.ErrorMessage, FrameID));
                    break;
                case "SYLT":
                    SynchronisedText TempSynchronisedText = new SynchronisedText(FrameID, Flags, Data, Length);
                    if (TempSynchronisedText.IsReadableFrame)
                    { _SynchronisedTextFrames.Add(TempSynchronisedText); return true; }
                    else
                        AddError(new ID3Error(TempSynchronisedText.ErrorMessage, FrameID));
                    break;
                case "GEOB":
                    GeneralFileFrame TempGeneralFileFrame = new GeneralFileFrame(FrameID, Flags, Data, Length);
                    if (TempGeneralFileFrame.IsReadableFrame)
                    { _EncapsulatedObjectFrames.Add(TempGeneralFileFrame); return true; }
                    else
                        AddError(new ID3Error(TempGeneralFileFrame.ErrorMessage, FrameID));
                    break;
                case "POPM":
                    PopularimeterFrame TempPopularimeterFrame = new PopularimeterFrame(FrameID, Flags, Data, Length);
                    if (TempPopularimeterFrame.IsReadableFrame)
                    { _PopularimeterFrames.Add(TempPopularimeterFrame); return true; }
                    else
                        AddError(new ID3Error(TempPopularimeterFrame.ErrorMessage, FrameID));
                    break;
                case "AENC":
                    AudioEncryptionFrame TempAudioEncryptionFrame = new AudioEncryptionFrame(FrameID, Flags, Data, Length);
                    if (TempAudioEncryptionFrame.IsReadableFrame)
                    { _AudioEncryptionFrames.Add(TempAudioEncryptionFrame); return true; }
                    else
                        AddError(new ID3Error(TempAudioEncryptionFrame.ErrorMessage, FrameID));
                    break;
                case "USER":
                    TermOfUseFrame TempTermOfUseFrame = new TermOfUseFrame(FrameID, Flags, Data, Length);
                    if (TempTermOfUseFrame.IsReadableFrame)
                    { _TermOfUseFrames.Add(TempTermOfUseFrame); return true; }
                    else
                        AddError(new ID3Error(TempTermOfUseFrame.ErrorMessage, FrameID));
                    break;
                case "ENCR":
                case "GRID":
                    DataWithSymbolFrame TempDataWithSymbolFrame = new DataWithSymbolFrame(FrameID, Flags, Data, Length);
                    if (TempDataWithSymbolFrame.IsReadableFrame)
                    { _DataWithSymbolFrames.Add(TempDataWithSymbolFrame); return true; }
                    else
                        AddError(new ID3Error(TempDataWithSymbolFrame.ErrorMessage, FrameID));
                    break;
                case "LINK":
                    LinkFrame LF = new LinkFrame(FrameID, Flags, Data, Length);
                    if (LF.IsReadableFrame)
                    {
                        _LinkFrames.Add(LF);
                        if (_LoadLinkedFrames)
                        { LoadFrameFromFile(LF.FrameIdentifier, LF.URL); return true; }
                    }
                    else
                        AddError(new ID3Error(LF.ErrorMessage, FrameID));
                    break;
                case "APIC":
                    AttachedPictureFrame TempAttachedPictureFrame = new AttachedPictureFrame(FrameID, Flags, Data, Length);
                    if (TempAttachedPictureFrame.IsReadableFrame)
                    { _AttachedPictureFrames.Add(TempAttachedPictureFrame); return true; }
                    else
                        AddError(new ID3Error(TempAttachedPictureFrame.ErrorMessage, FrameID));
                    break;
                case "MCDI":
                    BinaryFrame MCDI = new BinaryFrame(FrameID, Flags, Data, Length);
                    if (MCDI.IsReadableFrame)
                    { _MCDIFrame = MCDI; return true; }
                    else
                        AddError(new ID3Error(MCDI.ErrorMessage, FrameID));
                    break;
                case "SYTC":
                    SynchronisedTempoFrame SYTC = new SynchronisedTempoFrame(FrameID, Flags, Data, Length);
                    if (SYTC.IsReadableFrame)
                    { _SYTCFrame = SYTC; return true; }
                    else
                        AddError(new ID3Error(SYTC.ErrorMessage, FrameID));
                    break;
                case "PCNT":
                    PlayCounterFrame PCNT = new PlayCounterFrame(FrameID, Flags, Data, Length);
                    if (PCNT.IsReadableFrame)
                    { _PCNTFrame = PCNT; return true; }
                    else
                        AddError(new ID3Error(PCNT.ErrorMessage, FrameID));
                    break;
                case "RBUF":
                    RecomendedBufferSizeFrame RBUF = new RecomendedBufferSizeFrame(FrameID, Flags, Data, Length);
                    if (RBUF.IsReadableFrame)
                    { _RBUFFrame = RBUF; return true; }
                    else
                        AddError(new ID3Error(RBUF.ErrorMessage, FrameID));
                    break;
                case "OWNE":
                    OwnershipFrame OWNE = new OwnershipFrame(FrameID, Flags, Data, Length);
                    if (OWNE.IsReadableFrame)
                    { _OWNEFrame = OWNE; return true; }
                    else
                        AddError(new ID3Error(OWNE.ErrorMessage, FrameID));
                    break;
                case "COMR":
                    CommercialFrame COMR = new CommercialFrame(FrameID, Flags, Data, Length);
                    if (COMR.IsReadableFrame)
                    { _COMRFrame = COMR; return true; }
                    else
                        AddError(new ID3Error(COMR.ErrorMessage, FrameID));
                    break;
                case "RVRB":
                    ReverbFrame RVRB = new ReverbFrame(FrameID, Flags, Data, Length);
                    if (RVRB.IsReadableFrame)
                    { _RVRBFrame = RVRB; return true; }
                    else
                        AddError(new ID3Error(RVRB.ErrorMessage, FrameID));
                    break;
                case "EQUA":
                    Equalisation EQUA = new Equalisation(FrameID, Flags, Data, Length);
                    if (EQUA.IsReadableFrame)
                    { _EQUAFrame = EQUA; return true; }
                    else
                        AddError(new ID3Error(EQUA.ErrorMessage, FrameID));
                    break;
                case "RVAD":
                    RelativeVolumeFrame RVAD = new RelativeVolumeFrame(FrameID, Flags, Data, Length);
                    if (RVAD.IsReadableFrame)
                    { _RVADFrame = RVAD; return true; }
                    else
                        AddError(new ID3Error(RVAD.ErrorMessage, FrameID));
                    break;
                case "ETCO":
                    EventTimingCodeFrame ETCO = new EventTimingCodeFrame(FrameID, Flags, Data, Length);
                    if (ETCO.IsReadableFrame)
                    { _ETCOFrame = ETCO; return true; }
                    else
                        AddError(new ID3Error(ETCO.ErrorMessage, FrameID));
                    break;
                case "POSS":
                    PositionSynchronisedFrame POSS = new PositionSynchronisedFrame(FrameID, Flags, Data, Length);
                    if (POSS.IsReadableFrame)
                    { _POSSFrame = POSS; return true; }
                    else
                        AddError(new ID3Error(POSS.ErrorMessage, FrameID));
                    break;
                default:
                    BinaryFrame Temp = new BinaryFrame(FrameID, Flags, Data, Length);
                    if (Temp.IsReadableFrame)
                    { _UnknownFrames.Add(Temp); return true; }
                    else
                        AddError(new ID3Error(Temp.ErrorMessage, FrameID));
                    break;
                // TODO: Mpeg Location
            }

            return false;
        }
Exemplo n.º 15
0
        private void SaveRestOfFile(int StartIndex, FileStreamEx Orgin,
            FileStreamEx Temp, int Ver)
        {
            Orgin.Seek(StartIndex, SeekOrigin.Begin);

            byte[] Buf = new byte[Orgin.Length - StartIndex];
            Orgin.Read(Buf, 0, Buf.Length);
            Temp.Write(Buf, 0, Buf.Length);
            Orgin.Close();
            Temp.Close();

            if (Ver != 0)
                SetMinorVersion(Ver);

            File.Delete(Orgin.Name);
            string FinallyName = Temp.Name.Remove(Temp.Name.Length - 5);
            File.Move(Temp.Name, FinallyName);
            _FilePath = FinallyName;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Read all frames from specific FileStream
        /// </summary>
        /// <param name="Data">FileStream to read data from</param>
        /// <param name="Length">Length of data to read from FileStream</param>
        private void ReadFrames(FileStreamEx Data, int Length)
        {
            string FrameID;
            int FrameLength;
            FrameFlags Flags = new FrameFlags();
            byte Buf;
            // If ID3v2 is ID3v2.2 FrameID, FrameLength of Frames is 3 byte
            // otherwise it's 4 character
            int FrameIDLen = VersionInfo.Minor == 2 ? 3 : 4;

            // Minimum frame size is 10 because frame header is 10 byte
            while (Length > 10)
            {
                // check for padding( 00 bytes )
                Buf = Data.ReadByte();
                if (Buf == 0)
                {
                    Length--;
                    continue;
                }

                // if readed byte is not zero. it must read as FrameID
                Data.Seek(-1, SeekOrigin.Current);

                // ---------- Read Frame Header -----------------------
                FrameID = Data.ReadText(FrameIDLen, TextEncodings.Ascii);
                if (FrameIDLen == 3)
                    FrameID = FramesInfo.Get4CharID(FrameID);

                FrameLength = Convert.ToInt32(Data.ReadUInt(FrameIDLen));

                if (FrameIDLen == 4)
                    Flags = (FrameFlags)Data.ReadUInt(2);
                else
                    Flags = 0; // must set to default flag

                long Position = Data.Position;

                if (Length > 0x10000000)
                    throw (new FileLoadException("This file contain frame that have more than 256MB data"));

                bool Added = false;
                if (IsAddable(FrameID)) // Check if frame is not filter
                    Added = AddFrame(Data, FrameID, FrameLength, Flags);

                if (!Added)
                    // if don't read this frame
                    // we must go forward to read next frame
                    Data.Position = Position + FrameLength;

                Length -= FrameLength + 10;
            }
        }
Exemplo n.º 17
0
 /// <summary>
 /// Save ID3v1 information to file
 /// </summary>
 public void Save()
 {
     FileStreamEx fs = new FileStreamEx(_FilePath, FileMode.Open);
     bool HTag = fs.HaveID3v1();
     if (HTag && !_HaveTag) // just delete ID3
         fs.SetLength(fs.Length - 128);
     else if (!HTag && _HaveTag)
     {
         fs.Seek(0, SeekOrigin.End);
         fs.Write(GetTagBytes, 0, 128);
     }
     else if (HTag && _HaveTag)
     {
         fs.Seek(-128, SeekOrigin.End);
         fs.Write(GetTagBytes, 0, 128);
     }
     fs.Close();
 }
Exemplo n.º 18
0
        /// <summary>
        /// Save ID3 info to file
        /// </summary>
        /// <param name="Ver">minor version of ID3v2</param>
        /// <param name="Formula">Formula to renaming file</param>
        public void Save(int Ver, string Formula)
        {
            if (Ver < 3 || Ver > 4)
                throw (new ArgumentOutOfRangeException("Version of ID3 can be between 3-4"));

            string NewFilePath;
            if (Formula == "")
                NewFilePath = _FilePath;
            else
            {
                string DirName = Path.GetDirectoryName(_FilePath);
                NewFilePath = DirName + ((DirName.Length > 0) ? "\\" : "") +
                    this.FormulaFileName(Formula);
            }

            FileStreamEx Orgin = new FileStreamEx(_FilePath, FileMode.Open);
            FileStreamEx Temp = new FileStreamEx(NewFilePath + "~TEMP", FileMode.Create);

            int StartIndex = 0;
            if (Orgin.HaveID3v2())
            {
                Orgin.Seek(3, SeekOrigin.Current);
                StartIndex = Orgin.ReadSize();
            }

            if (!_HaveTag)
            {
                SaveRestOfFile(StartIndex, Orgin, Temp, 0);
                return;
            }
            // if Orginal file and current file both don't contain ID3
            // we don't need to do anything

            WriteID3Header(Temp, Ver);

            foreach (TextFrame TF in _TextFrames.Items)
            {
                if (!FramesInfo.IsCompatible(TF.FrameID, Ver))
                    continue;

                if (TF.IsAvailable)
                    TF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (UserTextFrame UTF in _UserTextFrames.Items)
            {
                if (!FramesInfo.IsCompatible(UTF.FrameID, Ver))
                    continue;

                if (UTF.IsAvailable)
                    UTF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (PrivateFrame PF in _PrivateFrames.Items)
            {
                if (!FramesInfo.IsCompatible(PF.FrameID, Ver))
                    continue;

                if (PF.IsAvailable)
                    PF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (TextWithLanguageFrame TWLF in _TextWithLangFrames.Items)
            {
                if (!FramesInfo.IsCompatible(TWLF.FrameID, Ver))
                    continue;

                if (TWLF.IsAvailable)
                    TWLF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (SynchronisedText ST in _SynchronisedTextFrames.Items)
            {
                if (!FramesInfo.IsCompatible(ST.FrameID, Ver))
                    continue;

                if (ST.IsAvailable)
                    ST.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (AttachedPictureFrame AP in _AttachedPictureFrames.Items)
            {
                if (!FramesInfo.IsCompatible(AP.FrameID, Ver))
                    continue;

                if (AP.IsAvailable)
                    AP.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (GeneralFileFrame GF in _EncapsulatedObjectFrames.Items)
            {
                if (!FramesInfo.IsCompatible(GF.FrameID, Ver))
                    continue;

                if (GF.IsAvailable)
                    GF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (PopularimeterFrame PF in _PopularimeterFrames.Items)
            {
                if (!FramesInfo.IsCompatible(PF.FrameID, Ver))
                    continue;

                if (PF.IsAvailable)
                    PF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (AudioEncryptionFrame AE in _AudioEncryptionFrames.Items)
            {
                if (!FramesInfo.IsCompatible(AE.FrameID, Ver))
                    continue;

                if (AE.IsAvailable)
                    AE.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (LinkFrame LF in _LinkFrames.Items)
            {
                if (!FramesInfo.IsCompatible(LF.FrameID, Ver))
                    continue;

                if (LF.IsAvailable)
                    LF.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (TermOfUseFrame TU in _TermOfUseFrames.Items)
            {
                if (!FramesInfo.IsCompatible(TU.FrameID, Ver))
                    continue;

                if (TU.IsAvailable)
                    TU.FrameStream(Ver).WriteTo(Temp);
            }
            foreach (DataWithSymbolFrame DS in _DataWithSymbolFrames.Items)
            {
                if (!FramesInfo.IsCompatible(DS.FrameID, Ver))
                    continue;

                if (DS.IsAvailable)
                    DS.FrameStream(Ver).WriteTo(Temp);
            }
            // Saving Unknown Frames
            if (!_DropUnknown)
                foreach (BinaryFrame BF in _UnknownFrames.Items)
                    if (BF.IsAvailable)
                        BF.FrameStream(Ver).WriteTo(Temp);

            if (_MCDIFrame != null && FramesInfo.IsCompatible(_MCDIFrame.FrameID, Ver))
                if (_MCDIFrame.IsAvailable)
                    _MCDIFrame.FrameStream(Ver).WriteTo(Temp);
            if (_SYTCFrame != null && FramesInfo.IsCompatible(_SYTCFrame.FrameID, Ver))
                if (_SYTCFrame.IsAvailable)
                    _SYTCFrame.FrameStream(Ver).WriteTo(Temp);
            if (_PCNTFrame != null && FramesInfo.IsCompatible(_PCNTFrame.FrameID, Ver))
                if (_PCNTFrame.IsAvailable)
                    _PCNTFrame.FrameStream(Ver).WriteTo(Temp);
            if (_RBUFFrame != null && FramesInfo.IsCompatible(_RBUFFrame.FrameID, Ver))
                if (_RBUFFrame.IsAvailable)
                    _RBUFFrame.FrameStream(Ver).WriteTo(Temp);
            if (_OWNEFrame != null && FramesInfo.IsCompatible(_OWNEFrame.FrameID, Ver))
                if (_OWNEFrame.IsAvailable)
                    _OWNEFrame.FrameStream(Ver).WriteTo(Temp);
            if (_COMRFrame != null && FramesInfo.IsCompatible(_COMRFrame.FrameID, Ver))
                if (_COMRFrame.IsAvailable)
                    _COMRFrame.FrameStream(Ver).WriteTo(Temp);
            if (_RVRBFrame != null && FramesInfo.IsCompatible(_RVRBFrame.FrameID, Ver))
                if (_RVRBFrame.IsAvailable)
                    _RVRBFrame.FrameStream(Ver).WriteTo(Temp);
            if (_EQUAFrame != null && FramesInfo.IsCompatible(_EQUAFrame.FrameID, Ver))
                if (_EQUAFrame.IsAvailable)
                    _EQUAFrame.FrameStream(Ver).WriteTo(Temp);
            if (_RVADFrame != null && FramesInfo.IsCompatible(_RVADFrame.FrameID, Ver))
                if (_RVADFrame.IsAvailable)
                    _RVADFrame.FrameStream(Ver).WriteTo(Temp);
            if (_ETCOFrame != null && FramesInfo.IsCompatible(_ETCOFrame.FrameID, Ver))
                if (_ETCOFrame.IsAvailable)
                    _ETCOFrame.FrameStream(_ver.Minor).WriteTo(Temp);
            if (_POSSFrame != null && FramesInfo.IsCompatible(_POSSFrame.FrameID, Ver))
                if (_POSSFrame.IsAvailable)
                    _POSSFrame.FrameStream(Ver).WriteTo(Temp);

            SaveRestOfFile(StartIndex, Orgin, Temp, Ver);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Create new UserTextFrameClass
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">Frame Flagsr</param>
        /// <param name="Data">MemoryStream to read information from</param>
        internal UserTextFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            TextEncoding = (TextEncodings)Data.ReadByte();
            Length--;
            if (!IsValidEnumValue(TextEncoding, ValidatingErrorTypes.ID3Error))
            {
                _MustDrop = true;
                return;
            }

            _Description = Data.ReadText(Length, TextEncoding, ref Length, true);

            if (!IsUrl) // is text frame
                Text = Data.ReadText(Length, TextEncoding);
            else
                Text = Data.ReadText(Length, TextEncodings.Ascii);

            // User URL frames use this class and use Text property as URL
            // URL property must be in AScii format
            // all URL frames start with W and text frames with T
        }
Exemplo n.º 20
0
        /// <summary>
        /// New PopularimeterFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">Frame Flags</param>
        /// <param name="Data">FileStream contain frame data</param>
        internal PopularimeterFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            EMail = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true); // Read Email Address

            _Rating = Data.ReadByte(); // Read Rating
            Length--;

            if (Length > 8)
            {
                ErrorOccur("Counter value for Popularimeter frame is more than 8 byte." +
                    " this is not supported by this program", true);
                return;
            }

            byte[] LBuf = new byte[8];
            byte[] Buf = new byte[Length];

            Data.Read(Buf, 0, Length);
            Buf.CopyTo(LBuf, 8 - Buf.Length);
            Array.Reverse(LBuf);

            _Counter = BitConverter.ToInt64(LBuf, 0);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Create new TermOfUseFrame class
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        internal TermOfUseFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            TextEncoding = (TextEncodings)Data.ReadByte();
            Length--;
            if (!IsValidEnumValue(TextEncoding, ValidatingErrorTypes.ID3Error))
            {
                _MustDrop = true;
                return;
            }

            _Language = new Language(Data);
            Length -= 3;

            Text = Data.ReadText(Length, TextEncoding);
        }
Exemplo n.º 22
0
 /// <summary>
 /// New BinaryFrame
 /// </summary>
 /// <param name="FrameID">FrameID</param>
 /// <param name="Flags">Frame Flag</param>
 /// <param name="Data">FileStream contain frame data</param>
 internal BinaryFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
     : base(FrameID, Flags)
 {
     _Data = Data.ReadData(Length);
 }
Exemplo n.º 23
0
        /// <summary>
        /// New PrivateFrame
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Frame Flags</param>
        /// <param name="Data">FileStream to read frame data from</param>
        internal PrivateFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _Owner = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _Data = Data.ReadData(Length); // Read Data
        }
Exemplo n.º 24
0
        public void When_checking_line_contains_characters_false_is_returned_if_they_do_not()
        {
            var filename = $"{this.GetDataDirectory()}\\data\\SingleLine.txt";
            using (var fileStreamEx = new FileStreamEx(filename))
            {
                Line line;
                while ((line = fileStreamEx.ReadLine()) != null)
                {
                    var actual = line.Contains('Y');

                    actual.Should().BeFalse();
                }
            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// New SDate from specific FileStream
        /// </summary>
        /// <param name="Data">FileStream represent SDate data</param>
        internal SDate(FileStreamEx Data)
        {
            string DateSt = Data.ReadText(8, TextEncodings.Ascii, false);

            int Temp;
            if (Int32.TryParse(DateSt.Substring(0, 4), out Temp))
                Year = Temp;

            if (Int32.TryParse(DateSt.Substring(4, 2), out Temp))
                Month = Temp;

            if (Int32.TryParse(DateSt.Substring(6, 2), out Temp))
                Day = Temp;
        }
Exemplo n.º 26
0
        /// <summary>
        /// Create new OwnershipFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        internal OwnershipFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            TextEncoding = (TextEncodings)Data.ReadByte();
            Length--;
            if (!IsValidEnumValue(TextEncoding, ValidatingErrorTypes.ID3Error))
                return;

            _Price = new Price(Data, Length);
            Length -= _Price.Length;
            if (!_Price.IsValid)
            {
                ErrorOccur("Price is not valid value. ownership frame will not read", true);
                return;
            }

            if (Length >= 8)
            {
                _DateOfPurch = new SDate(Data);
                Length -= 8;
            }
            else
            {
                ErrorOccur("Date is not valid for this frame", true);
                return;
            }

            Seller = Data.ReadText(Length, TextEncoding);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Load ID3 information from file
        /// </summary>
        /// <exception cref="FileNotFoundException">File Not Found</exception>
        public void Load()
        {
            FileStreamEx ID3File = new FileStreamEx(_FilePath, FileMode.Open);
            if (!ID3File.HaveID3v2()) // If file don't contain ID3v2 exit function
            {
                _HaveTag = false;
                ID3File.Close();
                return;
            }

            _ver = ID3File.ReadVersion(); // Read ID3v2 version           
            _Flags = (ID3v2Flags)ID3File.ReadByte();

            // Extended Header Must Read Here

            ReadFrames(ID3File, ID3File.ReadSize());
            ID3File.Close();
            _HaveTag = true;
        }
Exemplo n.º 28
0
        /// <summary>
        /// New DataWithSymbolFrame
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Frame Flags</param>
        /// <param name="Data">FileStream to read frame data from</param>
        internal DataWithSymbolFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _Owner = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _Symbol = Data.ReadByte();
            Length--;

            _Data = Data.ReadData(Length);
        }
Exemplo n.º 29
0
        public void When_checking_line_IsNullOrEmpty_true_is_returned_if_line_does_not_contain_characters_other_than_spaces()
        {
            var filename = $"{this.GetDataDirectory()}\\data\\ThreeLinesWIthSeondEmpty.txt";
            using (var fileStreamEx = new FileStreamEx(filename))
            {
                Line line;
                line = fileStreamEx.ReadLine(); // Not Empty
                line = fileStreamEx.ReadLine(); // Empty
                var actual = line.IsNullOrEmpty();

                actual.Should().BeTrue();
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Create new TextFrame Class
        /// </summary>
        /// <param name="FrameID">4 Characters frame identifier</param>
        /// <param name="Flags">Flag of frame</param>
        /// <param name="Data">FileStream to read frame data from</param>
        /// <param name="Length">Maximum length of frame</param>
        internal TextFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            // If it was URL frame the TextEncoding is ascii and must not read
            if (IsUrl)
                TextEncoding = TextEncodings.Ascii;
            else
            {
                TextEncoding = (TextEncodings)Data.ReadByte();
                Length--;
                if (!IsValidEnumValue(TextEncoding, ValidatingErrorTypes.ID3Error))
                    return;
            }

            Text = Data.ReadText(Length, _TextEncoding);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Create new AudioEncryptionFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        internal AudioEncryptionFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _Owner = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _PreviewStart = Convert.ToInt32(Data.ReadUInt(2));
            _PreviewLength = Convert.ToInt32(Data.ReadUInt(2));
            Length -= 4;

            _Data = Data.ReadData(Length);
        }
Exemplo n.º 32
0
        /// <summary>
        /// Create new RaltiveVolumeFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">2 Bytes flags identifier</param>
        /// <param name="Data">Contain Data for this frame</param>
        /// <param name="Lenght">Length to read from FileStream</param>
        internal RelativeVolumeFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _Descriptors = new uint[12];

            _IncDec = Data.ReadByte(); // Read Increment Decrement Byte

            _BitForVolumeDescription = Data.ReadByte(); // Read Volume Description Length
            Length -= 2;

            if (_BitForVolumeDescription == 0)
            {
                ErrorOccur("BitForVolumeDescription of Relative volume information frame can't be zero", true);
                return;
            }

            if (_BitForVolumeDescription / 8 > 4 ||
                _BitForVolumeDescription % 8 != 0)
            {
                ErrorOccur("This program don't support " + _BitForVolumeDescription.ToString() +
                    " Bits of description for Relative Volume information", true);
                return;
            }

            int DesLen = _BitForVolumeDescription / 8; // Length of each volume descriptor
            int Counter = 0;
            while (CanContinue(Length, DesLen, 2))
            {
                _Descriptors[Counter++] = Data.ReadUInt(DesLen);
                _Descriptors[Counter++] = Data.ReadUInt(DesLen);
                Length -= 2;
            }
        }
Exemplo n.º 33
0
        /// <summary>
        /// New CommercialFrame
        /// </summary>
        /// <param name="FrameID">FrameID</param>
        /// <param name="Flags">Frame Flags</param>
        /// <param name="Data">Data of frame</param>
        /// <param name="Length">MaxLength of frame</param>
        internal CommercialFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            _TextEncoding = (TextEncodings)Data.ReadByte();
            Length--;
            if (!IsValidEnumValue(_TextEncoding, ValidatingErrorTypes.ID3Error))
                return;

            _Price = new Price(Data, Length);
            Length -= _Price.Length;

            _ValidUntil = new SDate(Data);
            Length -= 8;


            _ContactUrl = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _RecievedAs = (RecievedAsEnum)Data.ReadByte();
            Length--;

            _SellerName = Data.ReadText(Length, _TextEncoding, ref Length, true);

            _Description = Data.ReadText(Length, _TextEncoding, ref Length, true);

            if (Length < 1) // Data finished
                return;

            _MIMEType = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true);

            _Data = Data.ReadData(Length);
        }