类com.google.common.io.CharStreams源码实例Demo

下面列出了怎么用com.google.common.io.CharStreams的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: glowroot   文件: AdminJsonService.java
@GET(path = "/backend/admin/json", permission = "admin:view")
String getAllAdmin() throws Exception {
    Object config;
    if (central) {
        config = configRepository.getAllCentralAdminConfig();
    } else {
        config = configRepository.getAllEmbeddedAdminConfig();
    }
    ObjectNode rootNode = mapper.valueToTree(config);
    AllAdminConfigUtil.removePasswords(rootNode);
    ObjectMappers.stripEmptyContainerNodes(rootNode);
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    try {
        jg.setPrettyPrinter(ObjectMappers.getPrettyPrinter());
        jg.writeObject(rootNode);
    } finally {
        jg.close();
    }
    // newline is not required, just a personal preference
    sb.append(ObjectMappers.NEWLINE);
    return sb.toString();
}
 
源代码2 项目: apiman-cli   文件: ApiDefinitionCommand.java
@Override
public void performFinalAction(JCommander parser) throws CommandException {
    if (!definitionStdIn && null == definitionFile) {
        throw new ExitWithCodeException(1, "API definition must be provided", true);
    }

    // read definition from STDIN or file
    String definition;
    try (InputStream is = (definitionStdIn ? System.in : Files.newInputStream(definitionFile))) {
        definition = CharStreams.toString(new InputStreamReader(is));

    } catch (IOException e) {
        throw new CommandException(e);
    }

    LOGGER.debug("Adding definition to API '{}' with contents: {}", this::getModelName, () -> definition);

    ManagementApiUtil.invokeAndCheckResponse(() ->
            getManagerConfig().buildServerApiClient(VersionAgnosticApi.class, serverVersion).setDefinition(orgName, name, version, definitionType, new TypedString(definition)));
}
 
源代码3 项目: util   文件: PosixFileOperations.java
public static int getPID() throws IOException {
    Process proc = Runtime.getRuntime().exec(new String[]{"bash", "-c", "echo $PPID"});
    try {
        int result = proc.waitFor();
        if (result != 0) {
            throw new IOException(
                    formatWithStdStreams(
                            proc,
                            "error running bash -c \"echo $PPID\", exit code: " + result + "\nstdout:\n%s\nstderr:\n%s"
                    ));
        }
        StringWriter out = new StringWriter();
        CharStreams.copy(new InputStreamReader(proc.getInputStream(), Charsets.UTF_8), out);
        return Integer.parseInt(out.toString().trim());
    } catch (InterruptedException e) {
        log.error("exception during exec", e);
        throw new IOException("exec failed", e);
    }
}
 
源代码4 项目: util   文件: PosixFileOperations.java
public static String lsla(File file) throws IOException {
    Process proc = Runtime.getRuntime().exec(new String[]{"ls", "-la"}, null, file);
    try {
        final int result = proc.waitFor();
        if (result != 0) {
            throw new IOException(
                    formatWithStdStreams(
                            proc,
                            "error running ls -la process, exit code: " + result + "\nstdout:\n%s\nstderr:\n%s"
                    )
            );
        }
        final StringWriter out = new StringWriter();
        CharStreams.copy(new InputStreamReader(proc.getInputStream(), Charsets.UTF_8), out);
        return out.toString();
    } catch (InterruptedException e) {
        log.error("exception during exec", e);
        throw new IOException("exec failed", e);
    }
}
 
源代码5 项目: weslang   文件: DetectorFactory.java
public static void loadDefaultProfiles() throws LangDetectException,
                                                IOException,
                                                UnsupportedEncodingException {
    // Return immediately in case profiles were already loaded.
    if (DetectorFactory.getLangList().size() != 0) {
        return;
    }
    InputStream is = DetectorFactory.class.getResourceAsStream(profilesFile);
    List<String> languages = CharStreams.readLines(
        new InputStreamReader(is, StandardCharsets.UTF_8));
    List<String> jsonProfiles = new ArrayList<String>();
    for (String language : languages) {
        jsonProfiles.add(
            getStringFromFile(profilesPath + language));
    }
    if (jsonProfiles.size() > 1) {
        DetectorFactory.loadProfile(jsonProfiles);
    }
}
 
源代码6 项目: xtext-eclipse   文件: AbstractLexerTest.java
private LinkedHashMap<Integer, String> tokenNames(Reader tokensReader) throws Exception {
	LinkedHashMap<Integer, String> result = new LinkedHashMap<>();
	List<String> lines = CharStreams.readLines(tokensReader);
	for (String line : lines) {
		String[] s = line.split("=");
		String name = s[0];
		int index = Integer.parseInt(s[1]);
		String nameOrKEYWORD = null;
		if (name.startsWith("KEYWORD")) {
			nameOrKEYWORD = "KEYWORD";
		} else {
			nameOrKEYWORD = name;
		}
		result.put(index, nameOrKEYWORD);
	}
	return result;
}
 
