?? config.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package biz.tbuy.huliqing.jloading.downloader;import biz.tbuy.huliqing.jloading.Elog;import java.io.File;import java.util.ArrayList;import java.util.List;import java.util.Vector;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * * @author huliqing */public class Config { private String configPath; private Document doc; public Config(Document doc, String configPath) { this.doc = doc; this.configPath = configPath; } /** * 獲取當前狀態文件的保存路徑 * @return configPath */ public String getConfigPath() { return configPath; } /** 任務標識,這個標識用于區別每個不同的任務,應該是絕對的唯一*/ public String getId() { Element file = doc.getDocumentElement(); String id = file.getAttribute("id"); return id; } /** * 獲取短文件名,不包括后綴名,也不包括文件路徑等信息 * @return name */ public String getName() { Element file = doc.getDocumentElement(); String filename = file.getAttribute("name"); return filename; } /** * 獲取最終文件名,包括完整的絕對路徑,即完整的文件保存路徑級后綴名, * 這是最終下載完任務之后的路徑及文件名 * @return save */ public String getSave() { Element file = doc.getDocumentElement(); String filename = file.getAttribute("save"); return filename; } /** * 獲取文件的長度(字節) * @return length */ public long getLength() { Element file = doc.getDocumentElement(); long length = Long.valueOf(file.getAttribute("length")); return length; } /** * 獲取原始的線程數,如果沒有設置,則返回1 * @return threads */ public int getThreads() { Element file = doc.getDocumentElement(); int s = Integer.valueOf(file.getAttribute("threads")); return s >= 0 ? s : 1; } /** * 獲取狀態保存文件中的資源地址(即下載源),在有可能的情況下,可以讓任務支持 * 多個下載源,即從多個URL地址中同時下載一個文件。 * @return urls */ public List<String> getURLs() { List<String> urls = new ArrayList<String>(); Element file = doc.getDocumentElement(); Element eURLs = (Element) file.getElementsByTagName("urls").item(0); NodeList nURLs = eURLs.getElementsByTagName("url"); int len = nURLs.getLength(); for (int i = 0; i < len; i++) { Element eURL = (Element) nURLs.item(i); String src = eURL.getAttribute("src"); urls.add(src); } return urls; } /** * 獲取狀態保存文件中的分片信息 * @return pieces */ public List<Piece> loadPieces() { List<Piece> pieces = new ArrayList<Piece>(); try { doc = XmlOper.getDocument(configPath); Element file = doc.getDocumentElement(); NodeList nPieces = file.getElementsByTagName("pieces"); Element ePieces = (Element) nPieces.item(0); NodeList np = ePieces.getElementsByTagName("piece"); int len = np.getLength(); for (int i = 0; i < len; i++) { Element ep = (Element) np.item(i); int start = Integer.valueOf(ep.getAttribute("start")); int pos = Integer.valueOf(ep.getAttribute("pos")); int end = Integer.valueOf(ep.getAttribute("end")); Piece piece = new Piece(start, pos, end); pieces.add(piece); } } catch (Exception e) { Elog.log("載入狀態文件時遇到問題!" + getClass().getName()); } return pieces; } /** * 將所有的分片信息保存至磁盤文件中,用于斷點續傳 * @param pieces */ public synchronized void savePieces(Vector<Piece> pieces) { Element file = doc.getDocumentElement(); NodeList nodes = file.getElementsByTagName("pieces"); Node node = nodes.item(0); if (node.getNodeType() == Node.ELEMENT_NODE) { Element ePieces = (Element) node; NodeList nPieces = ePieces.getElementsByTagName("piece"); if (nPieces.getLength() <= 0) { // 保存所有piece信息 for (Piece piece : pieces) { Element ePiece = doc.createElement("piece"); ePiece.setAttribute("start", String.valueOf(piece.getStart())); ePiece.setAttribute("pos", String.valueOf(piece.getPos())); ePiece.setAttribute("end", String.valueOf(piece.getEnd())); ePieces.appendChild(ePiece); } } else { // 更新 List<Element> newPieces = new ArrayList<Element>(); int len = nPieces.getLength(); for (Piece piece : pieces) { boolean existPiece = false; for (int i = 0; i < len; i++) { Element ePiece = (Element) nPieces.item(i); if (ePiece.getAttribute("start").equals(String.valueOf(piece.getStart()))) { ePiece.setAttribute("pos", (String.valueOf(piece.getPos()))); ePiece.setAttribute("end", (String.valueOf(piece.getEnd()))); existPiece = true; break; } } // 如果不存在該分片的結點信息,則保存之 if (!existPiece) { Element newPiece = doc.createElement("piece"); newPiece.setAttribute("start", (String.valueOf(piece.getStart()))); newPiece.setAttribute("pos", (String.valueOf(piece.getPos()))); newPiece.setAttribute("end", (String.valueOf(piece.getEnd()))); newPieces.add(newPiece); } } for (Element e : newPieces) { ePieces.appendChild(e); } } try { XmlOper.saveDocument(doc, configPath); } catch (Exception e) { Elog.log("狀態文件在保存分片信息時遇到問題!" + getClass().getName()); } } } /** 當下載任務完成之后(完整下載完),可以調用這個方法將狀態文件(config)刪除掉 */ public boolean delete() { File f = new File(configPath); return f.delete(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -