List of usage examples for java.util.zip DeflaterInputStream DeflaterInputStream
public DeflaterInputStream(InputStream in, Deflater defl)
From source file:ubicrypt.core.provider.RemoteRepository.java
private Observable<Boolean> saveSerial(final FileProvenience fp) { //acquire permission final AtomicReference<FileEvent.Type> fileEventType = new AtomicReference<>(); return create(acquirer).flatMap(releaser -> { releaserRef.set(releaser);/*from w ww . j av a 2 s . c o m*/ final RemoteConfig remoteConfig = releaser.getRemoteConfig(); UbiFile<UbiFile> file = fp.getFile(); Optional<RemoteFile> rfile = remoteConfig.getRemoteFiles().stream().filter(file1 -> file1.equals(file)) .findFirst(); if (!rfile.isPresent()) { if (!Utils.ignoredFiles.test(file)) { return just(false); } //create new one remotely RemoteFile rf = RemoteFile.createFrom(file); final byte[] key = AESGCM.rndKey(); rf.setKey(new Key(key)); fileEventType.set(FileEvent.Type.created); return fp.getOrigin().get(file) .flatMap(is -> provider .post(AESGCM.encryptIs(key, new DeflaterInputStream(monitor(fp, is), new Deflater(BEST_COMPRESSION)))) .map(name -> { log.info("created file:{}, to provider:{}", rf.getPath(), provider); //add name and add to config rf.setRemoteName(name); remoteConfig.getRemoteFiles().add(rf); return true; })) .defaultIfEmpty(false).filter(BooleanUtils::isTrue) .doOnCompleted(fileEvents(fp, fileEventType.get())); } //update already present file if (file.compare(rfile.get()) == VClock.Comparison.newer) { //coming file is new version log.debug("file:{} newer than:{} on provider:{}", file.getPath(), rfile.get(), provider); rfile.get().copyFrom(file); if (rfile.get().isDeleted() || rfile.get().isRemoved()) { //delete remotely fileEventType.set(rfile.get().isDeleted() ? FileEvent.Type.deleted : FileEvent.Type.removed); return provider.delete(rfile.get().getName()) .doOnNext(saved -> log.info("deleted:{} file:{}, to provider:{}", saved, rfile.get().getPath(), provider)) .filter(BooleanUtils::isTrue).doOnCompleted(fileEvents(fp, fileEventType.get())); } //update remotely fileEventType.set(FileEvent.Type.updated); return fp.getOrigin().get(file).flatMap(is -> { //renew encryption key rfile.get().setKey(new Key(AESGCM.rndKey(), UbiFile.KeyType.aes)); return provider .put(rfile.get().getName(), AESGCM.encryptIs(rfile.get().getKey().getBytes(), new DeflaterInputStream(monitor(fp, is), new Deflater(BEST_COMPRESSION)))) .doOnNext(saved -> log.info("updated:{} file:{}, to provider:{}", saved, rfile.get().getPath(), provider)) .filter(BooleanUtils::isTrue).doOnCompleted(fileEvents(fp, fileEventType.get())); }); } log.debug("no update file:{} for provider:{}", file.getPath(), provider); return Observable.just(false); }).doOnError(releaserRef.get() != null ? err -> releaserRef.get().getReleaser().call() : Actions.empty()) .doOnError(err -> progressEvents.onNext(new ProgressFile(fp, this, false, true))) .onErrorReturn(err -> { log.error(err.getMessage(), err); return false; }) .doOnCompleted(releaserRef.get() != null ? releaserRef.get().getReleaser()::call : Actions.empty()); }