源代码7 项目: connector-sdk   文件: UploaderTest.java
@Test
public void testUrlInputStreamContent() throws Exception {
  String fileContent = "This is the file content";
  File file = tempFolder.newFile("content.txt");
  try (FileWriter writer = new FileWriter(file)) {
    writer.write(fileContent);
  }
  Uploader uploader =
      new Uploader.Builder()
          .setServiceAccountKeyFilePath(SERVICE_ACCOUNT_FILE_PATH)
          .setUploaderHelper(uploaderHelper)
          .build();

  Uploader.UrlInputStreamContent input = uploader.new UrlInputStreamContent(
      "text/plain", file.toURI().toURL());
  assertTrue(input.retrySupported());
  assertEquals(fileContent.length(), input.getLength());
  try (InputStream in = input.getInputStream()) {
    String content = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
    assertEquals(fileContent, content);
  }
}
 
源代码8 项目: Dream-Catcher   文件: ClasspathResourceUtil.java
/**
 * Retrieves a classpath resource using the {@link ClasspathResourceUtil} classloader and converts it to a String using the specified
 * character set. If any error occurs while reading the resource, this method throws
 * {@link net.lightbody.bmp.mitm.exception.UncheckedIOException}. If the classpath resource cannot be found, this
 * method throws a FileNotFoundException wrapped in an UncheckedIOException.
 *
 * @param resource classpath resource to load
 * @param charset charset to use to decode the classpath resource
 * @return a String
 * @throws UncheckedIOException if the classpath resource cannot be found or cannot be read for any reason
 */
public static String classpathResourceToString(String resource, Charset charset) throws UncheckedIOException {
    if (resource == null) {
        throw new IllegalArgumentException("Classpath resource to load cannot be null");
    }

    if (charset == null) {
        throw new IllegalArgumentException("Character set cannot be null");
    }

    try {
        InputStream resourceAsStream = ClasspathResourceUtil.class.getResourceAsStream(resource);
        if (resourceAsStream == null) {
            throw new UncheckedIOException(new FileNotFoundException("Unable to locate classpath resource: " + resource));
        }

        // the classpath resource was found and opened. wrap it in a Reader and return its contents.
        Reader resourceReader = new InputStreamReader(resourceAsStream, charset);

        return CharStreams.toString(resourceReader);
    } catch (IOException e) {
        throw new UncheckedIOException("Error occurred while reading classpath resource", e);
    }
}
 
源代码9 项目: kurento-java   文件: Shell.java
public static String runAndWaitNoLog(final String... command) {
  Process p;
  try {
    p = new ProcessBuilder(command).redirectErrorStream(true).start();

    String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), "UTF-8"));

    p.destroy();

    return output;

  } catch (IOException e) {
    throw new KurentoException(
        "Exception executing command on the shell: " + Arrays.toString(command), e);
  }
}
 
源代码10 项目: sonar-p3c-pmd   文件: PmdTemplateTest.java
@Test
public void should_process_input_file() throws Exception {
  doAnswer(new Answer<Void>() {
    @Override
    public Void answer(InvocationOnMock invocation) throws Throwable {
      InputStream inputStreamArg = (InputStream) invocation.getArguments()[0];
      List<String> inputStreamLines = CharStreams.readLines(new InputStreamReader(inputStreamArg));
      assertThat(inputStreamLines).containsExactly("Example source");
      return null;
    }
  }).when(processor).processSourceCode(any(InputStream.class), eq(rulesets), eq(ruleContext));
  
  new PmdTemplate(configuration, processor).process(inputFile, rulesets, ruleContext);

  verify(ruleContext).setSourceCodeFilename(inputFile.getAbsolutePath());
  verify(processor).processSourceCode(any(InputStream.class), eq(rulesets), eq(ruleContext));
}
 
@Override
public X509Certificate decodePemEncodedCertificate(Reader certificateReader) {
    // JCA supports reading PEM-encoded X509Certificates fairly easily, so there is no need to use BC to read the cert
    Certificate certificate;

    // the JCA CertificateFactory takes an InputStream, so convert the reader to a stream first. converting to a String first
    // is not ideal, but is relatively straightforward. (PEM certificates should only contain US_ASCII-compatible characters.)
    try {
        InputStream certificateAsStream = new ByteArrayInputStream(CharStreams.toString(certificateReader).getBytes(Charset.forName("US-ASCII")));
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        certificate = certificateFactory.generateCertificate(certificateAsStream);
    } catch (CertificateException | IOException e) {
        throw new ImportException("Unable to read PEM-encoded X509Certificate", e);
    }

    if (!(certificate instanceof X509Certificate)) {
        throw new ImportException("Attempted to import non-X.509 certificate as X.509 certificate");
    }

    return (X509Certificate) certificate;
}
 
源代码12 项目: coming   文件: Closure_31_Compiler_s.java
/** Load a library as a resource */
@VisibleForTesting
Node loadLibraryCode(String resourceName) {
  String originalCode;
  try {
    originalCode = CharStreams.toString(new InputStreamReader(
        Compiler.class.getResourceAsStream(
            String.format("js/%s.js", resourceName)),
        Charsets.UTF_8));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return Normalize.parseAndNormalizeSyntheticCode(
      this, originalCode,
      String.format("jscomp_%s_", resourceName));
}
 
源代码13 项目: super-cloudops   文件: WechatMpMessageController.java
/**
 * Receiving messages sent by the wechat server
 *
 * @param request
 * @param response
 * @throws IOException
 * @throws Exception
 */
@PostMapping(path = URI_S_WECHAT_MP_RECEIVE, consumes = { "application/xml;charset=UTF-8",
		"text/xml;charset=UTF-8" }, produces = { "application/xml;charset=UTF-8", "text/xml;charset=UTF-8" })
public void postReceive(@NotBlank @RequestParam(name = "signature") String signature,
		@NotBlank @RequestParam(name = "timestamp") String timestamp, @NotBlank @RequestParam(name = "nonce") String nonce,
		@RequestParam(name = "openid", required = false) String openId, HttpServletRequest request,
		HttpServletResponse response) throws IOException {
	log.info("Receive from WeChat post [{}]", getFullRequestURI(request));
	// Validation
	assertionSignature(signature, timestamp, nonce);

	// Processing
	String input = CharStreams.toString(new InputStreamReader(request.getInputStream(), Charsets.UTF_8));
	String output = onReceive(input);
	log.info("Reply to WeChat Server => {}", output);

	write(response, HttpServletResponse.SC_OK, MediaType.APPLICATION_XML_UTF_8.toString(), output.getBytes(Charsets.UTF_8));
}
 
源代码14 项目: digdag   文件: YamlConfigLoader.java
ObjectNode loadParameterizedInclude(Path path, Config params)
    throws IOException
{
    String content;
    try (InputStream in = Files.newInputStream(path)) {
        content = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
    }

    Yaml yaml = new Yaml(new YamlParameterizedConstructor(), new Representer(), new DumperOptions(), new YamlTagResolver());
    ObjectNode object = normalizeValidateObjectNode(yaml.load(content));

    Path includeDir = path.toAbsolutePath().getParent();
    if (includeDir == null) {
        throw new FileNotFoundException("Loading file named '/' is invalid");
    }

    return new ParameterizeContext(includeDir, params).evalObjectRecursive(object);
}
 
源代码15 项目: packagedrone   文件: CacheStoreTest.java
private static String streamed ( final MetaKey key, final Streamer func ) throws IOException
{
    final Holder<String> result = new Holder<> ();

    final boolean found = func.streamer ( key, ( stream ) -> {
        final InputStreamReader reader = new InputStreamReader ( stream, StandardCharsets.UTF_8 );
        result.value = CharStreams.toString ( reader );
    } );

    if ( !found )
    {
        return null;
    }

    return result.value;
}
 
源代码16 项目: brooklyn-server   文件: Streams.java
/**
 * Consider using {@link #readFullyAndClose(Reader)} instead.
 */
public static String readFully(Reader is) {
    try {
        return CharStreams.toString(is);
    } catch (IOException ioe) {
        throw Exceptions.propagate(ioe);
    }
}
 
源代码17 项目: kite   文件: TestSchemaCommandCluster.java
@Test
public void testHDFSCSVSchemaToHDFSFile() throws Exception {
  String csvSample = "hdfs:/tmp/sample/users.csv";
  FSDataOutputStream out = getDFS()
      .create(new Path(csvSample), true /* overwrite */);
  OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
  writer.append("id, username, email\n");
  writer.append("1, test, [email protected]\n");
  writer.close();

  Schema schema = SchemaBuilder.record("User").fields()
      .optionalLong("id")
      .optionalString("username")
      .optionalString("email")
      .endRecord();

  String hdfsSchemaPath = "hdfs:/tmp/schemas/csv2.avsc";
  CSVSchemaCommand command = new CSVSchemaCommand(console);
  command.setConf(getConfiguration());
  command.samplePaths = Lists.newArrayList(csvSample);
  command.outputPath = hdfsSchemaPath;
  command.recordName = "User";
  int rc = command.run();
  Assert.assertEquals("Should return success code", 0, rc);
  String fileContent = CharStreams.toString(
      new InputStreamReader(getDFS().open(new Path(hdfsSchemaPath)), "utf8"));
  Assert.assertTrue("File should contain pretty printed schema",
      TestUtil.matchesSchema(schema).matches(fileContent));
  verifyNoMoreInteractions(console);
}
 
源代码18 项目: The-5zig-Mod   文件: ModuleMaster.java
private JsonObject getDefaultRoot() throws Throwable {
	String json;
	JsonElement element;
	try {
		json = CharStreams.toString(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("core/modules.json")));
		element = parseJson(json);
	} catch (Throwable e) {
		The5zigMod.logger.error("Could not load default module data!");
		throw e;
	}

	return element.getAsJsonObject();
}
 
源代码19 项目: tutorials   文件: JavaReaderToXUnitTest.java
@Test
public void givenUsingGuava_whenConvertingReaderIntoInputStreamWithCharset_thenCorrect() throws IOException {
    final Reader initialReader = new StringReader("With Guava");

    final InputStream targetStream = new ByteArrayInputStream(CharStreams.toString(initialReader).getBytes(Charsets.UTF_8));

    initialReader.close();
    targetStream.close();
}
 
源代码20 项目: concurrency-limits   文件: StringMarshaller.java
@Override
public String parse(InputStream stream) {
    try {
        return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
源代码21 项目: maven-framework-project   文件: GuavaTutorial.java
/**
 * 将读取的内容拷贝到一个writer中去
 * @throws Exception
 */
@Test
public void example7() throws Exception{
	File file = new File("src/main/resources/sample.txt");
	StringWriter writer = new StringWriter();
	FileWriter fileWriter = new FileWriter(new File("src/main/resources/sample-copy.txt"));
	CharStreams.copy(new FileReader(file), fileWriter);//写文件
	fileWriter.flush();
	fileWriter.close();
	System.out.println(writer.toString());
}
 
@Parameterized.Parameters
public static Iterable<String[]> parameters() throws IOException {
  return CharStreams.readLines(
          new InputStreamReader(
              BLS12G1MultiExpPrecompiledContractTest.class.getResourceAsStream("g1_multiexp.csv"),
              UTF_8))
      .stream()
      .map(line -> line.split(",", 4))
      .collect(Collectors.toList());
}
 
源代码23 项目: besu   文件: BLS12G2AddPrecompiledContractTest.java
@Parameterized.Parameters
public static Iterable<String[]> parameters() throws IOException {
  return CharStreams.readLines(
          new InputStreamReader(
              BLS12G2AddPrecompiledContractTest.class.getResourceAsStream("g2_add.csv"), UTF_8))
      .stream()
      .map(line -> line.split(",", 4))
      .collect(Collectors.toList());
}
 
@Parameterized.Parameters
public static Iterable<String[]> parameters() throws IOException {
  return CharStreams.readLines(
          new InputStreamReader(
              BLS12MapFp2ToG2PrecompiledContractTest.class.getResourceAsStream("fp2_to_g2.csv"),
              UTF_8))
      .stream()
      .map(line -> line.split(",", 4))
      .collect(Collectors.toList());
}
 
源代码25 项目: presto   文件: S3TableConfigClient.java
/**
 * Connect to S3 directory to look for new or updated table definitions and then
 * update the map.
 */
private void updateTablesFromS3()
{
    long now = System.currentTimeMillis();

    AmazonS3Client s3client = clientManager.getS3Client();

    for (S3ObjectSummary summary : getObjectSummaries()) {
        if (!descriptors.containsKey(summary.getKey()) || summary.getLastModified().getTime() >= lastCheck) {
            // New or updated file, so we must read from AWS
            if (summary.getKey().endsWith("/")) {
                continue;
            }

            log.info("Getting : %s - %s", summary.getBucketName(), summary.getKey());
            S3Object object = s3client.getObject(new GetObjectRequest(summary.getBucketName(), summary.getKey()));

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent(), UTF_8))) {
                KinesisStreamDescription table = streamDescriptionCodec.fromJson(CharStreams.toString(reader));
                descriptors.put(summary.getKey(), table);
                log.info("Put table description into the map from %s", summary.getKey());
            }
            catch (IOException iox) {
                log.error("Problem reading input stream from object.", iox);
                throwIfUnchecked(iox);
                throw new RuntimeException(iox);
            }
        }
    }

    log.info("Completed updating table definitions from S3.");
    lastCheck = now;
}
 
源代码26 项目: packagedrone   文件: Uploader.java
private String toChecksumString ( final InputStream stream ) throws IOException
{
    if ( stream == null )
    {
        return "";
    }
    return CharStreams.toString ( new InputStreamReader ( stream, StandardCharsets.UTF_8 ) );
}
 
源代码27 项目: eclipse.jdt.ls   文件: ResourceUtils.java
public static String getContent(IFile file) throws CoreException {
	if (file == null) {
		return null;
	}
	String content;
	try {
		try (final Reader reader = new InputStreamReader(file.getContents())) {
			content = CharStreams.toString(reader);
		}
	} catch (IOException e) {
		throw new CoreException(StatusFactory.newErrorStatus("Can not get " + file.getRawLocation() + " content", e));
	}
	return content;
}
 
源代码28 项目: nomulus   文件: PosixTarHeaderSystemTest.java
@Test
@Ignore
public void testCreateBigWebScaleData() throws Exception {
  assumeTrue(hasCommand("tar"));

  String name = "rando_numberissian.mov";
  byte[] data = new byte[4 * 1024 * 1024];
  Random rand = new Random();
  rand.nextBytes(data);

  String tarName = "occupy.tar";
  File tarFile = folder.newFile(tarName);
  try (FileOutputStream output = new FileOutputStream(tarFile)) {
    output.write(new PosixTarHeader.Builder()
        .setName(name)
        .setSize(data.length)
        .build()
        .getBytes());
    output.write(data);
    output.write(new byte[1024]);
  }
  assertThat(tarFile.length() % 512).isEqualTo(0);

  String[] cmd = {"tar", "-xf", tarName};
  String[] env = {"PATH=" + System.getenv("PATH")};
  File     cwd = folder.getRoot();
  Process  pid = Runtime.getRuntime().exec(cmd, env, cwd);
  String   err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
  assertThat(pid.waitFor()).isEqualTo(0);
  assertThat(err.trim()).isEmpty();

  File dataFile = new File(cwd, name);
  assertThat(dataFile.exists()).isTrue();
  assertThat(dataFile.isFile()).isTrue();
  assertThat(Files.asByteSource(dataFile).read()).isEqualTo(data);

  Set<String> expectedFiles = ImmutableSet.of(tarName, name);
  assertThat(ImmutableSet.copyOf(folder.getRoot().list())).isEqualTo(expectedFiles);
}
 
源代码29 项目: tutorials   文件: GuavaIOUnitTest.java
@Test
public void whenReadUsingCharStream_thenRead() throws IOException {
    final String expectedValue = "Hello world";

    final FileReader reader = new FileReader("src/test/resources/test1.in");
    final String result = CharStreams.toString(reader);

    assertEquals(expectedValue, result);
    reader.close();
}
 
源代码30 项目: sstable-tools   文件: CassandraUtils.java
public static CFMetaData tableFromCQL(InputStream source, UUID cfid) throws IOException {
    String schema = CharStreams.toString(new InputStreamReader(source, "UTF-8"));
    logger.trace("Loading Schema" + schema);
    CFStatement statement = (CFStatement) QueryProcessor.parseStatement(schema);
    String keyspace = "";
    try {
        keyspace = statement.keyspace() == null ? "turtles" : statement.keyspace();
    } catch (AssertionError e) { // if -ea added we should provide lots of warnings that things probably wont work
        logger.warn("Remove '-ea' JVM option when using sstable-tools library");
        keyspace = "turtles";
    }
    statement.prepareKeyspace(keyspace);
    if(Schema.instance.getKSMetaData(keyspace) == null) {
        Schema.instance.setKeyspaceMetadata(KeyspaceMetadata.create(keyspace, KeyspaceParams.local(), Tables.none(),
                Views.none(), getTypes(), Functions.none()));
    }
    CFMetaData cfm;
    if(cfid != null) {
        cfm = ((CreateTableStatement) statement.prepare().statement).metadataBuilder().withId(cfid).build();
        KeyspaceMetadata prev = Schema.instance.getKSMetaData(keyspace);
        List<CFMetaData> tables = Lists.newArrayList(prev.tablesAndViews());
        tables.add(cfm);
        Schema.instance.setKeyspaceMetadata(prev.withSwapped(Tables.of(tables)));
        Schema.instance.load(cfm);
    } else {
        cfm = ((CreateTableStatement) statement.prepare().statement).getCFMetaData();
    }
    return cfm;
}