Java tutorial
package ch.cyberduck.core.shared; /* * Copyright (c) 2002-2015 David Kocher. All rights reserved. * http://cyberduck.ch/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch */ import ch.cyberduck.core.Credentials; import ch.cyberduck.core.DisabledCancelCallback; import ch.cyberduck.core.DisabledConnectionCallback; import ch.cyberduck.core.DisabledHostKeyCallback; import ch.cyberduck.core.DisabledLoginCallback; import ch.cyberduck.core.DisabledPasswordCallback; import ch.cyberduck.core.DisabledPasswordStore; import ch.cyberduck.core.Host; import ch.cyberduck.core.Local; import ch.cyberduck.core.Path; import ch.cyberduck.core.features.Delete; import ch.cyberduck.core.features.Touch; import ch.cyberduck.core.io.BandwidthThrottle; import ch.cyberduck.core.io.DisabledStreamListener; import ch.cyberduck.core.io.StreamCopier; import ch.cyberduck.core.sftp.SFTPHomeDirectoryService; import ch.cyberduck.core.sftp.SFTPProtocol; import ch.cyberduck.core.sftp.SFTPReadFeature; import ch.cyberduck.core.sftp.SFTPSession; import ch.cyberduck.core.sftp.SFTPWriteFeature; import ch.cyberduck.core.transfer.TransferStatus; import ch.cyberduck.test.IntegrationTest; import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.experimental.categories.Category; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Collections; import java.util.EnumSet; import java.util.Random; import java.util.UUID; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotNull; @Category(IntegrationTest.class) public class DefaultDownloadFeatureTest { @Test public void testTransferAppend() throws Exception { final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch", new Credentials(System.getProperties().getProperty("sftp.user"), System.getProperties().getProperty("sftp.password"))); final SFTPSession session = new SFTPSession(host); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final Path test = new Path(new SFTPHomeDirectoryService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); session.getFeature(Touch.class).touch(test, new TransferStatus()); final byte[] content = new byte[39864]; new Random().nextBytes(content); { final TransferStatus status = new TransferStatus().length(content.length); final OutputStream out = new SFTPWriteFeature(session).write(test, status, new DisabledConnectionCallback()); assertNotNull(out); new StreamCopier(status, status).withLimit(new Long(content.length)) .transfer(new ByteArrayInputStream(content), out); out.close(); } final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); { final TransferStatus status = new TransferStatus().length(content.length / 2); new DefaultDownloadFeature(new SFTPReadFeature(session)).download(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback(), new DisabledPasswordCallback()); } { final TransferStatus status = new TransferStatus().length(content.length / 2).skip(content.length / 2) .append(true); new DefaultDownloadFeature(new SFTPReadFeature(session)).download(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback(), new DisabledPasswordCallback()); } final byte[] buffer = new byte[content.length]; final InputStream in = local.getInputStream(); IOUtils.readFully(in, buffer); in.close(); assertArrayEquals(content, buffer); final Delete delete = session.getFeature(Delete.class); delete.delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); session.close(); } @Test public void testTransferUnknownSize() throws Exception { final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch", new Credentials(System.getProperties().getProperty("sftp.user"), System.getProperties().getProperty("sftp.password"))); final SFTPSession session = new SFTPSession(host); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final Path test = new Path(new SFTPHomeDirectoryService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); session.getFeature(Touch.class).touch(test, new TransferStatus()); final byte[] content = new byte[1]; new Random().nextBytes(content); { final TransferStatus status = new TransferStatus().length(content.length); final OutputStream out = new SFTPWriteFeature(session).write(test, status, new DisabledConnectionCallback()); assertNotNull(out); new StreamCopier(status, status).withLimit(new Long(content.length)) .transfer(new ByteArrayInputStream(content), out); out.close(); } final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); { final TransferStatus status = new TransferStatus().length(-1L); new DefaultDownloadFeature(new SFTPReadFeature(session)).download(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback(), new DisabledPasswordCallback()); } final byte[] buffer = new byte[content.length]; final InputStream in = local.getInputStream(); IOUtils.readFully(in, buffer); in.close(); assertArrayEquals(content, buffer); final Delete delete = session.getFeature(Delete.class); delete.delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); session.close(); } }