Example usage for com.google.common.io FileBackedOutputStream FileBackedOutputStream

List of usage examples for com.google.common.io FileBackedOutputStream FileBackedOutputStream

Introduction

In this page you can find the example usage for com.google.common.io FileBackedOutputStream FileBackedOutputStream.

Prototype

public FileBackedOutputStream(int fileThreshold) 

Source Link

Document

Creates a new instance that uses the given file threshold, and does not reset the data when the ByteSource returned by #asByteSource is finalized.

Usage

From source file:de.l3s.concatgz.util.CacheInputStream.java

public CacheInputStream(InputStream in, int bufferSize) {
    this.in = in;
    this.bufferSize = bufferSize;
    this.cache = new FileBackedOutputStream(bufferSize * bufferSize);
}

From source file:org.codice.ddf.platform.util.TemporaryFileBackedOutputStream.java

/**
 * @param fileThreshold the number of bytes before the stream should switch to buffering to a file
 *///from w w  w.  jav a 2  s.co  m
public TemporaryFileBackedOutputStream(int fileThreshold) {
    this.fileBackedOutputStream = new FileBackedOutputStream(fileThreshold);
}

From source file:ddf.catalog.registry.transformer.RegistryTransformer.java

@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {

    MetacardImpl metacard;/*from  w  w  w .  ja  va 2 s .co m*/

    try (FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(1000000)) {

        try {
            IOUtils.copy(inputStream, fileBackedOutputStream);

        } catch (IOException e) {
            throw new CatalogTransformerException(
                    "Unable to transform from CSW RIM Service Record to Metacard. Error reading input stream.",
                    e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metacard = (MetacardImpl) unmarshal(inputStreamCopy);
        } catch (ParserException e) {
            throw new CatalogTransformerException(
                    "Unable to transform from CSW RIM Service Record to Metacard. Parser exception caught", e);
        } catch (RegistryConversionException e) {
            throw new CatalogTransformerException(
                    "Unable to transform from CSW RIM Service Record to Metacard. Conversion exception caught",
                    e);
        }

        if (metacard == null) {
            throw new CatalogTransformerException(
                    "Unable to transform from CSW RIM Service Record to Metacard.");
        } else if (StringUtils.isNotEmpty(id)) {
            metacard.setAttribute(Metacard.ID, id);
        }

        String xml = CharStreams
                .toString(fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream());

        metacard.setAttribute(Metacard.METADATA, xml);
        metacard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));

    } catch (IOException e) {
        throw new CatalogTransformerException(
                "Unable to transform from CSW RIM Service Record to Metacard. Error using file-backed stream.",
                e);
    }

    return metacard;
}

From source file:org.jclouds.logging.internal.Wire.java

public InputStream copy(final String header, InputStream instream) {
    int limit = 256 * 1024;
    final FileBackedOutputStream out = new FileBackedOutputStream(limit);
    try {/*from   w ww.  j  a  va 2s  .c om*/
        long bytesRead = ByteStreams.copy(instream, out);
        if (bytesRead >= limit)
            logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit);
        wire(header, out.getSupplier().getInput());
        // we must call FileBackedOutputStream.reset to remove temporary file
        return new FilterInputStream(out.getSupplier().getInput()) {
            @Override
            public void close() throws IOException {
                super.close();
                out.reset();
            }
        };
    } catch (IOException e) {
        throw new RuntimeException("Error tapping line", e);
    } finally {
        closeQuietly(out);
        closeQuietly(instream);
    }
}

From source file:org.locationtech.geogig.spring.service.LegacyConsoleService.java

private ConsoleRunCommandResponse processRequest(JsonObject json, final Repository repo) {
    final String command = json.get("method").getAsString();
    final String queryId = json.get("id").getAsString();
    // not used, we're getting the whole command and args in the "method" object
    // JsonArray paramsArray = json.get("params").getAsJsonArray();

    InputStream in = new ByteArrayInputStream(new byte[0]);
    // dumps output to a temp file if > threshold
    FileBackedOutputStream out = new FileBackedOutputStream(4096);
    try {//  w  ww.j  a va2 s  .  c  o m
        // pass it a BufferedOutputStream 'cause it doesn't buffer the internal FileOutputStream
        Console console = new Console(in, new BufferedOutputStream(out)).disableAnsi();
        Platform platform = repo.platform();

        GeoGIG geogig = new GeoGIG(repo);
        GeogigCLI geogigCLI = new GeogigCLI(geogig, console);
        geogigCLI.setPlatform(platform);
        geogigCLI.disableProgressListener();

        String[] args = ArgumentTokenizer.tokenize(command);
        final int exitCode = geogigCLI.execute(args);

        final int charCountLimit = getOutputLimit(repo.context());
        final StringBuilder output = getLimitedOutput(out, charCountLimit);
        String result = null;
        ConsoleError error = null;

        if (exitCode == 0) {
            result = output.toString();
        } else {
            Exception exception = geogigCLI.exception;
            error = buildError(exitCode, output, exception);
        }
        return new ConsoleRunCommandResponse(queryId, result, error);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        // delete temp file
        try {
            out.reset();
        } catch (IOException ignore) {
            ignore.printStackTrace();
        }
    }
}

From source file:de.l3s.concatgz.io.FileBackedBytesWritable.java

