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

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

Introduction

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

Prototype

public CharSource asCharSource(Charset charset) 

Source Link

Document

Returns a CharSource view of this byte source that decodes bytes read from this source as characters using the given Charset .

Usage

From source file:org.renjin.maven.PackageDescription.java

public static PackageDescription fromInputStream(ByteSource in) throws IOException {
    return fromString(in.asCharSource(Charsets.UTF_8).read());
}

From source file:com.machak.idea.plugins.actions.CopyHippoSharedFiles.java

public static String readText(final File file) {
    try {// w  ww  . j  a v  a 2s. c  o  m
        final ByteSource source = Resources.asByteSource(file.toURI().toURL());
        return source.asCharSource(Charsets.UTF_8).read();
    } catch (IOException e) {

    }
    return null;
}

From source file:be.nbb.sdmx.util.QuickStax.java

public XMLStreamReader open(ByteSource byteSource) throws XMLStreamException, IOException {
    return factory.createXMLStreamReader(byteSource.asCharSource(UTF_8).openStream());
}

From source file:io.druid.data.input.MapPopulator.java

/**
 * Read through the `source` line by line and populate `map` with the data returned from the `parser`
 *
 * @param source The ByteSource to read lines from
 * @param map    The map to populate//from  ww  w.  ja v  a 2 s  .c  om
 *
 * @return The number of entries parsed
 *
 * @throws IOException
 */
public long populate(final ByteSource source, final Map<K, V> map) throws IOException {
    return source.asCharSource(Charsets.UTF_8).readLines(new LineProcessor<Long>() {
        private long count = 0l;

        @Override
        public boolean processLine(String line) throws IOException {
            map.putAll(parser.parse(line));
            ++count;
            return true;
        }

        @Override
        public Long getResult() {
            return count;
        }
    });
}

From source file:org.apache.druid.data.input.MapPopulator.java

/**
 * Read through the `source` line by line and populate `map` with the data returned from the `parser`
 *
 * @param source The ByteSource to read lines from
 * @param map    The map to populate//from  w  w  w .j a  v a 2 s.  c  o m
 *
 * @return number of lines read and entries parsed
 *
 * @throws IOException
 */
public PopulateResult populate(final ByteSource source, final Map<K, V> map) throws IOException {
    return source.asCharSource(StandardCharsets.UTF_8).readLines(new LineProcessor<PopulateResult>() {
        private int lines = 0;
        private int entries = 0;

        @Override
        public boolean processLine(String line) {
            if (lines == Integer.MAX_VALUE) {
                throw new ISE("Cannot read more than %,d lines", Integer.MAX_VALUE);
            }
            final Map<K, V> kvMap = parser.parseToMap(line);
            map.putAll(kvMap);
            lines++;
            entries += kvMap.size();
            return true;
        }

        @Override
        public PopulateResult getResult() {
            return new PopulateResult(lines, entries);
        }
    });
}

From source file:io.airlift.airship.coordinator.HttpServiceInventory.java

private List<ServiceDescriptor> getServiceInventory(SlotStatus slotStatus) {
    Assignment assignment = slotStatus.getAssignment();
    if (assignment == null) {
        return null;
    }/*from  w  ww  . j  av a2  s  .  c  o  m*/

    String config = assignment.getConfig();

    File cacheFile = getCacheFile(config);
    if (cacheFile.canRead()) {
        try {
            String json = Files.asCharSource(cacheFile, Charsets.UTF_8).read();
            List<ServiceDescriptor> descriptors = descriptorsJsonCodec.fromJson(json);
            invalidServiceInventory.remove(config);
            return descriptors;
        } catch (Exception ignored) {
            // delete the bad cache file
            cacheFile.delete();
        }
    }

    ByteSource configFile = ConfigUtils.newConfigEntrySupplier(repository, config,
            "airship-service-inventory.json");
    if (configFile == null) {
        return null;
    }

    try {
        String json;
        try {
            json = configFile.asCharSource(Charsets.UTF_8).read();
        } catch (FileNotFoundException e) {
            // no service inventory in the config, so replace with json null so caching works
            json = "null";
        }
        invalidServiceInventory.remove(config);

        // cache json
        cacheFile.getParentFile().mkdirs();
        Files.write(json, cacheFile, Charsets.UTF_8);

        List<ServiceDescriptor> descriptors = descriptorsJsonCodec.fromJson(json);
        return descriptors;
    } catch (Exception e) {
        if (invalidServiceInventory.add(config)) {
            log.error(e, "Unable to read service inventory for %s" + config);
        }
    }
    return null;
}

From source file:org.onosproject.drivers.microsemi.yang.impl.AbstractYangServiceImpl.java

protected final String encodeMoToXmlStr(ModelObjectData yangObjectOpParamFilter,
        List<AnnotatedNodeInfo> annotations) throws NetconfException {
    //Convert the param to XML to use as a filter
    ResourceData rd = ((ModelConverter) yangModelRegistry).createDataNode(yangObjectOpParamFilter);

    DefaultCompositeData.Builder cdBuilder = DefaultCompositeData.builder().resourceData(rd);
    if (annotations != null) {
        for (AnnotatedNodeInfo ani : annotations) {
            cdBuilder.addAnnotatedNodeInfo(ani);
        }// ww  w.jav  a 2  s .  c o  m
    }
    CompositeStream cs = xSer.encode(cdBuilder.build(), yCtx);
    //Convert the param to XML to use as a filter

    try {
        ByteSource byteSource = new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return cs.resourceData();
            }
        };

        return byteSource.asCharSource(Charsets.UTF_8).read();
    } catch (IOException e) {
        throw new NetconfException("Error decoding CompositeStream to String", e);
    }
}

From source file:io.prestosql.plugin.example.ExampleRecordCursor.java

public ExampleRecordCursor(List<ExampleColumnHandle> columnHandles, ByteSource byteSource) {
    this.columnHandles = columnHandles;

    fieldToColumnIndex = new int[columnHandles.size()];
    for (int i = 0; i < columnHandles.size(); i++) {
        ExampleColumnHandle columnHandle = columnHandles.get(i);
        fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
    }/*from   w w w.  ja  va2s.c  om*/

    try (CountingInputStream input = new CountingInputStream(byteSource.openStream())) {
        lines = byteSource.asCharSource(UTF_8).readLines().iterator();
        totalBytes = input.getCount();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.facebook.presto.example.ExampleRecordCursor.java

public ExampleRecordCursor(List<ExampleColumnHandle> columnHandles, ByteSource byteSource) {
    this.columnHandles = columnHandles;

    fieldToColumnIndex = new int[columnHandles.size()];
    for (int i = 0; i < columnHandles.size(); i++) {
        ExampleColumnHandle columnHandle = columnHandles.get(i);
        fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
    }/*www  .j a  v a2s . c o m*/

    try (CountingInputStream input = new CountingInputStream(byteSource.openStream())) {
        lines = byteSource.asCharSource(UTF_8).readLines().iterator();
        totalBytes = input.getCount();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.onos.yangtools.yang.parser.builder.impl.ModuleBuilder.java

public void setSource(final ByteSource byteSource) throws IOException {
    setSource(byteSource.asCharSource(Charsets.UTF_8).read());
}