private static void transfer( final Session session, InputStream in, long length, final String remoteFilePath, String fileMode, Set<String> existingDirs) throws Throwable { String remoteDirPath = PathUtil.getParentDirectory(remoteFilePath); if (existingDirs == null || !existingDirs.contains(remoteDirPath)) { session.mkdir(remoteDirPath, true, "0755"); if (existingDirs != null) { existingDirs.add(remoteDirPath); } } if (length < 0 && in instanceof LongInputStream) { length = ((LongInputStream) in).length(); } if (length < 0) { File tf = PluginTask.createTemporaryFile(); OutputStream tfos = new BufferedOutputStream(new FileOutputStream(tf)); try { StreamCopy.copy(in, tfos); } finally { tfos.close(); in.close(); } in = PluginTask.deleteOnCloseInputStream(tf); length = tf.length(); } OutputStream out = null; try { out = session.scpPut(remoteFilePath, length, fileMode); StreamCopy.copy(in, out); } finally { if (out != null) { out.close(); } in.close(); } }