@Override
public void readFields(DataInput dataInput) throws IOException {
    if (!setExternally)
        closeStream();/*w  w  w  .j  av  a 2 s . co  m*/
    long remaining = dataInput.readLong();
    stream = new FileBackedOutputStream(BUFFER_SIZE * BUFFER_SIZE);
    byte[] buffer = new byte[BUFFER_SIZE];
    while (remaining > 0) {
        int read = BUFFER_SIZE < remaining ? BUFFER_SIZE : (int) remaining;
        dataInput.readFully(buffer, 0, read);
        stream.write(buffer, 0, read);
    }
    setExternally = false;
}

From source file:org.andrewhitchcock.duwamish.Partition.java

@SuppressWarnings("unchecked")
public Partition(DuwamishContext<C, V, E, M> duwamishContext, Map<String, Accumulator> accumulators,
        File tempDir, Partition[] partitions, int partitionNumber) {
    this.accumulators = accumulators;
    this.partitions = partitions;
    this.edgeFile = new File(tempDir, "edge-" + partitionNumber);
    this.vertexFile = new File(tempDir, "vertex-" + partitionNumber);
    this.newVertexFile = new File(tempDir, "new-vertex-" + partitionNumber);

    this.edgeFileWriter = FileUtil.newOutputStream(edgeFile);
    this.vertexFileWriter = FileUtil.newOutputStream(vertexFile);

    this.incomingMessages = new FileBackedOutputStream(incomingMessageThreshold);

    try {/* w ww .  ja v a2s.com*/
        vertex = duwamishContext.vertexClass.newInstance();
        newVertexBuilder = duwamishContext.vertexType.getMethod("newBuilder");
        newEdgeBuilder = duwamishContext.edgeType.getMethod("newBuilder");
        newMessageBuilder = duwamishContext.messageType.getMethod("newBuilder");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ddf.catalog.transformer.input.pptx.PptxInputTransformer.java

/**
 * This is a three step process. First, create a FileBackedOutputStream because we need to
 * consume the stream twice. Once for the injected inputTransformer and once for Apache POI.
 * Next, extract the metadata with the injected input transformer. And last, use Apache POI
 * to create the thumbnail./*www. j a va2 s.  com*/
 *
 * @param input
 * @param id
 * @return
 * @throws IOException
 * @throws CatalogTransformerException
 */
private Metacard transformLogic(InputStream input, String id) throws IOException, CatalogTransformerException {

    try (FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(FILE_BACKED_THRESHOLD)) {
        try {
            int c = IOUtils.copy(input, fileBackedOutputStream);
            LOGGER.debug("copied {} bytes from input stream to file backed output stream", c);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }

        Metacard metacard = extractInitialMetadata(fileBackedOutputStream.asByteSource().openStream());

        extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());

        return metacard;
    }

}

From source file:ddf.camel.component.catalog.inputtransformer.InputTransformerProducer.java

private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message)
        throws MetacardCreationException {
    LOGGER.trace("ENTERING: generateMetacard");

    List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("List of matches for mimeType [" + mimeType + "]:" + listOfCandidates);
    }//from w w  w.j  a  va  2  s .  c  o  m

    Metacard generatedMetacard = null;

    try (FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(1000000)) {

        try {
            IOUtils.copy(message, fileBackedOutputStream);
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }

        // Multiple InputTransformers may be found that match the mime type.
        // Need to try each InputTransformer until we find one that can successfully transform
        // the input stream's data into a metacard. Once an InputTransformer is found that
        // can create the metacard, then do not need to try any remaining InputTransformers.
        for (InputTransformer transformer : listOfCandidates) {

            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }

        if (generatedMetacard == null) {
            throw new MetacardCreationException(
                    "Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
        }

        LOGGER.trace("EXITING: generateMetacard");
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    }

    return generatedMetacard;
}

From source file:org.locationtech.geogig.web.console.ConsoleResourceResource.java

private JsonObject processRequest(JsonObject json, final GeoGIG geogig) {
    JsonObject response;/*from   w  w w  . j a  v  a  2s.c o m*/
    final String command = json.get("method").getAsString();
    final String queryId = json.get("id").getAsString();
    // not used, we're getting the whole command and args in the "method" object
    // JsonArray paramsArray = json.get("params").getAsJsonArray();

    InputStream in = new ByteArrayInputStream(new byte[0]);
    // dumps output to a temp file if > threshold
    FileBackedOutputStream out = new FileBackedOutputStream(4096);
    try {
        // pass it a BufferedOutputStream 'cause it doesn't buffer the internal FileOutputStream
        ConsoleReader console = new ConsoleReader(in, new BufferedOutputStream(out), new UnsupportedTerminal());
        Platform platform = geogig.getPlatform();

        GeogigCLI geogigCLI = new GeogigCLI(geogig, console);
        geogigCLI.setPlatform(platform);
        geogigCLI.disableProgressListener();

        String[] args = ArgumentTokenizer.tokenize(command);
        final int exitCode = geogigCLI.execute(args);
        response = new JsonObject();
        response.addProperty("id", queryId);

        final int charCountLimit = getOutputLimit(geogig.getContext());
        final StringBuilder output = getLimitedOutput(out, charCountLimit);

        if (exitCode == 0) {
            response.addProperty("result", output.toString());
            response.addProperty("error", (String) null);
        } else {
            Exception exception = geogigCLI.exception;
            JsonObject error = buildError(exitCode, output, exception);
            response.add("error", error);
        }
        return response;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    } finally {
        // delete temp file
        try {
            out.reset();
        } catch (IOException ignore) {
            ignore.printStackTrace();
        }
    }
}