Java Code Examples for com.google.common.io.CharSource#read()

The following examples show how to use com.google.common.io.CharSource#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCodeProvider.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/***/
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();
	}
}
 
Example 2
Source File: WalletManager.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
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);
      }
    }
  }
}
 
Example 3
Source File: AbstractFMT.java    From fmt-maven-plugin with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: UnicodeBomTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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);
}