public class SFTPv3Client
extends java.lang.Object
SFTPv3Client represents a SFTP (protocol version 3)
client connection tunnelled over a SSH-2 connection. This is a very simple
(synchronous) implementation.
Basically, most methods in this class map directly to one of the packet types described in draft-ietf-secsh-filexfer-02.txt.
Note: this is experimental code.
Error handling: the methods of this class throw IOExceptions. However, unless
there is catastrophic failure, exceptions of the type SFTPv3Client will
be thrown (a subclass of IOException). Therefore, you can implement more verbose
behavior by checking if a thrown exception if of this type. If yes, then you
can cast the exception and access detailed information about the failure.
Notes about file names, directory names and paths, copy-pasted from the specs:
If you are still not tired then please go on and read the comment for
setCharset(String).
| Constructor and Description |
|---|
SFTPv3Client(Connection conn)
Create a SFTP v3 client.
|
SFTPv3Client(Connection conn,
java.io.PrintStream debug)
Deprecated.
this constructor (debug version) will disappear in the future,
use
SFTPv3Client(Connection) instead. |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
canonicalPath(java.lang.String path)
Have the server canonicalize any given path name to an absolute path.
|
void |
close()
Close this SFTP session.
|
void |
closeFile(SFTPv3FileHandle handle)
Close a file.
|
SFTPv3FileHandle |
createFile(java.lang.String fileName)
Create a file and open it for reading and writing.
|
SFTPv3FileHandle |
createFile(java.lang.String fileName,
SFTPv3FileAttributes attr)
Create a file and open it for reading and writing.
|
SFTPv3FileHandle |
createFileTruncate(java.lang.String fileName)
Create a file (truncate it if it already exists) and open it for reading and writing.
|
SFTPv3FileHandle |
createFileTruncate(java.lang.String fileName,
SFTPv3FileAttributes attr)
reate a file (truncate it if it already exists) and open it for reading and writing.
|
void |
createSymlink(java.lang.String src,
java.lang.String target)
Create a symbolic link on the server.
|
void |
fsetstat(SFTPv3FileHandle handle,
SFTPv3FileAttributes attr)
Modify the attributes of a file.
|
SFTPv3FileAttributes |
fstat(SFTPv3FileHandle handle)
Retrieve the file attributes of an open file.
|
java.lang.String |
getCharset()
The currently used charset for filename encoding/decoding.
|
int |
getProtocolVersion()
Returns the negotiated SFTP protocol version between the client and the server.
|
java.util.Vector |
ls(java.lang.String dirName)
List the contents of a directory.
|
SFTPv3FileAttributes |
lstat(java.lang.String path)
Retrieve the file attributes of a file.
|
void |
mkdir(java.lang.String dirName,
int posixPermissions)
Create a new directory.
|
void |
mv(java.lang.String oldPath,
java.lang.String newPath)
Move a file or directory.
|
SFTPv3FileHandle |
openFileRO(java.lang.String fileName)
Open a file for reading.
|
SFTPv3FileHandle |
openFileRW(java.lang.String fileName)
Open a file for reading and writing.
|
int |
read(SFTPv3FileHandle handle,
long fileOffset,
byte[] dst,
int dstoff,
int len)
Read bytes from a file.
|
java.lang.String |
readLink(java.lang.String path)
Read the target of a symbolic link.
|
void |
rm(java.lang.String fileName)
Remove a file.
|
void |
rmdir(java.lang.String dirName)
Remove an empty directory.
|
void |
setCharset(java.lang.String charset)
Set the charset used to convert between Java Unicode Strings and byte encodings
used by the server for paths and file names.
|
void |
setstat(java.lang.String path,
SFTPv3FileAttributes attr)
Modify the attributes of a file.
|
SFTPv3FileAttributes |
stat(java.lang.String path)
Retrieve the file attributes of a file.
|
void |
write(SFTPv3FileHandle handle,
long fileOffset,
byte[] src,
int srcoff,
int len)
Write bytes to a file.
|
public SFTPv3Client(Connection conn, java.io.PrintStream debug) throws java.io.IOException
SFTPv3Client(Connection) instead.conn - The underlying SSH-2 connection to be used.debug - java.io.IOExceptionpublic SFTPv3Client(Connection conn) throws java.io.IOException
conn - The underlying SSH-2 connection to be used.java.io.IOExceptionpublic void setCharset(java.lang.String charset)
throws java.io.IOException
If you don't set anything, then the platform default will be used (this is the default behavior).
charset - the name of the charset to be used or null to use the platform's
default encoding.java.io.IOExceptiongetCharset()public java.lang.String getCharset()
null if the platform's default charset is being used)setCharset(String)public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws java.io.IOException
handle - a SFTPv3FileHandle handle.java.io.IOExceptionpublic SFTPv3FileAttributes stat(java.lang.String path) throws java.io.IOException
path - See the comment for the class for more details.java.io.IOExceptionlstat(String)public SFTPv3FileAttributes lstat(java.lang.String path) throws java.io.IOException
path - See the comment for the class for more details.java.io.IOExceptionstat(String)public java.lang.String readLink(java.lang.String path)
throws java.io.IOException
path - See the comment for the class for more details.java.io.IOExceptionpublic void setstat(java.lang.String path,
SFTPv3FileAttributes attr)
throws java.io.IOException
path - See the comment for the class for more details.attr - A SFTPv3FileAttributes object. Specifies the modifications to be
made to the attributes of the file. Empty fields will be ignored.java.io.IOExceptionpublic void fsetstat(SFTPv3FileHandle handle, SFTPv3FileAttributes attr) throws java.io.IOException
handle - a SFTPv3FileHandle handleattr - A SFTPv3FileAttributes object. Specifies the modifications to be
made to the attributes of the file. Empty fields will be ignored.java.io.IOExceptionpublic void createSymlink(java.lang.String src,
java.lang.String target)
throws java.io.IOException
public java.lang.String canonicalPath(java.lang.String path)
throws java.io.IOException
path - See the comment for the class for more details.java.io.IOExceptionpublic int getProtocolVersion()
public void close()
close() method, you are likely wasting resources.public java.util.Vector ls(java.lang.String dirName)
throws java.io.IOException
dirName - See the comment for the class for more details.SFTPv3DirectoryEntry objects.java.io.IOExceptionpublic void mkdir(java.lang.String dirName,
int posixPermissions)
throws java.io.IOException
dirName - See the comment for the class for more details.posixPermissions - the permissions for this directory, e.g., "0700" (remember that
this is octal noation). The server will likely apply a umask.java.io.IOExceptionpublic void rm(java.lang.String fileName)
throws java.io.IOException
fileName - See the comment for the class for more details.java.io.IOExceptionpublic void rmdir(java.lang.String dirName)
throws java.io.IOException
dirName - See the comment for the class for more details.java.io.IOExceptionpublic void mv(java.lang.String oldPath,
java.lang.String newPath)
throws java.io.IOException
public SFTPv3FileHandle openFileRO(java.lang.String fileName) throws java.io.IOException
fileName - See the comment for the class for more details.java.io.IOExceptionpublic SFTPv3FileHandle openFileRW(java.lang.String fileName) throws java.io.IOException
fileName - See the comment for the class for more details.java.io.IOExceptionpublic SFTPv3FileHandle createFile(java.lang.String fileName) throws java.io.IOException
createFile(fileName, null).fileName - See the comment for the class for more details.java.io.IOExceptionpublic SFTPv3FileHandle createFile(java.lang.String fileName, SFTPv3FileAttributes attr) throws java.io.IOException
fileName - See the comment for the class for more details.attr - may be null to use server defaults. Probably only
the uid, gid and permissions
(remember the server may apply a umask) entries of the SFTPv3FileHandle
structure make sense. You need only to set those fields where you want
to override the server's defaults.java.io.IOExceptionpublic SFTPv3FileHandle createFileTruncate(java.lang.String fileName) throws java.io.IOException
createFileTruncate(fileName, null).fileName - See the comment for the class for more details.java.io.IOExceptionpublic SFTPv3FileHandle createFileTruncate(java.lang.String fileName, SFTPv3FileAttributes attr) throws java.io.IOException
fileName - See the comment for the class for more details.attr - may be null to use server defaults. Probably only
the uid, gid and permissions
(remember the server may apply a umask) entries of the SFTPv3FileHandle
structure make sense. You need only to set those fields where you want
to override the server's defaults.java.io.IOExceptionpublic int read(SFTPv3FileHandle handle, long fileOffset, byte[] dst, int dstoff, int len) throws java.io.IOException
len),
and return them.-1 is returned.
handle - a SFTPv3FileHandle handlefileOffset - offset (in bytes) in the filedst - the destination byte arraydstoff - offset in the destination byte arraylen - how many bytes to read, 0 < len <= 32768 bytesEOFjava.io.IOExceptionpublic void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws java.io.IOException
len > 32768, then the write operation will
be split into multiple writes.handle - a SFTPv3FileHandle handle.fileOffset - offset (in bytes) in the file.src - the source byte array.srcoff - offset in the source byte array.len - how many bytes to write.java.io.IOExceptionpublic void closeFile(SFTPv3FileHandle handle) throws java.io.IOException
handle - a SFTPv3FileHandle handlejava.io.IOException