Methods Summary |
---|
private void | cd()Change the server directory to that specified in the URL
int idx = url.getFile().lastIndexOf('/");
if (idx > 0) {
String dir = url.getFile().substring(0, idx);
write("CWD " + dir + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
int reply = getReply();
if (reply != FTP_FILEOK && dir.length() > 0 && dir.charAt(0) == '/") {
write("CWD " + dir.substring(1) + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
reply = getReply();
}
if (reply != FTP_FILEOK) {
throw new IOException(Msg.getString("K0094")); //$NON-NLS-1$
}
}
|
public void | connect()Establishes the connection to the resource specified by this
URL
// Use system-wide ProxySelect to select proxy list,
// then try to connect via elements in the proxy list.
List<Proxy> proxyList = null;
if (null != proxy) {
proxyList = new ArrayList<Proxy>(1);
proxyList.add(proxy);
} else {
proxyList = NetUtil.getProxyList(uri);
}
if (null == proxyList) {
currentProxy = null;
connectInternal();
} else {
ProxySelector selector = ProxySelector.getDefault();
Iterator<Proxy> iter = proxyList.iterator();
boolean connectOK = false;
while (iter.hasNext() && !connectOK) {
currentProxy = iter.next();
try {
connectInternal();
connectOK = true;
} catch (IOException ioe) {
// If connect failed, callback "connectFailed"
// should be invoked.
if (null != selector && Proxy.NO_PROXY != currentProxy) {
selector
.connectFailed(uri, currentProxy.address(), ioe);
}
}
}
if (!connectOK) {
throw new IOException(Msg.getString("K0097")); //$NON-NLS-1$
}
}
|
private void | connectInternal()
int port = url.getPort();
int connectTimeout = getConnectTimeout();
if (port <= 0) {
port = FTP_PORT;
}
if (null == currentProxy || Proxy.Type.HTTP == currentProxy.type()) {
controlSocket = new Socket();
} else {
controlSocket = new Socket(currentProxy);
}
InetSocketAddress addr = new InetSocketAddress(hostName, port);
controlSocket.connect(addr, connectTimeout);
connected = true;
ctrlOutput = controlSocket.getOutputStream();
ctrlInput = controlSocket.getInputStream();
login();
setType();
if (!getDoInput()) {
cd();
}
try {
acceptSocket = new ServerSocket(0);
dataPort = acceptSocket.getLocalPort();
/* Cannot set REUSEADDR so we need to send a PORT command */
port();
if (connectTimeout == 0) {
// set timeout rather than zero as before
connectTimeout = 3000;
}
acceptSocket.setSoTimeout(getConnectTimeout());
if (getDoInput()) {
getFile();
} else {
sendFile();
}
dataSocket = acceptSocket.accept();
dataSocket.setSoTimeout(getReadTimeout());
acceptSocket.close();
} catch (InterruptedIOException e) {
throw new IOException(Msg.getString("K0095")); //$NON-NLS-1$
}
if (getDoInput()) {
// BEGIN android-modified
inputStream = new FtpURLInputStream(
new BufferedInputStream(dataSocket.getInputStream(), 8192),
controlSocket);
// END android-modified
}
|
public java.lang.String | getContentType()Returns the content type of the resource. Just takes a guess based on the
name.
String result = guessContentTypeFromName(url.getFile());
if (result == null) {
return MimeTable.UNKNOWN;
}
return result;
|
private void | getFile()
int reply;
String file = url.getFile();
write("RETR " + file + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
reply = getReply();
if (reply == FTP_NOTFOUND && file.length() > 0 && file.charAt(0) == '/") {
write("RETR " + file.substring(1) + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
reply = getReply();
}
if (!(reply == FTP_OPENDATA || reply == FTP_TRANSFEROK)) {
throw new FileNotFoundException(Msg.getString("K0096", reply)); //$NON-NLS-1$
}
|
public java.io.InputStream | getInputStream()Creates a input stream for writing to this URL Connection.
if (!connected) {
connect();
}
return inputStream;
|
public java.io.OutputStream | getOutputStream()Creates a output stream for writing to this URL Connection.
if (!connected) {
connect();
}
return dataSocket.getOutputStream();
|
public java.security.Permission | getPermission()Returns the permission object (in this case, SocketPermission) with the
host and the port number as the target name and "resolve, connect" as the
action list.
int port = url.getPort();
if (port <= 0) {
port = FTP_PORT;
}
return new SocketPermission(hostName + ":" + port, "connect, resolve"); //$NON-NLS-1$ //$NON-NLS-2$
|
private int | getReply()
byte[] code = new byte[3];
ctrlInput.read(code, 0, code.length);
replyCode = new String(code, "ISO8859_1"); //$NON-NLS-1$
boolean multiline = false;
if (ctrlInput.read() == '-") {
multiline = true;
}
readLine(); /* Skip the rest of the first line */
if (multiline) {
while (readMultiLine()) {/* Read all of a multiline reply */
}
}
return Integer.parseInt(new String(code, "ISO8859_1")); //$NON-NLS-1$
|
private void | login()
int reply;
reply = getReply();
if (reply == FTP_USERREADY) {
} else {
throw new IOException(Msg.getString("K0097", url.getHost())); //$NON-NLS-1$
}
write("USER " + username + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
reply = getReply();
if (reply == FTP_PASWD || reply == FTP_LOGGEDIN) {
} else {
throw new IOException(Msg.getString("K0098", url.getHost())); //$NON-NLS-1$
}
if (reply == FTP_PASWD) {
write("PASS " + password + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
reply = getReply();
if (!(reply == FTP_OK || reply == FTP_USERREADY || reply == FTP_LOGGEDIN)) {
throw new IOException(Msg.getString("K0098", url.getHost())); //$NON-NLS-1$
}
}
|
private void | port()
write("PORT " //$NON-NLS-1$
+ controlSocket.getLocalAddress().getHostAddress().replace('.",
',") + '," + (dataPort >> 8) + ',"
+ (dataPort & 255)
+ "\r\n"); //$NON-NLS-1$
if (getReply() != FTP_OK) {
throw new IOException(Msg.getString("K0099")); //$NON-NLS-1$
}
|
private java.lang.String | readLine()Read a line of text and return it for possible parsing
StringBuffer sb = new StringBuffer();
int c;
while ((c = ctrlInput.read()) != '\n") {
sb.append((char) c);
}
return sb.toString();
|
private boolean | readMultiLine()
String line = readLine();
if (line.length() < 4) {
return true;
}
if (line.substring(0, 3).equals(replyCode)
&& (line.charAt(3) == (char) 32)) {
return false;
}
return true;
|
private void | sendFile()Issue the STOR command to the server with the file as the parameter
int reply;
write("STOR " //$NON-NLS-1$
+ url.getFile().substring(url.getFile().lastIndexOf('/") + 1,
url.getFile().length()) + "\r\n"); //$NON-NLS-1$
reply = getReply();
if (!(reply == FTP_OPENDATA || reply == FTP_OK || reply == FTP_DATAOPEN)) {
throw new IOException(Msg.getString("K009a")); //$NON-NLS-1$
}
|
public void | setDoInput(boolean newValue)Set the flag if this URLConnection supports input (read).
It cannot be set after the connection is made. FtpURLConnections cannot
support both input and output
if (connected) {
throw new IllegalAccessError();
}
this.doInput = newValue;
this.doOutput = !newValue;
|
public void | setDoOutput(boolean newValue)Set the flag if this URLConnection supports output(read).
It cannot be set after the connection is made.\ FtpURLConnections cannot
support both input and output.
if (connected) {
throw new IllegalAccessError();
}
this.doOutput = newValue;
this.doInput = !newValue;
|
private void | setType()Set the type of the file transfer. Only Image is supported
write("TYPE I\r\n"); //$NON-NLS-1$
if (getReply() != FTP_OK) {
throw new IOException(Msg.getString("K009b")); //$NON-NLS-1$
}
|
private void | write(java.lang.String command)
ctrlOutput.write(command.getBytes("ISO8859_1")); //$NON-NLS-1$
|