当前位置: 首页>>代码示例>>Java>>正文


Java CharSource.read方法代码示例

本文整理汇总了Java中com.google.common.io.CharSource.read方法的典型用法代码示例。如果您正苦于以下问题:Java CharSource.read方法的具体用法?Java CharSource.read怎么用?Java CharSource.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.io.CharSource的用法示例。


在下文中一共展示了CharSource.read方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import com.google.common.io.CharSource; //导入方法依赖的package包/类
/**
 * @param testFilePaths paths to files containing testcases.
 */
public static void main(String... testFilePaths) throws IOException {
  int testCount = 0;
  for (String testFilePath : testFilePaths) {
    File testFile = new File(testFilePath);
    ByteSource bytes = Files.asByteSource(testFile);
    // Just read bytes as chars since the URL grammar is an octet-based grammar.
    CharSource chars = bytes.asCharSource(Charsets.ISO_8859_1);

    String input = chars.read();
    boolean ok = false;
    try {
      UrlValue url = UrlValue.from(input);
      url.getAuthority(Diagnostic.Receiver.NULL);
      url.getContentMediaType();
      url.getContentMetadata();
      url.getDecodedContent();
      url.getFragment();
      url.getQuery();
      url.getRawAuthority();
      url.getRawContent();
      url.getRawPath();
      ok = true;
    } finally {
      if (!ok) {
        System.err.println("Failed on `" + input + "` from " + testFilePath);
      }
    }
    testCount += 1;
  }
  System.out.println("Ran " + testCount + " tests");
  if (testCount == 0) {
    throw new Error("No tests read");
  }
}
 
开发者ID:OWASP,项目名称:url-classifier,代码行数:38,代码来源:FuzzUrlValue.java

示例2: load

import com.google.common.io.CharSource; //导入方法依赖的package包/类
public static String load(String resourceName) {
  try {
    URL healthJsonURI = Resources.getResource(resourceName);
    CharSource healthJsonSource = Resources.asCharSource(healthJsonURI, Charsets.UTF_8);
    return healthJsonSource.read();
  } catch (Throwable t) {
    throw new RuntimeException(t);
  }
}
 
开发者ID:totango,项目名称:discovery-agent,代码行数:10,代码来源:ResourceLoader.java

示例3: readResource

import com.google.common.io.CharSource; //导入方法依赖的package包/类
public static String readResource(final String filename, final ClassLoader cl) {
    try {
        ByteSource bs = new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return getStream(filename, cl);
            }
        };
        CharSource cs = bs.asCharSource(StandardCharsets.UTF_8);
        return cs.read();
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
        return "";
    }
}
 
开发者ID:jub77,项目名称:grafikon,代码行数:16,代码来源:ResourceHelper.java

示例4: whenReadUsingCharSource_thenRead

import com.google.common.io.CharSource; //导入方法依赖的package包/类
@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);
}
 
开发者ID:wenzhucjy,项目名称:GeneralUtils,代码行数:11,代码来源:GuavaIOTest.java

示例5: whenReadMultipleCharSources_thenRead

import com.google.common.io.CharSource; //导入方法依赖的package包/类
@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);
}
 
开发者ID:wenzhucjy,项目名称:GeneralUtils,代码行数:15,代码来源:GuavaIOTest.java

示例6: test_toCharSource_noBomUtf8

import com.google.common.io.CharSource; //导入方法依赖的package包/类
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();
  assertEquals(str, "Hello");
  assertEquals(charSource.asByteSource(StandardCharsets.UTF_8), byteSource);
  assertEquals(charSource.toString().startsWith("UnicodeBom"), true);
}
 
开发者ID:OpenGamma,项目名称:Strata,代码行数:10,代码来源:UnicodeBomTest.java

示例7: prettyPrintXml

import com.google.common.io.CharSource; //导入方法依赖的package包/类
private String prettyPrintXml(CharSource inputXml) {
    try {
        Document document = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder()
                .parse(new InputSource(inputXml.openStream()));

        document.normalize();

        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']",
                                                      document,
                                                      XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        // Setup pretty print options
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        // Return pretty print xml string
        StringWriter strWriter = new StringWriter();
        t.transform(new DOMSource(document), new StreamResult(strWriter));
        return strWriter.toString();
    } catch (Exception e) {
        log.warn("Pretty printing failed", e);
        try {
            String rawInput = inputXml.read();
            log.debug("  failed input: \n{}", rawInput);
            return rawInput;
        } catch (IOException e1) {
            log.error("Failed to read from input", e1);
            return inputXml.toString();
        }
    }
}
 
开发者ID:opennetworkinglab,项目名称:onos,代码行数:42,代码来源:XmlString.java


注:本文中的com.google.common.io.CharSource.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。