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

📄 id3v1tag.java

📁 YOYOPlayer MP3播放器 java+JMF实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (getFirstTitle().length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.TITLE.name(), getFirstTitle());
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList();
        }
    }

    /**
     * Add Year
     * <p/>
     * <p>Only one year can be added so if one already exists it will be replaced.
     *
     * @param year
     */
    public void addYear(String year)
    {
        setYear(year);
    }

    /**
     * Set year
     *
     * @param year
     */
    public void setYear(String year)
    {
        this.year = ID3Tags.truncate(year, this.FIELD_YEAR_LENGTH);
    }

    /**
     * Get year
     *
     * @return year
     */
    public String getFirstYear()
    {
        return year;
    }

    /**
     * Get year field
     * <p/>
     * <p>Only a single year is available in ID3v1
     *
     * @return
     */
    public List getYear()
    {
        if (getFirstYear().length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.YEAR.name(), getFirstYear());
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList();
        }
    }

    public void addTrack(String track)
    {
        throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
    }

    public String getFirstTrack()
    {
        return "-1";
    }

    public void setTrack(String track)
    {
        throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
    }

    public List getTrack()
    {
        throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
    }

    public TagField getFirstField(String id)
    {
       //TODO
       throw new UnsupportedOperationException("TODO:Not done yet");
    }

    public Iterator getFields()
    {
        throw new UnsupportedOperationException("TODO:Not done yet");
    }

    public boolean hasCommonFields()
    {
        //TODO
        return true;
    }

    public boolean hasField(String id)
    {
        //TODO
        throw new UnsupportedOperationException("TODO:Not done yet");
    }

    public boolean isEmpty()
    {
        //TODO
        throw new UnsupportedOperationException("TODO:Not done yet");
    }



    public void set(TagField field)
    {
        TagFieldKey genericKey = TagFieldKey.valueOf(field.getId());
        switch(genericKey)
        {
            case ARTIST:
                setArtist(field.toString());

            case ALBUM:
                setAlbum(field.toString());

            case TITLE:
                setTitle(field.toString());

            case GENRE:
                setGenre(field.toString());

            case YEAR:
                setYear(field.toString());

            case COMMENT:
                setComment(field.toString());
        }
    }

    /**
     *
     * @param encoding
     * @return
     */
    public boolean setEncoding(String encoding)
    {
        this.encoding=encoding;
        return true;
    }

    /**
     * Create Tag Field using generic key
     */
    public TagField createTagField(TagFieldKey genericKey, String value)
    {
        return new ID3v1TagField(tagFieldToID3v1Field.get(genericKey).name(), value);
    }

    public String getEncoding()
    {
        return "ISO-8859-1";
    }

    /**
     * Returns a {@linkplain List list} of {@link TagField} objects whose &quot;{@linkplain TagField#getId() id}&quot;
     * is the specified one.<br>
     *
     * @param genericKey The generic field key
     * @return A list of {@link TagField} objects with the given &quot;id&quot;.
     */
    public List<TagField> get(TagFieldKey genericKey)
    {
        switch(genericKey)
        {
            case ARTIST:
                return getArtist();

            case ALBUM:
                return getAlbum();

            case TITLE:
                return getTitle();

            case GENRE:
                return getGenre();

            case YEAR:
                return getYear();

            case COMMENT:
                return getComment();

            default:
                return new ArrayList<TagField>();                        
        }
    }


    /**
     * Retrieve the first value that exists for this key id
     *
     * @param  genericKey
     * @return
     */
    public String getFirst(String genericKey)
    {
        TagFieldKey matchingKey = TagFieldKey.valueOf(genericKey);
        if(matchingKey!=null)
        {
            return getFirst(matchingKey);
        }
        else
        {
            return "";
        }
    }


    /**
     * Retrieve the first value that exists for this generic key
     *
     * @param genericKey
     * @return
     */
    public String getFirst(TagFieldKey genericKey)
    {
         switch(genericKey)
        {
            case ARTIST:
                return getFirstArtist();

            case ALBUM:
                return getFirstAlbum();

            case TITLE:
                return getFirstTitle();

            case GENRE:
                return getFirstGenre();

            case YEAR:
                return getFirstYear();

            case TRACK:
                return getFirstTrack();

            case COMMENT:
                return getFirstComment();

            default:
                return "";
        }
    }

    /**
     * Delete any instance of tag fields with this key
     *
     * @param genericKey
     */
    public void deleteTagField(TagFieldKey genericKey)
    {
         switch(genericKey)
        {
            case ARTIST:
                setArtist("");
                break;
            case ALBUM:
                setAlbum("");
                break;
            case TITLE:
                setTitle("");
                break;
            case GENRE:
                setGenre("");
                break;
            case YEAR:
                setYear("");
                break;
            case COMMENT:
                setComment("");
                break;
        }
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + (this.album != null ? this.album.hashCode() : 0);
        hash = 17 * hash + (this.artist != null ? this.artist.hashCode() : 0);
        hash = 17 * hash + (this.comment != null ? this.comment.hashCode() : 0);
        hash = 17 * hash + (this.title != null ? this.title.hashCode() : 0);
        hash = 17 * hash + (this.year != null ? this.year.hashCode() : 0);
        hash = 17 * hash + this.genre;
        return hash;
    }

    /**
     * @param obj
     * @return true if this and obj are equivalent
     */
    public boolean equals(Object obj)
    {
        if ((obj instanceof ID3v1Tag) == false)
        {
            return false;
        }
        ID3v1Tag object = (ID3v1Tag) obj;
        if (this.album.equals(object.album) == false)
        {
            return false;
        }
        if (this.artist.equals(object.artist) == false)
        {
            return false;
        }
        if (this.comment.equals(object.comment) == false)
        {
            return false;
        }
        if (this.genre != object.genre)
        {
            return false;
        }
        if (this.title.equals(object.title) == false)
        {
            return false;
        }
        if (this.year.equals(object.year) == false)
        {
            return false;
        }
        return super.equals(obj);
    }

    /**
     * @return an iterator to iterate through the fields of the tag
     */
    public Iterator iterator()
    {
        return new ID3v1Iterator(this);
    }


    /**
     * @param byteBuffer
     * @throws TagNotFoundException
     */
    public void read(ByteBuffer byteBuffer)
            throws TagNotFoundException
    {
        if (seek(byteBuffer) == false)
        {
            throw new TagNotFoundException(getLoggingFilename() + ":" + "ID3v1 tag not found");
        }
        logger.finer(getLoggingFilename() + ":" + "Reading v1 tag");
        //Do single file read of data to cut down on file reads
        byte[] dataBuffer = new byte[TAG_LENGTH];
        byteBuffer.position(0);
        byteBuffer.get(dataBuffer, 0, TAG_LENGTH);
        title = Utils.getString(dataBuffer, FIELD_TITLE_POS, this.FIELD_TITLE_LENGTH,"ISO-8859-1").trim();
        Matcher m = endofStringPattern.matcher(title);
        if (m.find() == true)
        {
            title = title.substring(0, m.start());
        }
        artist = Utils.getString(dataBuffer, FIELD_ARTIST_POS, this.FIELD_ARTIST_LENGTH,"ISO-8859-1").trim();
        m = endofStringPattern.matcher(artist);
        if (m.find() == true)
        {
            artist = artist.substring(0, m.start());
        }
        album = Utils.getString(dataBuffer, FIELD_ALBUM_POS, this.FIELD_ALBUM_LENGTH,"ISO-8859-1").trim();
        m = endofStringPattern.matcher(album);
        logger.finest(getLoggingFilename() + ":" + "Orig Album is:" + comment + ":");
        if (m.find() == true)
        {
            album = album.substring(0, m.start());
            logger.finest(getLoggingFilename() + ":" + "Album is:" + album + ":");
        }
        year = Utils.getString(dataBuffer, FIELD_YEAR_POS, this.FIELD_YEAR_LENGTH,"ISO-8859-1").trim();
        m = endofStringPattern.matcher(year);
        if (m.find() == true)
        {
            year = year.substring(0, m.start());
        }
        comment = Utils.getString(dataBuffer, FIELD_COMMENT_POS, this.FIELD_COMMENT_LENGTH,"ISO-8859-1").trim();
        m = endofStringPattern.matcher(comment);
        logger.finest(getLoggingFilename() + ":" + "Orig Comment is:" + comment + ":");
        if (m.find() == true)
        {
            comment = comment.substring(0, m.start());
            logger.finest(getLoggingFilename() + ":" + "Comment is:" + comment + ":");
        }
        genre = dataBuffer[this.FIELD_GENRE_POS];

    }

    /**
     * Does a tag of this version exist within the byteBuffer
     *
     * @return whether tag exists within the byteBuffer
     */
    public boolean seek(ByteBuffer byteBuffer)
    {
        byte[] buffer = new byte[FIELD_TAGID_LENGTH];
        // read the TAG value
        byteBuffer.get(buffer, 0, FIELD_TAGID_LENGTH);
        return (Arrays.equals(buffer, TAG_ID));
    }

    /**
     * Write this tag to the file, replacing any tag previously existing
     *
     * @param file
     * @throws IOException
     */
    public void write(RandomAccessFile file)
            throws IOException
    {
        logger.info("Saving file");
        byte[] buffer = new byte[TAG_LENGTH];
        int i;
        String str;
        delete(file);
        file.seek(file.length());
        //Copy the TAGID into new buffer
        System.arraycopy(TAG_ID, this.FIELD_TAGID_POS, buffer, this.FIELD_TAGID_POS, TAG_ID.length);
        int offset = this.FIELD_TITLE_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveTitle())
        {
            str = ID3Tags.truncate(title, this.FIELD_TITLE_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = this.FIELD_ARTIST_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveArtist())
        {
            str = ID3Tags.truncate(artist, this.FIELD_ARTIST_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = this.FIELD_ALBUM_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveAlbum())
        {
            str = ID3Tags.truncate(album, this.FIELD_ALBUM_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = this.FIELD_YEAR_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveYear())
        {
            str = ID3Tags.truncate(year, AbstractID3v1Tag.FIELD_YEAR_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = this.FIELD_COMMENT_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveComment())
        {
            str = ID3Tags.truncate(comment, this.FIELD_COMMENT_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = this.FIELD_GENRE_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveGenre())
        {
            buffer[offset] = genre;
        }
        file.write(buffer);
    }

    /**
     * Create strcutured representation of this item.
     */
    public void createStructure()
    {
        MP3File.getStructureFormatter().openHeadingElement(TYPE_TAG, getIdentifier());
        //Header
        MP3File.getStructureFormatter().addElement(TYPE_TITLE, this.title);
        MP3File.getStructureFormatter().addElement(TYPE_ARTIST, this.artist);
        MP3File.getStructureFormatter().addElement(TYPE_ALBUM, this.album);
        MP3File.getStructureFormatter().addElement(TYPE_YEAR, this.year);
        MP3File.getStructureFormatter().addElement(TYPE_COMMENT, this.comment);
        MP3File.getStructureFormatter().addElement(TYPE_GENRE, (int) this.genre);
        MP3File.getStructureFormatter().closeHeadingElement(TYPE_TAG);
    }
}

⌨️ 快捷键说明

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