Example usage for com.google.common.io Closeables closeQuietly

List of usage examples for com.google.common.io Closeables closeQuietly

Introduction

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

Prototype

public static void closeQuietly(@Nullable Reader reader) 

Source Link

Document

Closes the given Reader , logging any IOException that's thrown rather than propagating it.

Usage

From source file:org.geoserver.catalog.util.CloseableIteratorAdapter.java

public static void close(Iterator<?> iterator) {
    if (iterator instanceof Closeable) {
        Closeables.closeQuietly((Closeable) iterator);
    }/*from  ww  w  .  j a  va 2s .c  o  m*/
}

From source file:org.locationtech.geogig.rest.repository.SendObjectResource.java

@Override
public void post(Representation entity) {
    InputStream input = null;//  w  w  w.j a v  a  2 s .com

    Request request = getRequest();
    try {
        LOGGER.info("Receiving objects from {}", request.getClientInfo().getAddress());
        Representation representation = request.getEntity();
        input = representation.getStream();
        final GeoGIG ggit = getGeogig(request).get();
        final BinaryPackedObjects unpacker = new BinaryPackedObjects(ggit.getRepository().objectDatabase());

        CountingInputStream countingStream = new CountingInputStream(input);

        Stopwatch sw = Stopwatch.createStarted();
        IngestResults ingestResults = unpacker.ingest(countingStream);
        sw.stop();

        LOGGER.info(String.format(
                "SendObjectResource: Processed %,d objects.\nInserted: %,d.\nExisting: %,d.\nTime to process: %s.\nStream size: %,d bytes.\n",
                ingestResults.total(), ingestResults.getInserted(), ingestResults.getExisting(), sw,
                countingStream.getCount()));

    } catch (IOException e) {
        LOGGER.warn("Error processing incoming objects from {}", request.getClientInfo().getAddress(), e);
        throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        if (input != null)
            Closeables.closeQuietly(input);
    }
}

From source file:org.richfaces.cdk.resource.writer.impl.JavaScriptPackagingProcessor.java

@Override
public void process(String resourceName, InputStream in, OutputStream out, boolean closeAtFinish)
        throws IOException {

    Reader reader = null;//from   w w  w .j ava  2s.  com
    Writer writer = null;

    try {
        reader = new InputStreamReader(in, charset);
        writer = new OutputStreamWriter(out, charset);

        ByteStreams.copy(in, out);
        if (!closeAtFinish) {
            // add semicolon to satisfy end of context of each script when packing files
            writer.write(";");
            writer.flush();
        }
    } finally {
        Closeables.closeQuietly(reader);
        if (closeAtFinish) {
            Closeables.closeQuietly(writer);
        } else {
            writer.flush();
        }
    }
}

From source file:org.richfaces.resource.Java2DUserResourceWrapperImpl.java

public InputStream getInputStream() throws IOException {
    FastBufferOutputStream fbos = new FastBufferOutputStream();

    ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(fbos);
    try {// w  ww.  j  a  v  a2  s .com
        paintAndWrite(imageOutputStream);
    } finally {
        if (imageOutputStream != null) {
            try {
                imageOutputStream.close();
            } catch (IOException e) {
                LOGGER.debug(e.getMessage(), e);
            }

            Closeables.closeQuietly(fbos);
        }
    }
    ByteBuffer buffer = fbos.getFirstBuffer();
    buffer.compact();

    return new FastBufferInputStream(buffer);
}

From source file:com.backelite.sonarqube.swift.issues.SwiftProfile.java

@Override
public RulesProfile createProfile(final ValidationMessages messages) {

    LOGGER.info("Creating Swift Profile");

    Reader config = null;//  w  ww.j a v  a 2s.  com
    final RulesProfile profile = RulesProfile.create("Swift", Swift.KEY);
    profile.setDefaultProfile(true);

    try {
        // Add swift lint rules
        config = new InputStreamReader(getClass().getResourceAsStream(SwiftLintProfile.PROFILE_PATH));
        RulesProfile ocLintRulesProfile = this.swiftLintProfileImporter.importProfile(config, messages);
        for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) {
            profile.addActiveRule(rule);
        }

        return profile;
    } finally {

        Closeables.closeQuietly(config);
    }
}

From source file:com.geico.tfs.matcher.TfsMatcherRunner.java

