下面列出了com.google.common.io.CharSource#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***/
public static String getContentsFromFileEntry(final ZipEntry entry, String rootName) throws IOException,
URISyntaxException {
URL rootURL = Thread.currentThread().getContextClassLoader().getResource(rootName);
try (final ZipFile root = new ZipFile(new File(rootURL.toURI()));) {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return root.getInputStream(entry);
}
};
CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
return charSrc.read();
}
}
public static void scanWallets() {
File directory = getDefaultKeyDirectory();
keystoreMap.clear();
for (File file : directory.listFiles()) {
if (!file.getName().startsWith("identity")) {
try {
IMTKeystore keystore = null;
CharSource charSource = Files.asCharSource(file, Charset.forName("UTF-8"));
String jsonContent = charSource.read();
JSONObject jsonObject = new JSONObject(jsonContent);
int version = jsonObject.getInt("version");
if (version == 3) {
if (jsonContent.contains("encMnemonic")) {
keystore = unmarshalKeystore(jsonContent, V3MnemonicKeystore.class);
} else if (jsonObject.has("imTokenMeta") && ChainType.EOS.equals(jsonObject.getJSONObject("imTokenMeta").getString("chainType"))) {
keystore = unmarshalKeystore(jsonContent, LegacyEOSKeystore.class);
} else {
keystore = unmarshalKeystore(jsonContent, V3Keystore.class);
}
} else if (version == 1) {
keystore = unmarshalKeystore(jsonContent, V3Keystore.class);
} else if (version == 44) {
keystore = unmarshalKeystore(jsonContent, HDMnemonicKeystore.class);
} else if (version == 10001) {
keystore = unmarshalKeystore(jsonContent, EOSKeystore.class);
}
if (keystore != null) {
keystoreMap.put(keystore.getId(), keystore);
}
} catch (Exception ex) {
Log.e(LOG_TAG, "Can't loaded " + file.getName() + " file", ex);
}
}
}
}
private boolean formatSourceFile(
File file, Formatter formatter, JavaFormatterOptions.Style style) {
if (file.isDirectory()) {
getLog().info("File '" + file + "' is a directory. Skipping.");
return true;
}
if (verbose) {
getLog().debug("Formatting '" + file + "'.");
}
CharSource source = com.google.common.io.Files.asCharSource(file, Charsets.UTF_8);
try {
String input = source.read();
String formatted = formatter.formatSource(input);
formatted = RemoveUnusedImports.removeUnusedImports(formatted);
if (!skipSortingImports) {
formatted = ImportOrderer.reorderImports(formatted, style);
}
if (!input.equals(formatted)) {
onNonComplyingFile(file, formatted);
nonComplyingFiles += 1;
}
filesProcessed.add(file.getAbsolutePath());
if (filesProcessed.size() % 100 == 0) {
logNumberOfFilesProcessed();
}
} catch (FormatterException | IOException e) {
getLog().error("Failed to format file '" + file + "'.", e);
return false;
}
return true;
}
@Test
public void test_toCharSource_noBomUtf8() throws IOException {
byte[] bytes = {'H', 'e', 'l', 'l', 'o'};
ByteSource byteSource = ByteSource.wrap(bytes);
CharSource charSource = UnicodeBom.toCharSource(byteSource);
String str = charSource.read();
assertThat(str).isEqualTo("Hello");
assertThat(charSource.asByteSource(StandardCharsets.UTF_8).contentEquals(byteSource)).isTrue();
assertThat(charSource.toString().startsWith("UnicodeBom")).isEqualTo(true);
}
@Test
public void whenReadUsingCharSource_thenRead() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test1.in");
final CharSource source = Files.asCharSource(file, Charsets.UTF_8);
final String result = source.read();
assertEquals(expectedValue, result);
}
@Test
public void whenReadMultipleCharSources_thenRead() throws IOException {
final String expectedValue = "Hello worldTest";
final File file1 = new File("src/test/resources/test1.in");
final File file2 = new File("src/test/resources/test1_1.in");
final CharSource source1 = Files.asCharSource(file1, Charsets.UTF_8);
final CharSource source2 = Files.asCharSource(file2, Charsets.UTF_8);
final CharSource source = CharSource.concat(source1, source2);
final String result = source.read();
assertEquals(expectedValue, result);
}