1 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 hostName, String path, String userName, String 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 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 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 host, String path) 69 { 70 if (host == null) 71 return null; 72 73 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 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; 112 int index = canonicalPath.lastIndexOf('/'); 113 114 if (index < 0) 115 return null; 117 if (index == 0) 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 getDirectoryListing() 142 { 143 return getDirectoryListing(false); 144 } 145 146 public String getDirectoryListing(boolean forceRefresh) 147 { 148 if (!forceRefresh) { 149 String 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 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 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 |