private void runTfsMatcher() {
    Process process = null;//from   www.  j av  a 2 s. c  om
    try {
        LOG.info("LAUNCHING MATCHER");
        process = new ProcessBuilder(executable.getAbsolutePath()).start();
        process.waitFor();
    } catch (InterruptedException e) {
        LOG.info(e.getMessage());
        throw Throwables.propagate(e);
    } catch (ArrayIndexOutOfBoundsException e) {
        LOG.info(e.getMessage());
        throw Throwables.propagate(e);
    } catch (IOException e) {
        LOG.info(e.getMessage());
        throw Throwables.propagate(e);
    } finally {
        if (process != null) {
            Closeables.closeQuietly(process.getErrorStream());
        }
    }
}

From source file:io.fabric8.process.manager.commands.support.KindCompleter.java

public void init() {
    // lets load the kinds from the classpath
    InputStream in = ProcessManager.class.getResourceAsStream("controllerKinds");
    if (in == null) {
        LOG.warn("Cannot find controllerKinds on the classpath!");
    } else {//from   w w w  .j av  a 2  s .  c o  m
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
            while (true) {
                String line = reader.readLine();
                if (line == null)
                    break;
                line = line.trim();
                if (!line.isEmpty()) {
                    kinds.add(line);
                }
            }
        } catch (IOException e) {
            Closeables.closeQuietly(in);
        }
    }
}

From source file:org.jclouds.openstack.nova.v1_1.handlers.NovaErrorHandler.java

public void handleError(HttpCommand command, HttpResponse response) {
    // it is important to always read fully and close streams
    String message = parseMessage(response);
    Exception exception = message != null ? new HttpResponseException(command, response, message)
            : new HttpResponseException(command, response);
    try {/*from   w w  w.ja va  2  s .c om*/
        message = message != null ? message
                : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
                        response.getStatusLine());
        switch (response.getStatusCode()) {
        case 401:
        case 403:
            exception = new AuthorizationException(message, exception);
            break;
        case 404:
            if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
                exception = new ResourceNotFoundException(message, exception);
            }
            break;
        case 500:
            if (message != null && message.indexOf("Unable to determine package for") != -1) {
                exception = new ResourceNotFoundException(message, exception);
            }
        }
    } finally {
        if (response.getPayload() != null)
            Closeables.closeQuietly(response.getPayload().getInput());
        command.setException(exception);
    }
}

From source file:druid.examples.flights.FlightsFirehoseFactory.java

@Override
public Firehose connect() throws IOException {
    File dir = new File(flightsFileLocation);

    final Iterator<File> files = Iterators.forArray(dir.listFiles());

    return new Firehose() {
        BufferedReader in = null;
        String line = null;// ww w . j  a va2  s  . co m

        @Override
        public boolean hasMore() {
            try {
                if (line != null) {
                    return true;
                } else if (in != null) {
                    line = in.readLine();

                    if (line == null) {
                        Closeables.closeQuietly(in);
                        in = null;
                    }

                    return true;
                } else if (files.hasNext()) {
                    final File nextFile = files.next();

                    if (nextFile.getName().endsWith(".gz")) {
                        in = new BufferedReader(new InputStreamReader(
                                new GZIPInputStream(new FileInputStream(nextFile)), Charsets.UTF_8));
                    } else {
                        in = new BufferedReader(new FileReader(nextFile));
                    }
                    return hasMore();
                }
                Thread.currentThread().join();
                return false;
            } catch (IOException e) {
                throw Throwables.propagate(e);
            } catch (InterruptedException e) {
                throw Throwables.propagate(e);
            }
        }

        @Override
        public InputRow nextRow() {
            final InputRow retVal = parser.parse(line);
            line = null;
            return retVal;
        }

        @Override
        public Runnable commit() {
            return new Runnable() {
                @Override
                public void run() {

                }
            };
        }

        @Override
        public void close() throws IOException {
            Closeables.closeQuietly(in);
        }
    };
}

From source file:org.deephacks.confit.internal.jpa.EntityManagerProvider.java

public EntityManagerFactory createEntityManagerFactory(String unitName, URL prop) {
    if (factory != null && factory.isOpen()) {
        return factory;
    }/* w  w w . j  av a  2  s .  c  o m*/
    FileInputStream in = null;
    Properties p = new Properties();
    try {
        p.load(prop.openStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Closeables.closeQuietly(in);
    }
    factory = Persistence.createEntityManagerFactory(unitName, p);
    return factory;
}