Java Code Examples for com.google.common.io.FileBackedOutputStream

The following examples show how to use com.google.common.io.FileBackedOutputStream. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: james-project   Source File: ExportService.java    License: Apache License 2.0 5 votes vote down vote up
private BlobId zipToBlob(Username username, Flux<DeletedMessage> messages) throws IOException {
    try (FileBackedOutputStream fileOutputStream = new FileBackedOutputStream(FileUtils.ONE_MB_BI.intValue())) {
        zipper.zip(contentLoader(username), messages.toStream(), fileOutputStream);
        ByteSource byteSource = fileOutputStream.asByteSource();
        return Mono.from(blobStore.save(blobStore.getDefaultBucketName(), byteSource.openStream(), LOW_COST)).block();
    }
}
 
Example 2
Source Project: james-project   Source File: AESPayloadCodec.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Payload write(InputStream inputStream) {
    try (FileBackedOutputStream outputStream = new FileBackedOutputStream(MAX_BYTES.intValue())) {
        outputStream.write(aead.encrypt(IOUtils.toByteArray(inputStream), EMPTY_ASSOCIATED_DATA));
        ByteSource data = outputStream.asByteSource();
        return new Payload(Payloads.newByteSourcePayload(data), Optional.of(data.size()));
    } catch (IOException | GeneralSecurityException e) {
        throw new RuntimeException("Unable to build payload for object storage, failed to " +
            "encrypt", e);
    }
}
 
Example 3
Source Project: james-project   Source File: CassandraBlobStore.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Mono<BlobId> save(BucketName bucketName, InputStream data, StoragePolicy storagePolicy) {
    Preconditions.checkNotNull(bucketName);
    Preconditions.checkNotNull(data);
    HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data);
    return Mono.using(
        () -> new FileBackedOutputStream(FILE_THRESHOLD),
        fileBackedOutputStream -> saveAndGenerateBlobId(bucketName, hashingInputStream, fileBackedOutputStream),
        Throwing.consumer(FileBackedOutputStream::reset).sneakyThrow(),
        LAZY_RESOURCE_CLEANUP);
}
 
Example 4
Source Project: james-project   Source File: CassandraBlobStore.java    License: Apache License 2.0 5 votes vote down vote up
private Mono<BlobId> saveAndGenerateBlobId(BucketName bucketName, HashingInputStream hashingInputStream, FileBackedOutputStream fileBackedOutputStream) {
    return Mono.fromCallable(() -> {
        IOUtils.copy(hashingInputStream, fileBackedOutputStream);
        return Tuples.of(blobIdFactory.from(hashingInputStream.hash().toString()), fileBackedOutputStream.asByteSource());
    })
        .flatMap(tuple -> dumbBlobStore.save(bucketName, tuple.getT1(), tuple.getT2()).thenReturn(tuple.getT1()));
}
 
Example 5
Source Project: selenium   Source File: ProtocolHandshake.java    License: Apache License 2.0 5 votes vote down vote up
public Result createSession(HttpClient client, Command command)
    throws IOException {
  Capabilities desired = (Capabilities) command.getParameters().get("desiredCapabilities");
  desired = desired == null ? new ImmutableCapabilities() : desired;

  int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE);
  FileBackedOutputStream os = new FileBackedOutputStream(threshold);
  try (
      CountingOutputStream counter = new CountingOutputStream(os);
      Writer writer = new OutputStreamWriter(counter, UTF_8);
      NewSessionPayload payload = NewSessionPayload.create(desired)) {

    payload.writeTo(writer);

    try (InputStream rawIn = os.asByteSource().openBufferedStream();
         BufferedInputStream contentStream = new BufferedInputStream(rawIn)) {
      Optional<Result> result = createSession(client, contentStream, counter.getCount());

      if (result.isPresent()) {
        Result toReturn = result.get();
        LOG.info(String.format("Detected dialect: %s", toReturn.dialect));
        return toReturn;
      }
    }
  } finally {
    os.reset();
  }

  throw new SessionNotCreatedException(
      String.format(
          "Unable to create new remote session. " +
          "desired capabilities = %s",
          desired));
}
 
Example 6
Source Project: java-client   Source File: NewAppiumSessionPayload.java    License: Apache License 2.0 4 votes vote down vote up
private NewAppiumSessionPayload(Reader source, boolean forceMobileJSONWP) throws IOException {
    this.forceMobileJSONWP = forceMobileJSONWP;
    // Dedicate up to 10% of all RAM or 20% of available RAM (whichever is smaller) to storing this
    // payload.
    int threshold = (int) Math.min(
            Integer.MAX_VALUE,
            Math.min(
                    Runtime.getRuntime().freeMemory() / 5,
                    Runtime.getRuntime().maxMemory() / 10));

    backingStore = new FileBackedOutputStream(threshold);
    try (Writer writer = new OutputStreamWriter(backingStore, UTF_8)) {
        CharStreams.copy(source, writer);
    }

    ImmutableSet.Builder<CapabilitiesFilter> adapters = ImmutableSet.builder();
    ServiceLoader.load(CapabilitiesFilter.class).forEach(adapters::add);
    adapters
            .add(new ChromeFilter())
            .add(new EdgeFilter())
            .add(new FirefoxFilter())
            .add(new InternetExplorerFilter())
            .add(new OperaFilter())
            .add(new SafariFilter());
    this.adapters = adapters.build();

    ImmutableSet.Builder<CapabilityTransform> transforms = ImmutableSet.builder();
    ServiceLoader.load(CapabilityTransform.class).forEach(transforms::add);
    transforms
            .add(new ProxyTransform())
            .add(new StripAnyPlatform())
            .add(new W3CPlatformNameNormaliser());
    this.transforms = transforms.build();

    ImmutableSet.Builder<Dialect> dialects = ImmutableSet.builder();
    if (getOss() != null) {
        dialects.add(Dialect.OSS);
    }
    if (getAlwaysMatch() != null || getFirstMatch() != null) {
        dialects.add(Dialect.W3C);
    }

    validate();
}