com.google.common.io.CharStreams Java Examples
The following examples show how to use
com.google.common.io.CharStreams.
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: AdminJsonService.java From glowroot with Apache License 2.0 | 6 votes |
@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(); }
Example #2
Source File: ApiDefinitionCommand.java From apiman-cli with Apache License 2.0 | 6 votes |
@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))); }
Example #3
Source File: PosixFileOperations.java From util with Apache License 2.0 | 6 votes |
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); } }
Example #4
Source File: PosixFileOperations.java From util with Apache License 2.0 | 6 votes |
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); } }
Example #5
Source File: DetectorFactory.java From weslang with Apache License 2.0 | 6 votes |
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); } }
Example #6
Source File: AbstractLexerTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
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; }
Example #7
Source File: UploaderTest.java From connector-sdk with Apache License 2.0 | 6 votes |
@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); } }
Example #8
Source File: ClasspathResourceUtil.java From Dream-Catcher with MIT License | 6 votes |
/** * 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); } }
Example #9
Source File: Shell.java From kurento-java with Apache License 2.0 | 6 votes |
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); } }
Example #10
Source File: PmdTemplateTest.java From sonar-p3c-pmd with MIT License | 6 votes |
@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)); }
Example #11
Source File: DefaultSecurityProviderTool.java From Dream-Catcher with MIT License | 6 votes |
@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; }
Example #12
Source File: Closure_31_Compiler_s.java From coming with MIT License | 6 votes |
/** 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)); }
Example #13
Source File: WechatMpMessageController.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * 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)); }
Example #14
Source File: YamlConfigLoader.java From digdag with Apache License 2.0 | 6 votes |
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); }
Example #15
Source File: CacheStoreTest.java From packagedrone with Eclipse Public License 1.0 | 6 votes |
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; }
Example #16
Source File: Streams.java From brooklyn-server with Apache License 2.0 | 5 votes |
/** * Consider using {@link #readFullyAndClose(Reader)} instead. */ public static String readFully(Reader is) { try { return CharStreams.toString(is); } catch (IOException ioe) { throw Exceptions.propagate(ioe); } }
Example #17
Source File: TestSchemaCommandCluster.java From kite with Apache License 2.0 | 5 votes |
@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); }
Example #18
Source File: ModuleMaster.java From The-5zig-Mod with GNU General Public License v3.0 | 5 votes |
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(); }
Example #19
Source File: JavaReaderToXUnitTest.java From tutorials with MIT License | 5 votes |
@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(); }
Example #20
Source File: StringMarshaller.java From concurrency-limits with Apache License 2.0 | 5 votes |
@Override public String parse(InputStream stream) { try { return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); } catch (IOException e) { throw new RuntimeException(e); } }
Example #21
Source File: GuavaTutorial.java From maven-framework-project with MIT License | 5 votes |
/** * 将读取的内容拷贝到一个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()); }
Example #22
Source File: BLS12G1MultiExpPrecompiledContractTest.java From besu with Apache License 2.0 | 5 votes |
@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()); }
Example #23
Source File: BLS12G2AddPrecompiledContractTest.java From besu with Apache License 2.0 | 5 votes |
@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()); }
Example #24
Source File: BLS12MapFp2ToG2PrecompiledContractTest.java From besu with Apache License 2.0 | 5 votes |
@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()); }
Example #25
Source File: S3TableConfigClient.java From presto with Apache License 2.0 | 5 votes |
/** * 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; }
Example #26
Source File: Uploader.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
private String toChecksumString ( final InputStream stream ) throws IOException { if ( stream == null ) { return ""; } return CharStreams.toString ( new InputStreamReader ( stream, StandardCharsets.UTF_8 ) ); }
Example #27
Source File: ResourceUtils.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
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; }
Example #28
Source File: PosixTarHeaderSystemTest.java From nomulus with Apache License 2.0 | 5 votes |
@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); }
Example #29
Source File: GuavaIOUnitTest.java From tutorials with MIT License | 5 votes |
@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(); }
Example #30
Source File: CassandraUtils.java From sstable-tools with Apache License 2.0 | 5 votes |
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; }