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

📄 fileinfo.java

📁 We intend to develop a wifi enabled p2p file sharing system on a linux platform using jxta and java.
💻 JAVA
字号:
package edu.uiuc.cs.cs327.linuxwifi.services;

import java.io.*;
import java.lang.*;
import java.net.*;
//import java.net.URI;
import net.jxta.protocol.*;
import java.io.*;
import net.jxta.peergroup.PeerGroup;

  /**

    * Class that is created for every file being downloaded or uploaded

    */    





public class FileInfo implements Serializable
{

  private static final boolean DEBUG = false;
  
  public static final int UNKNOWN = 0;  
  public static final int PENDING = 1;
  public static final int IN_PROGRESS = 2;
  public static final int PAUSED = 3;
  public static final int COMPLETED = 4;  
  public static final int CANCELLED = 5;

  private String filePathType = "Absolute"; // Could be URI, Absolute or Relative
  private URI fileURI = null;			// UNC Path to file: \\hostname\path\filename.mp3
  private String fileName = null;		// Name of file: filename.mp3
  private String filePath = null;   	// Path to file: E:\linuxwifi\src
  private int fileState = UNKNOWN;  	// True = In Progress; False = queued
  private int fileStatus = 0;       	// From 0 to 100  
  private boolean fileDownload=false;	// True = Download; False = Upload
  
  private PipeAdvertisement pipeAd = null;
    
  private byte[] fileBytes = null;  	// Content of file read or to be saved
  
	
  /**
    * Constructs a fileInfo class for the associated file
    * @param aFileName Name of File
    * @param aFilePath Full UNC Path to filename
    * @param aFileDownload true=download; false=upload
    */
	

  public FileInfo(String aFileName, String aFilePath, boolean aFileDownload ) {  	
    fileName = aFileName;
    filePath = aFilePath;
    fileDownload = aFileDownload;
  }
  
  /**
    * Constructs a fileInfo class for the associated file
    * @param aFileURI URI of a File
    * @param aFileDownload true=download; false=upload
    */
	
  public FileInfo(URI aFileURI, boolean aFileDownload ) {  	
    fileURI = aFileURI;
    fileDownload = aFileDownload;
  }
  
    /**
    * Gets the file URI
    * @return a string referring to the associated filename
    */
    
  public String toString()
  {
    return fileName + " at " + filePath;
  }

  public URI getFileURI()
  {
      return(fileURI);
  }
  
  /**
    * Gets the file name
    * @return a string referring to the associated filename
    */
    
  public String getFileName()
  {
      return(fileName);
  }

  /**
    * Gets the file path
    * @return a string referring to the full UNC path to file: \\hostname\path\filename.mp3
    */
    
  public String getFilePath()
  {
      return(filePath);
  }
  
  /**
    * Returns if the file download/upload is currently in progress
    * @returns one of the following: FileInfo.COMPLETED, FileInfo.PENDING, FileInfo.IN_PROGRESS
    */  
    
  public int getFileState()
  {
      return(fileState);
  }  
  
  /**
    * Returns the status of the file
    * @return an integer between 0 - 100 referring to the percent completion
    *         If pending value = 0; If completed value = 100
    */  

  public int getFileStatus()
  {
      return(fileStatus);
  }

  /**
    * Returns the download status of the file
    * @return a boolean: true = download; false = upload
    */  
    
  public boolean getFileDownload()
  {
      return(fileDownload);
  }
  
  /**
    * Returns the content of file as bytes
    * @return an array of bytes
    */  
    
  public byte[] getFileBytes()
  {
      return (fileBytes);
  }
  
  /**
    * Returns peer advertisement value
    * @return the peer advertisement
    */  
  public PipeAdvertisement getPipeAdvertisement()
  {
  	return pipeAd;
  }
 
  /**
    * Set the file download/upload is currently in progress
    * @param aFileState one of the following: FileInfo.COMPLETED, FileInfo.PENDING, FileInfo.IN_PROGRESS
    */  
    
  public void setFileState(int aFileState)
  {
      fileState=aFileState;
  }  
  
  /**
    * Sets the status of the file
    * @param aFileStatus an integer between 0 - 100 referring to the percent completion
    *         If pending value = 0; If completed value = 100
    */  

  public void setFileStatus(int aFileStatus)
  {
      fileStatus=aFileStatus;
  }
  
  /**
    * Sets the download status of the file
    * @param aFileStatus an integer between 0 - 100 referring to the percent completion
    *         If pending value = 0; If completed value = 100
    */  

  public void setFileDownload(boolean aFileDownload)
  {
      fileDownload = aFileDownload;
  }

  /**
    * Sets the bytes array value that need to be saved in a file
    * @param aFileBytes, which is an array of bytes
    *         If pending value = 0; If completed value = 100
    */  
  public void setFileBytes(byte[] aFileBytes)
  {
      fileBytes = aFileBytes;
  }
  
