Java Code Examples for org.apache.commons.io.IOUtils.readFully()

The following are Jave code examples for showing how to use readFully() of the org.apache.commons.io.IOUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: cyberduck   File: OneDriveWriteFeatureTest.java   View Source Code Vote up 7 votes
@Test
public void testWriteUmlautZeroLength() throws Exception {
    final OneDriveWriteFeature feature = new OneDriveWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final Path file = new Path(container, String.format("%sä", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 2
Project: cyberduck   File: OneDriveBufferWriteFeatureTest.java   View Source Code Vote up 6 votes
@Test
public void testWriteUnknownLength() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(5 * 1024 * 1024);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    new StreamCopier(status, status).transfer(in, out);
    in.close();
    out.flush();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 3
Project: cyberduck   File: OneDriveBufferWriteFeatureTest.java   View Source Code Vote up 6 votes
@Test
public void testWriteZeroLength() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    new StreamCopier(status, status).transfer(in, out);
    in.close();
    out.flush();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 4
Project: cyberduck   File: OneDriveWriteFeatureTest.java   View Source Code Vote up 6 votes
@Test
public void testWrite() throws Exception {
    final OneDriveWriteFeature feature = new OneDriveWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(5 * 1024 * 1024);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    final byte[] buffer = new byte[32 * 1024];
    assertEquals(content.length, IOUtils.copyLarge(in, out, buffer));
    in.close();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 5
Project: ditb   File: CellCodecWithTags.java   View Source Code Vote up 6 votes
protected Cell parseCell() throws IOException {
  byte[] row = readByteArray(this.in);
  byte[] family = readByteArray(in);
  byte[] qualifier = readByteArray(in);
  byte[] longArray = new byte[Bytes.SIZEOF_LONG];
  IOUtils.readFully(this.in, longArray);
  long timestamp = Bytes.toLong(longArray);
  byte type = (byte) this.in.read();
  byte[] value = readByteArray(in);
  byte[] tags = readByteArray(in);
  // Read memstore version
  byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
  IOUtils.readFully(this.in, memstoreTSArray);
  long memstoreTS = Bytes.toLong(memstoreTSArray);
  return CellUtil.createCell(row, family, qualifier, timestamp, type, value, tags, memstoreTS);
}
 
Example 6
Project: cyberduck   File: MantaWriteFeatureTest.java   View Source Code Vote up 6 votes
@Test
public void testWrite() throws Exception {
    final MantaWriteFeature feature = new MantaWriteFeature(session);
    final Path container = new MantaDirectoryFeature(session).mkdir(randomDirectory(), "", new TransferStatus());
    final byte[] content = RandomUtils.nextBytes(5 * 1024 * 1024);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    final byte[] buffer = new byte[32 * 1024];
    assertEquals(content.length, IOUtils.copyLarge(in, out, buffer));
    in.close();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new MantaReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new MantaDeleteFeature(session).delete(Collections.singletonList(container), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 7
Project: cyberduck   File: CryptoOutputStreamTest.java   View Source Code Vote up 6 votes
@Test
public void testWriteLargeChunk() throws Exception {
    final CryptoVault vault = this.getVault();
    final ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    final FileHeader header = vault.getCryptor().fileHeaderCryptor().create();
    final CryptoOutputStream<?> stream = new CryptoOutputStream<>(new StatusOutputStream<Void>(cipherText) {
        @Override
        public Void getStatus() throws BackgroundException {
            return null;
        }
    }, vault.getCryptor(), header, new RandomNonceGenerator(), 0);

    final byte[] cleartext = RandomUtils.nextBytes(vault.getCryptor().fileContentCryptor().cleartextChunkSize() + 1);
    stream.write(cleartext, 0, cleartext.length);
    stream.close();

    final byte[] read = new byte[cleartext.length];
    final CryptoInputStream cryptoInputStream = new CryptoInputStream(new ByteArrayInputStream(cipherText.toByteArray()), vault.getCryptor(), header, 0);
    IOUtils.readFully(cryptoInputStream, read);
    cryptoInputStream.close();

    assertArrayEquals(cleartext, read);
}
 
Example 8
Project: cyberduck   File: SDSWriteFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testReadWrite() throws Exception {
    final Host host = new Host(new SDSProtocol(), "duck.ssp-europe.eu", new Credentials(
            System.getProperties().getProperty("sds.user"), System.getProperties().getProperty("sds.key")
    ));
    final SDSSession session = new SDSSession(host, new DisabledX509TrustManager(), new DefaultX509KeyManager());
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path room = new SDSDirectoryFeature(session).mkdir(
            new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume)), null, new TransferStatus());
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final SDSWriteFeature writer = new SDSWriteFeature(session);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    final VersionId version = out.getStatus();
    assertNotNull(version);
    assertTrue(new DefaultFindFeature(session).find(test));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new SDSReadFeature(session).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new SDSDeleteFeature(session).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 9
Project: gerrit-plugin   File: GerritWebHook.java   View Source Code Vote up 5 votes
private GerritProjectEvent getBody(HttpServletRequest req) throws IOException {
  char[] body = new char[req.getContentLength()];
  try (InputStreamReader is = new InputStreamReader(req.getInputStream())) {
    IOUtils.readFully(is, body);
    String bodyString = new String(body);
    log.info("Received body: " + bodyString);
    return gson.fromJson(bodyString, GerritProjectEvent.class);
  }
}
 
Example 10
Project: cyberduck   File: SDSMultipartWriteFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testReadWrite() throws Exception {
    final Host host = new Host(new SDSProtocol(), "duck.ssp-europe.eu", new Credentials(
            System.getProperties().getProperty("sds.user"), System.getProperties().getProperty("sds.key")
    ));
    final SDSSession session = new SDSSession(host, new DisabledX509TrustManager(), new DefaultX509KeyManager());
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path room = new SDSDirectoryFeature(session).mkdir(
            new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume)), null, new TransferStatus());
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final SDSMultipartWriteFeature writer = new SDSMultipartWriteFeature(session);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    final VersionId version = out.getStatus();
    assertNotNull(version);
    assertTrue(new DefaultFindFeature(session).find(test));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new SDSReadFeature(session).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new SDSDeleteFeature(session).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 11
Project: ditb   File: CellCodecWithTags.java   View Source Code Vote up 5 votes
/**
 * @return Byte array read from the stream.
 * @throws IOException
 */
private byte[] readByteArray(final InputStream in) throws IOException {
  byte[] intArray = new byte[Bytes.SIZEOF_INT];
  IOUtils.readFully(in, intArray);
  int length = Bytes.toInt(intArray);
  byte[] bytes = new byte[length];
  IOUtils.readFully(in, bytes);
  return bytes;
}
 
Example 12
Project: cyberduck   File: B2LargeUploadWriteFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testWriteZeroLength() 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(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final B2LargeUploadWriteFeature feature = new B2LargeUploadWriteFeature(session);
    final Path container = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final StatusOutputStream<VersionId> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNotNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new B2ReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new B2DeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 13
Project: cyberduck   File: S3MultipartWriteFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testWriteZeroLength() throws Exception {
    final S3Session session = new S3Session(
            new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
                    new Credentials(
                            System.getProperties().getProperty("s3.key"), System.getProperties().getProperty("s3.secret")
                    )));
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final S3MultipartWriteFeature feature = new S3MultipartWriteFeature(session);
    final Path container = new Path("test-eu-central-1-cyberduck", EnumSet.of(Path.Type.volume));
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<List<MultipartPart>> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNotNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new S3ReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new S3DefaultDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example 14
Project: cyberduck   File: SwiftLargeUploadWriteFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testWrite() throws Exception {
    final Host host = new Host(new SwiftProtocol(), "identity.api.rackspacecloud.com", new Credentials(
            System.getProperties().getProperty("rackspace.key"), System.getProperties().getProperty("rackspace.secret")
    ));
    final SwiftSession session = new SwiftSession(host);
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path home = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.volume, Path.Type.directory));
    home.attributes().setRegion("DFW");
    final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
    final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final CryptoVault cryptomator = new CryptoVault(vault, new DisabledPasswordStore());
    cryptomator.create(session, null, new VaultCredentials("test"));
    session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
    final SwiftRegionService regionService = new SwiftRegionService(session);
    final CryptoWriteFeature feature = new CryptoWriteFeature<List<StorageObject>>(session, new SwiftLargeUploadWriteFeature(session, regionService,
            new SwiftSegmentService(session, ".segments-test/")), 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 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 SwiftFindFeature(session), cryptomator).find(test));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new CryptoReadFeature(session, new SwiftReadFeature(session, regionService), cryptomator).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new CryptoDeleteFeature(session, new SwiftDeleteFeature(session), cryptomator).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 15
Project: cyberduck   File: IRODSUploadFeatureTest.java   View Source Code Vote up 5 votes
@Test
public void testWrite() throws Exception {
    final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
    final Profile profile = new ProfilePlistReader(factory).read(
            new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
    final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(
            System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")
    ));

    final IRODSSession session = new IRODSSession(host);
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final int length = 32770;
    final byte[] content = RandomUtils.nextBytes(length);
    final OutputStream out = local.getOutputStream(false);
    IOUtils.write(content, out);
    out.close();
    final Checksum checksum;
    final Path test = new Path(new IRODSHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final TransferStatus status = new TransferStatus().length(content.length);
    checksum = new IRODSUploadFeature(session).upload(
            test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
            status,
            new DisabledConnectionCallback());
    assertTrue(status.isComplete());
    assertEquals(content.length, status.getOffset());
    assertEquals(checksum, new MD5ChecksumCompute().compute(new FileInputStream(local.getAbsolute()), status));
    final byte[] buffer = new byte[content.length];
    final InputStream in = new IRODSReadFeature(session).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    new IRODSDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 16
Project: cyberduck   File: SwiftLargeObjectUploadFeatureTest.java   View Source Code Vote up 4 votes
@Test
public void testAppendNoPartCompleted() throws Exception {
    final SwiftSession session = new SwiftSession(
            new Host(new SwiftProtocol(), "identity.api.rackspacecloud.com",
                    new Credentials(
                            System.getProperties().getProperty("rackspace.key"), System.getProperties().getProperty("rackspace.secret"))));
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
    container.attributes().setRegion("DFW");
    final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final int length = 2 * 1024 * 1024;
    final byte[] content = RandomUtils.nextBytes(length);
    IOUtils.write(content, local.getOutputStream(false));
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final AtomicBoolean interrupt = new AtomicBoolean();
    try {
        new SwiftLargeObjectUploadFeature(session, new SwiftRegionService(session), new SwiftWriteFeature(session, new SwiftRegionService(session)),
                1 * 1024L * 1024L, 1).upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener() {
            long count;

            @Override
            public void sent(final long bytes) {
                count += bytes;
                if(count >= 32768L) {
                    throw new RuntimeException();
                }
            }
        }, status, new DisabledLoginCallback());
    }
    catch(BackgroundException e) {
        // Expected
        interrupt.set(true);
    }
    assertTrue(interrupt.get());
    assertEquals(0L, status.getOffset(), 0L);
    assertFalse(status.isComplete());

    final TransferStatus append = new TransferStatus().append(true).length(content.length);
    new SwiftLargeObjectUploadFeature(session, new SwiftRegionService(session), new SwiftWriteFeature(session, new SwiftRegionService(session)),
            1 * 1024L * 1024L, 1).upload(test, local,
            new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), append,
            new DisabledLoginCallback());
    assertTrue(new SwiftFindFeature(session).find(test));
    assertEquals(content.length, new SwiftAttributesFinderFeature(session).find(test).getSize());
    assertEquals(content.length, append.getOffset(), 0L);
    assertTrue(append.isComplete());
    final byte[] buffer = new byte[content.length];
    final InputStream in = new SwiftReadFeature(session, new SwiftRegionService(session)).read(test, new TransferStatus(), new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    new SwiftDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    local.delete();
    session.close();
}
 
Example 17
Project: cyberduck   File: IRODSWriteFeatureTest.java   View Source Code Vote up 4 votes
@Test
public void testWriteConcurrent() throws Exception {
    final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
    final Profile profile = new ProfilePlistReader(factory).read(
            new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
    final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(
            System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")
    ));

    final IRODSSession session1 = new IRODSSession(host);
    session1.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session1.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());

    final IRODSSession session2 = new IRODSSession(host);
    session2.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session2.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());

    final Path test1 = new Path(new IRODSHomeFinderService(session1).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Path test2 = new Path(new IRODSHomeFinderService(session2).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));

    final byte[] content = RandomUtils.nextBytes(68400);

    final OutputStream out1 = new IRODSWriteFeature(session1).write(test1, new TransferStatus().append(false).length(content.length), new DisabledConnectionCallback());
    final OutputStream out2 = new IRODSWriteFeature(session2).write(test2, new TransferStatus().append(false).length(content.length), new DisabledConnectionCallback());
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out2);
    // Error code received from iRODS:-23000
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out1);

    {
        final InputStream in1 = session1.getFeature(Read.class).read(test1, new TransferStatus(), new DisabledConnectionCallback());
        final byte[] buffer1 = new byte[content.length];
        IOUtils.readFully(in1, buffer1);
        in1.close();
        assertArrayEquals(content, buffer1);
    }
    {
        final InputStream in2 = session2.getFeature(Read.class).read(test2, new TransferStatus(), new DisabledConnectionCallback());
        final byte[] buffer2 = new byte[content.length];
        IOUtils.readFully(in2, buffer2);
        in2.close();
        assertArrayEquals(content, buffer2);
    }
    session1.close();
    session2.close();
}
 
Example 18
Project: cyberduck   File: S3MultipartUploadServiceTest.java   View Source Code Vote up 4 votes
@Test
public void testAppendSecondPart() throws Exception {
    final S3Session session = new S3Session(
            new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
                    new Credentials(
                            System.getProperties().getProperty("s3.key"), System.getProperties().getProperty("s3.secret")
                    )));
    session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path container = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final String name = UUID.randomUUID().toString();
    final Path test = new Path(container, name, EnumSet.of(Path.Type.file));
    final int length = 12 * 1024 * 1024;
    final byte[] content = RandomUtils.nextBytes(length);
    Local local = new Local(System.getProperty("java.io.tmpdir"), name);
    IOUtils.write(content, local.getOutputStream(false));
    final AtomicBoolean started = new AtomicBoolean();
    final TransferStatus status = new TransferStatus() {
        @Override
        public void progress(long bytes) {
            super.progress(bytes);
            started.set(true);
        }
    };
    status.setLength(content.length);
    final AtomicBoolean interrupt = new AtomicBoolean();
    try {
        new S3MultipartUploadService(session, new S3WriteFeature(session, new S3DisabledMultipartService()), 10L * 1024L * 1024L, 1).upload(test, new Local(System.getProperty("java.io.tmpdir"), name) {
                    @Override
                    public InputStream getInputStream() throws AccessDeniedException {
                        return new CountingInputStream(super.getInputStream()) {
                            @Override
                            protected void beforeRead(int n) throws IOException {
                                if(started.get()) {
                                    if(this.getByteCount() >= 11L * 1024L * 1024L) {
                                        throw new IOException();
                                    }
                                }
                            }
                        };
                    }
                },
                new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status,
                new DisabledLoginCallback());
    }
    catch(BackgroundException e) {
        // Expected
        interrupt.set(true);
    }
    assertTrue(interrupt.get());
    assertEquals(10L * 1024L * 1024L, status.getOffset(), 0L);
    assertFalse(status.isComplete());
    assertFalse(new S3FindFeature(session).find(test));

    final TransferStatus append = new TransferStatus().append(true).length(2L * 1024L * 1024L).skip(10L * 1024L * 1024L);
    new S3MultipartUploadService(session, new S3WriteFeature(session, new S3DisabledMultipartService()), 10L * 1024L * 1024L, 1).upload(test, local,
            new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), append,
            new DisabledConnectionCallback());
    assertEquals(12L * 1024L * 1024L, append.getOffset(), 0L);
    assertTrue(append.isComplete());
    assertTrue(new S3FindFeature(session).find(test));
    assertEquals(12L * 1024L * 1024L, new S3AttributesFinderFeature(session).find(test).getSize(), 0L);
    final byte[] buffer = new byte[content.length];
    final InputStream in = new S3ReadFeature(session).read(test, new TransferStatus(), new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    new S3DefaultDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    local.delete();
    session.close();
}
 
