Exemple #1
0
        //Retrieves the tag as a byte array. This method does not attempt to read the tag data,
        //it simply reads the header and if present the tag bytes are read directly from the
        //stream. This means that typical exceptions that get thrown in a tag read will not
        //occur in this method.
        public byte[] GetTagBytes(int majorVersion, int minorVersion)
        {
            RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(majorVersion, minorVersion);

            byte[] tagBytes = registeredHandler.Handler.GetTagBytes(_stream);
            return(tagBytes);
        }
Exemple #2
0
        public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotWriteTag);
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            //The tag should specify major version number
            if (tag.MajorVersion == 0)
            {
                throw new ArgumentException(Id3Messages.MajorTagVersionMissing, "tag");
            }

            //Get any existing handlers from the same family as the tag
            IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(tag.Family);

            //If a tag already exists from the same family, but is a different version than the passed tag,
            //delete it if conflictAction is Replace.
            RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault();

            if (familyHandler != null)
            {
                Id3Handler handler = familyHandler.Handler;
                if (handler.MajorVersion != tag.MajorVersion || handler.MinorVersion != tag.MinorVersion)
                {
                    if (conflictAction == WriteConflictAction.NoAction)
                    {
                        return(false);
                    }
                    if (conflictAction == WriteConflictAction.Replace)
                    {
                        Id3Handler handlerCopy = handler;
                        handlerCopy.DeleteTag(_stream);
                    }
                }
            }

            //Write the tag to the file. The handler will know how to overwrite itself.
            RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(tag.MajorVersion, tag.MinorVersion);
            bool writeSuccessful = registeredHandler.Handler.WriteTag(_stream, tag);

            if (writeSuccessful)
            {
                InvalidateExistingHandlers();
            }
            return(writeSuccessful);
        }