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

📄 fileutilities.java

📁 peeranha42是jxta的 p2p程序核心
💻 JAVA
字号:
package de.uni_bremen.informatik.p2p.plugins.filesharing.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import de.uni_bremen.informatik.p2p.peeranha42.util.DigitalFingerPrint;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.FilesharingState;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.Share;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.download.DownloadProtocol;

/**
 * The class offers utility-methods for file handling.
 * 
 * @author Lars Kordes, Daniel Gehrke
 */
public class FileUtilities {
	public static String TEMP_DIR = "temp";
	
	/** Suffix of temp files. */
	public static String TEMPSUFFIX = ".p42";
	
	/** Logger for warnings, debugs and fatals */
    protected static Logger log = Logger.getLogger(FileUtilities.class);

    private static int BUFFERLENGTH = 2048;
    
	/**
	 * Creates empty file for download. The memory of the files gets allocated,
	 * but the file is still empty.
	 * 
	 * @param filename Filename
	 * @param size Size of the file
	 * @return The created file.
	 */
	public static File createDownloadFile(String filename, long size) {
		File file = null;
		try {
			file = new File(TEMP_DIR, filename + TEMPSUFFIX);
			file.createNewFile();
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			raf.setLength(size);
			raf.close();
		}
		catch(FileNotFoundException e) {
			log.error(e);
			return null;
		}
		catch(IOException e) {
			log.error(e);
			return null;
		}
		
		return file;
	}
	
	/**
	 * Checks of how many chunks a file consists depending on the chunksize
	 * 
	 * @return Nr of chunks in a file
	 */
	public static long getNrOfChunks(File file) {
	    
	    long result;	    
	    
	    if (file.length()%DownloadProtocol.CHK_SIZE == 0) {
	        result = file.length()/DownloadProtocol.CHK_SIZE;
	    } else {
	        result = file.length()/DownloadProtocol.CHK_SIZE+1;
	    }

	    return result;
	}
	
	/**
	 * Lists all files in a given directory.
	 * 
	 * @param dir Directory which contains the files
	 * @return List of files in the directory
	 */
	public static ArrayList getFiles(File dir) {
		ArrayList list = new ArrayList();
		
		File[] files = dir.listFiles();
		if(files == null) return list;
		
		for(int i=0; i<files.length; i++) {
			if(files[i].isDirectory()) list.addAll(getFiles(files[i]));
			else list.add(files[i]);
		}
		
		return list;
	}
	
	/**
	 * Moves a file to the download directory
	 * @param file to move
	 * @return success
	 */
	public static boolean moveToDownloadDir(File src) {
	    // remove temp suffix
	    String dest_filename = src.getName().substring(0,src.getName().lastIndexOf("."));
	    
	    //move file
        File dest = new File(FilesharingState.config.getValue("download_directory"), dest_filename); 
        
        // put file to sharelist
        FilesharingState.sharedfiles.add(new Share(dest));
        FilesharingState.sharedpanel.getTable().updateUI();
        
        
        // write down in config file
        String shares = FilesharingState.config.getValue("shares");
    	if(shares == null) shares = "";
    	shares += dest + ";";
    	FilesharingState.config.setValue("shares", shares);
        FilesharingState.config.applyChanges();
        
        return src.renameTo(dest);
	}

	/**
	 * The method creates a string representing a hash value of a given file. 
	 * The hash value gets calculated by a SHA1 algorithm.
	 * 
	 * If the file doens't exists, isn't readable or doesn't contain any data the 
	 * method returns null. null gets also returned if the algorithm can't be found.
	 * 
	 * @param filename Name of the file
	 * @return String representing hash value of the given file or null.
	 */
	public static String generateHashString(String filename) {
		if(filename == null) return null;
		
		MessageDigest md = null;
		try { md = MessageDigest.getInstance("SHA1"); }
		catch(NoSuchAlgorithmException e) {
			log.error("Can't find SHA1 algorithm for hashing shared files.");
			return null;
		}
		
		byte[] buf = new byte[1024];
		try {
			FileInputStream stream = new FileInputStream(filename);
			if(stream.read(buf) != -1) md.update(buf);
			else return null;
		}
		catch(FileNotFoundException e) {
			log.error(e);
			return null;
		}
		catch(IOException e) {
			log.error(e);
			return null;
		}
        
		return DigitalFingerPrint.stringRepresentation(md.digest());
	}
	
	
}

⌨️ 快捷键说明

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