gnu.java.net.protocol.ftp

Class FTPConnection


public class FTPConnection
extends Object

An FTP client connection, or PI. This implements RFC 959, with the following exceptions:

Field Summary

protected static String
ABOR
protected static String
ACCT
protected static String
ALLO
protected static String
APPE
protected static String
AUTH
protected static String
CCC
protected static String
CDUP
protected static String
CWD
protected static String
DELE
static int
FTP_DATA_PORT
The FTP data port.
static int
FTP_PORT
The default FTP transmission control port.
protected static String
HELP
protected static String
LIST
protected static String
MKD
protected static String
MODE
static int
MODE_BLOCK
static int
MODE_COMPRESSED
static int
MODE_STREAM
protected static String
NLST
protected static String
NOOP
protected static String
PASS
protected static String
PASV
protected static String
PBSZ
protected static String
PORT
protected static String
PROT
protected static String
PWD
protected static String
QUIT
protected static String
REIN
protected static String
REST
protected static String
RETR
protected static String
RMD
protected static String
RNFR
protected static String
RNTO
protected static String
SITE
protected static String
SMNT
protected static String
STAT
protected static String
STOR
protected static String
STOU
protected static String
STRU
static int
STRUCTURE_FILE
static int
STRUCTURE_PAGE
static int
STRUCTURE_RECORD
protected static String
SYST
protected static String
TLS
protected static String
TYPE
static int
TYPE_ASCII
static int
TYPE_BINARY
static int
TYPE_EBCDIC
protected static String
USER
protected int
connectionTimeout
The timeout when attempting to connect a socket.
protected boolean
debug
If true, print debugging information.
protected gnu.java.net.protocol.ftp.DTP
dtp
The current data transfer process in use by this connection.
protected int
fileStructure
The current file structure type.
protected LineInputStream
in
The socket input stream.
protected CRLFOutputStream
out
The socket output stream.
protected boolean
passive
If true, use passive mode.
protected int
representationType
The current representation type.
protected Socket
socket
The socket used to communicate with the server.
protected int
timeout
The read timeout on sockets.
protected int
transferMode
The current transfer mode.

Constructor Summary

FTPConnection(String hostname)
Creates a new connection to the server using the default port.
FTPConnection(String hostname, int port)
Creates a new connection to the server.
FTPConnection(String hostname, int port, int connectionTimeout, int timeout, boolean debug)
Creates a new connection to the server.

Method Summary

boolean
abort()
Aborts the transfer in progress.
void
allocate(long size)
This command may be required by some servers to reserve sufficient storage to accommodate the new file to be transferred.
OutputStream
append(String filename)
Returns a stream for uploading a file.
boolean
authenticate(String username, String password)
Authenticate using the specified username and password.
boolean
changeToParentDirectory()
Changes directory to the parent of the current working directory.
boolean
changeWorkingDirectory(String path)
Changes directory to the specified path.
boolean
delete(String filename)
Causes the file specified to be deleted at the server site.
int
getFileStructure()
Returns the current file structure type.
int
getRepresentationType()
Returns the current representation type of the transfer data.
protected FTPResponse
getResponse()
Reads the next response from the server.
int
getTransferMode()
Returns the current transfer mode.
String
getWorkingDirectory()
Returns the current working directory.
protected void
initialiseDTP()
Initialise the data transfer process.
InputStream
list(String pathname)
Returns a listing of information about the specified pathname.
void
logout()
Terminates the control connection.
boolean
makeDirectory(String pathname)
Causes the directory specified to be created at the server site.
List
nameList(String pathname)
Returns a directory listing.
void
noop()
Does nothing.
protected FTPResponse
readResponse()
Reads and parses the next response from the server.
void
reinitialize()
Terminates an authenticated login.
boolean
removeDirectory(String pathname)
Causes the directory specified to be deleted.
boolean
rename(String oldName, String newName)
Renames a file.
InputStream
retrieve(String filename)
Retrieves the specified file.
protected void
send(String cmd)
Sends the specified command line to the server.
void
setFileStructure(int structure)
Sets the desired file structure type.
void
setPassive(boolean flag)
Set passive mode.
void
setRepresentationType(int type)
Sets the desired representation type of the transfer data.
void
setTransferMode(int mode)
Sets the desired transfer mode.
boolean
starttls(boolean confidential)
Negotiates TLS over the current connection.
boolean
starttls(boolean confidential, TrustManager tm)
Negotiates TLS over the current connection.
OutputStream
store(String filename)
Returns a stream for uploading a file.
String
system()
Returns the type of operating system at the server.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

ABOR

protected static final String ABOR
Field Value:
"ABOR"

ACCT

protected static final String ACCT
Field Value:
"ACCT"

ALLO

protected static final String ALLO
Field Value:
"ALLO"

APPE

protected static final String APPE
Field Value:
"APPE"

AUTH

protected static final String AUTH
Field Value:
"AUTH"

CCC

protected static final String CCC
Field Value:
"CCC"

CDUP

protected static final String CDUP
Field Value:
"CDUP"

CWD

