|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectch.ethz.ssh2.SFTPv3Client
public class SFTPv3Client
A 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)
.
Field Summary | |
---|---|
(package private) java.lang.String |
charsetName
|
(package private) Connection |
conn
|
(package private) java.io.PrintStream |
debug
|
(package private) boolean |
flag_closed
|
(package private) java.io.InputStream |
is
|
(package private) int |
next_request_id
|
(package private) java.io.OutputStream |
os
|
(package private) int |
protocol_version
|
(package private) java.util.HashMap |
server_extensions
|
(package private) Session |
sess
|
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
java.lang.String |
canonicalPath(java.lang.String path)
Have the server canonicalize any given path name to an absolute path. |
private void |
checkHandleValidAndOpen(SFTPv3FileHandle handle)
|
void |
close()
Close this SFTP session. |
void |
closeFile(SFTPv3FileHandle handle)
Close a file. |
private void |
closeHandle(byte[] handle)
|
private byte[] |
createAttrs(SFTPv3FileAttributes attr)
|
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. |
private java.lang.String |
expandString(byte[] b,
int off,
int len)
|
private void |
expectStatusOKMessage(int id)
|
void |
fsetstat(SFTPv3FileHandle handle,
SFTPv3FileAttributes attr)
Modify the attributes of a file. |
SFTPv3FileAttributes |
fstat(SFTPv3FileHandle handle)
Retrieve the file attributes of an open file. |
private int |
generateNextRequestID()
|
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. |
private void |
init()
|
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. |
private byte[] |
openDirectory(java.lang.String path)
|
private SFTPv3FileHandle |
openFile(java.lang.String fileName,
int flags,
SFTPv3FileAttributes attr)
|
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. |
private SFTPv3FileAttributes |
readAttrs(TypesReader tr)
|
private void |
readBytes(byte[] buff,
int pos,
int len)
|
java.lang.String |
readLink(java.lang.String path)
Read the target of a symbolic link. |
private byte[] |
receiveMessage(int maxlen)
Read a message and guarantee that the contents is not larger than maxlen bytes. |
void |
rm(java.lang.String fileName)
Remove a file. |
void |
rmdir(java.lang.String dirName)
Remove an empty directory. |
private java.util.Vector |
scanDirectory(byte[] handle)
|
private void |
sendMessage(int type,
int requestId,
byte[] msg)
|
private void |
sendMessage(int type,
int requestId,
byte[] msg,
int off,
int len)
|
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. |
private SFTPv3FileAttributes |
statBoth(java.lang.String path,
int statMethod)
|
void |
write(SFTPv3FileHandle handle,
long fileOffset,
byte[] src,
int srcoff,
int len)
Write bytes to a file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
final Connection conn
final Session sess
final java.io.PrintStream debug
boolean flag_closed
java.io.InputStream is
java.io.OutputStream os
int protocol_version
java.util.HashMap server_extensions
int next_request_id
java.lang.String charsetName
Constructor Detail |
---|
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.IOException
public SFTPv3Client(Connection conn) throws java.io.IOException
conn
- The underlying SSH-2 connection to be used.
java.io.IOException
Method Detail |
---|
public 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.IOException
getCharset()
public java.lang.String getCharset()
null
if the platform's default charset is being used)setCharset(String)
private final void checkHandleValidAndOpen(SFTPv3FileHandle handle) throws java.io.IOException
java.io.IOException
private final void sendMessage(int type, int requestId, byte[] msg, int off, int len) throws java.io.IOException
java.io.IOException
private final void sendMessage(int type, int requestId, byte[] msg) throws java.io.IOException
java.io.IOException
private final void readBytes(byte[] buff, int pos, int len) throws java.io.IOException
java.io.IOException
private final byte[] receiveMessage(int maxlen) throws java.io.IOException
maxlen
bytes.
Note: receiveMessage(34000) actually means that the message may be up to 34004 bytes (the length attribute preceeding the contents is 4 bytes).
maxlen
-
java.io.IOException
private final int generateNextRequestID()
private final void closeHandle(byte[] handle) throws java.io.IOException
java.io.IOException
private SFTPv3FileAttributes readAttrs(TypesReader tr) throws java.io.IOException
java.io.IOException
public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws java.io.IOException
handle
- a SFTPv3FileHandle handle.
java.io.IOException
private SFTPv3FileAttributes statBoth(java.lang.String path, int statMethod) throws java.io.IOException
java.io.IOException
public SFTPv3FileAttributes stat(java.lang.String path) throws java.io.IOException
path
- See the comment
for the class for more details.
java.io.IOException
lstat(String)
public SFTPv3FileAttributes lstat(java.lang.String path) throws java.io.IOException
path
- See the comment
for the class for more details.
java.io.IOException
stat(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.IOException
private void expectStatusOKMessage(int id) throws java.io.IOException
java.io.IOException
public 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.IOException
public 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.IOException
public void createSymlink(java.lang.String src, java.lang.String target) throws java.io.IOException
src
- See the comment
for the class for more details.target
- See the comment
for the class for more details.
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.IOException
private final java.util.Vector scanDirectory(byte[] handle) throws java.io.IOException
java.io.IOException
private final byte[] openDirectory(java.lang.String path) throws java.io.IOException
java.io.IOException
private final java.lang.String expandString(byte[] b, int off, int len)
private void init() throws java.io.IOException
java.io.IOException
public 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.IOException
public 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.IOException
public void rm(java.lang.String fileName) throws java.io.IOException
fileName
- See the comment
for the class for more details.
java.io.IOException
public void rmdir(java.lang.String dirName) throws java.io.IOException
dirName
- See the comment
for the class for more details.
java.io.IOException
public void mv(java.lang.String oldPath, java.lang.String newPath) throws java.io.IOException
oldPath
- See the comment
for the class for more details.newPath
- See the comment
for the class for more details.
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.IOException
public SFTPv3FileHandle openFileRW(java.lang.String fileName) throws java.io.IOException
fileName
- See the comment
for the class for more details.
java.io.IOException
public 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.IOException
public 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.IOException
public 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.IOException
public 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.IOException
private byte[] createAttrs(SFTPv3FileAttributes attr)
private SFTPv3FileHandle openFile(java.lang.String fileName, int flags, SFTPv3FileAttributes attr) throws java.io.IOException
java.io.IOException
public 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 bytes
EOF
java.io.IOException
public 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.IOException
public void closeFile(SFTPv3FileHandle handle) throws java.io.IOException
handle
- a SFTPv3FileHandle handle
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |