📄 id3v2tag.java
字号:
* instead of increasing the size of the file. That is only if there is * a sufficient amount of padding for the updated tag. * * @return the new amount of padding */ private int getUpdatedPadding() { int curSize = getSize(); int pad = 0; if( (origPadding == padding) && (curSize > origSize) && (padding >= (curSize - origSize)) ) { pad = padding - (curSize - origSize); } else if( curSize < origSize ) { pad = (origSize - curSize) + padding; } return pad; } /** * Set the data contained in a text frame. This includes all frames with * an id that starts with 'T' but excludes "TXXX". If an improper id * is passed, then nothing will happen. * * @param id the id of the frame to set the data for * @param data the data for the frame */ public void setTextFrame( String id, String data ) { if( (id.charAt(0) == 'T') && !id.equals(ID3v2Frames.USER_DEFINED_TEXT_INFO) ) { try { byte[] b = new byte[data.length() + 1]; b[0] = 0; System.arraycopy( data.getBytes( ENC_TYPE ), 0, b, 1, data.length() ); updateFrameData( id, b ); } catch( UnsupportedEncodingException e ) { e.printStackTrace(); } } } /** * Set the data contained in a URL frame. This includes all frames with * an id that starts with 'W' but excludes "WXXX". If an improper id is * passed, then nothing will happen. * * @param id the id of the frame to set the data for * @param data the data for the frame */ public void setURLFrame( String id, String data ) { if( (id.charAt(0) == 'W') && !id.equals(ID3v2Frames.USER_DEFINED_URL) ) { updateFrameData( id, data.getBytes() ); } } /** * Sets the data contained in the user defined text frame (TXXX). * * @param description a description of the data * @param value the data for the frame */ public void setUserDefinedTextFrame( String description, String value ) { try { byte[] b = new byte[description.length() + value.length() + 2]; int bytesCopied = 0; b[bytesCopied++] = 0; System.arraycopy( description.getBytes(ENC_TYPE), 0, b, bytesCopied, description.length() ); bytesCopied += description.length(); b[bytesCopied++] = 0; System.arraycopy( value.getBytes(ENC_TYPE), 0, b, bytesCopied, value.length() ); bytesCopied += value.length(); updateFrameData( ID3v2Frames.USER_DEFINED_TEXT_INFO, b ); } catch( UnsupportedEncodingException e ) { e.printStackTrace(); } } /** * Sets the data contained in the user defined url frame (WXXX). * * @param description a description of the url * @param value the url for the frame */ public void setUserDefinedURLFrame( String description, String value ) { try { byte[] b = new byte[description.length() + value.length() + 2]; int bytesCopied = 0; b[bytesCopied++] = 0; System.arraycopy( description.getBytes(ENC_TYPE), 0, b, bytesCopied, description.length() ); bytesCopied += description.length(); b[bytesCopied++] = 0; System.arraycopy( value.getBytes(), 0, b, bytesCopied, value.length() ); bytesCopied += value.length(); updateFrameData( ID3v2Frames.USER_DEFINED_URL, b ); } catch( UnsupportedEncodingException e ) { e.printStackTrace(); } } /** * Set the data contained in the comments frame (COMM). * * @param description a description of the comment * @param comment the comment */ public void setCommentFrame( String description, String comment ) { try { byte[] b = new byte[description.length() + comment.length() + 5]; int bytesCopied = 0; b[bytesCopied++] = 0; b[bytesCopied++] = 'e'; b[bytesCopied++] = 'n'; b[bytesCopied++] = 'g'; System.arraycopy( description.getBytes(ENC_TYPE), 0, b, bytesCopied, description.length() ); bytesCopied += description.length(); b[bytesCopied++] = 0; System.arraycopy( comment.getBytes(ENC_TYPE), 0, b, bytesCopied, comment.length() ); bytesCopied += comment.length(); updateFrameData( ID3v2Frames.COMMENTS, b ); } catch( UnsupportedEncodingException e ) { e.printStackTrace(); } } /** * Remove the frame with the specified id from the file. If there is no * frame with that id nothing will happen. * * @param id the id of the frame to remove */ public void removeFrame( String id ) { frames.remove( id ); updateSize(); } /** * Updates the data for the frame specified by id. If no frame exists for * the id specified, a new frame with that id is created. * * @param id the id of the frame to update * @param data the data for the frame */ public void updateFrameData( String id, byte[] data ) { if( frames.containsKey( id ) ) { ((ID3v2Frame)frames.get( id )).setFrameData( data ); } else { ID3v2Frame frame = new ID3v2Frame( id, data ); frames.put( id, frame ); } updateSize(); } /** * Returns the textual information contained in the frame specified by the * id. Not every type of frame has textual information. If an id is * specified that will not work, the empty string is returned. * * @param id the id of the frame to get text from * @return the text information contained in the frame * @exception ID3v2FormatException if an error is encountered parsing data */ public String getFrameDataString( String id ) throws ID3v2FormatException { String str = new String(); if( frames.containsKey( id ) ) { str = ((ID3v2Frame)frames.get( id )).getDataString(); } return str; } /** * Returns the data found in the frame specified by the id. If the frame * doesn't exist, then a zero length array is returned. * * @param id the id of the frame to get the data from * @return the data found in the frame */ public byte[] getFrameData( String id ) { byte[] b = new byte[0]; if( frames.containsKey( id ) ) { b = ((ID3v2Frame)frames.get( id )).getFrameData(); } return b; } /** * Updates the size field of the id3 header and footer (if it exists) * from the current size of the id3v2 frames plus the extended header * size (if it exists). * */ private void updateSize() { int length = frames.getLength(); if( head.getExtendedHeader() ){ length += ext_head.getSize(); } head.setTagSize( length ); if( head.getFooter() ) { foot.setTagSize( length ); } } /** * Returns true if an id3v2 tag exists in the file that was passed to the * constructor and false otherwise * * @return true if an id3v2 tag exists in the file passed to the ctor */ public boolean tagExists() { return exists; } /** * Returns the size of this id3v2 tag. This includes the header, * extended header, frames, padding, and footer. * * @return the size (in bytes) of the entire id3v2 tag */ public int getSize() { int retval = head.getTagSize() + head.getHeaderSize(); if( head.getFooter() ) { retval += foot.getFooterSize(); } return retval; } /** * Returns the current number of padding bytes in this id3v2 tag. * * @return the current number of padding bytes in this id3v2 tag */ public int getPadding() { return padding; } /** * Set the amount of padding to use when writing this tag. There cannot * be any padding if a footer exists. Nothing will happen if this function * is called and a footer exists or if the number is negative. * * @param pad the amount of padding to use when writing this tag */ public void setPadding( int pad ) { if( !head.getFooter() && (pad >= 0) ) { padding = pad; } } /** * Return a string representation of this object. This includes all data * contained in all parts of this tag. * * @return a string representation of this object */ public String toString() { String str = head.toString(); str += "\nPadding:\t\t\t" + getPadding() + " bytes"; if( head.getExtendedHeader() ) { str += "\n" + ext_head.toString(); } str += "\n" + frames.toString(); if( head.getFooter() ) { str += foot.toString(); } return str; }} // ID3v2Tag
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -