?? taghead.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.hadeslee.audiotag.tag.ape;import com.hadeslee.yoyoplayer.util.Util;import java.util.Arrays;import java.util.logging.Level;import java.util.logging.Logger;/** * 內部的私有類,它代表了一個APE標簽的頭部 * @author hadeslee */public class TagHead { private static Logger log = Logger.getLogger(TagHead.class.getName()); private byte[] data;//頭部的數據 private boolean valid;//是否是合法的頭部 private int version = 2000;//版本,默認是2000 private int tagSize;//標簽的長度,包括尾標簽以及所有的項目,不包括頭標簽 private int itemCount;//項目的數量 private int flag = FOOT;//標簽的其它標志,指示它是頭部還是尾部 public static final int HEAD = 0xA0000000; public static final int FOOT = 0x80000000; public static final int V1=1000;//表示APE的版本號 public static final int V2=2000; private int index;//這個標頭的起始點的位置,相對于文件 public TagHead(byte[] data) { this.data = data; parseData(); } public TagHead() { } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public byte[] getBytes() { //標頭總共是32個字節 byte[] head = new byte[32]; byte[] temp = {(byte) 'A', (byte) 'P', (byte) 'E', (byte) 'T', (byte) 'A', (byte) 'G', (byte) 'E', (byte) 'X' }; //先把頭的標量填進去,8字節 System.arraycopy(temp, 0, head, 0, 8); temp = Util.getBytesFromInt(version); //再把版本號寫進去,4字節 System.arraycopy(temp, 0, head, 8, 4); temp = Util.getBytesFromInt(tagSize); log.log(Level.SEVERE,"TAGSIZE="+tagSize); //再把標簽的長度寫進去,4字節 System.arraycopy(temp, 0, head, 12, 4); temp = Util.getBytesFromInt(itemCount); //再把標簽的數量寫進去,4字節 System.arraycopy(temp, 0, head, 16, 4); temp = Util.getBytesFromInt(flag); //再把標志寫進去,表示是標簽頭部還是尾部 System.arraycopy(temp, 0, head, 20, 4); //把標志空的8個字節進去,因為默認就是空的,所以不用寫了 //頭部或者尾部的數據塊已經構造好了 return head; } private void parseData() { try { checkHead(); checkVersion(); checkTagSize(); checkItemCount(); checkFlag(); valid = true; } catch (Exception exe) { log.log(Level.SEVERE, "分析標簽異常!"); valid = false; } } /** * 這個標簽是否有頭標簽,因為一般讀都是從尾部讀過去的 * @return 是否有頭標簽,重寫的時候有用 */ public boolean hasHeader() { return ((1 << 31) & flag) != 0; } /** * 檢查頭部八個字節的的數據是否一樣 */ private void checkHead() { byte[] temp = new byte[8]; byte[] head = {(byte) 'A', (byte) 'P', (byte) 'E', (byte) 'T', (byte) 'A', (byte) 'G', (byte) 'E', (byte) 'X' }; System.arraycopy(data, 0, temp, 0, 8); //比較兩個頭部的數據是否一樣,這是第一要素 if (!Arrays.equals(head, temp)) { throw new RuntimeException("頭部數據不一樣!"); } } /** * 檢查版本號是否合法,必須是1000或者2000 */ private void checkVersion() { byte[] temp = new byte[4]; System.arraycopy(data, 8, temp, 0, 4); int v = Util.getInt(temp); if (v == 2000 || v == 1000) { version = v; log.log(Level.INFO, "版本號是:" + v); } else { throw new RuntimeException("版本號不合法!!"); } } private void checkTagSize() { byte[] temp = new byte[4]; System.arraycopy(data, 12, temp, 0, 4); tagSize = Util.getInt(temp); log.log(Level.INFO, "標簽大小:" + tagSize); } private void checkItemCount() { byte[] temp = new byte[4]; System.arraycopy(data, 16, temp, 0, 4); itemCount = Util.getInt(temp); log.log(Level.INFO, "標簽項目數:" + itemCount); } private void checkFlag() { byte[] temp = new byte[4]; System.arraycopy(data, 20, temp, 0, 4); flag = Util.getInt(temp); log.log(Level.INFO, "標志:" + flag); } public void setFlag(int flag) { this.flag = flag; } public void setItemCount(int itemCount) { this.itemCount = itemCount; } public void setTagSize(int tagSize) { this.tagSize = tagSize; } public void setVersion(int version) { if(!(version==V1||version==V2)){ throw new RuntimeException("非法的版本號,只能是V2或者V1."); } this.version = version; } public int getFlag() { return flag; } public int getItemCount() { return itemCount; } public int getTagSize() { return tagSize; } public boolean isValid() { return valid; } public int getVersion() { return version; } public static void main(String[] args) { } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -