Example usage for com.google.common.io Resources asCharSource

List of usage examples for com.google.common.io Resources asCharSource

Introduction

In this page you can find the example usage for com.google.common.io Resources asCharSource.

Prototype

public static CharSource asCharSource(URL url, Charset charset) 

Source Link

Document

Returns a CharSource that reads from the given URL using the given character set.

Usage

From source file:org.nmdp.ngs.hml.HmlUtils.java

/**
 * Create and return zero or more DNA HML Sequences read from the specified URL in FASTA format.
 *
 * @param url URL to read from, must not be null
 * @return zero or more DNA HML Sequences read from the specified URL in FASTA format
 * @throws IOException if an I/O error occurs
 *//*  w w  w .ja  v  a2s  .c o  m*/
public static Iterable<Sequence> createSequences(final URL url) throws IOException {
    checkNotNull(url);
    try (BufferedReader reader = Resources.asCharSource(url, Charsets.UTF_8).openBufferedStream()) {
        return createSequences(reader);
    }
}

From source file:org.obm.sync.H2GuiceServletContextListener.java

private List<String> getAdditionalDBScripts() throws Exception {
    try {// w  w w . j  ava 2  s. c o m
        return Resources.asCharSource(Resources.getResource(ADDITIONAL_DB_SCRIPTS_FILE), Charsets.UTF_8)
                .readLines();
    } catch (IllegalArgumentException e) {
        return ImmutableList.of();
    }
}

From source file:com.palantir.atlasdb.shell.AtlasShellHelp.java

private String read(String path) {
    try {//from w w  w  .  j  a va 2 s.c om
        URL url = this.getClass().getResource(path);
        Charset charSet = Charset.forName("UTF-8");
        Reader reader = Resources.asCharSource(url, charSet).openStream();
        return CharStreams.toString(reader);
    } catch (IOException e) {
        throw Throwables.throwUncheckedException(e);
    }
}

From source file:org.nmdp.ngs.variant.vcf.VcfReader.java

/**
 * Read the VCF header from the specified URL.
 *
 * @param url URL to read from, must not be null
 * @return the VCF header read from the specified URL
 * @throws IOException if an I/O error occurs
 *//* w w  w  .  ja v a2s  .  com*/
public static VcfHeader header(final URL url) throws IOException {
    checkNotNull(url);
    try (BufferedReader reader = Resources.asCharSource(url, Charsets.UTF_8).openBufferedStream()) {
        return header(reader);
    }
}

From source file:net.freifunk.autodeploy.printing.LabelPrintingServiceImpl.java

private final String createFilledInSVG(final Firmware firmware, final DetailedDevice device,
        final FirmwareConfiguration configuration, final String updateToken, final URI updateUri) {
    if (configuration instanceof FreifunkNordFirmwareConfiguration) {
        final FreifunkNordFirmwareConfiguration freifunkNordConfiguration = (FreifunkNordFirmwareConfiguration) configuration;
        final CharSource svgSource = Resources.asCharSource(Resources.getResource(getSVGFilename(firmware)),
                UTF_8);//from ww  w.  j  a  v  a2  s.  c o  m
        final String svgString;
        try {
            svgString = svgSource.read();
        } catch (final IOException e) {
            throw new IllegalStateException("Could not read SVG.", e);
        }

        final String vpnKey = freifunkNordConfiguration.getVpnKey();
        final int vpnKeyLength = vpnKey.length();
        final int vpnKeyMiddle = vpnKeyLength / 2;
        final String vpnKey1 = vpnKey.substring(0, vpnKeyMiddle);
        final String vpnKey2 = vpnKey.substring(vpnKeyMiddle, vpnKeyLength);

        return svgString.replace("{nodename}", freifunkNordConfiguration.getNodename())
                .replace("{password}", freifunkNordConfiguration.getPassword())
                .replace("{device}", device.getDevice().getModel() + " " + device.getDevice().getVersion())
                .replace("{mac}", device.getMac()).replace("{vpnKey1}", vpnKey1).replace("{vpnKey2}", vpnKey2)
                .replace("{token}", updateToken == null ? "---" : updateToken)
                .replace("{updateUri}", updateUri.toString());
    } else {
        throw new IllegalArgumentException("Unsupported configuration: " + configuration.getClass());
    }
}

From source file:com.opengamma.strata.pricer.impl.credit.isda.IsdaModelDatasetsSheetReader.java

/**
 * Load specified sheet.//w  ww .  java 2  s  .  c  o  m
 *
 * @param sheetName the sheet name
 * @param recoveryRate the recovery rate 
 */
