📄 file.java
字号:
/* * Copyright (C) 2004-2005 Rutger M. Ovidius for use with the sancho project. * See LICENSE.txt for license information. */package sancho.model.mldonkey;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.Observable;import java.util.Observer;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeSet;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.ImageData;import org.eclipse.swt.program.Program;import sancho.core.CoreFactory;import sancho.core.ICore;import sancho.core.Sancho;import sancho.model.mldonkey.enums.AbstractEnum;import sancho.model.mldonkey.enums.EnumExtension;import sancho.model.mldonkey.enums.EnumFileState;import sancho.model.mldonkey.enums.EnumNetwork;import sancho.model.mldonkey.enums.EnumPriority;import sancho.model.mldonkey.utility.FileState;import sancho.model.mldonkey.utility.Format;import sancho.model.mldonkey.utility.MessageBuffer;import sancho.model.mldonkey.utility.OpCodes;import sancho.model.mldonkey.utility.UtilityFactory;import sancho.utility.ObjectMap;import sancho.utility.SwissArmy;import sancho.utility.VersionInfo;import sancho.view.preferences.PreferenceLoader;import sancho.view.transfer.FileClient;import sancho.view.utility.SResources;import sancho.view.utility.ResourcesImageDescriptor;public class File extends AObjectO implements Observer { private static final String RS_DEFAULT_ICON = "defprog"; public static final int CHANGED_ACTIVE = 1; public static final int CHANGED_RAVAIL = 2; public static final int CHANGED_DOWNLOADED = 4; public static final int CHANGED_ETA = 8; public static final int CHANGED_LAST = 16; public static final int CHANGED_PERCENT = 32; public static final int CHANGED_RATE = 64; public static final int CHANGED_STATE = 128; public static final int CHANGED_NOT_INTERESTING = 256; public static final int CHANGED_SOURCES = 512; public static final int CHANGED_AVAIL = 1024; /* public static final String CHANGED_ACTIVE = "active"; public static final String CHANGED_AVAIL = "avail"; public static final String CHANGED_DOWNLOADED = "downloaded"; public static final String CHANGED_ETA = "eta"; public static final String CHANGED_LAST = "last"; public static final String CHANGED_PERCENT = "percent"; public static final String CHANGED_RATE = "rate"; public static final String CHANGED_STATE = "state"; public static final String CHANGED_NOT_INTERESTING = "not_interesting"; public static final String[] ALL_PROPERTIES = {CHANGED_RATE, CHANGED_DOWNLOADED, CHANGED_PERCENT, CHANGED_AVAIL, CHANGED_ETA, CHANGED_LAST, CHANGED_ACTIVE}; */ protected static final Set dummySet = new TreeSet(); private int changedBits; protected int activeSources; protected long age; protected long ageTS; protected String avail; protected int[] chunkAges; protected String chunks; protected ObjectMap clientWeakMap; protected String comment; protected long downloaded; protected String downloadedString; protected long etaSeconds; protected long eta2Seconds; protected String etaString; protected String eta2String; protected EnumExtension extensionEnum; protected Set fileClientSet; protected EnumFileState fileStateEnum; protected Format format; protected int id; protected int lastSeen; protected String md4; protected String name; protected String[] names; // protected Network network; protected EnumNetwork networkEnum; protected int numChunks; protected int numClients; protected int numConnectedClients; protected int percent; protected int priority; protected EnumPriority priorityEnum; protected String programImageResString; protected float rate; protected int relativeAvail = 0; protected long size; protected int sources; protected FileState state; File(ICore core) { super(core); state = UtilityFactory.getFileState(core); format = UtilityFactory.getFormat(core); clientWeakMap = new ObjectMap(true); } protected Set getFileClientSet() { if (fileClientSet == null) fileClientSet = Collections.synchronizedSet(new HashSet()); return fileClientSet; } public synchronized void addChangedBits(int i) { this.changedBits |= i; } public synchronized void removeChangedBits(int i) { this.changedBits &= ~i; } public synchronized int getChangedBits() { return this.changedBits; } public synchronized void clearChangedBits() { this.changedBits = 0; } public synchronized boolean hasChangedBit(int i) { return (this.changedBits & i) != 0; } protected void addFileClient(FileClient fileClient) { getFileClientSet().add(fileClient); addChangedBits(CHANGED_SOURCES); notifyChangedProperties(); } public void addSource(Client client) { if (!this.clientWeakMap.contains(client)) { this.clientWeakMap.add(client); client.addObserver(this); addChangedBits(CHANGED_SOURCES); if (client.isTransferring()) { numConnectedClients++; setActiveSources(+1); if (findFileClient(client) == null) { addClientToFileClientSet(client); } } else if (client.isConnected()) numConnectedClients++; notifyChangedProperties(); } } public void addClientToFileClientSet(Client client) { FileClient fileClient = new FileClient(this, client); addFileClient(fileClient); core.getFileCollection().sendUpdate(fileClient); } protected void calcDownloadedString() { String oldStringDownloaded = getDownloadedString(); this.downloadedString = SwissArmy.calcStringSize(this.getDownloaded()); if (!oldStringDownloaded.equals(downloadedString)) addChangedBits(CHANGED_DOWNLOADED); int oldPercent = this.percent; this.percent = getSize() > 0 ? (int) ((float) this.getDownloaded() / (float) this.getSize() * 100f) : 0; if (oldPercent != this.percent) addChangedBits(CHANGED_PERCENT); } protected void calcETA() { if (this.rate == 0) this.etaSeconds = Long.MAX_VALUE; else this.etaSeconds = (long) ((getSize() - getDownloaded()) / (this.getRate() + 1)); String oldStringETA = getEtaString(); EnumFileState thisState = getFileStateEnum(); boolean eta2 = core.getFileCollection().eta2(); if ((thisState == EnumFileState.QUEUED) || (thisState == EnumFileState.DOWNLOADED) || (thisState == EnumFileState.PAUSED)) { this.etaString = SResources.S_DASH; if (eta2) { this.eta2Seconds = Long.MAX_VALUE; this.eta2String = SResources.S_DASH; } } else { this.etaString = SwissArmy.calcStringOfSeconds(this.etaSeconds); if (eta2) { long dl = getDownloaded(); long remain = getSize() - dl; if (dl == 0 || remain == 0 || getAge() == 0) { this.eta2String = SResources.S_DASH; this.eta2Seconds = Long.MAX_VALUE; } else { if (ageTS == 0) ageTS = System.currentTimeMillis(); long realAge = getAge() + ((System.currentTimeMillis() - ageTS) / 1000); this.eta2Seconds = (remain * realAge) / dl; this.eta2String = SwissArmy.calcStringOfSeconds(eta2Seconds); } } } if (etaString.equals(SResources.S_ES)) etaString = SResources.S_DASH; if (!oldStringETA.equals(etaString)) addChangedBits(CHANGED_ETA); } public void connectAll() { core.send(OpCodes.S_CONNECT_ALL, new Integer(this.getId())); } public void dispose() { clientWeakMap.deleteObservers(); deleteObservers(); } public boolean equals(Object obj) { return (obj instanceof File && getId() == ((File) obj).getId()); } public synchronized FileClient findFileClient(Client client) { for (Iterator i = getFileClientSet().iterator(); i.hasNext();) { FileClient fileClient = (FileClient) i.next(); if (client == fileClient.getClient()) return fileClient; } return null; } public synchronized int getActiveSources() { return activeSources; } public synchronized long getAge() { return age; } public synchronized String getAgeString() { return SwissArmy.calcStringOfSeconds((System.currentTimeMillis() / 1000) - this.getAge()); } public Set getAllAvailNetworks() { return dummySet; } public synchronized String getAvail() { return avail; } public String getAvails(Network network) { return SResources.S_ES; } public synchronized int[] getChunkAges() { return chunkAges; } public synchronized String getChunks() { return chunks != null ? chunks : SResources.S_ES; } public ObjectMap getClientWeakMap() { return clientWeakMap; } public String getComment() { return SResources.S_ES; } public synchronized int getConnected() { return numConnectedClients; } public synchronized long getDownloaded() { return downloaded; } public synchronized String getDownloadedString() { return downloadedString != null ? downloadedString : SResources.S_ES; } public synchronized EnumNetwork getEnumNetwork() { return networkEnum; } public String getED2K() { return "ed2k://|file|" + this.getName() + "|" + this.getSize() + "|" + this.getMd4() + "|/"; } public synchronized long getETA() { return etaSeconds; } public synchronized long getETA2() { return eta2Seconds; } public synchronized String getEtaString() { return etaString != null ? etaString : SResources.S_ES; } public synchronized String getEta2String() { return eta2String != null ? eta2String : SResources.S_ES; } public synchronized int getFileClientSetSize() { if (fileClientSet == null) return 0; return getFileClientSet().size(); } public synchronized Object[] getFileClientSetArray() { return getFileClientSet().toArray(); } public String getFileFormat() { int index = this.getName().lastIndexOf("."); if (index != -1) return this.getName().substring(index + 1).toLowerCase(); else return SResources.S_ES; } public void getFileLocations() { core.send(OpCodes.S_GET_FILE_LOCATIONS, new Integer(this.getId())); } public synchronized EnumFileState getFileStateEnum() { return fileStateEnum; } public synchronized EnumExtension getFileType() { return extensionEnum != null ? extensionEnum : EnumExtension.UNKNOWN; } public Format getFormat() { return format; } public synchronized int getId() { return id; } public synchronized int getLastSeen() { return lastSeen; } public synchronized String getLastSeenString() { if (this.lastSeen == 8640000) return SResources.S_DASH; else return SwissArmy.calcStringOfSeconds(this.lastSeen); } public synchronized String getMd4() { return md4 != null ? md4 : SResources.S_ES; } public synchronized String getName() { return name != null ? name : SResources.S_ES; } public String[] getNames() { return names != null ? names : new String[0]; } // public Network getNetwork() { // return network; // } public synchronized int getNumChunks() { return numChunks; } public synchronized int getNumClients() { return numClients; } public synchronized int getNumSources() { return sources; } public synchronized int getPercent() { return percent; } public synchronized int getPriority() { return priority; } public synchronized AbstractEnum getPriorityEnum() { return priorityEnum; } public synchronized String getProgramImageString() { return programImageResString != null ? programImageResString : SResources.S_ES; } public synchronized float getRate() { return rate; } public synchronized String getRateString() { EnumFileState thisState = getFileStateEnum(); if (thisState == EnumFileState.PAUSED || thisState == EnumFileState.QUEUED || thisState == EnumFileState.DOWNLOADED) return thisState.getName(); else return getRate() == 0f ? SResources.S_DASH : String .valueOf((Math.round(10.0 * (getRate() / 1000f)) / 10.0)); } public synchronized int getRelativeAvail() { return relativeAvail; } public synchronized String getRelativeAvailString() { return relativeAvail + SResources.S_PERCENT; } public synchronized long getSize() { return size; } public synchronized String getSizeString() { return SwissArmy.calcStringSize(this.getSize()); } public int getSources() { // TODO: check mldonkey sources return clientWeakMap.size(); } public String getSourcesString() { int s = getSources(); if (s == 0) return SResources.S_DASH; else { int a = getActiveSources(); return (a > 0 ? (s + SResources.S_OB + a + SResources.S_CB) : String.valueOf(s)); } } public synchronized String getPercentString() { return getPercent() + SResources.S_PERCENT; } public synchronized String getPriorityString() { if (priority == 0) return priorityEnum.getName(); else return priorityEnum.getName() + SResources.S_OB + priority + SResources.S_CB; } public boolean hasAvails() { return false; } public int hashCode() { return getId();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -