net.schmizz.sshj.sftp.RemoteFile Java Examples
The following examples show how to use
net.schmizz.sshj.sftp.RemoteFile.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SFTPTouchFeature.java From cyberduck with GNU General Public License v3.0 | 6 votes |
@Override public Path touch(final Path file, final TransferStatus status) throws BackgroundException { if(file.isFile()) { try { final FileAttributes attrs; if(Permission.EMPTY != status.getPermission()) { attrs = new FileAttributes.Builder().withPermissions(Integer.parseInt(status.getPermission().getMode(), 8)).build(); } else { attrs = FileAttributes.EMPTY; } final RemoteFile handle = session.sftp().open(file.getAbsolute(), EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE), attrs); handle.close(); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Cannot create file {0}", e, file); } } return new Path(file.getParent(), file.getName(), file.getType(), new SFTPAttributesFinderFeature(session).find(file)); }
Example #2
Source File: SFTPReadFeature.java From cyberduck with GNU General Public License v3.0 | 5 votes |
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final RemoteFile handle = session.sftp().open(file.getAbsolute(), EnumSet.of(OpenMode.READ)); final int maxUnconfirmedReads = this.getMaxUnconfirmedReads(status); if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return handle.new ReadAheadRemoteFileInputStream(maxUnconfirmedReads, status.getOffset()) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }; } catch(IOException e) { throw new SFTPExceptionMappingService().map("Download {0} failed", e, file); } }
Example #3
Source File: ChrootSFTPClient.java From datacollector with Apache License 2.0 | 5 votes |
public InputStream openForReading(String path) throws IOException { RemoteFile remoteFile = sftpClient.open(prependRoot(path)); if (disableReadAheadStream) { return SFTPStreamFactory.createInputStream(remoteFile); } else { return SFTPStreamFactory.createReadAheadInputStream(remoteFile, bufferSizeReadAheadStream); } }
Example #4
Source File: ChrootSFTPClient.java From datacollector with Apache License 2.0 | 5 votes |
public OutputStream openForWriting(String path) throws IOException { String toPath = prependRoot(path); // Create the toPath's parent dir(s) if they don't exist String toDir = Paths.get(toPath).getParent().toString(); sftpClient.mkdirs(toDir); RemoteFile remoteFile = sftpClient.open( toPath, EnumSet.of(OpenMode.WRITE, OpenMode.CREAT, OpenMode.TRUNC), FileAttributes.EMPTY ); return SFTPStreamFactory.createOutputStream(remoteFile); }
Example #5
Source File: SFTPStreamFactory.java From datacollector with Apache License 2.0 | 5 votes |
public static InputStream createInputStream(RemoteFile remoteFile) { return remoteFile.new RemoteFileInputStream() { private boolean isClosed = false; @Override public synchronized void close() throws IOException { if (!isClosed) { remoteFile.close(); isClosed = true; } } }; }
Example #6
Source File: SFTPStreamFactory.java From datacollector with Apache License 2.0 | 5 votes |
public static OutputStream createOutputStream(RemoteFile remoteFile) { return remoteFile.new RemoteFileOutputStream(0, MAX_UNCONFIRMED_READ_WRITES) { private boolean isClosed = false; @Override public synchronized void close() throws IOException { if (!isClosed) { remoteFile.close(); isClosed = true; } } }; }
Example #7
Source File: TestChrootSFTPClient.java From datacollector with Apache License 2.0 | 5 votes |
@Test public void testOpenForReading() throws Exception { File file = testFolder.newFile("file.txt"); String text = "hello"; Files.write(text.getBytes(Charset.forName("UTF-8")), file); Assert.assertEquals(text, Files.readFirstLine(file, Charset.forName("UTF-8"))); path = testFolder.getRoot().getAbsolutePath(); setupSSHD(path); SSHClient sshClient = createSSHClient(); for (ChrootSFTPClient sftpClient : getClientsWithEquivalentRoots(sshClient)) { // We can specify a file as either a relative path "file" or an absolute path "/file" and they should be // equivalent for (String p : new String[] { file.getName(), "/" + file.getName(), }) { InputStream is = sftpClient.openForReading(p); Assert.assertThat(is, instanceOf(RemoteFile.ReadAheadRemoteFileInputStream.class)); Assert.assertTrue(is.getClass().getName().startsWith(SFTPStreamFactory.class.getCanonicalName())); Assert.assertNotNull(is); Assert.assertEquals(text, IOUtils.toString(is, Charset.forName("UTF-8"))); is.close(); } } }
Example #8
Source File: TestChrootSFTPClient.java From datacollector with Apache License 2.0 | 5 votes |
@Test public void testOpenForReadingReadAheadInputStreamDisabled() throws Exception { File file = testFolder.newFile("file.txt"); String text = "hello"; Files.write(text.getBytes(Charset.forName("UTF-8")), file); Assert.assertEquals(text, Files.readFirstLine(file, Charset.forName("UTF-8"))); path = testFolder.getRoot().getAbsolutePath(); setupSSHD(path); SSHClient sshClient = createSSHClient(); final SFTPClient sftpClient = sshClient.newSFTPClient(); final ChrootSFTPClient chrootSFTPClient = createChrootSFTPClient( sftpClient, path, false, null, false, true ); final InputStream is = chrootSFTPClient.openForReading(file.getName()); try { Assert.assertThat(is, instanceOf(RemoteFile.RemoteFileInputStream.class)); Assert.assertTrue(is.getClass().getName().startsWith(SFTPStreamFactory.class.getCanonicalName())); Assert.assertNotNull(is); Assert.assertEquals(text, IOUtils.toString(is, Charset.forName("UTF-8"))); } finally { is.close(); } }
Example #9
Source File: SFTPWriteFeature.java From cyberduck with GNU General Public License v3.0 | 4 votes |
@Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { // No append flag. Otherwise the offset field of SSH_FXP_WRITE requests is ignored. flags = EnumSet.of(OpenMode.WRITE); } else { // Allocate offset flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { // A new file is created; if the file already exists, it is opened and truncated to preserve ownership of file. if(status.isExists()) { if(file.isSymbolicLink()) { // Workaround for #7327 session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } // Open stream at offset return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } }
Example #10
Source File: SFTPStreamFactory.java From datacollector with Apache License 2.0 | 4 votes |
public static InputStream createReadAheadInputStream(RemoteFile remoteFile) { return createReadAheadInputStream(remoteFile, -1); }