protected static final String CWD
Field Value:
"CWD"

DELE

protected static final String DELE
Field Value:
"DELE"

FTP_DATA_PORT

public static final int FTP_DATA_PORT
The FTP data port.
Field Value:
20

FTP_PORT

public static final int FTP_PORT
The default FTP transmission control port.
Field Value:
21

HELP

protected static final String HELP
Field Value:
"HELP"

LIST

protected static final String LIST
Field Value:
"LIST"

MKD

protected static final String MKD
Field Value:
"MKD"

MODE

protected static final String MODE
Field Value:
"MODE"

MODE_BLOCK

public static final int MODE_BLOCK
Field Value:
2

MODE_COMPRESSED

public static final int MODE_COMPRESSED
Field Value:
3

MODE_STREAM

public static final int MODE_STREAM
Field Value:
1

NLST

protected static final String NLST
Field Value:
"NLST"

NOOP

protected static final String NOOP
Field Value:
"NOOP"

PASS

protected static final String PASS
Field Value:
"PASS"

PASV

protected static final String PASV
Field Value:
"PASV"

PBSZ

protected static final String PBSZ
Field Value:
"PBSZ"

PORT

protected static final String PORT
Field Value:
"PORT"

PROT

protected static final String PROT
Field Value:
"PROT"

PWD

protected static final String PWD
Field Value:
"PWD"

QUIT

protected static final String QUIT
Field Value:
"QUIT"

REIN

protected static final String REIN
Field Value:
"REIN"

REST

protected static final String REST
Field Value:
"REST"

RETR

protected static final String RETR
Field Value:
"RETR"

RMD

protected static final String RMD
Field Value:
"RMD"

RNFR

protected static final String RNFR
Field Value:
"RNFR"

RNTO

protected static final String RNTO
Field Value:
"RNTO"

SITE

protected static final String SITE
Field Value:
"SITE"

SMNT

protected static final String SMNT
Field Value:
"SMNT"

STAT

protected static final String STAT
Field Value:
"STAT"

STOR

protected static final String STOR
Field Value:
"STOR"

STOU

protected static final String STOU
Field Value:
"STOU"

STRU

protected static final String STRU
Field Value:
"STRU"

STRUCTURE_FILE

public static final int STRUCTURE_FILE
Field Value:
1

STRUCTURE_PAGE

public static final int STRUCTURE_PAGE
Field Value:
3

STRUCTURE_RECORD

public static final int STRUCTURE_RECORD
Field Value:
2

SYST

protected static final String SYST
Field Value:
"SYST"

TLS

protected static final String TLS
Field Value:
"TLS"

TYPE

protected static final String TYPE
Field Value:
"TYPE"

TYPE_ASCII

public static final int TYPE_ASCII
Field Value:
1

TYPE_BINARY

public static final int TYPE_BINARY
Field Value:
3

TYPE_EBCDIC

public static final int TYPE_EBCDIC
Field Value:
2

USER

protected static final String USER
Field Value:
"USER"

connectionTimeout

protected int connectionTimeout
The timeout when attempting to connect a socket.

debug

protected boolean debug
If true, print debugging information.

dtp

protected gnu.java.net.protocol.ftp.DTP dtp
The current data transfer process in use by this connection.

fileStructure

protected int fileStructure
The current file structure type.

in

protected LineInputStream in
The socket input stream.

out

protected CRLFOutputStream out
The socket output stream.

passive

protected boolean passive
If true, use passive mode.

representationType

protected int representationType
The current representation type.

socket

protected Socket socket
The socket used to communicate with the server.

timeout

protected int timeout
The read timeout on sockets.

transferMode

protected int transferMode
The current transfer mode.

Constructor Details

FTPConnection

public FTPConnection(String hostname)
            throws UnknownHostException,
                   IOException
Creates a new connection to the server using the default port.
Parameters:
hostname - the hostname of the server to connect to

FTPConnection

public FTPConnection(String hostname,
                     int port)
            throws UnknownHostException,
                   IOException
Creates a new connection to the server.
Parameters:
hostname - the hostname of the server to connect to
port - the port to connect to(if <=0, use default port)

FTPConnection

public FTPConnection(String hostname,
                     int port,
                     int connectionTimeout,
                     int timeout,
                     boolean debug)
            throws UnknownHostException,
                   IOException
Creates a new connection to the server.
Parameters:
hostname - the hostname of the server to connect to
port - the port to connect to(if <=0, use default port)
connectionTimeout - the connection timeout, in milliseconds
timeout - the I/O timeout, in milliseconds
debug - print debugging information

Method Details

abort

public boolean abort()
            throws IOException
Aborts the transfer in progress.
Returns:
true if a transfer was in progress, false otherwise

allocate

public void allocate(long size)
            throws IOException
This command may be required by some servers to reserve sufficient storage to accommodate the new file to be transferred. It should be immediately followed by a store or append.
Parameters:
size - the number of bytes of storage to allocate

append

public OutputStream append(String filename)
            throws IOException
