本文整理匯總了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();
}
}
}