本文整理匯總了Java中com.google.common.io.CharSource類的典型用法代碼示例。如果您正苦於以下問題:Java CharSource類的具體用法?Java CharSource怎麽用?Java CharSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CharSource類屬於com.google.common.io包,在下文中一共展示了CharSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: processLines
import com.google.common.io.CharSource; //導入依賴的package包/類
public void processLines( final Object stream, final Consumer<String> callback )
throws Exception
{
final CharSource source = toCharSource( stream );
source.readLines( new LineProcessor<Object>()
{
@Override
public boolean processLine( final String line )
throws IOException
{
callback.accept( line );
return true;
}
@Override
public Object getResult()
{
return null;
}
} );
}
示例2: identicalFileIsAlreadyGenerated
import com.google.common.io.CharSource; //導入依賴的package包/類
private boolean identicalFileIsAlreadyGenerated(CharSequence sourceCode) {
try {
String existingContent = new CharSource() {
final String packagePath = !key.packageName.isEmpty() ? (key.packageName.replace('.', '/') + '/') : "";
final String filename = key.relativeName + ".java";
@Override
public Reader openStream() throws IOException {
return getFiler()
.getResource(StandardLocation.SOURCE_OUTPUT,
"", packagePath + filename)
.openReader(true);
}
}.read();
if (existingContent.contentEquals(sourceCode)) {
// We are ok, for some reason the same file is already generated,
// happens in Eclipse for example.
return true;
}
} catch (Exception ignoredAttemptToGetExistingFile) {
// we have some other problem, not an existing file
}
return false;
}
示例3: main
import com.google.common.io.CharSource; //導入依賴的package包/類
public static void main(String[] argv) throws IOException {
if (argv.length != 1) {
usage();
}
final Parameters params = Parameters.loadSerifStyle(new File(argv[0]));
final File docIdToFileMapFile = params.getExistingFile("docIdToFileMap");
final File filterFile = params.getCreatableFile("quoteFilter");
final Map<Symbol, CharSource> docIdToFileMap = FileUtils.loadSymbolToFileCharSourceMap(
Files.asCharSource(docIdToFileMapFile, Charsets.UTF_8));
log.info("Building quote filter from {} documents in {}", docIdToFileMap.size(),
docIdToFileMapFile);
final QuoteFilter quoteFilter = QuoteFilter.createFromOriginalText(docIdToFileMap);
log.info("Writing quote filter to {}", filterFile);
quoteFilter.saveTo(Files.asByteSink(filterFile));
}
示例4: loadCorpusEventFrames
import com.google.common.io.CharSource; //導入依賴的package包/類
@Override
public CorpusEventLinking loadCorpusEventFrames(final CharSource source)
throws IOException {
int lineNo = 1;
try (final BufferedReader in = source.openBufferedStream()) {
final ImmutableSet.Builder<CorpusEventFrame> ret = ImmutableSet.builder();
String line;
while ((line = in.readLine()) != null) {
if (!line.isEmpty() && !line.startsWith("#")) {
// check for blank or comment lines
ret.add(parseLine(line));
}
}
return CorpusEventLinking.of(ret.build());
} catch (Exception e) {
throw new IOException("Error on line " + lineNo + " of " + source, e);
}
}
示例5: testQuotedRegionComputation
import com.google.common.io.CharSource; //導入依賴的package包/類
@Test
public void testQuotedRegionComputation() throws IOException {
final Map<String, ImmutableRangeSet<Integer>> testCases = ImmutableMap.of(
"Foo <quote>bar <quote>baz</quote> <quote>meep</quote></quote> blah <quote>another</quote>",
ImmutableRangeSet.<Integer>builder().add(Range.closed(4, 60)).add(Range.closed(67, 88))
.build(),
"<quote>lalala</quote>", ImmutableRangeSet.of(Range.closed(0, 20)),
"No quotes!", ImmutableRangeSet.<Integer>of());
for (final Map.Entry<String, ImmutableRangeSet<Integer>> entry : testCases.entrySet()) {
final Symbol docid = Symbol.from("dummy");
final QuoteFilter reference =
QuoteFilter.createFromBannedRegions(ImmutableMap.of(docid, entry.getValue()));
final QuoteFilter computed = QuoteFilter.createFromOriginalText(ImmutableMap.of(docid,
CharSource.wrap(entry.getKey())));
assertEquals(reference, computed);
}
}
示例6: loadDictionary
import com.google.common.io.CharSource; //導入依賴的package包/類
/**
* Load the word frequency list, skipping words that are not in the given white list.
*
* @param whitelist The white list.
* @return A Dictomation dictionary with the word probabilities.
* @throws IOException
* @throws DictionaryBuilderException
*/
public static DictomatonDictionary loadDictionary(Set<String> whitelist) throws IOException, DictionaryBuilderException {
CharSource source = getResource(WORD_FREQUENCIES_FILE);
DictomatonDictionary dictomatonDictionary;
BufferedReader reader = source.openBufferedStream();
try {
if (whitelist == null) {
dictomatonDictionary = DictomatonDictionary.read(reader);
} else {
dictomatonDictionary = DictomatonDictionary.read(reader, whitelist);
}
} finally {
reader.close();
}
return dictomatonDictionary;
}
示例7: testGet_io
import com.google.common.io.CharSource; //導入依賴的package包/類
public void testGet_io() throws IOException {
assertEquals(-1, ArbitraryInstances.get(InputStream.class).read());
assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read());
assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1)));
assertEquals(-1, ArbitraryInstances.get(Reader.class).read());
assertEquals(-1, ArbitraryInstances.get(StringReader.class).read());
assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity());
assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity());
ArbitraryInstances.get(PrintStream.class).println("test");
ArbitraryInstances.get(PrintWriter.class).println("test");
assertNotNull(ArbitraryInstances.get(File.class));
assertFreshInstanceReturned(
ByteArrayOutputStream.class, OutputStream.class,
Writer.class, StringWriter.class,
PrintStream.class, PrintWriter.class);
assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class));
assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class));
assertNotNull(ArbitraryInstances.get(ByteSink.class));
assertNotNull(ArbitraryInstances.get(CharSink.class));
}
示例8: call
import com.google.common.io.CharSource; //導入依賴的package包/類
@Override
public String call() throws Exception
{
try
{
final URL resource = Resources.getResource("configurable.txt");
final File f = new File(resource.toURI());
if( !f.exists() )
{
return NO_CONTENT;
}
if( lastMod == 0 || lastMod < f.lastModified() )
{
final CharSource charSource = Resources.asCharSource(resource, Charset.forName("utf-8"));
final StringWriter sw = new StringWriter();
charSource.copyTo(sw);
lastContent = sw.toString();
lastMod = f.lastModified();
}
return lastContent;
}
catch( Exception e )
{
return NO_CONTENT;
}
}
示例9: ModAccessTransformer
import com.google.common.io.CharSource; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public ModAccessTransformer() throws Exception
{
super(ModAccessTransformer.class);
//We are in the new ClassLoader here, so we need to get the static field from the other ClassLoader.
ClassLoader classLoader = this.getClass().getClassLoader().getClass().getClassLoader(); //Bit odd but it gets the class loader that loaded our current class loader yay java!
Class<?> otherClazz = Class.forName(this.getClass().getName(), true, classLoader);
Field otherField = otherClazz.getDeclaredField("embedded");
otherField.setAccessible(true);
embedded = (Map<String, String>)otherField.get(null);
for (Map.Entry<String, String> e : embedded.entrySet())
{
int old_count = getModifiers().size();
processATFile(CharSource.wrap(e.getValue()));
int added = getModifiers().size() - old_count;
if (added > 0)
{
FMLRelaunchLog.fine("Loaded %d rules from AccessTransformer mod jar file %s\n", added, e.getKey());
}
}
}
示例10: StableCssSubstitutionMapProvider
import com.google.common.io.CharSource; //導入依賴的package包/類
/**
* @param backingFile a file that need not exist, but if it does, contains
* the content of the renaming map as formatted by
* {@link OutputRenamingMapFormat#JSON}..
*/
public StableCssSubstitutionMapProvider(File backingFile)
throws IOException {
CharSource renameMapJson = Files.asCharSource(backingFile, Charsets.UTF_8);
RecordingSubstitutionMap.Builder substitutionMapBuilder =
new RecordingSubstitutionMap.Builder()
.withSubstitutionMap(
new MinimalSubstitutionMap());
ImmutableMap<String, String> mappings = ImmutableMap.of();
try {
try (Reader reader = renameMapJson.openBufferedStream()) {
mappings = OutputRenamingMapFormat.JSON.readRenamingMap(reader);
}
} catch (@SuppressWarnings("unused") FileNotFoundException ex) {
// Ok. Start with an empty map.
}
substitutionMapBuilder.withMappings(mappings);
this.substitutionMap = substitutionMapBuilder.build();
this.backingFile = backingFile;
this.originalMappings = mappings;
}
示例11: provideCssRenamingMap
import com.google.common.io.CharSource; //導入依賴的package包/類
/** Reads the CSS rename map */
@Provides
@Singleton
public SoyCssRenamingMap provideCssRenamingMap()
throws IOException {
ImmutableMap.Builder<String, String> cssMapBuilder = ImmutableMap.builder();
URL crUrl = getClass().getResource(CSS_RENAMING_MAP_RESOURCE_PATH);
if (crUrl != null) {
CharSource cssRenamingMapJson = Resources.asCharSource(
crUrl, Charsets.UTF_8);
JsonElement json;
try (Reader jsonIn = cssRenamingMapJson.openStream()) {
json = new JsonParser().parse(jsonIn);
}
for (Map.Entry<String, JsonElement> e
: json.getAsJsonObject().entrySet()) {
cssMapBuilder.put(e.getKey(), e.getValue().getAsString());
}
}
return new SoyCssRenamingMapImpl(cssMapBuilder.build());
}
示例12: injectAccessTransformer
import com.google.common.io.CharSource; //導入依賴的package包/類
public static void injectAccessTransformer(File file, String atName, LaunchClassLoader loader) throws IOException {
if (!AlchemyEngine.isRuntimeDeobfuscationEnabled() || file == null)
return;
String at = null;
try (JarFile jar = new JarFile(file)) {
ZipEntry entry = jar.getEntry("META-INF/" + atName);
if (entry != null)
at = Tool.read(jar.getInputStream(entry));
}
if (at != null) {
List<IClassTransformer> transformers = $(loader, "transformers");
for (IClassTransformer t : transformers)
if (t instanceof AccessTransformer) {
AccessTransformer transformer = (AccessTransformer) t;
$(transformer, "processATFile", CharSource.wrap(at));
break;
}
}
}
示例13: ModAccessTransformer
import com.google.common.io.CharSource; //導入依賴的package包/類
public ModAccessTransformer() throws Exception
{
super(ModAccessTransformer.class);
//We are in the new ClassLoader here, so we need to get the static field from the other ClassLoader.
ClassLoader classLoader = this.getClass().getClassLoader().getClass().getClassLoader(); //Bit odd but it gets the class loader that loaded our current class loader yay java!
Class<?> otherClazz = Class.forName(this.getClass().getName(), true, classLoader);
Field otherField = otherClazz.getDeclaredField("embedded");
otherField.setAccessible(true);
embedded = (Map<String, String>)otherField.get(null);
for (Map.Entry<String, String> e : embedded.entrySet())
{
int old_count = getModifiers().size();
processATFile(CharSource.wrap(e.getValue()));
int added = getModifiers().size() - old_count;
if (added > 0)
{
FMLRelaunchLog.fine("Loaded %d rules from AccessTransformer mod jar file %s\n", added, e.getKey());
}
}
}
示例14: scanProperties
import com.google.common.io.CharSource; //導入依賴的package包/類
public static Properties scanProperties(String... patterns) {
Properties p = new Properties();
Arrays.asList(patterns).forEach(pattern -> {
Resources.scan(pattern).forEach(url -> {
CharSource src = Resources.asCharSource(url);
try (Reader r = src.openBufferedStream()) {
p.load(r);
log.info("load property source: <{}>", url.toExternalForm());
} catch (IOException e) {
log.error("fail to load property source: <{}> [{}]", url.toExternalForm(), e.getMessage());
}
});
});
return encryptedProperties(p);
}
示例15: fromCharSource
import com.google.common.io.CharSource; //導入依賴的package包/類
public static List<Rewrite> fromCharSource(CharSource source) throws IOException {
return source.readLines(new LineProcessor<List<Rewrite>>() {
private List<Rewrite> refactorings = new ArrayList<>();
private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings();
@Override
public List<Rewrite> getResult() {
return Collections.unmodifiableList(refactorings);
}
@Override
public boolean processLine(String line) {
List<String> parts = SPLITTER.splitToList(line);
refactorings.add(Rewrite.of(parts.get(0), parts.get(1), Rewrite.Visit.Expressions));
return true;
}
});
}