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:com.axemblr.provisionr.cloudstack.activities.CloudStackActivity.java

@Override
public void execute(DelegateExecution execution) throws Exception {

    RestContext<CloudStackClient, CloudStackAsyncClient> restContext = null;
    try {/*from w ww .j a  v  a 2s  .  co  m*/
        Pool pool = Pool.class.cast(checkNotNull(execution.getVariable(CoreProcessVariables.POOL),
                "Please add 'pool' variable to the process"));
        // delegate
        restContext = newCloudStackClient(pool.getProvider());
        execute(restContext.getApi(), pool, execution);

    } finally {
        Closeables.closeQuietly(restContext);
    }
}

From source file:co.cask.cdap.api.dataset.lib.CompositeDatasetAdmin.java

@Override
public void close() throws IOException {
    for (DatasetAdmin admin : delegates) {
        Closeables.closeQuietly(admin);
    }
}

From source file:de.blizzy.documentr.web.filter.TrimFilter.java

private byte[] getTrimmedData(TrimResponseWrapper trimResponse) throws IOException {
    BufferedReader in = null;//ww w.  j  ava2  s  .  c om
    ByteArrayOutputStream out = null;
    try {
        Charset charset = Charset.forName(trimResponse.getCharacterEncoding());
        String text = new String(trimResponse.getData(), charset);
        in = new BufferedReader(new StringReader(text));
        out = new ByteArrayOutputStream();
        writer.write(text, out, charset);
    } finally {
        Closeables.closeQuietly(in);
        Closeables.closeQuietly(out);
    }
    return out.toByteArray();
}

From source file:com.handEval.ConfigurationLoader.java

/**
 * Load hand rank lookup table for poker hands.
 * This will load the file and do the byte conversions so we get a nice integer array back.
 * @param name file name of the precomputed hand rank file
 * @return integer array of hand rank lookup values in accordance with the 2+2 hand evaluation algorithm.
 * @throws RuntimeException If loading this file fails, prepare to crash because hand evals will not work
 *//*from w ww  .ja  va 2s  . c o m*/
public int[] loadHandRankResource(String name) throws RuntimeException {
    int handRankArray[] = new int[HAND_RANK_SIZE];
    try {
        int tableSize = HAND_RANK_SIZE * 4;
        byte[] b = new byte[tableSize];
        InputStream br = null;
        try {
            br = new BufferedInputStream(ConfigurationLoader.class.getResourceAsStream(name));
            int bytesRead = br.read(b, 0, tableSize);
            if (bytesRead != tableSize) {
                log.error("Read " + bytesRead + " bytes out of " + tableSize);
            }
        } finally {
            Closeables.closeQuietly(br);
        }
        for (int i = 0; i < HAND_RANK_SIZE; i++) {
            handRankArray[i] = littleEndianByteArrayToInt(b, i * 4);
        }
        return handRankArray;
    } catch (IOException e) {
        throw new RuntimeException("cannot read resource " + name, e);
    }
}

From source file:io.smartspaces.util.data.resource.MessageDigestResourceSignatureCalculator.java

@Override
public String getResourceSignature(File resourceFile) {
    FileInputStream fis = null;/*  w  ww .  j a va2 s .c o  m*/

    try {
        fis = new FileInputStream(resourceFile);
        return getResourceSignature(fis);
    } catch (Exception e) {
        throw new SimpleSmartSpacesException(
                String.format("Could not create signature for file %s", resourceFile.getAbsolutePath()), e);
    } finally {
        Closeables.closeQuietly(fis);
    }
}

From source file:org.sonar.java.checks.MissingNewLineAtEndOfFile_S00113_Check.java

public void visitFile(File file) {
    RandomAccessFile randomAccessFile = null;
    try {//from  www .j a v  a2 s  .  co  m
        randomAccessFile = new RandomAccessFile(file, "r");
        if (!endsWithNewline(randomAccessFile)) {

            addIssueOnFile("Add a new line at the end of this file.");
        }
    } catch (IOException e) {
        throw new SonarException(e);
    } finally {
        Closeables.closeQuietly(randomAccessFile);
    }
}

From source file:org.jclouds.boxdotnet.handlers.BoxDotNetErrorHandler.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 {// w  w w.  jav  a  2  s.  c o  m
        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;
        }
    } finally {
        Closeables.closeQuietly(response.getPayload());
        command.setException(exception);
    }
}

From source file:org.apache.provisionr.core.templates.xml.XmlTemplate.java

/**
 * @return an XmlTemplate instance resulted from parsing a file
 *//*  ww  w  .j  a  va 2 s .com*/
public static XmlTemplate newXmlTemplate(File file) {
    Reader reader = null;
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
        reader = new InputStreamReader(inputStream, Charsets.UTF_8);

        return newXmlTemplate(CharStreams.toString(reader));

    } catch (IOException e) {
        throw Throwables.propagate(e);

    } finally {
        Closeables.closeQuietly(inputStream);
        Closeables.closeQuietly(reader);
    }
}

From source file:org.plista.kornakapi.web.servlets.BatchAddCandidatesServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int batchSize = getParameterAsInt(request, Parameters.BATCH_SIZE, Parameters.DEFAULT_BATCH_SIZE);

    ServletFileUpload upload = new ServletFileUpload();

    FileItemIterator fileItems;//from   www .ja va2  s .  com
    InputStream in = null;

    boolean fileProcessed = false;

    Storage storage = this.getDomainIndependetStorage();

    try {
        fileItems = upload.getItemIterator(request);
        while (fileItems.hasNext()) {

            FileItemStream item = fileItems.next();

            if (Parameters.FILE.equals(item.getFieldName()) && !item.isFormField()) {

                in = item.openStream();
                Iterator<Candidate> candidates = new CSVCandidateFileIterator(in);

                storage.batchAddCandidates(candidates, batchSize);

                fileProcessed = true;

                break;
            }
        }
    } catch (FileUploadException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(in);
    }

    if (!fileProcessed) {
        throw new IllegalStateException("Unable to find supplied data file!");
    }
}

From source file:org.apache.provisionr.core.logging.StreamLogger.java

@Override
public void run() {
    BufferedReader bufferedReader = null;
    InputStreamReader inputStreamReader = null;
    try {//from   w  ww .  j  a  va 2s .co m
        inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
        bufferedReader = new BufferedReader(inputStreamReader);

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (!line.isEmpty()) {
                log(logger, marker, line);
            }
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);

    } finally {
        Closeables.closeQuietly(bufferedReader);
        Closeables.closeQuietly(inputStreamReader);
    }
}