示例#1
0
        public void StreamUtils_CopySameStream()
        {
            byte[] finalListForward = new byte[10] {
                0, 1, 2, 3, 2, 3, 4, 5, 6, 7
            };
            byte[] finalListBackward = new byte[10] {
                0, 1, 4, 5, 6, 7, 8, 9, 8, 9
            };

            using (MemoryStream stream = new MemoryStream(new byte[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            }))
            {
                StreamUtils.CopySameStream(stream, 2, 4, 6, 3);
                Assert.IsTrue(StreamUtils.ArrEqualsArr(finalListForward, stream.ToArray()));
            }

            using (MemoryStream stream = new MemoryStream(new byte[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            }))
            {
                StreamUtils.CopySameStream(stream, 4, 2, 6, 3);
                Assert.IsTrue(StreamUtils.ArrEqualsArr(finalListBackward, stream.ToArray()));
            }
        }
示例#2
0
        public void TagIO_RW_ID3v2_CommentFields()
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : MP3 with existing tag incl. comment fields (iTunNORM, iTunPGAP)
            String           testFileLocation = TestUtils.GetTempTestFile("MP3/id3v2.2_iTunNORM-iTunPGAP.mp3");
            AudioDataManager theFile          = new AudioDataManager(AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            // Check if the two fields are indeed accessible
            Assert.IsTrue(theFile.ReadFromFile(false, true));
            Assert.IsNotNull(theFile.ID3v2);
            Assert.IsTrue(theFile.ID3v2.Exists);

            Assert.AreEqual(4, theFile.ID3v2.AdditionalFields.Count);

            int found = 0;

            foreach (KeyValuePair <string, string> field in theFile.ID3v2.AdditionalFields)
            {
                if (field.Key.Equals("iTunNORM"))
                {
                    Assert.AreEqual(" 00000099 000000A2 000002F0 000002F4 0000002E 0000002E 00002E6E 00002C5C 00000017 00000017", field.Value); // Why an empty space at the beginning ?!
                    found++;
                }
                else if (field.Key.Equals("iTunPGAP"))
                {
                    Assert.AreEqual("1", field.Value);
                    found++;
                }
            }
            Assert.AreEqual(2, found);


            // Check if they are persisted as comment fields when editing the tag
            TagData theTag = new TagData();

            Assert.IsTrue(theFile.UpdateTagInFile(theTag, MetaDataIOFactory.TAG_ID3V2));

            // For this we need to open the file in binary mode and check that the two fields belong to a comment field
            byte[] readBytes = new byte[4];
            byte[] expected  = Utils.Latin1Encoding.GetBytes("COMM");

            using (FileStream fs = new FileStream(testFileLocation, FileMode.Open, FileAccess.Read))
            {
                Assert.IsTrue(StreamUtils.FindSequence(fs, Utils.Latin1Encoding.GetBytes("iTunNORM")));
                fs.Seek(-22, SeekOrigin.Current);
                fs.Read(readBytes, 0, 4);
                Assert.IsTrue(StreamUtils.ArrEqualsArr(expected, readBytes));

                fs.Seek(0, SeekOrigin.Begin);
                Assert.IsTrue(StreamUtils.FindSequence(fs, Utils.Latin1Encoding.GetBytes("iTunPGAP")));
                fs.Seek(-22, SeekOrigin.Current);
                fs.Read(readBytes, 0, 4);
                Assert.IsTrue(StreamUtils.ArrEqualsArr(expected, readBytes));
            }

            // Get rid of the working copy
            File.Delete(testFileLocation);
        }
示例#3
0
        public void PLIO_W_M3U_Simple()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add("aaa.mp3");
            pathsToWrite.Add("bbb.mp3");

            bool defaultSetting = ATL.Settings.M3U_useExtendedFormat;

            string testFileLocation = TestUtils.CreateTempTestFile("test.m3u");

            try
            {
                ATL.Settings.M3U_useExtendedFormat = false;
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);
                pls.FilePaths = pathsToWrite;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if the default UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsTrue(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (StreamReader sr = new StreamReader(fs))
                    {
                        Assert.AreEqual("aaa.mp3", sr.ReadLine());
                        Assert.AreEqual("bbb.mp3", sr.ReadLine());
                        Assert.IsTrue(sr.EndOfStream);
                    }
                }


                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.IsTrue(filePaths[0].EndsWith(pathsToWrite[0]));
                Assert.IsTrue(filePaths[1].EndsWith(pathsToWrite[1]));
            }
            finally
            {
                if (Settings.DeleteAfterSuccess)
                {
                    File.Delete(testFileLocation);
                }
                ATL.Settings.M3U_useExtendedFormat = defaultSetting;
            }
        }
示例#4
0
        private void cacheLanguageIndex(Stream source)
        {
            if (null == languages)
            {
                long   position, initialPosition;
                ulong  objectSize;
                byte[] bytes;

                languages = new List <string>();

                initialPosition = source.Position;
                source.Seek(fileData.ObjectListOffset, SeekOrigin.Begin);

                BinaryReader r = new BinaryReader(source);

                for (int i = 0; i < fileData.ObjectCount; i++)
                {
                    position   = source.Position;
                    bytes      = r.ReadBytes(16);
                    objectSize = r.ReadUInt64();

                    // Language index (optional; one only -- useful to map language codes to extended header tag information)
                    if (StreamUtils.ArrEqualsArr(WMA_LANGUAGE_LIST_OBJECT_ID, bytes))
                    {
                        ushort nbLanguages = r.ReadUInt16();
                        byte   strLen;

                        for (int j = 0; j < nbLanguages; j++)
                        {
                            strLen = r.ReadByte();
                            long position2 = source.Position;
                            if (strLen > 2)
                            {
                                languages.Add(Utils.StripEndingZeroChars(Encoding.Unicode.GetString(r.ReadBytes(strLen))));
                            }
                            source.Seek(position2 + strLen, SeekOrigin.Begin);
                        }
                    }

                    source.Seek(position + (long)objectSize, SeekOrigin.Begin);
                }

                source.Seek(initialPosition, SeekOrigin.Begin);
            }
        }
示例#5
0
        public void Utils_StrictLengthStringBytes()
        {
            byte[] data;
            byte[] testData;

            testData = new byte[] { 32, 32, 0, 0 };
            data     = Utils.BuildStrictLengthStringBytes("  ", 4, 0, Encoding.UTF8);
            Assert.IsTrue(StreamUtils.ArrEqualsArr(testData, data));

            testData = new byte[] { 0, 0, 32, 32 };
            data     = Utils.BuildStrictLengthStringBytes("  ", 4, 0, Encoding.UTF8, false);
            Assert.IsTrue(StreamUtils.ArrEqualsArr(testData, data));

            testData = new byte[] { 231, 136, 182, 0 };
            data     = Utils.BuildStrictLengthStringBytes("父父", 4, 0, Encoding.UTF8);
            Assert.IsTrue(StreamUtils.ArrEqualsArr(testData, data));

            testData = new byte[] { 0, 231, 136, 182 };
            data     = Utils.BuildStrictLengthStringBytes("父父", 4, 0, Encoding.UTF8, false);
            Assert.IsTrue(StreamUtils.ArrEqualsArr(testData, data));
        }
示例#6
0
        private static uint findPngChunk(Stream s, byte[] chunkID, long limit)
        {
            byte[] intData = new byte[4];
            uint   chunkSize;
            bool   foundChunk = false;

            while (s.Position < limit)
            {
                s.Read(intData, 0, 4); // Chunk Size
                chunkSize = StreamUtils.DecodeBEUInt32(intData);
                s.Read(intData, 0, 4); // Chunk ID
                foundChunk = StreamUtils.ArrEqualsArr(intData, chunkID);
                if (foundChunk)
                {
                    return(chunkSize);
                }
                else
                {
                    s.Seek(chunkSize + 4, SeekOrigin.Current);
                }
            }

            return(0);
        }
示例#7
0
        private static UInt32 findPngChunk(Stream s, Byte[] chunkID, Int64 limit)
        {
            var    intData = new Byte[4];
            UInt32 chunkSize;
            var    foundChunk = false;

            while (s.Position < limit)
            {
                s.Read(intData, 0, 4); // Chunk Size
                chunkSize = StreamUtils.DecodeBEUInt32(intData);
                s.Read(intData, 0, 4); // Chunk ID
                foundChunk = StreamUtils.ArrEqualsArr(intData, chunkID);
                if (foundChunk)
                {
                    return(chunkSize);
                }
                else
                {
                    s.Seek(chunkSize + 4, SeekOrigin.Current);
                }
            }

            return(0);
        }