public IsdaModelDatasetsSheetReader(final String sheetName, final double recoveryRate) {
    ArgChecker.notEmpty(sheetName, "filename");

    // Set up CSV reader
    String sheetFilePath = SHEET_LOCATION + sheetName;
    URL resource = IsdaModelDatasetsSheetReader.class.getClassLoader().getResource(sheetFilePath);
    if (resource == null) {
        throw new IllegalArgumentException(sheetFilePath + ": does not exist");
    }
    csvFile = CsvFile.of(Resources.asCharSource(resource, StandardCharsets.UTF_8), true);

    // Set columns
    headers = readHeaderRow();

    for (CsvRow row : csvFile.rows()) {
        ISDA_Results temp = getResult(parseRow(row));
        temp.recoveryRate = recoveryRate;
        results.add(temp);
    }
}

From source file:io.airlift.dbpool.H2EmbeddedDataSource.java

@Inject
public H2EmbeddedDataSource(H2EmbeddedDataSourceConfig config) throws Exception {
    super(config.getMaxConnections(), config.getMaxConnectionWait());

    Preconditions.checkNotNull(config.getFilename());
    if (config.getFilename().isEmpty()) {
        throw new IllegalArgumentException("filename is empty");
    }//from  w ww . jav a  2  s  .  c o m

    // build jdbc url connection string
    StringBuilder jdbcUrlBuilder = new StringBuilder().append("jdbc:h2:").append(config.getFilename())
            .append(";MVCC=").append(config.isMvccEnabled());

    if (config.getCipher() != Cipher.NONE) {
        jdbcUrlBuilder.append(";CIPHER=").append(config.getCipher());
    }

    String jdbcUrl = jdbcUrlBuilder.toString();

    // create dataSource
    dataSource = new JdbcDataSource();
    dataSource.setURL(jdbcUrl);
    dataSource.setUser("sa");
    if (config.getCipher() != Cipher.NONE) {
        dataSource.setPassword(config.getFilePassword() + " ");
    } else {
        dataSource.setPassword("");
    }
    dataSource.setLoginTimeout(Ints.checkedCast(config.getMaxConnectionWait().roundTo(SECONDS)));

    // connect to database and initialize database
    Connection connection = getConnection();
    try {
        setConfig(connection, "CACHE_SIZE", config.getCacheSize());
        setConfig(connection, "COMPRESS_LOB", config.getCompressLob());
        setConfig(connection, "MAX_MEMORY_ROWS", config.getMaxMemoryRows());
        setConfig(connection, "MAX_LENGTH_INPLACE_LOB", config.getMaxLengthInplaceLob());
        setConfig(connection, "DB_CLOSE_DELAY ", "-1");

        // handle init script
        String fileName = config.getInitScript();
        if (fileName != null) {
            // find init script
            File file = new File(fileName);
            URL url;
            if (file.exists()) {
                url = file.toURI().toURL();
            } else {
                url = getClass().getClassLoader().getResource(fileName);
            }

            if (url == null) {
                throw new FileNotFoundException(fileName);
            }

            // execute init script
            try (Reader reader = Resources.asCharSource(url, UTF_8).openStream()) {
                ScriptReader scriptReader = new ScriptReader(reader);
                for (String statement = scriptReader
                        .readStatement(); statement != null; statement = scriptReader.readStatement()) {
                    executeCommand(connection, statement);
                }
            }
        }

        // run last so script can contain literals
        setConfig(connection, "ALLOW_LITERALS", config.getAllowLiterals());
    } finally {
        closeQuietly(connection);
    }
}

From source file:com.zz.langchecker.LangChecker.java

private static Set<String> readVocabulary(String name) {
    try {//from w ww .j  a va2 s.c o  m
        return ImmutableSet.copyOf(
                Resources.asCharSource(LangChecker.class.getResource(name), Charsets.UTF_8).readLines());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.spongepowered.api.asset.Asset.java

/**
 * Reads all lines from the asset and returns the result.
 *
 * @param charset The charset to read the asset with
 * @return An immutable list of the lines read from the asset
 * @throws IOException//from  www.  j ava  2s. com
 */
default List<String> readLines(Charset charset) throws IOException {
    checkNotNull(charset, "charset");
    return Resources.asCharSource(getUrl(), charset).readLines();
}

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

void readMapFile(String rulesFile) throws IOException {
    File file = new File(rulesFile);
    URL rulesResource;/*  w w  w  . j  a v a  2 s . c  om*/
    if (file.exists()) {
        rulesResource = file.toURI().toURL();
    } else {
        rulesResource = Resources.getResource(rulesFile);
    }
    processATFile(Resources.asCharSource(rulesResource, Charsets.UTF_8));
    FMLRelaunchLog.fine("Loaded %d rules from AccessTransformer config file %s", modifiers.size(), rulesFile);
}