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

📄 id3v2controler.java

📁 自己用Java写的一个mp3文件改名的小工具
💻 JAVA
字号:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;

/**
 *  ID3v2标签操作类
 *
 * @author     kilo
 * @created    2003年12月22日
 */
public final class ID3v2Controler
		 extends ID3Interface {
	/**
	 *  已经读取的ID3标签帧内容
	 */
	private static HashMap mInfo = new HashMap();
	/**
	 *  当前文件的ID3标签头
	 */
	private Tag cTag;
	/**
	 *  当前的ID3标签帧
	 */
	private TagFrame cTagFrame;


	/**
	 *  Constructor for the ID3v2Controler object
	 *
	 * @param  filename                   要进行改名操作的mp3文件名
	 * @exception  BadID3Exception        损坏的ID3标签异常
	 * @exception  FileNotFoundException  文件未找到异常
	 * @exception  IOException            I/O操作异常
	 */
	public ID3v2Controler( Object filename )
		throws BadID3Exception, FileNotFoundException, IOException {
		if ( cAFile != null )
			cAFile.close();

		cAFile = null;
		if ( filename instanceof String ) {
			cFile = new File( (String)filename );
			cAFile = new RandomAccessFile( (String)filename, "r" );
		}
		else if ( filename instanceof File ) {
			cFile = (File)filename;
			cAFile = new RandomAccessFile( (File)filename, "r" );
		}
		if ( cAFile != null ) {
			cTag = new Tag( cAFile );
			cTagFrame = new TagFrame();
		}
		else
			throw new BadID3Exception( "No ID3v1 flag" );
		mInfo.clear();
	}


	/**
	 *  Description of the Method
	 *
	 * @param  fileName                   Description of the Parameter
	 * @return                            Description of the Return Value
	 * @exception  BadID3Exception        损坏的ID3标签异常
	 * @exception  FileNotFoundException
	 * @exception  IOException
	 */
	public static boolean ID3Check( Object fileName )
		throws BadID3Exception, FileNotFoundException, IOException {
	RandomAccessFile theFile = null;
	String theTag = null;//标签头标识
	int theTagSize = 0;//ID3标签大小

		if ( fileName instanceof String )
			theFile = new RandomAccessFile( (String)fileName, "r" );

		else
				if ( fileName instanceof File )
			theFile = new RandomAccessFile( (File)fileName, "r" );

		if ( theFile != null ) {
		byte[] bcTag;
		byte[] tagSizeBuf;
			bcTag = new byte[3];
			tagSizeBuf = new byte[4];
			try {
				theFile.seek( 0 );
				theFile.read( bcTag );
				theTag = new String( bcTag );

				if ( !"ID3".equals( theTag ) )
					return false;
				if ( theFile.skipBytes( 3 ) < 0 ) {
					theTagSize = 0;
					return false;
				}
				theFile.read( tagSizeBuf );//读取并计算标签头大小
			} finally {
				theFile.close();
			}

			theTagSize = tagSizeBuf[0] << 21
					 | tagSizeBuf[1] << 14
					 | tagSizeBuf[2] << 7
					 | tagSizeBuf[3];
//本来tagSizeBuf[i]是byte(8位),但是左移之后返回的是一个int型的(32位),因此可以用"|"(位或)
//这里用"|"和"+"是一样的
			if ( theTagSize <= 0 )
				return false;
			return true;
		}
		return false;
	}


	/**
	 *  Description of the Method
	 *
	 * @param  filename                   Description of the Parameter
	 * @exception  BadID3Exception        损坏的ID3标签异常
	 * @exception  FileNotFoundException  Description of the Exception
	 * @exception  IOException            Description of the Exception
	 */
	protected void initFiles( Object filename )
		throws BadID3Exception, FileNotFoundException, IOException {
		if ( cAFile != null ) {
			cTag = new Tag( cAFile );
			cTagFrame = new TagFrame();
		}
		else
			throw new BadID3Exception( "No ID3v1 flag" );
	}


	/**
	 *  Gets the frame attribute of the ID3v2Controler object
	 *
	 * @param  frameID              Description of the Parameter
	 * @return                      The frame value
	 * @exception  IOException      Description of the Exception
	 * @exception  BadID3Exception  Description of the Exception
	 */
	protected String getFrame( String frameID )
		throws IOException, BadID3Exception {
		if ( "".equals( frameID ) )
			return "";
		cTagFrame.frmContext = (String)mInfo.get( frameID );
		if ( cTagFrame.frmContext != null )
			return cTagFrame.frmContext;
		while ( cTagFrame.getFrame() == 0 && !cTagFrame.frameID.equals( frameID ) )
			;

		if ( frameID.equals( cTagFrame.frameID ) )
			return cTagFrame.frmContext;
		return "";
	}


	/**
	 *  Description of the Method
	 *
	 * @param  frameID  Description of the Parameter
	 * @return          Description of the Return Value
	 */
	protected String encodeFrame( String frameID ) {
	String newID;
		if ( "Title".equals( frameID ) )
			newID = "TIT2";

		else if ( "Artist".equals( frameID ) )
			newID = "TPE1";

		else if ( "Album".equals( frameID ) )
			newID = "TALB";

		else if ( "Year".equals( frameID ) )
			newID = "TYER";

		else
			newID = "";

		return newID;
	}


	/**
	 *  Sets the frame attribute of the ID3v2Controler object
	 *
	 * @param  frameID     The new frame value
	 * @param  frmContext  The new frame value
	 * @return             Description of the Return Value
	 */
	protected boolean setFrame( String frameID, String frmContext ) {
		return false;
	}


	/**
	 *  Description of the Class
	 *
	 * @author     kilo
	 * @created    2003年12月22日
	 */
	private class Tag {//ID3标签头类
		/**
		 *  Description of the Field
		 */
		public String cTag;//标签头标识
		/**
		 *  Description of the Field
		 */
		public int cTagSize;//ID3标签大小


		/**
		 *  Constructor for the Tag object
		 *
		 * @param  cAFile               Description of the Parameter
		 * @exception  IOException      Description of the Exception
		 * @exception  BadID3Exception  Description of the Exception
		 */
		public Tag( RandomAccessFile cAFile )
			throws IOException,
				BadID3Exception {
		byte[] bcTag;
		byte[] tagSizeBuf;
			bcTag = new byte[3];
			tagSizeBuf = new byte[4];

			cAFile.seek( 0 );
			cAFile.read( bcTag );
			cTag = new String( bcTag );

			if ( cAFile.skipBytes( 3 ) < 0 ) {
				cTagSize = 0;
				return;
			}
			cAFile.read( tagSizeBuf );
			cTagSize = tagSizeBuf[0] << 21
					 | tagSizeBuf[1] << 14
					 | tagSizeBuf[2] << 7
					 | tagSizeBuf[3];

			if ( !"ID3".equals( cTag ) || cTagSize <= 0 )
				throw new BadID3Exception( "No ID3v2 flag" );
		}

	}


	/**
	 *  ID3v2标签帧类
	 *
	 * @author     kilo
	 * @created    2003年12月22日
	 */
	private class TagFrame {

		private byte[] bframeID;
		/**
		 *  当前标签帧的标识
		 */
		public String frameID;

		private byte[] bframeSize;
		/**
		 *  当前标签帧的大小
		 */
		public int frameSize;

		private byte[] bfrmContext;
		/**
		 *  当前标签帧的内容
		 */
		public String frmContext;


		/**
		 *  Gets the frame attribute of the TagFrame object
		 *
		 * @return                      The frame value
		 * @exception  IOException      Description of the Exception
		 * @exception  BadID3Exception  Description of the Exception
		 */
		public int getFrame()
			throws IOException, BadID3Exception {
			if ( mInfo.isEmpty() )
				cAFile.seek( 10 );

			if ( cAFile.getFilePointer() >= cTag.cTagSize )
				return -1;
			readdisk();
			return 0;
		}


		/**
		 *  Description of the Method
		 *
		 * @exception  IOException      Description of the Exception
		 * @exception  BadID3Exception  Description of the Exception
		 */
		private void readdisk()
			throws IOException, BadID3Exception {
			bframeID = new byte[4];
			bframeSize = new byte[4];

			cAFile.read( bframeID );
			cAFile.read( bframeSize );

			frameID = new String( bframeID ).trim();
			frameSize = ComputeframeSize( bframeSize );
			if ( frameSize < 0 )
				throw new BadID3Exception( "Bad ID3" );

			cAFile.skipBytes( 2 );
			if ( frameSize == 0 )
				frmContext = "";

			else {
				bfrmContext = new byte[frameSize];
				cAFile.readFully( bfrmContext );
				frmContext = new String( bfrmContext ).trim();
			}
			mInfo.put( frameID, frmContext );
		}


		/**
		 *  Description of the Method
		 *
		 * @param  bframeSize  Description of the Parameter
		 * @return             Description of the Return Value
		 */
		private int ComputeframeSize( byte[] bframeSize ) {
			return bframeSize[0] << 24
					 | bframeSize[1] << 16
					 | bframeSize[2] << 8
					 | bframeSize[3];
		}
	}
}

⌨️ 快捷键说明

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