示例#8
0
        private bool readData(BinaryReader source, ReadTagParams readTagParams)
        {
            Stream fs = source.BaseStream;

            byte[] ID;
            uint   objectCount;
            ulong  headerSize, objectSize;
            long   initialPos, position;
            long   countPosition, sizePosition1, sizePosition2;
            bool   result = false;

            if (languages != null)
            {
                languages.Clear();
            }

            fs.Seek(sizeInfo.ID3v2Size, SeekOrigin.Begin);

            initialPos = fs.Position;

            // Check for existing header
            ID = source.ReadBytes(16);

            // Header (mandatory; one only)
            if (StreamUtils.ArrEqualsArr(WMA_HEADER_ID, ID))
            {
                sizePosition1 = fs.Position;
                headerSize    = source.ReadUInt64();
                countPosition = fs.Position;
                objectCount   = source.ReadUInt32();
                fs.Seek(2, SeekOrigin.Current); // Reserved data
                fileData.ObjectCount      = objectCount;
                fileData.ObjectListOffset = fs.Position;

                // Read all objects in header and get needed data
                for (int i = 0; i < objectCount; i++)
                {
                    position      = fs.Position;
                    ID            = source.ReadBytes(16);
                    sizePosition2 = fs.Position;
                    objectSize    = source.ReadUInt64();

                    // File properties (mandatory; one only)
                    if (StreamUtils.ArrEqualsArr(WMA_FILE_PROPERTIES_ID, ID))
                    {
                        source.BaseStream.Seek(40, SeekOrigin.Current);
                        duration = source.ReadUInt64() / 10000.0;       // Play duration (100-nanoseconds)
                        source.BaseStream.Seek(8, SeekOrigin.Current);  // Send duration; unused for now
                        duration -= source.ReadUInt64();                // Preroll duration (ms)
                    }
                    // Stream properties (mandatory; one per stream)
                    else if (StreamUtils.ArrEqualsArr(WMA_STREAM_PROPERTIES_ID, ID))
                    {
                        source.BaseStream.Seek(54, SeekOrigin.Current);
                        fileData.FormatTag  = source.ReadUInt16();
                        fileData.Channels   = source.ReadUInt16();
                        fileData.SampleRate = source.ReadInt32();
                    }
                    // Content description (optional; one only)
                    // -> standard, pre-defined metadata
                    else if (StreamUtils.ArrEqualsArr(WMA_CONTENT_DESCRIPTION_ID, ID) && readTagParams.ReadTag)
                    {
                        tagExists = true;
                        structureHelper.AddZone(position, (int)objectSize, ZONE_CONTENT_DESCRIPTION);
                        // Store frame information for future editing, since current frame is optional
                        if (readTagParams.PrepareForWriting)
                        {
                            structureHelper.AddSize(sizePosition1, headerSize, ZONE_CONTENT_DESCRIPTION);
                            structureHelper.AddCounter(countPosition, objectCount, ZONE_CONTENT_DESCRIPTION);
                        }
                        readContentDescription(source, readTagParams);
                    }
                    // Extended content description (optional; one only)
                    // -> extended, dynamic metadata
                    else if (StreamUtils.ArrEqualsArr(WMA_EXTENDED_CONTENT_DESCRIPTION_ID, ID) && readTagParams.ReadTag)
                    {
                        tagExists = true;
                        structureHelper.AddZone(position, (int)objectSize, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                        // Store frame information for future editing, since current frame is optional
                        if (readTagParams.PrepareForWriting)
                        {
                            structureHelper.AddSize(sizePosition1, headerSize, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                            structureHelper.AddCounter(countPosition, objectCount, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                        }
                        readExtendedContentDescription(source, readTagParams);
                    }
                    // Header extension (mandatory; one only)
                    // -> extended, dynamic additional metadata such as additional embedded pictures (any picture after the 1st one stored in extended content)
                    else if (StreamUtils.ArrEqualsArr(WMA_HEADER_EXTENSION_ID, ID) && readTagParams.ReadTag)
                    {
                        readHeaderExtended(source, sizePosition1, headerSize, sizePosition2, objectSize, readTagParams);
                    }

                    fs.Seek(position + (long)objectSize, SeekOrigin.Begin);
                }

                // Add absent zone definitions for further editing
                if (readTagParams.PrepareForWriting)
                {
                    if (!structureHelper.ZoneNames.Contains(ZONE_CONTENT_DESCRIPTION))
                    {
                        structureHelper.AddZone(fs.Position, 0, ZONE_CONTENT_DESCRIPTION);
                        structureHelper.AddSize(sizePosition1, headerSize, ZONE_CONTENT_DESCRIPTION);
                        structureHelper.AddCounter(countPosition, objectCount, ZONE_CONTENT_DESCRIPTION);
                    }
                    if (!structureHelper.ZoneNames.Contains(ZONE_EXTENDED_CONTENT_DESCRIPTION))
                    {
                        structureHelper.AddZone(fs.Position, 0, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                        structureHelper.AddSize(sizePosition1, headerSize, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                        structureHelper.AddCounter(countPosition, objectCount, ZONE_EXTENDED_CONTENT_DESCRIPTION);
                    }
                }

                result = true;
            }

            fileData.HeaderSize = fs.Position - initialPos;

            return(result);
        }
示例#9
0
        private void readHeaderExtended(BinaryReader source, long sizePosition1, ulong size1, long sizePosition2, ulong size2, MetaDataIO.ReadTagParams readTagParams)
        {
            byte[] headerExtensionObjectId;
            ulong  headerExtensionObjectSize = 0;
            long   position, framePosition, sizePosition3, dataPosition;
            ulong  limit;
            ushort streamNumber, languageIndex;

            source.BaseStream.Seek(16, SeekOrigin.Current); // Reserved field 1
            source.BaseStream.Seek(2, SeekOrigin.Current);  // Reserved field 2

            sizePosition3 = source.BaseStream.Position;
            uint headerExtendedSize = source.ReadUInt32(); // Size of actual data

            // Looping through header extension objects
            position = source.BaseStream.Position;
            limit    = (ulong)position + headerExtendedSize;
            while ((ulong)position < limit)
            {
                framePosition             = source.BaseStream.Position;
                headerExtensionObjectId   = source.ReadBytes(16);
                headerExtensionObjectSize = source.ReadUInt64();

                // Additional metadata (Optional frames)
                if (StreamUtils.ArrEqualsArr(WMA_METADATA_OBJECT_ID, headerExtensionObjectId) || StreamUtils.ArrEqualsArr(WMA_METADATA_LIBRARY_OBJECT_ID, headerExtensionObjectId))
                {
                    ushort nameSize;            // Length (in bytes) of Name field
                    ushort fieldDataType;       // Type of data stored in current field
                    int    fieldDataSize;       // Size of data stored in current field
                    string fieldName;           // Name of current field
                    ushort nbObjects       = source.ReadUInt16();
                    bool   isLibraryObject = StreamUtils.ArrEqualsArr(WMA_METADATA_LIBRARY_OBJECT_ID, headerExtensionObjectId);

                    string zoneCode = isLibraryObject ? ZONE_EXTENDED_HEADER_METADATA_LIBRARY : ZONE_EXTENDED_HEADER_METADATA;

                    structureHelper.AddZone(framePosition, (int)headerExtensionObjectSize, zoneCode);
                    // Store frame information for future editing, since current frame is optional
                    if (readTagParams.PrepareForWriting)
                    {
                        structureHelper.AddSize(sizePosition1, size1, zoneCode);
                        structureHelper.AddSize(sizePosition2, size2, zoneCode);
                        structureHelper.AddSize(sizePosition3, headerExtendedSize, zoneCode);
                    }

                    for (int i = 0; i < nbObjects; i++)
                    {
                        languageIndex = source.ReadUInt16();
                        streamNumber  = source.ReadUInt16();
                        nameSize      = source.ReadUInt16();
                        fieldDataType = source.ReadUInt16();
                        fieldDataSize = source.ReadInt32();
                        fieldName     = Utils.StripEndingZeroChars(Encoding.Unicode.GetString(source.ReadBytes(nameSize)));

                        dataPosition = source.BaseStream.Position;
                        readTagField(source, zoneCode, fieldName, fieldDataType, fieldDataSize, readTagParams, true, languageIndex, streamNumber);

                        source.BaseStream.Seek(dataPosition + fieldDataSize, SeekOrigin.Begin);
                    }
                }

                source.BaseStream.Seek(position + (long)headerExtensionObjectSize, SeekOrigin.Begin);
                position = source.BaseStream.Position;
            }

            // Add absent zone definitions for further editing
            if (readTagParams.PrepareForWriting)
            {
                if (!structureHelper.ZoneNames.Contains(ZONE_EXTENDED_HEADER_METADATA))
                {
                    structureHelper.AddZone(source.BaseStream.Position, 0, ZONE_EXTENDED_HEADER_METADATA);
                    structureHelper.AddSize(sizePosition1, size1, ZONE_EXTENDED_HEADER_METADATA);
                    structureHelper.AddSize(sizePosition2, size2, ZONE_EXTENDED_HEADER_METADATA);
                    structureHelper.AddSize(sizePosition3, headerExtendedSize, ZONE_EXTENDED_HEADER_METADATA);
                }
                if (!structureHelper.ZoneNames.Contains(ZONE_EXTENDED_HEADER_METADATA_LIBRARY))
                {
                    structureHelper.AddZone(source.BaseStream.Position, 0, ZONE_EXTENDED_HEADER_METADATA_LIBRARY);
                    structureHelper.AddSize(sizePosition1, size1, ZONE_EXTENDED_HEADER_METADATA_LIBRARY);
                    structureHelper.AddSize(sizePosition2, size2, ZONE_EXTENDED_HEADER_METADATA_LIBRARY);
                    structureHelper.AddSize(sizePosition3, headerExtendedSize, ZONE_EXTENDED_HEADER_METADATA_LIBRARY);
                }
            }
        }
示例#10
0
        public void PLIO_W_PLS()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add("aaa.mp3");
            pathsToWrite.Add("bbb.mp3");

            IList <Track> tracksToWrite = new List <Track>();

            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3\\empty.mp3"));
            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD\\mod.mod"));


            string testFileLocation = TestUtils.CreateTempTestFile("test.pls");

            try
            {
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);

                // Test Path writing
                pls.FilePaths = pathsToWrite;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if the default UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsTrue(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (StreamReader sr = new StreamReader(fs))
                    {
                        Assert.AreEqual("[playlist]", sr.ReadLine());
                        sr.ReadLine();
                        Assert.AreEqual("File1=aaa.mp3", sr.ReadLine());
                        Assert.AreEqual("Title1=aaa", sr.ReadLine());
                        Assert.AreEqual("Length1=-1", sr.ReadLine());
                        sr.ReadLine();
                        Assert.AreEqual("File2=bbb.mp3", sr.ReadLine());
                        Assert.AreEqual("Title2=bbb", sr.ReadLine());
                        Assert.AreEqual("Length2=-1", sr.ReadLine());
                        sr.ReadLine();
                        Assert.AreEqual("NumberOfEntries=2", sr.ReadLine());
                        Assert.AreEqual("Version=2", sr.ReadLine());
                        Assert.IsTrue(sr.EndOfStream);
                    }
                }
                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.IsTrue(filePaths[0].EndsWith(pathsToWrite[0]));
                Assert.IsTrue(filePaths[1].EndsWith(pathsToWrite[1]));


                // Test Track writing
                pls.Tracks = tracksToWrite;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        Assert.AreEqual("[playlist]", sr.ReadLine());
                        int counter = 1;
                        foreach (Track t in tracksToWrite)
                        {
                            sr.ReadLine();
                            Assert.AreEqual("File" + counter + "=" + t.Path, sr.ReadLine());
                            Assert.AreEqual("Title" + counter + "=" + t.Title, sr.ReadLine());
                            Assert.AreEqual("Length" + counter + "=" + t.Duration, sr.ReadLine());
                            counter++;
                        }
                        sr.ReadLine();
                        Assert.AreEqual("NumberOfEntries=2", sr.ReadLine());
                        Assert.AreEqual("Version=2", sr.ReadLine());
                        Assert.IsTrue(sr.EndOfStream);
                    }

                IList <Track> tracks = pls.Tracks;
                Assert.AreEqual(2, tracks.Count);
                Assert.AreEqual(tracksToWrite[0].Path, tracks[0].Path);
                Assert.AreEqual(tracksToWrite[1].Path, tracks[1].Path);
            }
            finally
            {
                if (Settings.DeleteAfterSuccess)
                {
                    File.Delete(testFileLocation);
                }
            }
        }
示例#11
0
        public void TagIO_RW_ID3v2_FieldCodev22Tov24()
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : MP3 with existing unsupported fields : RVA & TBP
            String           testFileLocation = TestUtils.GetTempTestFile("MP3/id3v2.2_iTunNORM-iTunPGAP.mp3");
            AudioDataManager theFile          = new AudioDataManager(AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            // Check if the two fields are indeed accessible
            Assert.IsTrue(theFile.ReadFromFile(false, true));
            Assert.IsNotNull(theFile.ID3v2);
            Assert.IsTrue(theFile.ID3v2.Exists);

            Assert.AreEqual("1997", theFile.ID3v2.Year);

            int    found    = 0;
            string rvaValue = "";
            string tbpValue = "";

            foreach (KeyValuePair <string, string> field in theFile.ID3v2.AdditionalFields)
            {
                if (field.Key.Equals("RVA"))
                {
                    rvaValue = field.Value;
                    found++;
                }
                else if (field.Key.Equals("TBP"))
                {
                    tbpValue = field.Value;
                    found++;
                }
            }
            Assert.AreEqual(2, found);

            // Check if they are persisted with proper ID3v2.4 field codes when editing the tag
            TagData theTag = new TagData();

            Assert.IsTrue(theFile.UpdateTagInFile(theTag, MetaDataIOFactory.TAG_ID3V2));

            Assert.IsTrue(theFile.ReadFromFile(false, true));

            // 1/ Check if values are the same
            found = 0;
            foreach (KeyValuePair <string, string> field in theFile.ID3v2.AdditionalFields)
            {
                if (field.Key.Equals("RVA2"))
                {
                    Assert.AreEqual(rvaValue, field.Value);
                    found++;
                }
                else if (field.Key.Equals("TBPM"))
                {
                    Assert.AreEqual(tbpValue, field.Value);
                    found++;
                }
            }
            Assert.AreEqual(2, found);

            Assert.AreEqual("1997", theFile.ID3v2.Year);


            // 2/ Check if they are indeed persisted as "classic" ID3v2 fields, and not as sub-codes inside a TXXX field
            byte[] readBytes = new byte[4];
            byte[] expected  = Utils.Latin1Encoding.GetBytes("TXXX");

            using (FileStream fs = new FileStream(testFileLocation, FileMode.Open, FileAccess.Read))
            {
                Assert.IsTrue(StreamUtils.FindSequence(fs, Utils.Latin1Encoding.GetBytes("RVA2")));
                fs.Seek(-15, SeekOrigin.Current);
                fs.Read(readBytes, 0, 4);
                Assert.IsFalse(StreamUtils.ArrEqualsArr(expected, readBytes));

                fs.Seek(0, SeekOrigin.Begin);


                expected = Utils.Latin1Encoding.GetBytes("TDRC");

                Assert.IsTrue(StreamUtils.FindSequence(fs, Utils.Latin1Encoding.GetBytes("1997")));
                fs.Seek(-15, SeekOrigin.Current);
                fs.Read(readBytes, 0, 4);
                Assert.IsTrue(StreamUtils.ArrEqualsArr(expected, readBytes));
            }

            // Get rid of the working copy
            File.Delete(testFileLocation);
        }
示例#12
0
        public void PLIO_W_ASX()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add(TestUtils.GetResourceLocationRoot() + "aaa.mp3");
            pathsToWrite.Add(TestUtils.GetResourceLocationRoot() + "bbb.mp3");

            IList <Track> tracksToWrite = new List <Track>();

            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3\\empty.mp3"));
            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD\\mod.mod"));


            string testFileLocation = TestUtils.CreateTempTestFile("test.asx");

            try
            {
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);

                // Test Path writing
                pls.FilePaths = pathsToWrite;
                IList <string> parents = new List <string>();
                int            index   = -1;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if the default UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsTrue(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        // Read file content
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("asx", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name.ToLower());
                                }
                                else if (source.Name.Equals("entry", StringComparison.OrdinalIgnoreCase) && parents.Contains("asx"))
                                {
                                    index++;
                                    parents.Add(source.Name.ToLower());
                                }
                                else if (source.Name.Equals("ref", StringComparison.OrdinalIgnoreCase) && parents.Contains("entry"))
                                {
                                    Assert.AreEqual(pathsToWrite[index], source.GetAttribute("HREF"));
                                }
                            }
                        }
                    }
                }

                Assert.AreEqual(3, parents.Count);

                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.AreEqual(pathsToWrite[0], filePaths[0]);
                Assert.AreEqual(pathsToWrite[1], filePaths[1]);


                // Test Track writing
                pls.Tracks = tracksToWrite;
                index      = -1;
                parents.Clear();

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("asx", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name.ToLower());
                                }
                                else if (source.Name.Equals("entry", StringComparison.OrdinalIgnoreCase) && parents.Contains("asx"))
                                {
                                    index++;
                                    parents.Add(source.Name.ToLower());
                                }
                                else if (parents.Contains("entry"))
                                {
                                    if (source.Name.Equals("ref", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(tracksToWrite[index].Path, source.GetAttribute("HREF"));
                                    }
                                    else if (source.Name.Equals("title", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(tracksToWrite[index].Title, getXmlValue(source));
                                    }
                                    else if (source.Name.Equals("author", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(tracksToWrite[index].Artist, getXmlValue(source));
                                    }
                                }
                            }
                        }
                    }
                Assert.AreEqual(3, parents.Count);

                IList <Track> tracks = pls.Tracks;
                Assert.AreEqual(2, tracks.Count);
                Assert.AreEqual(tracksToWrite[0].Path, tracks[0].Path);
                Assert.AreEqual(tracksToWrite[1].Path, tracks[1].Path);
            }
            finally
            {
                if (Settings.DeleteAfterSuccess)
                {
                    File.Delete(testFileLocation);
                }
            }
        }
