Example usage for java.util.zip DeflaterInputStream DeflaterInputStream

List of usage examples for java.util.zip DeflaterInputStream DeflaterInputStream

Introduction

In this page you can find the example usage for java.util.zip DeflaterInputStream DeflaterInputStream.

Prototype

public DeflaterInputStream(InputStream in) 

Source Link

Document

Creates a new input stream with a default compressor and buffer size.

Usage

From source file:ubicrypt.core.InitFileSyncronizerIT.java

@Test
public void remote2Local() throws Exception {
    final RemoteFile remoteFile = new RemoteFile() {
        {/*from   w  w  w . j a  va2  s. c o  m*/
            setRemoteName("origin");
        }
    };
    final CountDownLatch cd = new CountDownLatch(1);
    syncronizer.setOnComplete(cd::countDown);
    final FileProvider fp1 = TestUtils.fileProvider(TestUtils.tmp2);
    fp1.put("origin", AESGCM.encryptIs(remoteFile.getKey().getBytes(),
            new DeflaterInputStream(new ByteArrayInputStream("ciao".getBytes())))).toBlocking().last();
    final Persist os = new Persist(fp1) {
        {
            setPgpService(ipgpService);
        }
    };
    os.putObject(new RemoteConfig() {
        {
            getRemoteFiles().add(remoteFile);
        }
    }, fp1.getConfFile()).toBlocking().last();
    final ProviderHook hk1 = ctx.getBean(ProviderHook.class, fp1);
    assertThat(cd.await(4, SECONDS)).isTrue();
    assertThat(localConfig.getLocalFiles()).hasSize(1);
}

From source file:ubicrypt.core.local.LocalRepositoryTest.java

@Test
public void save() throws Exception {
    final LocalRepository lr = new LocalRepository(tmp);
    final Subject<FileEvent, FileEvent> fevents = PublishSubject.create();
    lr.setFileEvents(fevents);/*from www.  j a v  a 2 s. c  om*/
    lr.setOnNewFileLocal(new OnNewLocal() {
        {
            setFileEvents(fevents);
            setBasePath(tmp);
            setLocalConfig(lr.getLocalConfig());
        }
    });

    final byte[] key = AESGCM.rndKey();
    final FileProvider provider = TestUtils.fileProvider(tmp2);
    Utils.write(tmp2.resolve("origin"),
            AESGCM.encryptIs(key, new DeflaterInputStream(new ByteArrayInputStream("ciao".getBytes()))))
            .toBlocking().last();
    final RemoteFile rf = new RemoteFile() {
        {
            setRemoteName("origin");
            setPath(Paths.get("local"));
            setKey(new Key(key));
            setSize(4);
        }
    };

    final RemoteConfig remoteConfig = new RemoteConfig();
    remoteConfig.getRemoteFiles().add(rf);
    Observable.OnSubscribe<AcquirerReleaser> acquirer = (subscriber) -> {
        subscriber.onNext(new AcquirerReleaser(remoteConfig, Actions.empty()));
        subscriber.onCompleted();
    };
    final Persist ser = new Persist(provider) {
        {
            setPgpService(mock(IPGPService.class));
        }
    };
    ser.putObject(new RemoteConfig(), provider.getConfFile()).toBlocking().first();

    final RemoteRepository repo = new RemoteRepository(acquirer, provider,
            new ObjectIO<>(ser, provider.getConfFile(), RemoteConfig.class)) {
        {
            //            setSerializer(ser);
        }
    };
    repo.setQueueLiner(new QueueLiner(100));
    repo.init();
    assertThat(IOUtils.readLines(repo.get(rf).toBlocking().first())).contains("ciao");

    //create
    final CountDownLatch cd3 = new CountDownLatch(1);
    Subscription sub = fevents.subscribe(event -> {
        assertThat(event.getType()).isEqualTo(created);
        cd3.countDown();
    });
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().last()).isTrue();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().last())).contains("ciao");
    assertThat(lr.getLocalConfig().getLocalFiles()).hasSize(1);
    assertThat(cd3.await(2, TimeUnit.SECONDS)).isTrue();
    sub.unsubscribe();

    //update
    rf.getVclock().increment(deviceId);
    rf.setSize(5);
    final CountDownLatch cd4 = new CountDownLatch(1);
    sub = fevents.subscribe(event -> {
        assertThat(event.getType()).isEqualTo(updated);
        cd4.countDown();
    });
    Utils.write(tmp2.resolve("origin"),
            AESGCM.encryptIs(key, new DeflaterInputStream(new ByteArrayInputStream("ciao2".getBytes()))))
            .toBlocking().last();
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().last()).isTrue();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().last())).contains("ciao2");
    assertThat(lr.getLocalConfig().getLocalFiles()).hasSize(1);
    assertThat(lr.getLocalConfig().getLocalFiles().iterator().next().compare(rf))
            .isEqualTo(VClock.Comparison.equal);
    assertThat(cd3.await(2, TimeUnit.SECONDS)).isTrue();
    sub.unsubscribe();

    //not update
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().first()).isFalse();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().first())).contains("ciao2");
    assertThat(lr.getLocalConfig().getLocalFiles()).hasSize(1);
    assertThat(lr.getLocalConfig().getLocalFiles().iterator().next().compare(rf))
            .isEqualTo(VClock.Comparison.equal);
}

