當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。