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

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

Introduction

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

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

Calls #close if not already closed, and then resets this object back to its initial state, for reuse.

Usage

From source file:de.faustedition.reasoning.InscriptionPrecedenceResource.java

@Get("svg|html")
public Representation svg() throws IOException, ExecutionException, InterruptedException {
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final Process tred = new ProcessBuilder(environment.getRequiredProperty("graphviz.tred.path")).start();
    final Process dot = new ProcessBuilder(environment.getRequiredProperty("graphviz.dot.path"), "-Tsvg")
            .start();/*ww w .  j a v a2  s  .co  m*/

    executorService.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStream dataStream = null;
            OutputStream tredStream = null;
            try {
                ByteStreams.copy(
                        dataStream = new ByteArrayInputStream(asDot().getBytes(Charset.forName("UTF-8"))),
                        tredStream = tred.getOutputStream());
            } finally {
                Closeables.close(dataStream, false);
                Closeables.close(tredStream, false);
            }
            return null;
        }
    });

    executorService.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStream tredStream = null;
            OutputStream dotStream = null;
            try {
                ByteStreams.copy(tredStream = tred.getInputStream(), dotStream = dot.getOutputStream());
            } finally {
                Closeables.close(tredStream, false);
                Closeables.close(dotStream, false);
            }
            return null;
        }
    });

    final Future<FileBackedOutputStream> dotFuture = executorService
            .submit(new Callable<FileBackedOutputStream>() {
                @Override
                public FileBackedOutputStream call() throws Exception {
                    final FileBackedOutputStream buf = new FileBackedOutputStream(102400);
                    InputStream dotStream = null;
                    try {
                        ByteStreams.copy(dotStream = dot.getInputStream(), buf);
                    } finally {
                        Closeables.close(dotStream, false);
                        Closeables.close(buf, false);
                    }
                    return buf;
                }
            });

    Preconditions.checkState(tred.waitFor() == 0);
    Preconditions.checkState(dot.waitFor() == 0);

    final FileBackedOutputStream resultBuf = dotFuture.get();

    return new OutputRepresentation(MediaType.IMAGE_SVG) {
        @Override
        public void write(OutputStream outputStream) throws IOException {
            ByteStreams.copy(resultBuf.getSupplier(), outputStream);
            resultBuf.reset();
        }
    };
}

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  www.  ja v  a2s .  c o  m
        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:eu.interedition.text.rdbms.RelationalTextRepository.java

@Override
public Text concat(Iterable<Text> texts) throws IOException {
    final FileBackedOutputStream buf = createBuffer();
    final OutputStreamWriter bufWriter = new OutputStreamWriter(buf, Text.CHARSET);
    try {/*ww  w.  ja  va 2s.co  m*/
        for (Text text : texts) {
            read(new ReaderCallback<Void>(text) {
                @Override
                protected Void read(Clob content) throws SQLException, IOException {
                    Reader reader = null;
                    try {
                        CharStreams.copy(reader = content.getCharacterStream(), bufWriter);
                    } finally {
                        Closeables.close(reader, false);
                    }
                    return null;
                }
            });
        }
    } finally {
        Closeables.close(bufWriter, false);
    }

    Reader reader = null;
    try {
        return create(reader = new InputStreamReader(buf.getSupplier().getInput(), Text.CHARSET));
    } finally {
        Closeables.closeQuietly(reader);
        buf.reset();
    }
}

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 w w. java  2s. com*/
        // 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:org.codice.ddf.security.crl.generator.CrlGenerator.java

/** Pulls down the CRL file provided via URL and writes it locally. */
@Override//from  ww  w  . j ava2 s .co  m
public synchronized void run() {
    if (!crlByUrlEnabled || crlLocationUrl == null) {
        return;
    }

    if (!crlLocationUrl.startsWith(HTTPS)) {
        postErrorEvent("The provided URL was not an HTTPS URL.");
        return;
    }

    Object entity = getRemoteCrl();
    if (!(entity instanceof InputStream)) {
        postErrorEvent("Unable to download the remote CRL.");
        return;
    }

    FileBackedOutputStream fileBackedOutputStream = null;
    try {
        // Read the response content and get the byte source
        ByteSource byteSource;
        try (InputStream entityStream = (InputStream) entity) {
            fileBackedOutputStream = new FileBackedOutputStream(FILE_BACKED_STREAM_THRESHOLD);
            IOUtils.copy(entityStream, fileBackedOutputStream);
            fileBackedOutputStream.close();
            byteSource = fileBackedOutputStream.asByteSource();
        }

        File crlFile = getCrlFile(byteSource);
        // Verify that the CRL is valid
        if (!crlIsValid(byteSource)) {
            postErrorEvent("An error occurred while validating the CRL.");
            return;
        }
        writeCrlToFile(byteSource, crlFile);
    } catch (IOException e) {
        LOGGER.warn("Unable to copy CRL to local CRL. {}", e.getMessage());
        postErrorEvent("An error occurred while downloading the CRL.");
    } finally {
        // Cleanup temp file
        if (fileBackedOutputStream != null) {
            try {
                fileBackedOutputStream.reset();
            } catch (IOException e) {
                LOGGER.warn("Error occurred while deleting the temporary file. {}", e.getMessage());
            }
        }
    }
}

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

private JsonObject processRequest(JsonObject json, final GeoGIG geogig) {
    JsonObject response;/*  w  w  w . j a  v  a 2  s  .  c om*/
    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();
        }
    }
}

From source file:ddf.camel.component.content.ContentProducer.java

@Override
public void process(Exchange exchange) throws ContentComponentException, ContentFrameworkException {
    LOGGER.debug("ENTERING: process");

    if (!exchange.getPattern().equals(ExchangePattern.InOnly)) {
        return;/*from   w  w w  . j  a  v a  2s  .  c o  m*/
    }

    Message in = exchange.getIn();
    Object body = in.getBody();
    File ingestedFile = null;
    if (body instanceof GenericFile) {
        GenericFile<File> genericFile = (GenericFile<File>) body;
        ingestedFile = (File) genericFile.getFile();
    } else {
        LOGGER.warn("Unable to cast message body to Camel GenericFile, so unable to process ingested file");
        throw new ContentComponentException(
                "Unable to cast message body to Camel GenericFile, so unable to process ingested file");
    }

    if (ingestedFile == null) {
        LOGGER.debug("EXITING: process - ingestedFile is NULL");
        return;
    }

    String operation = in.getHeader(Request.OPERATION, String.class);
    if (StringUtils.isEmpty(operation)) {
        throw new ContentComponentException("Unable to process file " + ingestedFile.getName()
                + "  -  Must specify an operation of create, read, update, or delete");
    }

    String directive = in.getHeader(Request.DIRECTIVE, String.class);
    if (StringUtils.isEmpty(directive)) {
        throw new ContentComponentException("Unable to process file " + ingestedFile.getName()
                + "  -  Must specify a directive of STORE, PROCESS, or STORE_PROCESS");
    }

    String contentUri = (String) in.getHeader(Request.CONTENT_URI, "");

    LOGGER.debug("operation = {}", operation);
    LOGGER.debug("directive = {}", directive);
    LOGGER.debug("contentUri = {}", contentUri);

    FileInputStream fis = null;
    FileBackedOutputStream fbos = null;
    try {
        fis = FileUtils.openInputStream(ingestedFile);
    } catch (IOException e) {
        throw new ContentComponentException("Unable to open file {}" + ingestedFile.getName());
    }

    Request.Directive requestDirective = Request.Directive.valueOf(directive);

    String fileExtension = FilenameUtils.getExtension(ingestedFile.getAbsolutePath());

    String mimeType = null;
    if (fileExtension != null) {
        MimeTypeMapper mimeTypeMapper = endpoint.getComponent().getMimeTypeMapper();
        if (mimeTypeMapper != null) {
            try {
                // XML files are mapped to multiple mime types, e.g., CSW, XML Metacard, etc.
                // If processing a .xml file, then use a stream that allows multiple reads on it
                // i.e., FileBackedOutputStream (FBOS), since the MimeTypeMapper will need to introspect
                // the xml file for its root element namespace and later the stream will need to
                // be read to persist it to the content store and/or create a metacard for ingest.
                // The FBOS is not used for every ingested file because do not want the performance
                // hit of potentially reading in a 1 GB NITF file that does not need to be introspected
                // to determine the mime type.
                if (fileExtension.equals("xml")) {
                    // FBOS reads file into byte array in memory up to this threshold, then it transitions
                    // to writing to a file.
                    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
                    IOUtils.copy(fis, fbos);
                    // Using fbos.asByteSource().openStream() allows us to pass in a copy of the InputStream
                    mimeType = mimeTypeMapper.guessMimeType(fbos.asByteSource().openStream(), fileExtension);
                } else {
                    mimeType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
                }

            } catch (MimeTypeResolutionException | IOException e) {
                IOUtils.closeQuietly(fis);
                IOUtils.closeQuietly(fbos);
                throw new ContentComponentException(e);
            }
        } else {
            IOUtils.closeQuietly(fis);
            LOGGER.error("Did not find a MimeTypeMapper service");
            throw new ContentComponentException(
                    "Unable to find a mime type for the ingested file " + ingestedFile.getName());
        }
    }

    try {
        LOGGER.debug("Preparing content item for mimeType = {}", mimeType);

        if (StringUtils.isNotEmpty(mimeType)) {
            ContentItem newItem;
            // If the FileBackedOutputStream (FBOS) was used, then must be processing an XML file and need to
            // get a fresh InputStream from the FBOS to use in the content item
            if (fbos != null) {
                newItem = new IncomingContentItem(fbos.asByteSource().openStream(), mimeType,
                        ingestedFile.getName());
            } else {
                // Otherwise, the InputStream created from the ingested file has never been read,
                // so can use it in the content item
                newItem = new IncomingContentItem(fis, mimeType, ingestedFile.getName());
            }
            newItem.setUri(contentUri);

            LOGGER.debug("Creating content item.");

            CreateRequest createRequest = new CreateRequestImpl(newItem, null);
            CreateResponse createResponse = endpoint.getComponent().getContentFramework().create(createRequest,
                    requestDirective);
            if (createResponse != null) {
                ContentItem contentItem = createResponse.getCreatedContentItem();

                LOGGER.debug("content item created with id = {}", contentItem.getId());
                LOGGER.debug(contentItem.toString());
            }
        } else {
            LOGGER.debug("mimeType is NULL");
            throw new ContentComponentException(
                    "Unable to determine mime type for the file " + ingestedFile.getName());
        }
    } catch (IOException e) {
        throw new ContentComponentException(
                "Unable to determine mime type for the file " + ingestedFile.getName());
    } finally {
        IOUtils.closeQuietly(fis);
        if (fbos != null) {
            try {
                fbos.reset();
            } catch (IOException e) {
                LOGGER.info("Unable to reset FileBackedOutputStream");
            }
        }
    }

    LOGGER.debug("EXITING: process");
}