本文整理汇总了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");
}
}
示例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);
}
}
示例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 "";
}
}
示例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);
}
示例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);
}
示例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);
}
示例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();
}
}
}