if (is != null) {
return is;
}
MessageHeader msgh = new MessageHeader();
try {
decodePath(url.getPath());
if (filename == null || type == DIR) {
ftp.ascii();
cd(pathname);
if (filename == null)
is = new FtpInputStream(ftp, ftp.list());
else
is = new FtpInputStream(ftp, ftp.nameList(filename));
} else {
if (type == ASCII)
ftp.ascii();
else
ftp.binary();
cd(pathname);
is = new FtpInputStream(ftp, ftp.get(filename));
}
/* Try to get the size of the file in bytes. If that is
successful, then create a MeteredStream. */
try {
String response = ftp.getResponseString();
int offset;
if ((offset = response.indexOf(" bytes)")) != -1) {
int i = offset;
int c;
while (--i >= 0 && ((c = response.charAt(i)) >= '0'
&& c <= '9'))
;
i = Integer.parseInt(response.substring(i + 1, offset));
msgh.add("content-length", ""+i);
if (i > 0) {
// Wrap input stream with MeteredStream to ensure read() will always return -1
// at expected length.
// Check if URL should be metered
boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
ProgressSource pi = null;
if (meteredInput) {
pi = new ProgressSource(url, "GET", i);
pi.beginTracking();
}
is = new MeteredStream(is, pi, i);
}
}
} catch (Exception e) {
e.printStackTrace();
/* do nothing, since all we were doing was trying to
get the size in bytes of the file */
}
String type = guessContentTypeFromName(fullpath);
if (type == null && is.markSupported()) {
type = guessContentTypeFromStream(is);
}
if (type != null) {
msgh.add("content-type", type);
}
} catch (FileNotFoundException e) {
try {
cd(fullpath);
/* if that worked, then make a directory listing
and build an html stream with all the files in
the directory */
ftp.ascii();
is = new FtpInputStream(ftp, ftp.list());
msgh.add("content-type", "text/plain");
} catch (IOException ex) {
throw new FileNotFoundException(fullpath);
}
}
setProperties(msgh);