Java Code Examples for com.google.common.io.Files#asCharSource()

The following examples show how to use com.google.common.io.Files#asCharSource() . 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: 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 2
Source File: IsFlutterProjectPropertyTester.java    From dartboard with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (IS_FLUTTER_PROJECT_PROPERTY.equalsIgnoreCase(property)) {
		IResource resource = Adapters.adapt(receiver, IResource.class);
		if (resource == null) {
			return false;
		}

		IProject project = resource.getProject();
		if (project == null) {
			return false;
		}
		IResource pubspec = project.findMember(GlobalConstants.PUBSPEC_YAML);
		if (pubspec == null) {
			return false;
		}
		File pubspecFile = pubspec.getRawLocation().toFile();
		CharSource pubspecContent = Files.asCharSource(pubspecFile, Charset.defaultCharset());
		try {
			for (String line : pubspecContent.readLines()) {
				if (FLUTTER_SDK.matcher(line).matches()) {
					return true;
				}
			}
		} catch (IOException e) {
			LOG.log(DartLog.createError("Could not open pubspec.yaml", e));
		}
	}
	return false;
}
 
Example 3
Source File: Convert.java    From jopenfst with MIT License 5 votes vote down vote up
/**
 * Imports an openfst text format. You pass in the file pointing to the fst.txt file and
 * it assumes that the other files has the same prefix, but with input.syms and output.syms
 * as suffixes.  For exmaple if you pass in:
 * path/to/mymodel.fst.txt
 * It assumes that you also have:
 * path/to/mymodel.input.syms
 * path/to/mymodel.output.syms
 *
 * @param fileToFst the files' base name
 * @param semiring  the fst's semiring
 */
public static MutableFst importFst(File fileToFst, Semiring semiring) {
  Preconditions.checkArgument(fileToFst.exists(), "File to the fst.txt openfst output doesnt exist", fileToFst);
  Preconditions.checkArgument(fileToFst.getName().endsWith(FST_TXT), "fst.txt path must end in .fst.txt", fileToFst);
  String basepath = fileToFst.getAbsolutePath();
  basepath = StringUtils.removeEnd(basepath, FST_TXT);
  CharSource cs = Files.asCharSource(fileToFst, Charsets.UTF_8);

  Optional<MutableSymbolTable> maybeInputs = importSymbols(new File(basepath + INPUT_SYMS));
  Optional<MutableSymbolTable> maybeOutputs = importSymbols(new File(basepath + OUTPUT_SYMS));
  Optional<MutableSymbolTable> maybeStates = importSymbols(new File(basepath + STATES_SYMS));

  return convertFrom(cs, maybeInputs, maybeOutputs, maybeStates, semiring);
}
 
Example 4
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 5
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);
}
 
Example 6
Source File: AssessmentSpecFormats.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
private synchronized AnswerKey uncachedRead(final Symbol docid) throws IOException {
  final ImmutableList.Builder<AssessedResponse> annotated = ImmutableList.builder();
  final ImmutableList.Builder<Response> unannotated = ImmutableList.builder();
  final CorefAnnotation.Builder corefBuilder = assessmentCreator.corefBuilder(docid);

  final File f = bareOrWithSuffix(directory, docid.asString(), ACCEPTABLE_SUFFIXES);

  final CharSource source = Files.asCharSource(f, UTF_8);
  for (final String line : source.readLines()) {
    try {
      if (line.isEmpty() || line.startsWith("#")) {
        continue;
      }
      final String[] parts = line.split("\t");
      final List<String> annotationParts = Arrays.asList(parts).subList(11, parts.length);

      if (annotationParts.isEmpty()) {
        throw new IOException(String.format(
            "The assessment store file for document ID %s appears to be a system " +
                "output file with no assessment columns.", docid));
      }

      final Response response = parseArgumentFields(format, ImmutableList.copyOf(parts));
      final AssessmentCreator.AssessmentParseResult annotation =
          parseAnnotation(annotationParts);

      if (annotation.assessment().isPresent()) {
        annotated.add(AssessedResponse.of(response, annotation.assessment().get()));
      } else {
        unannotated.add(response);
      }

      if (annotation.corefId().isPresent()) {
        corefBuilder.corefCAS(response.canonicalArgument(), annotation.corefId().get());
      } else {
        corefBuilder.addUnannotatedCAS(response.canonicalArgument());
      }
    } catch (Exception e) {
      throw new IOException(String.format(
          "While reading answer key for document %s, error on line %s", docid, line), e);
    }
  }

  return assessmentCreator.createAnswerKey(docid, annotated.build(), unannotated.build(),
      corefBuilder.build());
}
 
Example 7
Source File: Grep.java    From tcl-regex-java with Apache License 2.0 4 votes vote down vote up
private void processFile(File input) throws IOException, RegexException {
    CharSource inputCharSource = Files.asCharSource(input, Charsets.UTF_8);
    processReader(inputCharSource.openBufferedStream());
}
 
Example 8
Source File: CharSources.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an instance of {@link CharSource} from a file name, specified as a String.
 *
 * @param fileName  the file name, as a String
 * @return  a new instance of {@link CharSource} with UTF-8 for charset.
 */
public static CharSource ofFileName(String fileName) {
  return Files.asCharSource(new File(fileName), Charsets.UTF_8);
}
 
Example 9
Source File: CharSources.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an instance of {@link CharSource} from a file name, specified as a String.
 * This also takes in a specific character set, as a {@link Charset}.
 *
 * @param fileName  the file name, as a String
 * @param charset  the charset to build the new CharSource based on
 * @return  a new instance of {@link CharSource}
 */
public static CharSource ofFileName(String fileName, Charset charset) {
  return Files.asCharSource(new File(fileName), charset);
}
 
Example 10
Source File: CharSources.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an instance of {@link CharSource} from a file object, specified as a {@link File}.
 *
 * @param file  the file object
 * @return  a new instance of {@link CharSource} with UTF-8 for charset.
 */
public static CharSource ofFile(File file) {
  return Files.asCharSource(file, Charsets.UTF_8);
}
 
Example 11
Source File: CharSources.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an instance of {@link CharSource} from a file object, specified as a {@link File}.
 * This also takes in a specific character set, as a {@link Charset}.
 *
 * @param file  the file object
 * @param charset  the charset to build the new CharSource based on
 * @return  a new instance of {@link CharSource}
 */
public static CharSource ofFile(File file, Charset charset) {
  return Files.asCharSource(file, charset);
}