Example usage for com.google.common.io Files toString

List of usage examples for com.google.common.io Files toString

Introduction

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

Prototype

public static String toString(File file, Charset charset) throws IOException 

Source Link

Usage

From source file:org.sonar.java.ast.visitors.LinesVisitor.java

/**
 * Workaround for SSLRSQBR-10//from w w w  . j  a v a  2 s.  c o m
 */
@Override
public void leaveFile(AstNode astNode) {
    if (astNode == null) {
        // TODO do not compute number of lines, when not able to parse
        return;
    }
    try {
        String content = Files.toString(getContext().getFile(), charset);
        String[] lines = content.split("(\r)?\n|\r", -1);
        getContext().peekSourceCode().setMeasure(JavaMetric.LINES, lines.length);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:io.dropwizard.jetty.NetUtil.java

/**
 * The SOMAXCONN value of the current machine.  If failed to get the value, <code>defaultBacklog</code> argument is
 * used//  w w  w  .  ja  va2s.co m
 */
public static int getTcpBacklog(int tcpBacklog) {
    // Taken from netty.

    // As a SecurityManager may prevent reading the somaxconn file we wrap this in a privileged block.
    //
    // See https://github.com/netty/netty/issues/3680
    return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> {
        // Determine the default somaxconn (server socket backlog) value of the platform.
        // The known defaults:
        // - Windows NT Server 4.0+: 200
        // - Linux and Mac OS X: 128
        try {
            String setting = Files.toString(new File(TCP_BACKLOG_SETTING_LOCATION), StandardCharsets.UTF_8);
            return Integer.parseInt(setting.trim());
        } catch (SecurityException | IOException | NumberFormatException | NullPointerException e) {
            return tcpBacklog;
        }
    });

}

From source file:keywhiz.service.config.Templates.java

/**
 * Returns trimmed contents of a file when input string of form 'external:some/file/path' or just
 * the original input otherwise.//  w  w  w  .  j a v a2 s  . c  om
 *
 * @param template string which may optionally begin with 'external:'.
 * @return string with content of file when input templates, otherwise unchanged input.
 * @throws IOException on errors reading from filesystem.
 */
public static String evaluateExternal(String template) throws IOException {
    if (template.startsWith(EXTERNAL_PREFIX)) {
        File file = new File(template.substring(EXTERNAL_PREFIX.length()));
        logger.info("Reading configuration value from file {}", file.getPath());
        try {
            return Files.toString(file, UTF_8).trim();
        } catch (IOException exception) {
            logger.error("Error reading configuration value '{}':{}", template, exception);
            throw exception;
        }
    }

    return template;
}

From source file:org.apache.provisionr.api.access.AdminAccessBuilder.java

public AdminAccessBuilder asCurrentUser() {
    String userHome = System.getProperty("user.home");

    try {//from w ww. j a va  2  s  .c  o  m
        String publicKey = Files.toString(new File(userHome, ".ssh/id_rsa.pub"), Charsets.UTF_8);
        String privateKey = Files.toString(new File(userHome, ".ssh/id_rsa"), Charsets.UTF_8);

        return username(System.getProperty("user.name")).publicKey(publicKey).privateKey(privateKey);

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

From source file:net.chris54721.infinitycubed.workers.AssetsWorker.java

@Override
public void run() {
    try {/* www .j ava 2  s.  c  om*/
        LogHelper.info("Downloading assets for Minecraft " + minecraft);
        assetList = new ArrayList<Asset>();
        URL indexUrl = Resources.getUrl(Resources.ResourceType.ASSET_INDEX, minecraft + ".json");
        File indexFile = Resources.getFile(Resources.ResourceType.ASSET_INDEX, minecraft + ".json");
        Downloadable indexDownloadable = new Downloadable(indexUrl, indexFile);
        indexDownloadable.setForce(false);
        if (indexDownloadable.download()) {
            String indexJson = Files.toString(indexFile, Charsets.UTF_8);
            AssetIndex index = Reference.ASSETS_GSON.fromJson(indexJson, AssetIndex.class);
            this.totalAssets = index.getObjects().entrySet().size();
            for (Map.Entry<String, AssetObject> entry : index.getObjects().entrySet()) {
                String assetName = entry.getKey();
                AssetObject object = entry.getValue();
                String assetFileName = object.getHash().substring(0, 2) + "/" + object.getHash();
                URL assetFileUrl = Resources.getUrl(Resources.ResourceType.ASSET_OBJECT, assetFileName);
                File assetFile = Resources.getFile(Resources.ResourceType.ASSET_OBJECT, assetFileName);
                Asset asset = new Asset(assetName, object);
                if (!asset.isLocalValid())
                    assetList.add(asset);
            }
            LogHelper.info(totalAssets + " assets queued for download");
            DownloadJob assetDownloadJob = new DownloadJob(assetList);
            assetDownloadJob.setForceDownload(false);
            assetDownloadJob.setActionListener(new AssetProgressListener(this));
            assetDownloadJob.run();
        } else
            throw new NullPointerException("Couldn't download index file");
    } catch (Exception e) {
        LogHelper.error("Failed loading assets for Minecraft " + minecraft, e);
    }
}

From source file:gem.talk_to_outside_world.validation.SimpleValidationBoardState.java

public static ConwayState conwayStateFromJsonFile(File file) {
    String json = "";
    try {//from w w  w. j a  va2  s.co m
        json = Files.toString(file, Charsets.UTF_8);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Gson gs = new Gson();
    SimpleValidationCell[] cs = gs.fromJson(json, SimpleValidationCell[].class);
    return new SimpleValidationBoardState(cs).toConwayState();
}

From source file:org.eclipse.jdt.ls.core.internal.ResourceUtils.java

/**
 * Reads file content directly from the filesystem.
 *///w w w  . j  a va 2 s  .c  o m
public static String getContent(URI fileURI) throws CoreException {
    if (fileURI == null) {
        return null;
    }
    String content;
    try {
        content = Files.toString(new File(fileURI), Charsets.UTF_8);
    } catch (IOException e) {
        throw new CoreException(StatusFactory.newErrorStatus("Can not get " + fileURI + " content", e));
    }
    return content;
}

From source file:org.apache.whirr.service.ServiceSpec.java

public String readPrivateKey() throws IOException {
    return Files.toString(new File(getSecretKeyFile()), Charsets.UTF_8);
}

From source file:org.sonar.python.checks.MissingNewlineAtEndOfFileCheck.java

@Override
public void visitFile(@Nullable AstNode astNode) {
    String fileContent;/*from   w w w.  ja  v  a  2s . c  o  m*/
    try {
        fileContent = Files.toString(getContext().getFile(), charset);
    } catch (IOException e) {
        throw new IllegalStateException("Could not read " + getContext().getFile(), e);
    }
    if (!fileContent.endsWith("\n") && !fileContent.endsWith("\r")) {
        getContext().createFileViolation(this, String.format(MESSAGE, getContext().getFile().getName()));
    }
}

From source file:de.unipassau.isl.evs.ssh.drivers.lib.EvsIo.java

/**
 * Read sensor value/* ww  w  .  j av  a2s. com*/
 *
 * @param pin pin number (see shifter shield)
 * @return the value of the sensor
 */
public static String readValue(int pin) throws EvsIoException {
    try {
        return Files.toString(getValueFile(pin), Charsets.UTF_8);
    } catch (IOException e) {
        throw new EvsIoException("Could not read value of pin " + pin, e);
    }
}