示例#13
0
        public void PLIO_W_B4S()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add("aaa.mp3");
            pathsToWrite.Add("bbb.mp3");

            IList <Track> tracksToWrite = new List <Track>();

            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3\\empty.mp3"));
            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD\\mod.mod"));


            string testFileLocation = TestUtils.CreateTempTestFile("test.b4s");

            try
            {
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);

                // Test Path writing
                pls.FilePaths = pathsToWrite;
                IList <string> parents = new List <string>();
                int            index   = -1;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if _no_ UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsFalse(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("WinampXML", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("playlist", StringComparison.OrdinalIgnoreCase) && parents.Contains("WinampXML"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("entry", StringComparison.OrdinalIgnoreCase) && parents.Contains("playlist"))
                                {
                                    parents.Add(source.Name);
                                    index++;
                                    Assert.AreEqual(pathsToWrite[index], source.GetAttribute("Playstring"));
                                }
                            }
                        }
                    }
                }

                Assert.AreEqual(4, parents.Count);

                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.IsTrue(filePaths[0].EndsWith(pathsToWrite[0]));
                Assert.IsTrue(filePaths[1].EndsWith(pathsToWrite[1]));

                // Test Track writing
                pls.Tracks = tracksToWrite;
                index      = -1;
                parents.Clear();

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("WinampXML", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("playlist", StringComparison.OrdinalIgnoreCase) && parents.Contains("WinampXML"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("entry", StringComparison.OrdinalIgnoreCase) && parents.Contains("playlist"))
                                {
                                    parents.Add(source.Name);
                                    index++;
                                    Assert.IsTrue(source.GetAttribute("Playstring").EndsWith(tracksToWrite[index].Path.Replace('\\', '/')));
                                }
                                else if (parents.Contains("entry"))
                                {
                                    if (source.Name.Equals("Name", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(tracksToWrite[index].Title, getXmlValue(source));
                                    }
                                    else if (source.Name.Equals("Length", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(((long)Math.Round(tracksToWrite[index].DurationMs)).ToString(), getXmlValue(source));
                                    }
                                }
                            }
                        }
                    }
                Assert.AreEqual(4, parents.Count);

                IList <Track> tracks = pls.Tracks;
                Assert.AreEqual(2, tracks.Count);
                Assert.AreEqual(tracksToWrite[0].Path, tracks[0].Path);
                Assert.AreEqual(tracksToWrite[1].Path, tracks[1].Path);
            }
            finally
            {
                if (Settings.DeleteAfterSuccess)
                {
                    File.Delete(testFileLocation);
                }
            }
        }