  /**
    * Sets the peer advertisement value
    * @param aPipeAd, which is the peer advertisement that needs to be set
    */  
  public void setPipeAdvertisement(PipeAdvertisement aPipeAd)
  {
  	pipeAd = aPipeAd;
  }
  
  /**
    * Reads the file whose name and path are specified.
    * @retruns a byte stream
    */  
    
  public byte[] readFile() throws Exception
  {
  	File myFile = null;
  	FileInputStream myFileInputStream = null;
  	
  	String strFile;
  	int fileLength = 0;
  	int bytesRead = 0;
  	  	
  	try {
  		if (filePathType.equals("Absolute")) {
  		 	if (fileName == null) throw (new Exception("File Name is null"));
	  		if (filePath == null) throw (new Exception("File Path is null"));
	  		if (fileDownload == true) throw (new Exception("File should be saved not read."));
	  	
	  		strFile = filePath;
	  	
	  		System.out.println("Complete file with path: " + strFile);
	  	
	  		myFile = new File(strFile);
		} else if (filePathType.equals("URI")) {
	  		if (fileURI == null) throw (new Exception("File URI is null"));
	  		if (fileDownload == true) throw (new Exception("File should be saved not read."));
	  	
	  		System.out.println("File URI is: " + fileURI);
	  	
	  		myFile = new File(fileURI);
	  		strFile = fileURI.toString();
	  	}
	  	else {
	  		throw (new Exception("File path type is set to invalid value."));
	  	}
	  	
	  	if (myFile.exists()) System.out.println("File: " + strFile + " exists.");
	  	else {
	  		System.out.println("File: " + strFile + " does not exist.");
	  		throw (new Exception("File " + strFile + " does not exist."));
	  	}
	  	
	  	if (myFile.canRead()) System.out.println("File: " + strFile + " is readable.");
	  	else {
	  		System.out.println("File: " + strFile + " is not readable.");
	  		throw (new Exception("File " + strFile + " is not readable."));
	  	}
	  	
	  	fileLength = (int) myFile.length();
	  	fileBytes = new byte[fileLength + 1];
	  	
	  	myFileInputStream = new FileInputStream(myFile);
	
	  	bytesRead = myFileInputStream.read(fileBytes);
	  	
	  	return fileBytes;
  	}
  	catch (Exception e) {
  		throw e;
  	}
  }

  /**
    * Writes to a file whose name and path are specified.
    * @param the byte stream that need to be saved to a file
    */  
  
  public int saveFile(byte[] aFileBytes) throws Exception 
  {
  	
  	File myFile = null;
  	FileOutputStream myFileOutputStream = null;
  	
  	String strFile;
  	  	
  	try {
  		if (filePathType.equals("Absolute")) {
	  		if (fileName == null) throw (new Exception("File Name is null"));
	  		if (filePath == null) throw (new Exception("File Path is null"));
	  		if (fileDownload == false) throw (new Exception("File should be read not saved."));
	  	
	  		strFile = filePath;
	  	
	  		System.out.println("Complete file with path: " + strFile);
	  	
	  		myFile = new File(strFile);
	  	} else if (filePathType.equals("URI")) {
	  	
	  		if (fileURI == null) throw (new Exception("File URI is null"));
	  		if (fileDownload == false) throw (new Exception("File should be read not saved."));
	  	
	  		System.out.println("File URI is: " + fileURI);
	  	
	  		myFile = new File(fileURI);
	  		strFile = fileURI.toString();
	 	}
	  	else {
	  		throw (new Exception("File path type is set to invalid value."));
	  	}
	  	
	  	if (myFile.exists()) {
	  		System.out.println("File: " + strFile + " exists.");
	  		if (myFile.canWrite()) System.out.println("File: " + strFile + " is writable.");
	  		else {
	  			System.out.println("File: " + strFile + " is not writable.");
	  			throw (new Exception("File " + strFile + " is not writable."));
	  		}
	  	}
	  	else {
	  		System.out.println("File: " + strFile + " does not exist.");
	  		if (myFile.createNewFile()) System.out.println("File: " + strFile + " is created.");
	  		else {
	  			System.out.println("File: " + strFile + " exists and can not be recreated.");
	  			throw (new Exception("File " + strFile + " does not exist."));
	  		}
	  	}
	  	
	  	myFileOutputStream = new FileOutputStream(myFile);
	
	  	myFileOutputStream.write(aFileBytes);
	  	myFileOutputStream.close();

	} catch (Exception e) {
  		throw e;
  	}	  	
  	
  	return 0;
  }
  
  /**
    * Either reads a file or writes to a file based on the value of fileDownload
    * Writes to a file if fileDownload = true (download)
    * Reads a file if fileDownload = false (upload)
    */  

  
  public int transferFile() throws Exception {
		try {
			if (fileDownload) saveFile(fileBytes);
			else fileBytes = readFile();
		} catch (Exception e) {
				throw e;
		}
		
		return 0;
	}
}

⌨️ 快捷键说明

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