Returns a stream for uploading a file. If a file with the same filename already exists on the server, the content specified will be appended to the existing file.
Parameters:
filename - the name of the file to save the content as
Returns:
an OutputStream to write the file data to

authenticate

public boolean authenticate(String username,
                            String password)
            throws IOException
Authenticate using the specified username and password. If the username suffices for the server, the password will not be used and may be null.
Parameters:
username - the username
password - the optional password
Returns:
true on success, false otherwise

changeToParentDirectory

public boolean changeToParentDirectory()
            throws IOException
Changes directory to the parent of the current working directory.
Returns:
true on success, false otherwise

changeWorkingDirectory

public boolean changeWorkingDirectory(String path)
            throws IOException
Changes directory to the specified path.
Parameters:
path - an absolute or relative pathname
Returns:
true on success, false if the specified path does not exist

delete

public boolean delete(String filename)
            throws IOException
Causes the file specified to be deleted at the server site.
Parameters:
filename - the file to delete

getFileStructure

public int getFileStructure()
Returns the current file structure type.
Returns:
STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE

getRepresentationType

public int getRepresentationType()
Returns the current representation type of the transfer data.
Returns:
TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY

getResponse

protected FTPResponse getResponse()
            throws IOException
Reads the next response from the server. If the server sends the "transfer complete" code, this is handled here, and the next response is passed to the caller.

getTransferMode

public int getTransferMode()
Returns the current transfer mode.
Returns:
MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED

getWorkingDirectory

public String getWorkingDirectory()
            throws IOException
Returns the current working directory.

initialiseDTP

protected void initialiseDTP()
            throws IOException
Initialise the data transfer process.

list

public InputStream list(String pathname)
            throws IOException
Returns a listing of information about the specified pathname. If the pathname specifies a directory or other group of files, the server should transfer a list of files in the specified directory. If the pathname specifies a file then the server should send current information on the file. A null argument implies the user's current working or default directory.
Parameters:
pathname - the context pathname, or null

logout

public void logout()
            throws IOException
Terminates the control connection. The file transfer connection remains open for result response only. This connection is invalid and no further commands may be issued.

makeDirectory

public boolean makeDirectory(String pathname)
            throws IOException
Causes the directory specified to be created at the server site. This may be an absolute or relative pathname.
Parameters:
pathname - the directory to create

nameList

public List nameList(String pathname)
            throws IOException
Returns a directory listing. The pathname should specify a directory or other system-specific file group descriptor; a null argument implies the user's current working or default directory.
Parameters:
pathname - the directory pathname, or null
Returns:
a list of filenames(strings)

noop

public void noop()
            throws IOException
Does nothing. This method can be used to ensure that the connection does not time out.

readResponse

protected FTPResponse readResponse()
            throws IOException
Reads and parses the next response from the server.

reinitialize

public void reinitialize()
            throws IOException
Terminates an authenticated login. If file transfer is in progress, it remains active for result response only.

removeDirectory

public boolean removeDirectory(String pathname)
            throws IOException
Causes the directory specified to be deleted. This may be an absolute or relative pathname.
Parameters:
pathname - the directory to delete

rename

public boolean rename(String oldName,
                      String newName)
            throws IOException
Renames a file.
Parameters:
oldName - the current name of the file
newName - the new name
Returns:
true if successful, false otherwise

retrieve

public InputStream retrieve(String filename)
            throws IOException
Retrieves the specified file.
Parameters:
filename - the filename of the file to retrieve
Returns:
an InputStream containing the file content

send

protected void send(String cmd)
            throws IOException
Sends the specified command line to the server. The CRLF sequence is automatically appended.
Parameters:
cmd - the command line to send

setFileStructure

public void setFileStructure(int structure)
            throws IOException
Sets the desired file structure type.
Parameters:
structure - STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE

setPassive

public void setPassive(boolean flag)
            throws IOException
Set passive mode.
Parameters:
flag - true if we should use passive mode, false otherwise

setRepresentationType

public void setRepresentationType(int type)
            throws IOException
Sets the desired representation type of the transfer data.
Parameters:
type - TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY

setTransferMode

public void setTransferMode(int mode)
            throws IOException
Sets the desired transfer mode.
Parameters:
mode - MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED

starttls

public boolean starttls(boolean confidential)
            throws IOException
Negotiates TLS over the current connection. See IETF draft-murray-auth-ftp-ssl-15.txt for details.
Parameters:
confidential - whether to provide confidentiality for the connection

starttls

public boolean starttls(boolean confidential,
                        TrustManager tm)
            throws IOException
Negotiates TLS over the current connection. See IETF draft-murray-auth-ftp-ssl-15.txt for details.
Parameters:
confidential - whether to provide confidentiality for the connection
tm - the trust manager used to validate the server certificate.

store

public OutputStream store(String filename)
            throws IOException
Returns a stream for uploading a file. If a file with the same filename already exists on the server, it will be overwritten.
Parameters:
filename - the name of the file to save the content as
Returns:
an OutputStream to write the file data to

system

public String system()
            throws IOException
Returns the type of operating system at the server.

FTPConnection.java -- Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.