示例#14
0
        public void StreamUtils_Exceptions()
        {
            Assert.IsFalse(StreamUtils.ArrEqualsArr(new byte[1], new byte[2]));
            Assert.IsFalse(StreamUtils.StringEqualsArr(".", new char[2]));

            try
            {
                StreamUtils.DecodeBEUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEUInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeBEUInt24(0x01FFFFFF);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeUInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeSynchSafeInt(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeSynchSafeInt32(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 6);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 33);
                Assert.Fail();
            }
            catch { }
        }
示例#15
0
        public void PLIO_W_XSPF()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add(TestUtils.GetResourceLocationRoot() + "aaa.mp3");
            pathsToWrite.Add(TestUtils.GetResourceLocationRoot() + "bbb.mp3");

            IList <Track> tracksToWrite = new List <Track>();

            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3\\empty.mp3"));
            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD\\mod.mod"));


            string testFileLocation = TestUtils.CreateTempTestFile("test.xspf");

            try
            {
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);

                // Test Path writing
                pls.FilePaths = pathsToWrite;
                IList <string> parents = new List <string>();
                int            index   = -1;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if the default UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsTrue(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("playlist", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("tracklist", StringComparison.OrdinalIgnoreCase) && parents.Contains("playlist"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("track", StringComparison.OrdinalIgnoreCase) && parents.Contains("trackList"))
                                {
                                    parents.Add(source.Name);
                                    index++;
                                }
                                else if (source.Name.Equals("location", StringComparison.OrdinalIgnoreCase) && parents.Contains("track"))
                                {
                                    Assert.AreEqual(pathsToWrite[index], getXmlValue(source).Replace('/', '\\'));
                                }
                            }
                        }
                    }
                }

                Assert.AreEqual(4, parents.Count);

                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.IsTrue(filePaths[0].EndsWith(pathsToWrite[0]));
                Assert.IsTrue(filePaths[1].EndsWith(pathsToWrite[1]));


                // Test Track writing
                pls.Tracks = tracksToWrite;
                index      = -1;
                parents.Clear();

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("playlist", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("trackList", StringComparison.OrdinalIgnoreCase) && parents.Contains("playlist"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("track", StringComparison.OrdinalIgnoreCase) && parents.Contains("trackList"))
                                {
                                    parents.Add(source.Name);
                                    index++;
                                }
                                else if (parents.Contains("track"))
                                {
                                    if (source.Name.Equals("location", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].Path);
                                    }
                                    else if (source.Name.Equals("title", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].Title);
                                    }
                                    else if (source.Name.Equals("creator", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].Artist);
                                    }
                                    else if (source.Name.Equals("album", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].Album);
                                    }
                                    else if (source.Name.Equals("annotation", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].Comment);
                                    }
                                    else if (source.Name.Equals("trackNum", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), tracksToWrite[index].TrackNumber);
                                    }
                                    else if (source.Name.Equals("duration", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Assert.AreEqual(getXmlValue(source), ((long)Math.Round(tracksToWrite[index].DurationMs)).ToString());
                                    }
                                }
                            }
                        }
                    }
                Assert.AreEqual(4, parents.Count);

                IList <Track> tracks = pls.Tracks;
                Assert.AreEqual(2, tracks.Count);
                Assert.AreEqual(tracksToWrite[0].Path, tracks[0].Path);
                Assert.AreEqual(tracksToWrite[1].Path, tracks[1].Path);
            }
            finally
            {
                File.Delete(testFileLocation);
            }
        }