From source file:ubicrypt.core.provider.LocalRepositoryTest.java

@Test
public void save() throws Exception {
    final LocalRepository lr = new LocalRepository(TestUtils.tmp);
    final Subject<FileEvent, FileEvent> fevents = PublishSubject.create();
    lr.setFileEvents(fevents);// w w  w  . j  a va 2s.  com
    final byte[] key = AESGCM.rndKey();
    final FileProvider provider = TestUtils.fileProvider(TestUtils.tmp2);
    Utils.write(TestUtils.tmp2.resolve("origin"),
            AESGCM.encryptIs(key, new DeflaterInputStream(new ByteArrayInputStream("ciao".getBytes()))))
            .toBlocking().last();
    final RemoteFile rf = new RemoteFile() {
        {
            setRemoteName("origin");
            setPath(Paths.get("local"));
            setKey(new Key(key));
            setSize(4);
        }
    };

    final RemoteConfig remoteConfig = new RemoteConfig();
    remoteConfig.getRemoteFiles().add(rf);
    Observable.OnSubscribe<AcquirerReleaser> acquirer = (subscriber) -> {
        subscriber.onNext(new AcquirerReleaser(remoteConfig, Actions.empty()));
        subscriber.onCompleted();
    };
    final ObjectSerializer ser = new ObjectSerializer(provider) {
        {
            setPgpService(mock(IPGPService.class));
        }
    };
    ser.putObject(new RemoteConfig(), provider.getConfFile()).toBlocking().first();

    final RemoteRepository repo = new RemoteRepository(acquirer, provider,
            new ObjectIO<>(ser, provider.getConfFile(), RemoteConfig.class)) {
        {
            //            setSerializer(ser);
        }
    };

    assertThat(IOUtils.readLines(repo.get(rf).toBlocking().first())).contains("ciao");

    //create
    final CountDownLatch cd3 = new CountDownLatch(1);
    Subscription sub = fevents.subscribe(event -> {
        assertThat(event.getType()).isEqualTo(created);
        cd3.countDown();
    });
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().last()).isTrue();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().last())).contains("ciao");
    assertThat(lr.localConfig.getLocalFiles()).hasSize(1);
    assertThat(cd3.await(2, TimeUnit.SECONDS)).isTrue();
    sub.unsubscribe();

    //update
    rf.getVclock().increment(deviceId);
    rf.setSize(5);
    final CountDownLatch cd4 = new CountDownLatch(1);
    sub = fevents.subscribe(event -> {
        assertThat(event.getType()).isEqualTo(updated);
        cd4.countDown();
    });
    Utils.write(TestUtils.tmp2.resolve("origin"),
            AESGCM.encryptIs(key, new DeflaterInputStream(new ByteArrayInputStream("ciao2".getBytes()))))
            .toBlocking().last();
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().last()).isTrue();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().last())).contains("ciao2");
    assertThat(lr.localConfig.getLocalFiles()).hasSize(1);
    assertThat(lr.localConfig.getLocalFiles().iterator().next().compare(rf)).isEqualTo(VClock.Comparison.equal);
    assertThat(cd3.await(2, TimeUnit.SECONDS)).isTrue();
    sub.unsubscribe();

    //not update
    assertThat(lr.save(new FileProvenience(rf, repo)).toBlocking().first()).isFalse();
    assertThat(IOUtils.readLines(lr.get(rf).toBlocking().first())).contains("ciao2");
    assertThat(lr.localConfig.getLocalFiles()).hasSize(1);
    assertThat(lr.localConfig.getLocalFiles().iterator().next().compare(rf)).isEqualTo(VClock.Comparison.equal);

}