FileInfo.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.furthurnet.servergui |
![]() |
![]() |
Furthurnet |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* FURTHUR - A distributed peer-to-peer file sharing system.
* Copyright (C) 2001-2002 Jamie M. Addessi, Furthur Network
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.furthurnet.servergui;
import java.io.File;
import java.text.ParseException;
import java.util.StringTokenizer;
import org.furthurnet.datastructures.supporting.Common;
import org.furthurnet.datastructures.supporting.Constants;
public class FileInfo implements Comparable {
/*
* This class is now immutable. That means that once it is constructed,
* an object of this type can never be changed. The advantage to this is
* that the FileInfo can safely be passed around all over the place
* with no concerns about one thread modifying it as another uses it.
*
* The slight disadvantage is that to "modify" a FileInfo, you have to
* construct an entirely new one with the modification.
*
* The pros outweigh the cons here. Eliminating thread-induced
* race/inconsistency problems is a priority, and immutable Objects are a
* powerful tool toward that end.
*
* None of the fields of a FileInfo are ever null, so the caller
* does not need to check for that (getFileName(), getMd5(), etc. will
* never return null).
*
* IDN 9/21/02
*/
private String fileName;
private String filePath;
private String md5;
// constructors:
// disable
private FileInfo() {}
// only used internally
private FileInfo(String fileName, String filePath, String md5) {
this.fileName = fileName;
this.filePath = filePath;
this.md5 = md5;
}
public FileInfo(String fileName, String filePath) {
if (fileName == null)
fileName = "";
if (filePath == null)
filePath = "";
this.fileName = fileName;
this.filePath = filePath;
this.md5 = "";
}
public FileInfo(File f) {
this.fileName = f.getName();
this.filePath = extractPath(f.getAbsolutePath());
this.md5 = "";
}
private static String extractPath(String fullPath) {
int i = fullPath.lastIndexOf(File.separatorChar);
return fullPath.substring(0, i);
}
public FileInfo(String configLine) throws ParseException {
StringTokenizer tok = new StringTokenizer(configLine, "|");
int numTokens = tok.countTokens();
if (numTokens == 3) {
// new format
fileName = tok.nextToken();
filePath = tok.nextToken();
md5 = tok.nextToken();
}
else if (numTokens == 7) {
// old format
fileName = tok.nextToken();
filePath = tok.nextToken();
if (! tok.nextToken().equals("0"))
throw new ParseException("Invalid old FileInfo line", 2);
if (! tok.nextToken().equals("0"))
throw new ParseException("Invalid old FileInfo line", 3);
md5 = tok.nextToken();
if (! tok.nextToken().equals("0"))
throw new ParseException("Invalid old FileInfo line", 5);
if (! tok.nextToken().equals("0"))
throw new ParseException("Invalid old FileInfo line", 6);
}
else {
throw new ParseException("Invalid FileInfo line", 0);
}
}
// various accessor methods
public String getFileName() { return fileName; }
public String getFilePath() { return filePath; }
public String getMd5() { return md5; }
public String getFileNameWithPath() {
return filePath + File.separator + fileName;
}
// this is used to store a FileInfo as a line of a config file
public String toString() {
StringBuffer temp = new StringBuffer(100);
temp.append(fileName);
temp.append("|");
temp.append(filePath);
temp.append("|");
temp.append(md5);
return temp.toString();
}
public void checkName() throws FileSetValidationException {
int i;
char c;
if (fileName.length() == 0)
throw new FileSetValidationException(
"zero length file names are not allowed");
for (i = 0; i < fileName.length(); i++) {
c = fileName.charAt(i);
if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
c == '"' || c == '<' || c == '>' || c == '|') {
throw new FileSetValidationException(
"file name '" + fileName +
"' contains the illegal character '" + c + "'");
}
if (Character.isISOControl(c)) {
throw new FileSetValidationException(
"file name '" + fileName +
"' contains an illegal control character");
}
if (i == 0 && Character.isWhitespace(c)) {
throw new FileSetValidationException(
"file name '" + fileName +
"' begins with whitespace (not allowed)");
}
}
}
// implement Comparable (for sorting)
public int compareTo(Object o) {
// sort differently than just by filename
// (anything with .txt comes before anything with .shn)
FileInfo f = (FileInfo) o;
String ext1 = Common.getExtension(fileName);
String ext2 = Common.getExtension(f.fileName);
if (ext1.equals(ext2))
return fileName.compareTo(f.fileName);
// extension is unknown/other unless we find it in known list
int i1 = Constants.OTHER_EXT_INDEX;
int i2 = Constants.OTHER_EXT_INDEX;
for (int i = 0; i < Constants.EXT_SORT_ORDER.length; i++) {
if (ext1.equals(Constants.EXT_SORT_ORDER[i]))
i1 = i;
if (ext2.equals(Constants.EXT_SORT_ORDER[i]))
i2 = i;
}
// if both have unknown extensions, sort by whole filename
if (i1 == i2)
return fileName.compareTo(f.fileName);
return (i1 - i2);
}
public boolean exists() {
return (new File(filePath + File.separator + fileName)).exists();
}
public boolean isFlacFile() {
return Common.getExtension(fileName).equals("flac");
}
public boolean isMd5File() {
return Common.getExtension(fileName).equals("md5");
}
public boolean isMpegFile() {
String ext = Common.getExtension(fileName);
return ext.equals("mpg") || ext.equals("mpeg");
}
public boolean isSeekTableFile() {
return Common.getExtension(fileName).equals("skt");
}
public boolean isShnFile() {
return Common.getExtension(fileName).equals("shn");
}
public boolean isTxtFile() {
return Common.getExtension(fileName).equals("txt");
}
public boolean isFfpFile() {
String s = Common.removeExtension(fileName);
try {
s = s.substring(s.length()-3, s.length());
}
catch (IndexOutOfBoundsException e){
return false;
}
return s.equals("ffp");
}
public boolean isVcdFile() {
String ext = Common.getExtension(fileName);
return ext.equals("mpg") || ext.equals("mpeg") || ext.equals("dat");
}
// a "playable music file" is one we should try to add to a playlist
public boolean isPlayableMusicFile() {
String fileUpper = fileName.toUpperCase();
for (int i = 0; i < Constants.PLAYLIST_MUSIC_FORMATS.length; i++) {
if (fileUpper.endsWith(Constants.PLAYLIST_MUSIC_FORMATS[i]))
return true;
}
return false;
}
public long length() {
return (new File(filePath + File.separator + fileName)).length();
}
// a "set" method; doesn't really set anything, but makes a new object
// of what would have happened if this object were mutable.
public FileInfo setMd5(String newMd5) {
// make sure this field never ends up null to avoid checks later
if (newMd5 == null)
newMd5 = "";
return new FileInfo(fileName, filePath, newMd5);
}
}
The table below shows all metrics for FileInfo.java.