示例#16
0
        public void PLIO_W_SMIL()
        {
            IList <string> pathsToWrite = new List <string>();

            pathsToWrite.Add("aaa.mp3");
            pathsToWrite.Add("bbb.mp3");

            IList <Track> tracksToWrite = new List <Track>();

            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3\\empty.mp3"));
            tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD\\mod.mod"));


            string testFileLocation = TestUtils.CreateTempTestFile("test.smil");

            try
            {
                IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(testFileLocation);

                // Test Path writing
                pls.FilePaths = pathsToWrite;
                IList <string> parents = new List <string>();
                int            index   = -1;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                {
                    // Test if the default UTF-8 BOM has been written at the beginning of the file
                    byte[] bom = new byte[3];
                    fs.Read(bom, 0, 3);
                    Assert.IsTrue(StreamUtils.ArrEqualsArr(bom, PlaylistIO.BOM_UTF8));
                    fs.Seek(0, SeekOrigin.Begin);

                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("smil", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("body", StringComparison.OrdinalIgnoreCase) && parents.Contains("smil"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("seq", StringComparison.OrdinalIgnoreCase) && parents.Contains("body"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("media", StringComparison.OrdinalIgnoreCase) && parents.Contains("seq"))
                                {
                                    index++;
                                    Assert.AreEqual(pathsToWrite[index], source.GetAttribute("src"));
                                }
                            }
                        }
                    }
                }
                Assert.AreEqual(3, parents.Count);

                IList <string> filePaths = pls.FilePaths;
                Assert.AreEqual(2, filePaths.Count);
                Assert.IsTrue(filePaths[0].EndsWith(pathsToWrite[0]));
                Assert.IsTrue(filePaths[1].EndsWith(pathsToWrite[1]));


                // Test Track writing
                pls.Tracks = tracksToWrite;
                parents.Clear();
                index = -1;

                using (FileStream fs = new FileStream(testFileLocation, FileMode.Open))
                    using (XmlReader source = XmlReader.Create(fs))
                    {
                        while (source.Read())
                        {
                            if (source.NodeType == XmlNodeType.Element)
                            {
                                if (source.Name.Equals("smil", StringComparison.OrdinalIgnoreCase))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("body", StringComparison.OrdinalIgnoreCase) && parents.Contains("smil"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("seq", StringComparison.OrdinalIgnoreCase) && parents.Contains("body"))
                                {
                                    parents.Add(source.Name);
                                }
                                else if (source.Name.Equals("media", StringComparison.OrdinalIgnoreCase) && parents.Contains("seq"))
                                {
                                    index++;
                                    Assert.AreEqual("file:///" + tracksToWrite[index].Path.Replace('\\', '/'), source.GetAttribute("src"));
                                }
                            }
                        }
                    }
                Assert.AreEqual(3, parents.Count);

                IList <Track> tracks = pls.Tracks;
                Assert.AreEqual(2, tracks.Count);
                Assert.AreEqual(tracksToWrite[0].Path, tracks[0].Path);
                Assert.AreEqual(tracksToWrite[1].Path, tracks[1].Path);
            }
            finally
            {
                if (Settings.DeleteAfterSuccess)
                {
                    File.Delete(testFileLocation);
                }
            }
        }