Class AbstractSftpClient

    • Field Detail

      • parsedExtensionsHolder

        private final java.util.concurrent.atomic.AtomicReference<java.util.Map<java.lang.String,​java.lang.Object>> parsedExtensionsHolder
    • Constructor Detail

    • Method Detail

      • getParsedServerExtensions

        protected java.util.Map<java.lang.String,​java.lang.Object> getParsedServerExtensions()
      • getParsedServerExtensions

        protected java.util.Map<java.lang.String,​java.lang.Object> getParsedServerExtensions​(java.util.Map<java.lang.String,​byte[]> extensions)
      • getReferencedName

        protected java.lang.String getReferencedName​(int cmd,
                                                     Buffer buf,
                                                     int nameIndex)
        Parameters:
        cmd - The command that was sent whose response contains the name to be decoded
        buf - The Buffer containing the encoded name
        nameIndex - The zero-based order of the requested names for the command - e.g.,
        • When listing a directory's contents each successive name will have an increasing index.
        • For SFTP version 3, when retrieving a single name, short name will have index=0 and the long one index=1.
        Returns:
        The decoded referenced name
      • putReferencedName

        protected <B extends Buffer> B putReferencedName​(int cmd,
                                                         B buf,
                                                         java.lang.String name,
                                                         int nameIndex)
        Type Parameters:
        B - Type of Buffer being updated
        Parameters:
        cmd - The command for which this name is being added
        buf - The buffer instance to update
        name - The name to place in the buffer
        nameIndex - The zero-based order of the name for the specific command if more than one name required - e.g., rename, link/symbolic link
        Returns:
        The updated buffer
      • checkResponseStatus

        protected void checkResponseStatus​(int cmd,
                                           Buffer buffer)
                                    throws java.io.IOException
        Checks if the incoming response is an SSH_FXP_STATUS one, and if so whether the substatus is SSH_FX_OK.
        Parameters:
        cmd - The sent command opcode
        buffer - The received response Buffer
        Throws:
        java.io.IOException - If response does not carry a status or carries a bad status code
        See Also:
        checkResponseStatus(int, int, int, String, String)
      • checkResponseStatus

        protected void checkResponseStatus​(int cmd,
                                           int id,
                                           int substatus,
                                           java.lang.String msg,
                                           java.lang.String lang)
                                    throws java.io.IOException
        Parameters:
        cmd - The sent command opcode
        id - The request id
        substatus - The sub-status value
        msg - The message
        lang - The language
        Throws:
        java.io.IOException - if the sub-status is not SSH_FX_OK
        See Also:
        throwStatusException(int, int, int, String, String)
      • throwStatusException

        protected void throwStatusException​(int cmd,
                                            int id,
                                            int substatus,
                                            java.lang.String msg,
                                            java.lang.String lang)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • checkHandleResponse

        protected byte[] checkHandleResponse​(int cmd,
                                             Buffer buffer)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnexpectedHandlePacket

        protected byte[] handleUnexpectedHandlePacket​(int cmd,
                                                      int id,
                                                      int type,
                                                      int length,
                                                      Buffer buffer)
                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • checkAttributesResponse

        protected SftpClient.Attributes checkAttributesResponse​(int cmd,
                                                                Buffer buffer)
                                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnexpectedAttributesPacket

        protected SftpClient.Attributes handleUnexpectedAttributesPacket​(int cmd,
                                                                         int id,
                                                                         int type,
                                                                         int length,
                                                                         Buffer buffer)
                                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • checkOneNameResponse

        protected java.lang.String checkOneNameResponse​(int cmd,
                                                        Buffer buffer)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnknownOneNamePacket

        protected java.lang.String handleUnknownOneNamePacket​(int cmd,
                                                              int id,
                                                              int type,
                                                              int length,
                                                              Buffer buffer)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • readAttributes

        protected SftpClient.Attributes readAttributes​(int cmd,
                                                       Buffer buffer,
                                                       java.util.concurrent.atomic.AtomicInteger nameIndex)
                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public void close​(SftpClient.Handle handle)
                   throws java.io.IOException
        Description copied from interface: SftpClient
        Close the handle obtained from one of the open methods
        Specified by:
        close in interface SftpClient
        Parameters:
        handle - The Handle to close
        Throws:
        java.io.IOException - If failed to execute
      • remove

        public void remove​(java.lang.String path)
                    throws java.io.IOException
        Specified by:
        remove in interface SftpClient
        Parameters:
        path - The remote path to remove
        Throws:
        java.io.IOException - If failed to execute
      • rename

        public void rename​(java.lang.String oldPath,
                           java.lang.String newPath,
                           java.util.Collection<SftpClient.CopyMode> options)
                    throws java.io.IOException
        Specified by:
        rename in interface SftpClient
        Throws:
        java.io.IOException
      • read

        public int read​(SftpClient.Handle handle,
                        long fileOffset,
                        byte[] dst,
                        int dstOffset,
                        int len,
                        java.util.concurrent.atomic.AtomicReference<java.lang.Boolean> eofSignalled)
                 throws java.io.IOException
        Description copied from interface: SftpClient
        Reads data from the open (file) handle
        Specified by:
        read in interface SftpClient
        Parameters:
        handle - The file SftpClient.Handle to read from
        fileOffset - The file offset to read from
        dst - The destination buffer
        dstOffset - Offset in destination buffer to place the read data
        len - Available destination buffer size to read
        eofSignalled - If not null then upon return holds a value indicating whether EOF was reached due to the read. If null indicator value then this indication is not available
        Returns:
        Number of read bytes - -1 if EOF reached
        Throws:
        java.io.IOException - If failed to read the data
        See Also:
        SFTP v6 - section 9.3
      • checkData

        protected int checkData​(int cmd,
                                Buffer request,
                                int dstOffset,
                                byte[] dst,
                                java.util.concurrent.atomic.AtomicReference<java.lang.Boolean> eofSignalled)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • checkDataResponse

        protected int checkDataResponse​(int cmd,
                                        Buffer buffer,
                                        int dstoff,
                                        byte[] dst,
                                        java.util.concurrent.atomic.AtomicReference<java.lang.Boolean> eofSignalled)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnknownDataPacket

        protected int handleUnknownDataPacket​(int cmd,
                                              int id,
                                              int type,
                                              int length,
                                              Buffer buffer)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • errorData

        public void errorData​(byte[] buf,
                              int start,
                              int len)
                       throws java.io.IOException
        Description copied from interface: SftpErrorDataHandler
        Receive binary data from server error stream
        Specified by:
        errorData in interface SftpErrorDataHandler
        Parameters:
        buf - The buffer of the incoming data
        start - Offset in buffer to read the data
        len - Available data in buffer
        Throws:
        java.io.IOException - If failed to receive incoming data
      • write

        public void write​(SftpClient.Handle handle,
                          long fileOffset,
                          byte[] src,
                          int srcOffset,
                          int len)
                   throws java.io.IOException
        Description copied from interface: SftpClient
        Write data to (open) file handle
        Specified by:
        write in interface SftpClient
        Parameters:
        handle - The file SftpClient.Handle
        fileOffset - Zero-based offset to write in file
        src - Data buffer
        srcOffset - Offset of valid data in buffer
        len - Number of bytes to write
        Throws:
        java.io.IOException - If failed to write the data
      • mkdir

        public void mkdir​(java.lang.String path)
                   throws java.io.IOException
        Description copied from interface: SftpClient
        Create remote directory
        Specified by:
        mkdir in interface SftpClient
        Parameters:
        path - Remote directory path
        Throws:
        java.io.IOException - If failed to execute
      • rmdir

        public void rmdir​(java.lang.String path)
                   throws java.io.IOException
        Description copied from interface: SftpClient
        Remove remote directory
        Specified by:
        rmdir in interface SftpClient
        Parameters:
        path - Remote directory path
        Throws:
        java.io.IOException - If failed to execute
      • openDir

        public SftpClient.CloseableHandle openDir​(java.lang.String path)
                                           throws java.io.IOException
        Description copied from interface: SftpClient
        Obtain a handle for a directory
        Specified by:
        openDir in interface SftpClient
        Parameters:
        path - Remote directory path
        Returns:
        The associated directory SftpClient.Handle
        Throws:
        java.io.IOException - If failed to execute
      • readDir

        public java.util.List<SftpClient.DirEntry> readDir​(SftpClient.Handle handle,
                                                           java.util.concurrent.atomic.AtomicReference<java.lang.Boolean> eolIndicator)
                                                    throws java.io.IOException
        Specified by:
        readDir in interface SftpClient
        Parameters:
        handle - Directory SftpClient.Handle to read from
        eolIndicator - An indicator that can be used to get information whether end of list has been reached - ignored if null. Upon return, set value indicates whether all entries have been exhausted - a null value means that this information cannot be provided and another call to readDir is necessary in order to verify that no more entries are pending
        Returns:
        A List of entries - null to indicate no more entries
        Throws:
        java.io.IOException - If failed to access the remote site
        See Also:
        SFTP v6 - section 9.4
      • checkDirResponse

        protected java.util.List<SftpClient.DirEntry> checkDirResponse​(int cmd,
                                                                       Buffer buffer,
                                                                       java.util.concurrent.atomic.AtomicReference<java.lang.Boolean> eolIndicator)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • validateIncomingResponse

        protected void validateIncomingResponse​(int cmd,
                                                int id,
                                                int type,
                                                int length,
                                                Buffer buffer)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnknownDirListingPacket

        protected java.util.List<SftpClient.DirEntry> handleUnknownDirListingPacket​(int cmd,
                                                                                    int id,
                                                                                    int type,
                                                                                    int length,
                                                                                    Buffer buffer)
                                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • handleUnexpectedPacket

        protected java.io.IOException handleUnexpectedPacket​(int cmd,
                                                             int expected,
                                                             int id,
                                                             int type,
                                                             int length,
                                                             Buffer buffer)
                                                      throws java.io.IOException
        Parameters:
        cmd - The initial command sent
        expected - The expected packet type
        id - The reported identifier
        type - The reported SFTP response type
        length - The packet length
        buffer - The Buffer after reading from it whatever data led to this call
        Returns:
        The exception to throw - if null then implementor assumed to handle the exception internal. Otherwise, the exception is re-thrown
        Throws:
        java.io.IOException - If failed to handle the exception internally
      • canonicalPath

        public java.lang.String canonicalPath​(java.lang.String path)
                                       throws java.io.IOException
        Description copied from interface: SftpClient
        The effective "normalized" remote path
        Specified by:
        canonicalPath in interface SftpClient
        Parameters:
        path - The requested path - may be relative, and/or contain dots - e.g., ".", "..", "./foo", "../bar"
        Returns:
        The effective "normalized" remote path
        Throws:
        java.io.IOException - If failed to execute
      • stat

        public SftpClient.Attributes stat​(java.lang.String path)
                                   throws java.io.IOException
        Description copied from interface: SftpClient
        Retrieve remote path meta-data - follow symbolic links if encountered
        Specified by:
        stat in interface SftpClient
        Parameters:
        path - The remote path
        Returns:
        The associated SftpClient.Attributes
        Throws:
        java.io.IOException - If failed to execute
      • lstat

        public SftpClient.Attributes lstat​(java.lang.String path)
                                    throws java.io.IOException
        Description copied from interface: SftpClient
        Retrieve remote path meta-data - do not follow symbolic links
        Specified by:
        lstat in interface SftpClient
        Parameters:
        path - The remote path
        Returns:
        The associated SftpClient.Attributes
        Throws:
        java.io.IOException - If failed to execute
      • setStat

        public void setStat​(java.lang.String path,
                            SftpClient.Attributes attributes)
                     throws java.io.IOException
        Description copied from interface: SftpClient
        Update remote node meta-data
        Specified by:
        setStat in interface SftpClient
        Parameters:
        path - The remote path
        attributes - The SftpClient.Attributes to update
        Throws:
        java.io.IOException - If failed to execute
      • readLink

        public java.lang.String readLink​(java.lang.String path)
                                  throws java.io.IOException
        Description copied from interface: SftpClient
        Retrieve target of a link
        Specified by:
        readLink in interface SftpClient
        Parameters:
        path - Remote path that represents a link
        Returns:
        The link target
        Throws:
        java.io.IOException - If failed to execute
      • link

        public void link​(java.lang.String linkPath,
                         java.lang.String targetPath,
                         boolean symbolic)
                  throws java.io.IOException
        Description copied from interface: SftpClient
        Create a link
        Specified by:
        link in interface SftpClient
        Parameters:
        linkPath - The link location
        targetPath - The referenced target by the link
        symbolic - If true then make this a symbolic link, otherwise a hard one
        Throws:
        java.io.IOException - If failed to execute
      • lock

        public void lock​(SftpClient.Handle handle,
                         long offset,
                         long length,
                         int mask)
                  throws java.io.IOException
        Specified by:
        lock in interface SftpClient
        Throws:
        java.io.IOException
      • unlock

        public void unlock​(SftpClient.Handle handle,
                           long offset,
                           long length)
                    throws java.io.IOException
        Specified by:
        unlock in interface SftpClient
        Throws:
        java.io.IOException
      • listDir

        public java.lang.Iterable<SftpClient.DirEntry> listDir​(SftpClient.Handle handle)
                                                        throws java.io.IOException
        Specified by:
        listDir in interface SftpClient
        Parameters:
        handle - A directory SftpClient.Handle
        Returns:
        An Iterable that can be used to iterate over all the directory entries (like SftpClient.readDir(String)). Note: the iterable instance is not re-usable - i.e., files can be iterated only once
        Throws:
        java.io.IOException - If failed to access the directory
      • openRemoteFileChannel

        public java.nio.channels.FileChannel openRemoteFileChannel​(java.lang.String path,
                                                                   java.util.Collection<SftpClient.OpenMode> modes)
                                                            throws java.io.IOException
        Description copied from interface: SftpClient
        Opens an FileChannel on the specified remote path
        Specified by:
        openRemoteFileChannel in interface SftpClient
        Parameters:
        path - The remote path
        modes - The access mode(s) - if null/empty then the SftpClient.DEFAULT_CHANNEL_MODES are used
        Returns:
        The open FileChannel - Note: do not close this owner client instance until the channel is no longer needed since it uses the client for providing the channel's functionality.
        Throws:
        java.io.IOException - If failed to open the channel
        See Also:
        Channels.newInputStream(java.nio.channels.ReadableByteChannel), Channels.newOutputStream(java.nio.channels.WritableByteChannel)
      • read

        public java.io.InputStream read​(java.lang.String path,
                                        int bufferSize,
                                        java.util.Collection<SftpClient.OpenMode> mode)
                                 throws java.io.IOException
        Description copied from interface: SftpClient
        Read a remote file's data via an input stream
        Specified by:
        read in interface SftpClient
        Parameters:
        path - The remote file path
        bufferSize - The internal read buffer size
        mode - The remote file SftpClient.OpenModes
        Returns:
        An InputStream for reading the remote file data
        Throws:
        java.io.IOException - If failed to execute
      • write

        public java.io.OutputStream write​(java.lang.String path,
                                          int bufferSize,
                                          java.util.Collection<SftpClient.OpenMode> mode)
                                   throws java.io.IOException
        Description copied from interface: SftpClient
        Write to a remote file via an output stream
        Specified by:
        write in interface SftpClient
        Parameters:
        path - The remote file path
        bufferSize - The internal write buffer size
        mode - The remote file SftpClient.OpenModes
        Returns:
        An OutputStream for writing the data
        Throws:
        java.io.IOException - If failed to execute
      • getReadBufferSize

        protected int getReadBufferSize()
      • getWriteBufferSize

        protected int getWriteBufferSize()