📄 file.java
字号:
} public boolean isActive() { EnumFileState thisState = getFileStateEnum(); return (thisState == EnumFileState.DOWNLOADING) || (thisState == EnumFileState.PAUSED) || (thisState == EnumFileState.QUEUED); } public boolean isInteresting() { EnumFileState thisState = getFileStateEnum(); return thisState == EnumFileState.DOWNLOADING || thisState == EnumFileState.PAUSED || thisState == EnumFileState.DOWNLOADED || thisState == EnumFileState.QUEUED; } public void manualClean() { synchronized (clientWeakMap) { Object[] oArray = clientWeakMap.getKeyArray(); for (int i = 0; i < oArray.length; i++) { Client c = (Client) oArray[i]; if (!core.getClientCollection().containsKey(c.getId())) clientWeakMap.removeFromMain(c); } } } public void notifyChangedProperties() { this.setChanged(); this.notifyObservers(); // only observers are ChunkCanvases removeChangedBits(CHANGED_AVAIL); // this.notifyObservers(new String[]{"z"}); } public void preview() { preview(null); } public void preview(String app) { String previewExecutable = PreferenceLoader.loadString("previewExecutable"); String previewWorkingDirectory = PreferenceLoader.loadString("previewWorkingDirectory"); boolean previewUseHttp = PreferenceLoader.loadBoolean("previewUseHttp"); String previewExtensions = PreferenceLoader.loadString("previewExtensions"); if (!previewExtensions.equals(SResources.S_ES)) { StringTokenizer st = new StringTokenizer(previewExtensions, ";"); int ct = st.countTokens(); String ext = SResources.S_ES; String prog = SResources.S_ES; while (st.hasMoreTokens()) { ext = st.nextToken(); if (st.hasMoreTokens()) { prog = st.nextToken(); if (getName().toLowerCase().endsWith(ext.toLowerCase())) { previewExecutable = prog; } } } } if (app != null) previewExecutable = app; CoreFactory coreFactory = Sancho.getCoreFactory(); String httpPort = SResources.S_ES; Option option = (Option) core.getOptionCollection().get("http_port"); if (option != null) httpPort = option.getValue(); String userPass = SResources.S_ES; if (!coreFactory.getPassword().equals(SResources.S_ES)) userPass = coreFactory.getUsername() + ":" + coreFactory.getPassword() + "@"; String httpString = "http://" + userPass + coreFactory.getHostname() + ":" + httpPort + "/preview_download?q=" + getId(); String fileString = getEnumNetwork().getTempFilePrefix() + getMd4().toUpperCase(); if (previewExecutable.equals(SResources.S_ES)) { core.send(OpCodes.S_PREVIEW, new Integer(this.getId())); } else { String[] cmdArray = new String[2]; cmdArray[0] = previewExecutable; cmdArray[1] = previewUseHttp ? httpString : fileString; SwissArmy.execInThread(cmdArray, previewWorkingDirectory.equals(SResources.S_ES) ? null : previewWorkingDirectory); } } // buf_file public void read(int fileID, MessageBuffer messageBuffer) { synchronized (this) { this.id = fileID; this.networkEnum = readNetwork(messageBuffer.getInt32()); this.names = messageBuffer.getStringList(); this.md4 = messageBuffer.getMd4(); this.size = readSize(messageBuffer); this.downloaded = readDownloaded(messageBuffer); calcDownloadedString(); this.sources = messageBuffer.getInt32(); this.numClients = messageBuffer.getInt32(); this.readState(messageBuffer); setChunks(messageBuffer.getString()); readAvailability(messageBuffer); readRate(messageBuffer); this.chunkAges = readChunkAges(messageBuffer); this.age = readAge(messageBuffer); this.getFormat().read(messageBuffer); this.name = messageBuffer.getString(); setLastSeen(messageBuffer.getInt32()); this.setPriority(messageBuffer.getSignedInt32()); this.comment = readComment(messageBuffer); calcETA(); calcFileType(); if (programImageResString == null) setProgramImage(); } // Not sync: if ((this.getChangedBits() & CHANGED_STATE) != 0 && this.state.getState() == EnumFileState.DOWNLOADED) Sancho.getCoreFactory().notifyObject(this); notifyChangedProperties(); } public void read(MessageBuffer messageBuffer) { read(messageBuffer.getInt32(), messageBuffer); } protected long readAge(MessageBuffer messageBuffer) { if (core.getFileCollection().eta2()) ageTS = System.currentTimeMillis(); try { return Long.parseLong(messageBuffer.getString()); } catch (NumberFormatException e) { return 0; } } protected int[] readChunkAges(MessageBuffer messageBuffer) { String[] chunkAgesList = messageBuffer.getStringList(); int[] iArray = new int[chunkAgesList.length]; for (int i = 0; i < chunkAgesList.length; i++) iArray[i] = ((int) (System.currentTimeMillis() / 1000) - Integer.parseInt(chunkAgesList[i])); return iArray; } protected String readComment(MessageBuffer messageBuffer) { return SResources.S_ES; } protected long readDownloaded(MessageBuffer messageBuffer) { return messageBuffer.getInt32() & 0xFFFFFFFFL; } protected void readRate(MessageBuffer messageBuffer) { float newRate = 0f; try { newRate = Float.parseFloat(messageBuffer.getString()); } catch (NumberFormatException e) { } if (this.rate != newRate) addChangedBits(CHANGED_RATE); this.rate = newRate; } protected long readSize(MessageBuffer messageBuffer) { return messageBuffer.getInt32() & 0xFFFFFFFFL; } protected void readState(MessageBuffer messageBuffer) { boolean wasInteresting = isInteresting(); EnumFileState oldState = this.getFileStateEnum(); this.state.read(messageBuffer); this.fileStateEnum = state.getState(); if (wasInteresting && !isInteresting()) addChangedBits(CHANGED_NOT_INTERESTING); if (oldState != this.getFileStateEnum()) { addChangedBits(CHANGED_STATE); // On file completion if (this.state.getState() == EnumFileState.DOWNLOADED) { // can't be synced: //Sancho.getCoreFactory().notifyObject(this); if (PreferenceLoader.loadBoolean("downloadCompleteLog") && getName() != null && !getName().equals(SResources.S_ES)) { try { PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter(new java.io.File(VersionInfo .getDownloadLogFile()), true))); p.write(System.currentTimeMillis() + " " + getED2K() + "\n"); p.close(); } catch (FileNotFoundException fnf) { Sancho.pDebug(fnf.toString()); } catch (IOException io) { Sancho.pDebug(io.toString()); } } } } } public void readUpdate(MessageBuffer messageBuffer) { synchronized (this) { this.downloaded = readDownloaded(messageBuffer); calcDownloadedString(); readRate(messageBuffer); setLastSeen(messageBuffer.getInt32()); calcETA(); } notifyChangedProperties(); } public FileClient removeFileClient(Client client) { FileClient foundFileClient = findFileClient(client); if (foundFileClient != null) { getFileClientSet().remove(foundFileClient); addChangedBits(CHANGED_SOURCES); notifyChangedProperties(); } return foundFileClient; } public void removeSource(MessageBuffer messageBuffer) { int clientNum = messageBuffer.getInt32(); Client client = (Client) core.getClientCollection().get(clientNum); if (client != null) { clientWeakMap.remove(client); client.deleteObserver(this); if (client.countObservers() == 0) { if (client.isConnected() && numConnectedClients > 0) numConnectedClients--; core.getClientCollection().removeSource(clientNum, client); } addChangedBits(CHANGED_SOURCES); notifyChangedProperties(); } } public void saveFileAs(String aName) { Object[] oArray = new Object[2]; oArray[0] = new Integer(this.getId()); oArray[1] = aName; core.send(OpCodes.S_SAVE_FILE_AS, oArray); } public void sendPriority(boolean relative, int i) { Object[] oArray = new Object[2]; oArray[0] = new Integer(this.getId()); if (relative) i += getPriority(); oArray[1] = new Integer(i); core.send(OpCodes.S_SET_FILE_PRIO, oArray); } public void sendPriority(EnumPriority enumPriority) { sendPriority(false, enumPriority.getMaxValue()); } public void setActiveSources(int i) { int oldActiveSources = getActiveSources(); synchronized (this) { if (i == 0) { activeSources = 0; Object[] oArray = clientWeakMap.getKeyArray(); for (int j = 0; j < oArray.length; j++) { Client client = (Client) oArray[j]; if (client.isTransferring()) activeSources++; } } else activeSources += i; } if (oldActiveSources != getActiveSources()) { addChangedBits(CHANGED_ACTIVE); notifyChangedProperties(); } } // changed_avail protected void readAvailability(MessageBuffer messageBuffer) { this.avail = messageBuffer.getString(); setRelativeAvail(); } protected void setChunks(String s) { this.chunks = s; numChunks = 0; char tempChar; for (int i = 0; i < chunks.length(); i++) { tempChar = chunks.charAt(i); if ((tempChar == '2') || (tempChar == '3')) numChunks++; } } public void setComment(String comment) { String string = "comment " + this.getMd4() + " \"" + comment + "\""; core.send(OpCodes.S_CONSOLE_MESSAGE, string); core.send(OpCodes.S_GET_FILE_INFO, new Integer(getId())); } public void requestFileInfo() { core.send(OpCodes.S_GET_FILE_INFO, new Integer(getId())); } protected void calcFileType() { int index = getName().lastIndexOf("."); String extension = name.substring(index + 1, name.length()); EnumExtension enumExtension = EnumExtension.GET_EXT(extension); this.extensionEnum = enumExtension != null ? enumExtension : EnumExtension.UNKNOWN; } protected void setLastSeen(int i) { int oldLastSeen = getLastSeen(); this.lastSeen = i; if (oldLastSeen != lastSeen) addChangedBits(CHANGED_LAST); } public void rename(String string) { string = "rename " + this.getId() + " \"" + string + "\""; core.send(OpCodes.S_CONSOLE_MESSAGE, string); core.send(OpCodes.S_GET_FILE_INFO, new Integer(id)); } protected EnumNetwork readNetwork(int i) { return this.core.getNetworkCollection().getNetworkEnum(i); } protected void setPriority(int i) { priority = i; priorityEnum = EnumPriority.intToEnum(i); } public void setProgramImage() { Program p = null; if (!this.getFileFormat().equals(SResources.S_ES)) p = Program.findProgram(this.getFileFormat()); else { int index; String fileName = this.getName(); if ((fileName != null) && ((index = fileName.lastIndexOf(".")) != -1)) p = Program.findProgram(fileName.substring(index)); } // Set the program image: Image programImage = null; if (p != null) { if ((programImage = SResources.getImage(p.getName())) == null) { ImageData data = p.getImageData(); if (data != null) { ResourcesImageDescriptor rID = new ResourcesImageDescriptor(p.getName(), new Image(null, data)); SResources.putImage(p.getName(), rID); programImageResString = p.getName(); } else programImageResString = RS_DEFAULT_ICON; } else programImageResString = p.getName(); } else programImageResString = RS_DEFAULT_ICON; } public void setRelativeAvail() { int oldRelativeAvail = relativeAvail; relativeAvail = 0; int neededChunks = 0; int availChunks = 0; if (avail == null) avail = SResources.S_ES; String myChunks = getChunks(); if ((avail.length() > 0) && (avail.length() == myChunks.length())) { for (int i = 0; i < avail.length(); i++) { if ((myChunks.charAt(i) == '0') || (myChunks.charAt(i) == '1')) { neededChunks++; if (avail.charAt(i) > 0) availChunks++; } } if (neededChunks > 0) relativeAvail = (int) (((float) availChunks / (float) neededChunks) * 100f); } if (oldRelativeAvail != relativeAvail) addChangedBits(CHANGED_RAVAIL); } public void setState(EnumFileState newState) { EnumFileState oldState = this.getFileStateEnum(); Object[] content = new Object[2]; short opcode = OpCodes.S_SWITCH_DOWNLOAD; content[0] = new Integer(id); if (oldState == EnumFileState.PAUSED && newState == EnumFileState.DOWNLOADING) { content[1] = new Byte((byte) 1); } else if ((oldState == EnumFileState.DOWNLOADING || oldState == EnumFileState.QUEUED) && newState == EnumFileState.PAUSED) { content[1] = new Byte((byte) 0); } else if (newState == EnumFileState.CANCELLED) { opcode = OpCodes.S_REMOVE_DOWNLOAD; content = new Object[1]; content[0] = new Integer(id); } else { return; } core.send(opcode, content); } protected boolean checkFileNum(Client client) { return true; } // fileClients aren't updated(notified) as clients are updated public void update(Observable o, Object obj) { if (o instanceof Client) { if (obj instanceof Integer) { Client client = (Client) o; int nInt = ((Integer) obj).intValue(); if ((nInt & Client.TRANSFERRING_ADD) != 0) { if (checkFileNum(client)) { setActiveSources(+1); FileClient fileClient = findFileClient(client); if (fileClient == null) { fileClient = new FileClient(this, client); addFileClient(fileClient); } //this.setChanged(); // this.notifyObservers(fileClient); core.getFileCollection().sendUpdate(fileClient); } } else if ((nInt & Client.TRANSFERRING_REM) != 0) { FileClient foundFileClient; // Is fileNum avail here? if ((foundFileClient = removeFileClient(client)) != null) { foundFileClient.setDelete(); setActiveSources(-1); // this.setChanged(); // this.notifyObservers(foundFileClient); core.getFileCollection().sendUpdate(foundFileClient); } } if ((nInt & Client.CONNECTED) != 0) numConnectedClients++; else if ((nInt & Client.DISCONNECTED) != 0 && numConnectedClients > 0) numConnectedClients--; } //else { //this.setChanged(); //this.notifyObservers(obj); //} clientWeakMap.addOrUpdate(o); } //else } public void verifyChunks() { core.send(OpCodes.S_VERIFY_ALL_CHUNKS, new Integer(this.getId())); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -