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:io.hops.hopsworks.common.util.IoUtils.java

public static String readContentFromPath(String path) throws IOException {
    return Files.toString(new File(path), Charsets.UTF_8);
}

From source file:org.sonar.sslr.parser.ParserAdapter.java

private static char[] fileToCharArray(File file, Charset charset) {
    try {/*from   www  .  ja  v a2 s  .  c  o m*/
        return Files.toString(file, charset).toCharArray();
    } catch (IOException e) {
        throw new RecognitionException(0, e.getMessage(), e);
    }
}

From source file:datamine.storage.recordbuffers.example.GenerateTestData.java

public void run() {
    File schemaPath = new File(this.schemaFile);
    try {/*from  w  ww. ja  v  a2s .c  o  m*/
        schema = new JsonSchemaConvertor().apply(Files.toString(schemaPath, Charsets.UTF_8));

        // validate the schema
        new SchemaValidation().check(schema);

        // generate the codes
        generateTableAccessInterfaces();
        generateTableConversionImps();
        generateTableMetadataEnums();
        generateTableAccessImps();
        generateTableAccessTestData();
        generateTableContentPrinters();

    } catch (IOException e) {
        LOG.error("Cannot generate the code for the schema at " + this.schemaFile, e);
    } catch (AbstractValidationException e) {
        LOG.error("Schema validation failed for " + this.schemaFile, e);
    }
}

From source file:com.palantir.tslint.Linter.java

public void lint(IResource resource, String configurationPath) throws IOException {
    String resourceName = resource.getName();
    if (resource instanceof IFile && (resourceName.endsWith(".ts") || resourceName.endsWith(".tsx"))
            && !resourceName.endsWith(".d.ts")) {
        IFile file = (IFile) resource;/*from w w  w .j  a v  a2s  . com*/
        String resourcePath = resource.getRawLocation().toOSString();

        // remove any pre-existing markers for the given file
        deleteMarkers(file);

        // get a bridge
        if (this.bridge == null) {
            String configuration = Files.toString(new File(configurationPath), Charsets.UTF_8);
            Request configurationRequest = new Request("setConfiguration", configuration);

            this.bridge = new Bridge();
            this.bridge.call(configurationRequest, Void.class);
        }

        Request request = new Request("lint", resourcePath);
        String response = this.bridge.call(request, String.class);

        if (response != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            RuleFailure[] ruleFailures = objectMapper.readValue(response, RuleFailure[].class);
            for (RuleFailure ruleFailure : ruleFailures) {
                addMarker(ruleFailure);
            }
        }
    }
}

From source file:org.ops4j.pax.carrot.ui.RunnerController.java

public void loadFile() {
    file = new File(settings.getOutputRoot(), treeBean.getRelativePath());
    if (file == null || !file.exists() || file.isDirectory()) {
        return;/*from   w  w  w  .  j  a va2 s .  c  o  m*/
    }
    content = "";
    try {
        content = Files.toString(file, Charset.forName("UTF-8"));
    } catch (IOException exc) {
        throw new CarrotException(exc);
    }
}

From source file:com.axmor.eclipse.typescript.debug.sourcemap.SourceMapParser.java

/**
 * @param file//from   ww  w. j av a 2 s  .  c om
 *            source map file
 * @return parsed source map
 */
public SourceMap parse(File file) {
    try {
        JSONObject json = new JSONObject(Files.toString(file, Charsets.UTF_8));
        SourceMap smap = new SourceMap();
        smap.setVersion(json.getInt("version"));
        smap.setFile(new File(file.getParentFile(), json.getString("file")).getCanonicalPath());
        JSONArray names = json.getJSONArray("names");
        for (int i = 0; i < names.length(); i++) {
            smap.getNames().add(names.getString(i));
        }

        String mappings = json.getString("mappings");
        String[] lines = mappings.split(";");
        int tsLinePrev = 0;
        int tsColumnPrev = 0;
        for (int i = 0; i < lines.length; i++) {
            int jsColumnPrev = 0;
            int tsFileIdxPrev = 0;
            String line = lines[i];
            String[] segments = line.split(",");
            for (String segment : segments) {
                StringCharacterIterator str = new StringCharacterIterator(segment);
                ArrayList<Integer> seg = new ArrayList<>();
                while (str.current() != StringCharacterIterator.DONE) {
                    seg.add(decode(str));
                }
                if (seg.size() >= 3) {
                    SourceMapItem item = new SourceMapItem();
                    item.setJsLine(i);
                    item.setJsFile(json.getString("file"));
                    jsColumnPrev += seg.get(0);
                    tsFileIdxPrev += seg.get(1);
                    tsLinePrev += seg.get(2);
                    tsColumnPrev += seg.get(3);
                    item.setJsColumn(jsColumnPrev);
                    item.setTsLine(tsLinePrev + 1);
                    item.setTsColumn(tsColumnPrev);
                    item.setTsFile(json.getJSONArray("sources").getString(tsFileIdxPrev));
                    smap.getMaps().add(item);
                }
            }
        }
        return smap;
    } catch (Exception e) {
        Activator.error(e);
    }
    return null;
}

From source file:net.minecraftforge.gradle.tasks.EtagDownloadTask.java

@TaskAction
public void doTask() throws IOException {
    URL url = getUrl();/*from  ww  w  .  ja va2  s. c  o  m*/
    File outFile = getFile();
    File etagFile = getProject().file(getFile().getPath() + ".etag");

    // ensure folder exists
    outFile.getParentFile().mkdirs();

    String etag;
    if (etagFile.exists()) {
        etag = Files.toString(etagFile, Charsets.UTF_8);
    } else {
        etag = "";
    }

    try {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setInstanceFollowRedirects(true);
        con.setRequestProperty("User-Agent", Constants.USER_AGENT);
        con.setRequestProperty("If-None-Match", etag);

        con.connect();

        switch (con.getResponseCode()) {
        case 404: // file not found.... duh...
            error("" + url + "  404'ed!");
            break;
        case 304: // content is the same.
            this.setDidWork(false);
            break;
        case 200: // worked

            // write file
            InputStream stream = con.getInputStream();
            Files.write(ByteStreams.toByteArray(stream), outFile);
            stream.close();

            // write etag
            etag = con.getHeaderField("ETag");
            if (!Strings.isNullOrEmpty(etag)) {
                Files.write(etag, etagFile, Charsets.UTF_8);
            }

            break;
        default: // another code?? uh.. 
            error("Unexpected reponse " + con.getResponseCode() + " from " + url);
            break;
        }

        con.disconnect();
    } catch (Throwable e) {
        // just in case people dont have internet at the moment.
        error(e.getLocalizedMessage());
    }
}

From source file:org.artifactory.converters.MimeTypeConverter.java

@Override
public boolean isInterested(CompoundVersionDetails source, CompoundVersionDetails target) {
    if (!path.exists()) {
        return false;
    }/* w  w  w  .j a va2  s  .c  o m*/
    try {
        String mimeTypesXml = Files.toString(path, Charsets.UTF_8);
        MimeTypesVersion mimeTypesVersion = MimeTypesVersion.findVersion(mimeTypesXml);
        return !mimeTypesVersion.isCurrent();
    } catch (Exception e) {
        throw new RuntimeException("Failed to execute mimetypes conversion", e);

    }
}

From source file:org.pshdl.model.types.builtIn.busses.ABP3BusCodeGen.java

public static void main(String[] args) throws FileNotFoundException, IOException, RecognitionException {
    final Unit unit = MemoryModelAST.parseUnit(Files.toString(new File(args[0]), Charsets.UTF_8),
            new LinkedHashSet<Problem>(), 0);
    System.out.println(unit);/*from  ww  w  .  ja v  a 2s.co  m*/
    System.out.println(get("Bla", unit, MemoryModel.buildRows(unit)));
}

From source file:com.netflix.simianarmy.chaos.SshConfig.java

/**
 * Constructor.//w w w.  j  a v a2 s. c o m
 *
 * @param config
 *            Configuration to use
 * @throws IOException
 */
public SshConfig(MonkeyConfiguration config) {
    String sshUser = config.getStrOrElse("simianarmy.chaos.ssh.user", "root");
    String privateKey = null;

    String sshKeyPath = config.getStrOrElse("simianarmy.chaos.ssh.key", null);
    if (sshKeyPath != null) {
        sshKeyPath = sshKeyPath.trim();
        if (sshKeyPath.startsWith("~/")) {
            String home = System.getProperty("user.home");
            if (!Strings.isNullOrEmpty(home)) {
                if (!home.endsWith("/")) {
                    home += "/";
                }
                sshKeyPath = home + sshKeyPath.substring(2);
            }
        }

        LOGGER.debug("Reading SSH key from {}", sshKeyPath);

        try {
            privateKey = Files.toString(new File(sshKeyPath), Charsets.UTF_8);
        } catch (IOException e) {
            throw new IllegalStateException("Unable to read the specified SSH key: " + sshKeyPath, e);
        }
    }

    if (privateKey == null) {
        this.sshCredentials = null;
    } else {
        this.sshCredentials = LoginCredentials.builder().user(sshUser).privateKey(privateKey).build();
    }
}