Example 19
Project: cyberduck   File: AzureWriteFeatureTest.java   View Source Code Vote up 4 votes
@Test
public void testWriteOverride() throws Exception {
    final OperationContext context
            = new OperationContext();
    final Host host = new Host(new AzureProtocol(), "kahy9boj3eib.blob.core.windows.net", new Credentials(
            System.getProperties().getProperty("azure.account"), System.getProperties().getProperty("azure.key")
    ));
    final AzureSession session = new AzureSession(host);
    new LoginConnectionService(new DisabledLoginCallback(), new DisabledHostKeyCallback(),
            new DisabledPasswordStore(), new DisabledProgressListener()).connect(session, PathCache.empty(), new DisabledCancelCallback());
    final TransferStatus status = new TransferStatus();
    status.setMime("text/plain");
    final byte[] content = RandomUtils.nextBytes(513);
    status.setLength(content.length);
    status.setChecksum(new MD5ChecksumCompute().compute(new ByteArrayInputStream(content), new TransferStatus().length(content.length)));
    status.setMetadata(Collections.singletonMap("Cache-Control", "public,max-age=86400"));
    final Path container = new Path("cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final OutputStream out = new AzureWriteFeature(session, context).write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
    assertTrue(new AzureFindFeature(session, context).find(test));
    final PathAttributes attributes = new AzureAttributesFinderFeature(session, context).find(test);
    assertEquals(content.length, attributes.getSize());
    final Map<String, String> metadata = new AzureMetadataFeature(session, context).getMetadata(test);
    assertEquals("text/plain", metadata.get("Content-Type"));
    assertEquals("public,max-age=86400", metadata.get("Cache-Control"));
    assertEquals(content.length, new AzureWriteFeature(session, context).append(test, status.getLength(), PathCache.empty()).size, 0L);
    final byte[] buffer = new byte[content.length];
    final InputStream in = new AzureReadFeature(session, context).read(test, new TransferStatus(), new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    final OutputStream overwrite = new AzureWriteFeature(session, context).write(test, new TransferStatus()
            .length("overwrite".getBytes("UTF-8").length).withMetadata(Collections.singletonMap("Content-Type", "text/plain")), new DisabledConnectionCallback());
    new StreamCopier(new TransferStatus(), new TransferStatus())
            .transfer(new ByteArrayInputStream("overwrite".getBytes("UTF-8")), overwrite);
    overwrite.close();
    // Test double close
    overwrite.close();
    assertEquals("overwrite".getBytes("UTF-8").length, new AzureAttributesFinderFeature(session, context).find(test).getSize());
    new AzureDeleteFeature(session, context).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
 
Example 20
Project: ditb   File: Encryption.java   View Source Code Vote up 3 votes
/**
 * Decrypt a block of ciphertext read in from a stream with the given
 * cipher and context
 * <p>
 * The decryptor's state will be finalized. It should be reinitialized or
 * returned to the pool.
 * @param dest
 * @param destOffset
 * @param in
 * @param destSize
 * @param d
 * @throws IOException
 */
public static void decrypt(byte[] dest, int destOffset, InputStream in,
    int destSize, Decryptor d) throws IOException {
  InputStream cin = d.createDecryptionStream(in);
  try {
    IOUtils.readFully(cin, dest, destOffset, destSize);
  } finally {
    cin.close();
  }
}