⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractid3v2tag.java

📁 java+eclipse做的TTPlayer
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            System.out.println("Frame exists");
            AbstractID3v2Frame oldFrame = (AbstractID3v2Frame) o;
            if (newFrame.getBody() instanceof FrameBodyTXXX)
            {
                //Different key so convert to list and add as new frame
                if (!((FrameBodyTXXX) newFrame.getBody()).getDescription()
                        .equals(((FrameBodyTXXX) oldFrame.getBody()).getDescription()))
                {
                    List<AbstractID3v2Frame> frames = new ArrayList<AbstractID3v2Frame>();
                    frames.add(oldFrame);
                    frames.add(newFrame);
                    frameMap.put(newFrame.getId(), frames);
                    System.out.println("Adding....");
                }
                //Same key so replace
                else
                {
                    System.out.println("Replacing key....");
                    frameMap.put(newFrame.getId(), newFrame);
                }
            }
        }
        else if (o instanceof List)
        {
            for (ListIterator<AbstractID3v2Frame> li = ((List<AbstractID3v2Frame>) o).listIterator(); li.hasNext();)
            {
                AbstractID3v2Frame nextFrame = li.next();

                if (newFrame.getBody() instanceof FrameBodyTXXX)
                {
                    //Value with matching key exists so replace 
                    if (((FrameBodyTXXX) newFrame.getBody()).getDescription()
                            .equals(((FrameBodyTXXX) nextFrame.getBody()).getDescription()))
                    {
                        li.set(newFrame);
                        frameMap.put(newFrame.getId(), o);
                    }
                }
            }
            //No match found so add
            ((List<AbstractID3v2Frame>) o).add(newFrame);
        }
    }

    public void setAlbum(String s) throws FieldDataInvalidException
    {
        set(createAlbumField(s));
    }

    public void setArtist(String s) throws FieldDataInvalidException
    {
        set(createArtistField(s));
    }

    public void setComment(String s) throws FieldDataInvalidException
    {
        set(createCommentField(s));
    }

    public void setGenre(String s) throws FieldDataInvalidException
    {
        set(createGenreField(s));
    }

    public void setTitle(String s) throws FieldDataInvalidException
    {
        set(createTitleField(s));
    }

    public void setTrack(String s) throws FieldDataInvalidException
    {
        set(createTrackField(s));
    }

    public void setYear(String s) throws FieldDataInvalidException
    {
        set(createYearField(s));
    }

    /**
     * @param field
     * @throws FieldDataInvalidException
     */
    public void add(TagField field) throws FieldDataInvalidException
    {
        if (field == null)
        {
            return;
        }

        if (!(field instanceof AbstractID3v2Frame))
        {
            throw new FieldDataInvalidException("Field " + field + " is not of type AbstractID3v2Frame");
        }

        Object o = frameMap.get(field.getId());

        //There are already frames of this type
        if (o instanceof List)
        {
            List list = (List) o;
            list.add(field);
        }
        //No frame of this type
        else if (o == null)
        {
            frameMap.put(field.getId(), field);
        }
        //One frame exists, we are adding another so convert to list
        else
        {
            List list = new ArrayList();
            list.add(o);
            list.add(field);
            frameMap.put(field.getId(), list);
        }
    }

    /**
     * Adds an album to the tag.<br>
     *
     * @param album Album description
     */
    public void addAlbum(String album) throws FieldDataInvalidException
    {
        add(createAlbumField(album));
    }

    /**
     * Adds an artist to the tag.<br>
     *
     * @param artist Artist's name
     */
    public void addArtist(String artist) throws FieldDataInvalidException
    {
        add(createArtistField(artist));
    }

    /**
     * Adds a comment to the tag.<br>
     *
     * @param comment Comment.
     */
    public void addComment(String comment) throws FieldDataInvalidException
    {
        add(createCommentField(comment));
    }

    /**
     * Adds a genre to the tag.<br>
     *
     * @param genre Genre
     */
    public void addGenre(String genre) throws FieldDataInvalidException
    {
        add(createGenreField(genre));
    }

    /**
     * Adds a title to the tag.<br>
     *
     * @param title Title
     */
    public void addTitle(String title) throws FieldDataInvalidException
    {
        add(createTitleField(title));
    }

    /**
     * Adds a track to the tag.<br>
     *
     * @param track Track
     */
    public void addTrack(String track) throws FieldDataInvalidException
    {
        add(createTrackField(track));
    }

    /**
     * Adds a year to the Tag.<br>
     *
     * @param year Year
     */
    public void addYear(String year) throws FieldDataInvalidException
    {
        add(createYearField(year));
    }


    /**
     * Used for setting multiple frames for a single frame Identifier
     * <p/>
     * Warning if frame(s) already exists for this identifier thay are overwritten
     * <p/>
     * TODO needs to ensure do not add an invalid frame for this tag
     */
    public void setFrame(String identifier, List<AbstractID3v2Frame> multiFrame)
    {
        frameMap.put(identifier, multiFrame);
    }

    /**
     * Return the number of frames in this tag of a particular type, multiple frames
     * of the same time will only be counted once
     *
     * @return a count of different frames
     */
    public int getFrameCount()
    {
        if (frameMap == null)
        {
            return 0;
        }
        else
        {
            return frameMap.size();
        }
    }

    /**
     * Return all frames which start with the identifier, this
     * can be more than one which is useful if trying to retrieve
     * similar frames e.g TIT1,TIT2,TIT3 ... and don't know exaclty
     * which ones there are.
     * <p/>
     * Warning the match is only done against the identifier so if a tag contains a frame with an unsupported body
     * but happens to have an identifier that is valid for another version of the tag it will be returned.
     *
     * @param identifier
     * @return an iterator of all the frames starting with a particular identifier
     */
    public Iterator getFrameOfType(String identifier)
    {
        Iterator iterator = frameMap.keySet().iterator();
        HashSet result = new HashSet();
        String key;
        while (iterator.hasNext())
        {
            key = (String) iterator.next();
            if (key.startsWith(identifier))
            {
                result.add(frameMap.get(key));
            }
        }
        return result.iterator();
    }


    /**
     * Delete Tag
     *
     * @param file to delete the tag from
     * @throws IOException if problem accessing the file
     *                     <p/>
     */
    //TODO should clear all data and preferably recover lost space.
    public void delete(RandomAccessFile file) throws IOException
    {
        // this works by just erasing the "TAG" tag at the beginning
        // of the file
        byte[] buffer = new byte[FIELD_TAGID_LENGTH];
        //Read into Byte Buffer
        final FileChannel fc = file.getChannel();
        fc.position();
        ByteBuffer byteBuffer = ByteBuffer.allocate(TAG_HEADER_LENGTH);
        fc.read(byteBuffer, 0);
        byteBuffer.flip();
        if (seek(byteBuffer))
        {
            file.seek(0L);
            file.write(buffer);
        }
    }

    /**
     * Is this tag equivalent to another
     *
     * @param obj to test for equivalence
     * @return true if they are equivalent
     */
    public boolean equals(Object obj)
    {
        if ((obj instanceof AbstractID3v2Tag) == false)
        {
            return false;
        }
        AbstractID3v2Tag object = (AbstractID3v2Tag) obj;
        if (this.frameMap.equals(object.frameMap) == false)
        {
            return false;
        }
        return super.equals(obj);
    }


    /**
     * Return the frames in the order they were added
     *
     * @return and iterator of the frmaes/list of multi value frames
     */
    public Iterator iterator()
    {
        return frameMap.values().iterator();
    }

    /**
     * Remove frame(s) with this identifier from tag
     *
     * @param identifier frameId to look for
     */
    public void removeFrame(String identifier)
    {
        logger.finest("Removing frame with identifier:" + identifier);
        frameMap.remove(identifier);
    }

    /**
     * Remove all frame(s) which have an unsupported body, in other words
     * remove all frames that are not part of the standard frameset for
     * this tag
     */
    public void removeUnsupportedFrames()
    {
        for (Iterator i = iterator(); i.hasNext();)
        {
            Object o = i.next();
            if (o instanceof AbstractID3v2Frame)
            {
                if (((AbstractID3v2Frame) o).getBody() instanceof FrameBodyUnsupported)
                {
                    logger.finest("Removing frame" + ((AbstractID3v2Frame) o).getIdentifier());
                    i.remove();
                }
            }
        }
    }

    /**
     * Remove any frames starting with this
     * identifier from tag
     *
     * @param identifier start of frameId to look for
     */
    public void removeFrameOfType(String identifier)
    {
        Iterator iterator = this.getFrameOfType(identifier);
        while (iterator.hasNext())
        {
            AbstractID3v2Frame frame = (AbstractID3v2Frame) iterator.next();
            logger.finest("Removing frame with identifier:" + frame.getIdentifier() + "because starts with:" + identifier);
            frameMap.remove(frame.getIdentifier());

        }
    }


    /**
     * Write tag to file.
     *
     * @param file
     * @param audioStartByte
     * @throws IOException TODO should be abstract
     */
    public void write(File file, long audioStartByte) throws IOException
    {
    }

    /**
     * Write tag to file.
     *
     * @param file
     * @throws IOException TODO should be abstract
     */
    public void write(RandomAccessFile file) throws IOException
    {
    }

    /**
     * Write tag to channel.
     *
     * @param channel
     * @throws IOException TODO should be abstract
     */
    public void write(WritableByteChannel channel) throws IOException
    {
    }


    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -