📄 ftpfile.java
字号:
/*
* Version 0.70 01/04/2002
*
* Visit my url for update: http://www.geocities.com/beapetrovicova/
*
* jFtp was developed by Bea Petrovicova <beapetrovicova@yahoo.com>.
* The design and implementation of jFtp are available for royalty-free
* adoption and use. This software is provided 'as is' without any
* guarantees. Copyright is retained by Bea Petrovicova. Redistribution
* of any part of jFtp or any derivative works must include this notice.
*
*/
package cz.dhl.ftp;
import cz.dhl.io.CoFile;
import cz.dhl.io.CoFilenameFilter;
import cz.dhl.io.CoOrder;
import cz.dhl.ui.CoConsole;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* Allows uniform manipulation with FTP files.
* Equivalent for File object.
*
* <P><B>Only absolute pathnames are supported!</B></P>
*
* @Version 0.70 01/04/2002
* @author Bea Petrovicova <beapetrovicova@yahoo.com>
*
* @see Ftp
* @see cz.dhl.io.CoFile
* @see java.io.File
*/
public final class FtpFile
implements CoFile
{
/* CoOrder Implementation. */
private String name=null, ext=null;
private void sortSetup(String name)
{ this.name=name.toUpperCase(); int index = this.name.lastIndexOf(".");
if(index != -1 && index < this.name.length())
ext = this.name.substring(index); else ext = " " +this.name; }
public int compareNameToIgnoreCase(CoOrder file)
{ if(file instanceof FtpFile)
{ FtpFile l2 = (FtpFile)file;
return name.compareTo(l2.name); }
else throw new ClassCastException(); }
public int compareExtToIgnoreCase(CoOrder file)
{ if(file instanceof FtpFile)
{ FtpFile l2 = (FtpFile)file;
int result=ext.compareTo(l2.ext);
if(result==0)
result=name.compareTo(l2.name);
return result; }
else throw new ClassCastException(); }
public boolean startsWithIgnoreCase(char ch)
{ return (name.charAt(0) == Character.toUpperCase(ch)); }
public boolean equalsExtTo(String filter)
{ return (ext.compareTo(filter)==0); }
public boolean equalsExtTo(String filter[])
{ boolean done = false;
for(int j=0;j<filter.length;j++)
if(ext.compareTo(filter[j])==0)
{ done = true; break; }
return done; }
public boolean equals(Object o)
{ if(o==null) return false; else return(compareTo(o)==0); }
public int compareTo(Object o)
{ String s1 = getHost()+getAbsolutePath(), s2;
if(o instanceof CoFile)
{ CoFile f2 = (CoFile)o;
s2 = f2.getHost()+f2.getAbsolutePath(); }
else if(o instanceof String)
s2 = (String)o;
else throw new ClassCastException();
return s1.compareTo(s2); }
public boolean isConnected() { return client.isConnected(); }
/* CoOrder Implementation. */
public char getDataType()
{ if(client.getContext().getFileTransferMode() == 'S')
if(equalsExtTo(client.getContext().getTextFilter()))
return 'A'; else return 'I';
else return client.getContext().getFileTransferMode(); }
public InputStream getInputStream() throws IOException
{ return new FtpInputStream(this); }
public OutputStream getOutputStream() throws IOException
{ return new FtpOutputStream(this,false); }
public OutputStream getOutputStream(boolean append) throws IOException
{ return new FtpOutputStream(this,append); }
public CoFile newFileChild(String child)
{ return new FtpFile(this,child,this.client); }
public CoFile newFileRename(String name)
{ return new FtpFile(this.getParent(),name,this.client); }
public CoConsole getConsole()
{ return client.getContext().getConsole(); }
/* CoFile Implementation. */
private static final String months[] = {"JAN", "FEB", "MAR",
"APR", "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};
private static final char LINK = 'l';
private static final char SPECIAL = 'c';
private static final char FOLDER = 'd';
private static final char FILE = '-';
Ftp client = null;
private String access = null;
private String owner = null;
private String group = null;
private long size = 0L;
private long date = 0L;
String path = null;
private FtpFile() {}
/** Creates a new FtpFile instance by converting the
* given pathname string into an abstract pathname. */
public FtpFile(String path, Ftp client)
{ access = "d?????????";
owner = ""; group = "";
size = -1; date = 0L;
if(path.compareTo("/")!=0 && path.endsWith("/"))
this.path = path.substring(0,path.length()-1);
else this.path = path;
this.client = client;
sortSetup(getName());
}
/** Creates a new FtpFile instance from a parent
* pathname string and a child pathname string. */
public FtpFile(String path, String name, Ftp client)
{ access = "f?????????";
owner = ""; group = "";
size = -1; date = 0L;
if(path.endsWith("/"))
this.path = path+name;
else this.path = path+"/"+name;
this.client = client;
sortSetup(name);
}
/** Creates a new FtpFile instance from a parent
* abstract pathname and a child pathname string. */
public FtpFile(FtpFile dir, String name, Ftp client)
{ this(dir.toString(),name,client); }
public String getHost()
{ String system = "";
try
{ system = client.host(); }
catch(IOException e)
{ client.getContext().printlog("< File: Can't obtain host name. >\n"+e); }
return system;
}
public String getAbsolutePath() { return path; }
public int getPathDepth()
{ int depth = -1; int length = -1;
while((length = path.indexOf('/',length + 1)) >= 0)
depth++;
if(!path.endsWith("/"))
depth++;
return depth;
}
public CoFile getPathFragment(int depth)
{ if(depth>0)
{ int length = -1;
for(int n=0;n<=depth;n++)
if((length = path.indexOf('/',length + 1)) < 0)
break;
if(length>0)
return new FtpFile(path.substring(0,length),client);
else return this;
} else return new FtpFile("/",client);
}
public String[] getPathArray()
{ Vector dv = new Vector();
dv.addElement(getHost());
StringTokenizer toker = new StringTokenizer(path,"/");
while (true)
try
{ String d = toker.nextToken();
dv.addElement(d);
} catch(NoSuchElementException e)
{ break; }
String[] ds = new String[dv.size()];
dv.copyInto(ds);
return ds;
}
public String getName()
{ if(path.lastIndexOf('/')>=0)
return path.substring(path.lastIndexOf('/')+1);
else return path; }
public String getParent()
{ if(path.lastIndexOf('/')>0)
return path.substring(0,path.lastIndexOf('/'));
else return new String("/"); }
public boolean delete() throws SecurityException
{ if(isDirectory())
{ /* release dir if current */
client.cd(getParent());
if(client.rmdir(path))
return true;
/* When using NAME_LIST, files/dirs
are distinguished by guessing. */
else return client.rm(path);
} else if(client.rm(path))
return true;
else
{ /* release dir if current */
client.cd(getParent());
/* When using NAME_LIST, files/dirs
are distinguished by guessing. */
return client.rmdir(path); }
}
public boolean mkdir() throws SecurityException
{ return client.mkdir(path); }
public boolean mkdirs() throws SecurityException
{ boolean done = true;
int depth = getPathDepth();
for(int i=0; i<depth;i++)
if(!((FtpFile)getPathFragment(depth)).mkdir())
done = false;
return done;
}
public boolean renameTo(CoFile dest) throws SecurityException
{ return client.mv(path,dest.getAbsolutePath()); }
public long length() { return size; }
public long lastModified() { return date; }
public String lastModifiedString()
{ return (DateFormat.getDateTimeInstance(
DateFormat.SHORT,DateFormat.SHORT)
).format(new Date(lastModified())); }
public boolean isAbsolute() { return (path.charAt(0) == '/'); }
public boolean isDirectory() { return (access.charAt(0) == FOLDER); }
public boolean isFile() { return (access.charAt(0) == FILE); }
public boolean isSpecial() { return (access.charAt(0) == SPECIAL); }
public boolean isLink() { return (access.charAt(0) == LINK); }
/** Tests if the file represented by this File object is executable. */
public boolean isExecutable() { return (access.indexOf('x') != -1); }
public boolean isHidden() { return false; }
public boolean canRead() { return true; }
public boolean canWrite() { return true; }
public boolean exists() { return false; }
public String getAccess() { return access; };
/** Get owner of this file object. */
public String getOwner() { return owner; };
/** Get group of users for this file object. */
public String getGroup() { return group; };
public String propertyString()
{ String desc = getAccess()+" "+getOwner()+" "+getGroup();
return (isFile()?""+length()+" "+desc:desc); }
public CoFile[] listCoRoots()
{ CoFile fs[] = new CoFile[1];
fs[0] = getPathFragment(0);
return fs; }
public CoFile[] listCoFiles()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -