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:eu.redzoo.article.planetcassandra.reactive.CassandraDB.java

/**
 * executes a CQL file //w  w w .ja  v a2 s  .c  o  m
 * 
 * @param cqlFile    the CQL file name 
 * @throws IOException  if the file could not be found
 */
public void executeCqlFile(String cqlFile) throws IOException {
    File file = new File(cqlFile);
    if (file.exists()) {
        executeCql(Files.toString(new File(cqlFile), Charsets.UTF_8));
    } else {
        executeCql(Resources.toString(Resources.getResource(cqlFile), Charsets.UTF_8));
    }
}

From source file:org.glowroot.agent.dist.PluginJsonTransformer.java

private static @Nullable String getGlowrootPluginJson(Artifact artifact) throws IOException {
    File artifactFile = artifact.getFile();
    if (!artifactFile.exists()) {
        return null;
    }/*from  w ww .j  a va 2 s  . c om*/
    if (artifactFile.isDirectory()) {
        File jsonFile = new File(artifactFile, "META-INF/glowroot.plugin.json");
        if (!jsonFile.exists()) {
            return null;
        }
        return Files.toString(jsonFile, UTF_8);
    }
    JarInputStream jarIn = new JarInputStream(new FileInputStream(artifact.getFile()));
    try {
        JarEntry jarEntry;
        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            String name = jarEntry.getName();
            if (jarEntry.isDirectory()) {
                continue;
            }
            if (!name.equals("META-INF/glowroot.plugin.json")) {
                continue;
            }
            InputStreamReader in = new InputStreamReader(jarIn, UTF_8);
            String content = CharStreams.toString(in);
            in.close();
            return content;
        }
        return null;
    } finally {
        jarIn.close();
    }
}

From source file:org.grycap.gpf4med.cloud.util.CloudUtils.java

/**
 * Creates a statement that will add an unprivileged user to a machine that could
 * be latter used for executing commands on the machine using RSA SSH authentication.
 * @return//from  w  ww.java  2s.co m
 */
public static UserAdd addLoginForCommandExecution() {
    final String publicKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa.pub";
    final String publicKey;
    try {
        publicKey = Files.toString(new File(publicKeyFile), UTF_8);
    } catch (IOException e) {
        throw propagate(e);
    }
    assert publicKey.startsWith("ssh-rsa ") : "invalid public key:\n" + publicKey;
    return UserAdd.builder().login(System.getProperty("user.name")).authorizeRSAPublicKey(publicKey).build();
}

From source file:info.servertools.teleport.HomeManager.java

/**
 * Load the home map from file/*w ww .ja v a 2s .  co  m*/
 */
private static void load() {
    if (!homeFile.exists())
        return;
    synchronized (lock) {
        try {
            String data = Files.toString(homeFile, Reference.CHARSET);
            userHomeMap = gson.fromJson(data, new TypeToken<Map<UUID, Map<Integer, Location>>>() {
            }.getType());
        } catch (IOException e) {
            ServerToolsTeleport.log.warn("Failed to load homes from file", e);
        } finally {
            if (userHomeMap == null) {
                userHomeMap = new HashMap<>();
            }
        }
    }
}

From source file:com.netflix.iep.config.ConfigFile.java

public static Properties loadProperties(Map<String, String> vars, File file) {
    try {//w  ww .  j a va2s .  co  m
        return loadProperties(vars, Files.toString(file, Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.asciidoctor.asciidoclet.DocletIterator.java

private boolean processOverview(RootDoc rootDoc, DocletRenderer renderer) {
    Optional<File> overview = docletOptions.overview();
    if (overview.isPresent()) {
        File overviewFile = overview.get();
        if (isAsciidocFile(overviewFile.getName())) {
            try {
                String overviewContent = Files.toString(overviewFile, docletOptions.encoding());
                rootDoc.setRawCommentText(overviewContent);
                renderer.renderDoc(rootDoc);
            } catch (IOException e) {
                rootDoc.printError("Error reading overview file: " + e.getLocalizedMessage());
                return false;
            }// w  w w  .  ja v  a  2s. c om
        } else {
            rootDoc.printNotice("Skipping non-AsciiDoc overview " + overviewFile
                    + ", will be processed by standard Doclet.");
        }
    }
    return true;
}

From source file:com.basistech.tclrebenchmark.BenchmarkDriver.java

private void setupInputs() throws IOException, RegexException {
    textContent = Files.toString(text, Charsets.UTF_8);
    patterns = Lists.newArrayList();//from   ww w .j  ava2  s  .com
    for (File regexFile : regexes) {
        List<String> regexLines = Files.readLines(regexFile, Charsets.UTF_8);
        // each one is three fields separated by tabs, but with no escaping on the last field
        int lineCount = 1;
        for (String line : regexLines) {
            int tx = line.indexOf('\t');
            tx = line.indexOf('\t', tx + 1);
            String regex = line.substring(tx + 1);
            try {
                patterns.add(HsrePattern.compile(regex, PatternFlags.ADVANCED));
                lineCount++;
            } catch (RegexException e) {
                System.err.printf("Error parsing line %d of file %s: %s%n", lineCount,
                        regexFile.getAbsolutePath(), regex);
                throw e;
            }
        }
    }
}

From source file:org.eclipse.buildship.core.configuration.internal.LegacyCleaningProjectConfigurationPersistence.java

private static Map<String, Map<String, String>> parseLegacyConfigurationFile(IProject project) {
    File gradlePrefsFile = getLegacyConfigurationFile(project);
    String malformedFileMessage = String.format(
            "Project %s contains a malformed gradle.prefs file. Please re-run the import wizard.",
            project.getName());//from ww  w. ja  va2 s. com

    Map<String, Map<String, String>> parsedJson = null;
    try {
        String json = Files.toString(gradlePrefsFile, Charsets.UTF_8);
        Gson gson = new GsonBuilder().create();
        parsedJson = gson.fromJson(json, createMapTypeToken());
    } catch (Exception e) {
        throw new GradlePluginsRuntimeException(malformedFileMessage, e);
    }
    if (parsedJson == null) {
        throw new GradlePluginsRuntimeException(malformedFileMessage);
    }
    return parsedJson;
}

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.ExpectedParseTreeGenerator.java

private static ParseTree parseJavadocFromFile(File file) throws IOException {
    final String content = Files.toString(file, Charsets.UTF_8);
    final InputStream in = new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));

    final ANTLRInputStream input = new ANTLRInputStream(in);
    final JavadocLexer lexer = new JavadocLexer(input);
    lexer.removeErrorListeners();//from w  w w .  j  a  v a2 s. c  om

    final BaseErrorListener errorListener = new FailOnErrorListener();
    lexer.addErrorListener(errorListener);

    final CommonTokenStream tokens = new CommonTokenStream(lexer);

    final JavadocParser parser = new JavadocParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    return parser.javadoc();
}

From source file:io.joynr.capabilities.ResourceContentProvider.java

private String readFromFileOrClasspath(String provisionedCapabilitiesJsonFilename) throws IOException {
    logger.trace("Attempting to read {} from file / classpath", provisionedCapabilitiesJsonFilename);
    File file = new File(provisionedCapabilitiesJsonFilename);
    if (file.exists()) {
        return Files.toString(file, UTF8);
    } else {/*from   ww  w .j a  va 2s . c  o m*/
        logger.trace("File {} doesn't exist on filesystem, attempting to read from classpath.",
                provisionedCapabilitiesJsonFilename);
        try (InputStream resourceAsStream = StaticCapabilitiesProvisioning.class.getClassLoader()
                .getResourceAsStream(provisionedCapabilitiesJsonFilename)) {
            if (resourceAsStream != null) {
                return readFromStream(resourceAsStream);
            }
        }
    }
    return null;
}