Java tutorial
package ch.cyberduck.core.cryptomator; /* * Copyright (c) 2002-2017 iterate GmbH. All rights reserved. * https://cyberduck.io/ * * 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 3 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. */ import ch.cyberduck.core.AlphanumericRandomStringService; 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.Path; import ch.cyberduck.core.VersionId; import ch.cyberduck.core.b2.B2DeleteFeature; import ch.cyberduck.core.b2.B2FindFeature; import ch.cyberduck.core.b2.B2LargeUploadWriteFeature; import ch.cyberduck.core.b2.B2Protocol; import ch.cyberduck.core.b2.B2ReadFeature; import ch.cyberduck.core.b2.B2Session; import ch.cyberduck.core.cryptomator.features.CryptoDeleteFeature; import ch.cyberduck.core.cryptomator.features.CryptoFindFeature; import ch.cyberduck.core.cryptomator.features.CryptoReadFeature; import ch.cyberduck.core.cryptomator.features.CryptoWriteFeature; import ch.cyberduck.core.cryptomator.random.RandomNonceGenerator; import ch.cyberduck.core.features.Delete; import ch.cyberduck.core.io.StreamCopier; import ch.cyberduck.core.transfer.TransferStatus; import ch.cyberduck.core.vault.DefaultVaultRegistry; import ch.cyberduck.core.vault.VaultCredentials; import ch.cyberduck.test.IntegrationTest; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomUtils; import org.cryptomator.cryptolib.api.Cryptor; import org.cryptomator.cryptolib.api.FileHeader; 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.Arrays; import java.util.EnumSet; import static org.junit.Assert.*; @Category(IntegrationTest.class) public class B2LargeUploadWriteFeatureTest { @Test public void testWrite() throws Exception { final B2Session session = new B2Session(new Host(new B2Protocol(), new B2Protocol().getDefaultHostname(), new Credentials(System.getProperties().getProperty("b2.user"), System.getProperties().getProperty("b2.key")))); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final Path container = new Path("test-cyberduck", EnumSet.of(Path.Type.volume, Path.Type.directory)); final CryptoVault cryptomator = new CryptoVault(new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new DisabledPasswordStore()); final Path vault = cryptomator.create(session, null, new VaultCredentials("test")); session.withRegistry( new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator)); final CryptoWriteFeature feature = new CryptoWriteFeature<VersionId>(session, new B2LargeUploadWriteFeature(session), cryptomator); final TransferStatus writeStatus = new TransferStatus(); final Cryptor cryptor = cryptomator.getCryptor(); final FileHeader header = cryptor.fileHeaderCryptor().create(); writeStatus.setHeader(cryptor.fileHeaderCryptor().encryptHeader(header)); writeStatus.setNonces(new RandomNonceGenerator()); writeStatus.setLength(-1L); final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)); final OutputStream out = feature.write(test, writeStatus, new DisabledConnectionCallback()); final byte[] content = RandomUtils.nextBytes(6 * 1024 * 1024); final ByteArrayInputStream in = new ByteArrayInputStream(content); final TransferStatus progress = new TransferStatus(); new StreamCopier(new TransferStatus(), progress).transfer(in, out); assertEquals(content.length, progress.getOffset()); assertTrue(new CryptoFindFeature(session, new B2FindFeature(session), cryptomator).find(test)); final byte[] compare = new byte[content.length]; final InputStream stream = new CryptoReadFeature(session, new B2ReadFeature(session), cryptomator) .read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback()); IOUtils.readFully(stream, compare); stream.close(); assertArrayEquals(content, compare); new CryptoDeleteFeature(session, new B2DeleteFeature(session), cryptomator) .delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback()); session.close(); } }