PipedOutputStream pos = new PipedOutputStream();
_io.setOutputStream(pos);
_io.setInputStream(new PipedInputStream(pos, 32 * 1024)); // TODO make pipe size configurable
_io_in = _io.in;
if( _io_in == null ) {
throw new JSchException("Channel is down");
}
new RequestSftp().request(_session, this);
_buffer = new Buffer(_remoteMaxPacketSize);
_packet = new Packet(_buffer);
// send SSH_FXP_INIT
sendINIT();
// receive SSH_FXP_VERSION
readHeader();
if( _header.length > MAX_MSG_LENGTH ) {
throw new SftpException(SSH_FX_FAILURE, "Received message is too long: " + _header.length);
}
_serverVersion = _header.rid; // Retrieve version from header
if( _header.length > 0 ) {
_extensions = new HashMap<String,String>();
fill(_buffer, _header.length); // read in extension data
byte[] extensionName, extensionData;
while( _header.length > 0 ) {
extensionName = _buffer.getString();
extensionData = _buffer.getString();
_header.length -= 4 + extensionName.length + 4 + extensionData.length;
_extensions.put(Util.byte2str(extensionName), Util.byte2str(extensionData));
}
}
// Set local current working directory and home directory after connecting
_lcwd = new File(".").getCanonicalPath(); // TODO Should be configurable location
_home = Util.byte2str(_realpath(""), _fileEncoding);
_cwd = _home;
} catch(JSchException e) {
throw e;
} catch(Exception e) {
throw new JSchException("Failed to start ChannelSftp", e);
}
}