001: /*
002: * FtpFile.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: FtpFile.java,v 1.2 2002/11/30 02:16:08 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class FtpFile extends File {
025: private FtpFile() {
026: isRemote = true;
027: protocol = PROTOCOL_FTP;
028: port = 21;
029: }
030:
031: public FtpFile(String hostName, String path, String userName,
032: String password, int port) {
033: this ();
034: this .hostName = hostName;
035: this .canonicalPath = path;
036: this .userName = userName;
037: this .password = password;
038: this .port = port;
039: }
040:
041: public static FtpFile getFtpFile(String name) {
042: FtpFile file = new FtpFile();
043: if (file.initRemote(name, PREFIX_FTP))
044: return file;
045: return null;
046: }
047:
048: public static FtpFile getFtpFile(FtpFile directory, String name) {
049: FtpFile file = new FtpFile();
050:
051: file.hostName = directory.hostName;
052: file.userName = directory.userName;
053: file.password = directory.password;
054: file.port = directory.port;
055:
056: if (Utilities.isFilenameAbsolute(name))
057: file.canonicalPath = canonicalize(name, "/");
058: else
059: file.canonicalPath = canonicalize(appendNameToPath(
060: directory.canonicalPath(), name, '/'), "/");
061:
062: return file;
063: }
064:
065: public static FtpFile getFtpFile(String host, String path) {
066: if (host == null)
067: return null;
068:
069: // Path can be null.
070:
071: FtpFile file = new FtpFile();
072:
073: file.hostName = host;
074: file.canonicalPath = path;
075:
076: return file;
077: }
078:
079: public File getRoot() {
080: FtpFile file = new FtpFile();
081:
082: file.hostName = this .hostName;
083: file.userName = this .userName;
084: file.password = this .password;
085: file.port = this .port;
086: file.canonicalPath = "/";
087: file.type = TYPE_DIRECTORY;
088:
089: return file;
090: }
091:
092: public final String getSeparator() {
093: return "/";
094: }
095:
096: public final char getSeparatorChar() {
097: return '/';
098: }
099:
100: public File getParentFile() {
101: if (canonicalPath() == null || canonicalPath.equals("/"))
102: return null; // No parent.
103:
104: int index = canonicalPath.lastIndexOf('/');
105:
106: if (index < 0)
107: return null; // No parent.
108:
109: if (index == 0) // "/usr"
110: return new FtpFile(hostName, "/", userName, password, port);
111:
112: return new FtpFile(hostName, canonicalPath.substring(0, index),
113: userName, password, port);
114: }
115:
116: public boolean isDirectory() {
117: if (type == TYPE_UNKNOWN) {
118: FtpSession session = FtpSession.getSession(this );
119: if (session != null) {
120: if (session.isDirectory(canonicalPath()))
121: type = TYPE_DIRECTORY;
122: session.unlock();
123: }
124: }
125: return type == TYPE_DIRECTORY;
126: }
127:
128: public boolean isLink() {
129: return type == TYPE_LINK;
130: }
131:
132: public String getDirectoryListing() {
133: return getDirectoryListing(false);
134: }
135:
136: public String getDirectoryListing(boolean forceRefresh) {
137: if (!forceRefresh) {
138: String listing = DirectoryCache.getDirectoryCache()
139: .getListing(this );
140: if (listing != null)
141: return listing;
142: }
143: FtpSession session = FtpSession.getSession(this );
144: if (session == null)
145: return null;
146: String listing = session.getDirectoryListing(canonicalPath());
147: session.unlock();
148: if (listing != null)
149: DirectoryCache.getDirectoryCache().put(this , listing);
150: return listing;
151: }
152:
153: public String netPath() {
154: FastStringBuffer sb = new FastStringBuffer(256);
155: sb.append(PREFIX_FTP);
156: sb.append(hostName);
157: if (port != 21) {
158: sb.append(':');
159: sb.append(port);
160: }
161: if (canonicalPath != null)
162: sb.append(canonicalPath);
163: return sb.toString();
164: }
165: }
|