KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FtpFile


1 /*
2  * FtpFile.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: FtpFile.java,v 1.2 2002/11/30 02:16:08 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class FtpFile extends File
25 {
26     private FtpFile()
27     {
28         isRemote = true;
29         protocol = PROTOCOL_FTP;
30         port = 21;
31     }
32
33     public FtpFile(String JavaDoc hostName, String JavaDoc path, String JavaDoc userName, String JavaDoc password, int port)
34     {
35         this();
36         this.hostName = hostName;
37         this.canonicalPath = path;
38         this.userName = userName;
39         this.password = password;
40         this.port = port;
41     }
42
43     public static FtpFile getFtpFile(String JavaDoc name)
44     {
45         FtpFile file = new FtpFile();
46         if (file.initRemote(name, PREFIX_FTP))
47             return file;
48         return null;
49     }
50
51     public static FtpFile getFtpFile(FtpFile directory, String JavaDoc name)
52     {
53         FtpFile file = new FtpFile();
54
55         file.hostName = directory.hostName;
56         file.userName = directory.userName;
57         file.password = directory.password;
58         file.port = directory.port;
59
60         if (Utilities.isFilenameAbsolute(name))
61             file.canonicalPath = canonicalize(name, "/");
62         else
63             file.canonicalPath = canonicalize(appendNameToPath(directory.canonicalPath(), name, '/'), "/");
64
65         return file;
66     }
67
68     public static FtpFile getFtpFile(String JavaDoc host, String JavaDoc path)
69     {
70         if (host == null)
71             return null;
72
73         // Path can be null.
74

75         FtpFile file = new FtpFile();
76
77         file.hostName = host;
78         file.canonicalPath = path;
79
80         return file;
81     }
82
83     public File getRoot()
84     {
85         FtpFile file = new FtpFile();
86
87         file.hostName = this.hostName;
88         file.userName = this.userName;
89         file.password = this.password;
90         file.port = this.port;
91         file.canonicalPath = "/";
92         file.type = TYPE_DIRECTORY;
93
94         return file;
95     }
96
97     public final String JavaDoc getSeparator()
98     {
99         return "/";
100     }
101
102     public final char getSeparatorChar()
103     {
104         return '/';
105     }
106
107     public File getParentFile()
108     {
109         if (canonicalPath() == null || canonicalPath.equals("/"))
110             return null; // No parent.
111

112         int index = canonicalPath.lastIndexOf('/');
113
114         if (index < 0)
115             return null; // No parent.
116

117         if (index == 0) // "/usr"
118
return new FtpFile(hostName, "/", userName, password, port);
119
120         return new FtpFile(hostName, canonicalPath.substring(0, index), userName, password, port);
121     }
122
123     public boolean isDirectory()
124     {
125         if (type == TYPE_UNKNOWN) {
126             FtpSession session = FtpSession.getSession(this);
127             if (session != null) {
128                 if (session.isDirectory(canonicalPath()))
129                     type = TYPE_DIRECTORY;
130                 session.unlock();
131             }
132         }
133         return type == TYPE_DIRECTORY;
134     }
135
136     public boolean isLink()
137     {
138         return type == TYPE_LINK;
139     }
140
141     public String JavaDoc getDirectoryListing()
142     {
143         return getDirectoryListing(false);
144     }
145     
146     public String JavaDoc getDirectoryListing(boolean forceRefresh)
147     {
148         if (!forceRefresh) {
149             String JavaDoc listing =
150                 DirectoryCache.getDirectoryCache().getListing(this);
151             if (listing != null)
152                 return listing;
153         }
154         FtpSession session = FtpSession.getSession(this);
155         if (session == null)
156             return null;
157         String JavaDoc listing = session.getDirectoryListing(canonicalPath());
158         session.unlock();
159         if (listing != null)
160             DirectoryCache.getDirectoryCache().put(this, listing);
161         return listing;
162     }
163
164     public String JavaDoc netPath()
165     {
166         FastStringBuffer sb = new FastStringBuffer(256);
167         sb.append(PREFIX_FTP);
168         sb.append(hostName);
169         if (port != 21) {
170             sb.append(':');
171             sb.append(port);
172         }
173         if (canonicalPath != null)
174             sb.append(canonicalPath);
175         return sb.toString();
176     }
177 }
178
Popular Tags