本文整理匯總了Java中com.google.common.io.CharStreams類的典型用法代碼示例。如果您正苦於以下問題:Java CharStreams類的具體用法?Java CharStreams怎麽用?Java CharStreams使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CharStreams類屬於com.google.common.io包,在下文中一共展示了CharStreams類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: withLineNumbers
import com.google.common.io.CharStreams; //導入依賴的package包/類
private String withLineNumbers(String code) {
try {
return CharStreams.readLines(new StringReader(code), new LineProcessor<String>() {
private final StringBuilder lines = new StringBuilder();
private int lineNo = 1;
@Override
public boolean processLine(String line) throws IOException {
lines.append(Strings.padStart(String.valueOf(lineNo++), 3, ' ')).append(": ").append(line)
.append("\n");
return true;
}
@Override
public String getResult() {
return lines.toString();
}
});
} catch (IOException e) {
throw new IllegalStateException(e.getMessage());
}
}
示例2: map
import com.google.common.io.CharStreams; //導入依賴的package包/類
void map(@NonNull HttpConfiguration httpConfiguration) throws IOException {
org.springframework.core.io.Resource[] resources;
try {
resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(
applicationProperties.getResourcePath() + "/openapi/**.y*ml");
} catch (FileNotFoundException exp) {
LOG.warn("No Open API resources found in path:{}/openapi",
applicationProperties.getResourcePath());
return;
}
for (org.springframework.core.io.Resource resource : resources) {
InputStream inputStream =
new EnvironmentAwareResource(resource.getInputStream(), environment).getInputStream();
String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
if (!StringUtils.isBlank(result)) {
Swagger swagger = openApiParser.parse(result);
mapOpenApiDefinition(swagger, httpConfiguration);
}
}
}
示例3: verifyTarArchive
import com.google.common.io.CharStreams; //導入依賴的package包/類
/**
* Helper method to verify that the files were archived correctly by reading {@code
* tarArchiveInputStream}.
*/
private void verifyTarArchive(TarArchiveInputStream tarArchiveInputStream) throws IOException {
// Verifies fileA was archived correctly.
TarArchiveEntry headerFileA = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("some/path/to/resourceFileA", headerFileA.getName());
String fileAString =
CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileAString, fileAString);
// Verifies fileB was archived correctly.
TarArchiveEntry headerFileB = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("crepecake", headerFileB.getName());
String fileBString =
CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileBString, fileBString);
// Verifies directoryA was archived correctly.
TarArchiveEntry headerDirectoryA = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("some/path/to/", headerDirectoryA.getName());
Assert.assertNull(tarArchiveInputStream.getNextTarEntry());
}
示例4: TextFileContentProvider
import com.google.common.io.CharStreams; //導入依賴的package包/類
public TextFileContentProvider ( final File file ) throws FileNotFoundException, IOException
{
try ( FileReader reader = new FileReader ( file ) )
{
if ( file != null )
{
String data = CharStreams.toString ( reader );
if ( needFix () )
{
data = fix ( data );
}
this.data = data.getBytes ( StandardCharsets.UTF_8 );
}
else
{
this.data = null;
}
}
}
示例5: testReadEverythingMarkReset
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Test
public void testReadEverythingMarkReset() throws Exception {
Buffer buffer = new Buffer();
PrintStream stream = new PrintStream(buffer.write());
stream.print("0123456789012345678901234567890123456789");
InputStream read = buffer.read();
read.mark(0);
assertThat(CharStreams.toString(new InputStreamReader(read))).isEqualTo(
"0123456789012345678901234567890123456789");
read.reset();
assertThat(CharStreams.toString(new InputStreamReader(read))).isEqualTo(
"0123456789012345678901234567890123456789");
read.reset();
assertThat(CharStreams.toString(new InputStreamReader(read))).isEqualTo(
"0123456789012345678901234567890123456789");
}
示例6: readTextFileAttachmentWithEncoding
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public String readTextFileAttachmentWithEncoding(AttachmentScriptType attachment, String encoding)
{
if( attachment != null )
{
String type = attachment.getType();
if( type.equals(AttachmentType.FILE.toString()) || type.equals(AttachmentType.IMSRES.toString())
|| type.equals(AttachmentType.HTML.toString()) )
{
final String filename = attachment.getFilename();
try( Reader reader = new UnicodeReader(fileSystem.read(staging, filename), encoding) )
{
StringWriter writer = new StringWriter();
CharStreams.copy(reader, writer);
return writer.toString();
}
catch( IOException io )
{
throw new RuntimeException("Error reading text file attachment " + filename, io);
}
}
throw new RuntimeException("Cannot read an attachment of this type as text");
}
return null;
}
示例7: sendGrade
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public void sendGrade(ImsxPOXEnvelopeType envelope, String consumerKey, String secret, String url,
HttpServletResponse servletResponse)
{
if( url != null )
{
try
{
final Request req = createLtiOutcomesRequest(envelope, url, consumerKey, secret);
try( Response httpResp = httpService.getWebContent(req, configService.getProxyDetails()) )
{
parseError(httpResp, url);
String respBody = httpResp.getBody();
final String bodyHash = calcSha1Hash(respBody);
final OAuthMessage message = createLaunchParameters(consumerKey, secret, url, bodyHash);
servletResponse.addHeader("Authorization", message.getAuthorizationHeader(""));
CharStreams.copy(new StringReader(httpResp.getBody()), servletResponse.getWriter());
}
}
catch( Exception e )
{
throw Throwables.propagate(e);
}
}
}
示例8: getStylesheetContents
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public String getStylesheetContents()
{
final PublicFile styles = new PublicFile("htmleditorstyles");
final HtmlEditorConfiguration editorConfig = getEditorConfig();
final String filename = (Check.isEmpty(editorConfig.getStylesheetUuid()) ? "styles.css" : PathUtils.filePath(
editorConfig.getStylesheetUuid(), "styles.css"));
if( fileSystemService.fileExists(styles, filename) )
{
try (InputStream in = fileSystemService.read(styles, filename); Reader rdr = new InputStreamReader(in))
{
final StringWriter sw = new StringWriter();
CharStreams.copy(rdr, sw);
return sw.toString();
}
catch( IOException e )
{
throw Throwables.propagate(e);
}
}
return "";
}
示例9: doGet
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
final String content;
try
{
content = responseCache.get("", cacheLoader);
}
catch( ExecutionException e )
{
resp.setStatus(500);
LOGGER.error("Error serving content", e);
return;
}
if( NO_CONTENT.equals(content) )
{
resp.setStatus(404);
}
else
{
resp.setContentType("text/plain");
resp.setStatus(200);
CharStreams.copy(new StringReader(content), resp.getWriter());
}
}
示例10: get
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public Set<String> get() {
try {
try (InputStream inputStream = getClass().getResourceAsStream("SQL_RESERVED_WORDS.txt")) {
if (inputStream == null) {
throw new RuntimeException("Could not find resource: [SQL_RESERVED_WORDS.txt] near [" + getClass() + "]");
}
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
HashSet<String> sqlReservedWords = Sets.newHashSet(Splitter.on("\r\n").split(CharStreams.toString(streamReader)));
// temporary removal of words we currently have to allow
sqlReservedWords.remove("TYPE"); // DB2
sqlReservedWords.remove("OPERATION"); // DB2, SQL Server "future", PostGres
sqlReservedWords.remove("METHOD"); // PostGres
sqlReservedWords.remove("LANGUAGE"); // DB2, ODBC (?), SQL Server "future", PostGres
sqlReservedWords.remove("LOCATION"); // PostGres
sqlReservedWords.remove("YEAR"); // DB2, ODBC (?), SQL Server "future", PostGres
sqlReservedWords.remove("DAY"); // DB2, ODBC (?), SQL Server "future", PostGres
sqlReservedWords.remove("SECURITY"); // DB2, PostGres
return ImmutableSet.copyOf(sqlReservedWords);
}
} catch (IOException e) {
throw new RuntimeException("Failed to load [SQL_RESERVED_WORDS.txt]", e);
}
}
示例11: loadText
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Nullable
private String loadText(StagingFile stagingFile, String file) throws InvalidHtmlEditorPluginException
{
if( fileSystemService.fileExists(stagingFile, file) )
{
try( final UnicodeReader reader = new UnicodeReader(fileSystemService.read(stagingFile, file), "UTF-8") )
{
final StringWriter sw = new StringWriter();
CharStreams.copy(reader, sw);
return sw.toString();
}
catch( IOException io )
{
throw Throwables.propagate(io);
}
}
return null;
}
示例12: doGetItem
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Nullable
private String doGetItem(String uuid, String version, String info)
{
final Request request = createRequest("item/" + uuid + '/' + version + '/');
request.addParameter("info", info);
try( Response response = httpService.getWebContent(request, configService.getProxyDetails()) )
{
if( response.isOk() )
{
StringWriter sw = new StringWriter();
CharStreams.copy(new InputStreamReader(response.getInputStream()), sw);
return sw.toString();
}
else if( response.getCode() == 404 )
{
return null;
}
throw new RuntimeException("Unable to query cloud item: " + response.getMessage());
}
catch( IOException io )
{
throw Throwables.propagate(io);
}
}
示例13: doFacetSearchQuery
import com.google.common.io.CharStreams; //導入依賴的package包/類
private String doFacetSearchQuery(String... nodes)
{
final Request request = createRequest("search/facet");
request.addParameter("nodes", Joiner.on(',').join(nodes));
request.addParameter("breadth", 200);
try( Response response = httpService.getWebContent(request, configService.getProxyDetails()) )
{
if( response.isOk() )
{
StringWriter sw = new StringWriter();
CharStreams.copy(new InputStreamReader(response.getInputStream()), sw);
return sw.toString();
}
throw new RuntimeException("Unable to query cloud search results: " + response.getMessage());
}
catch( IOException io )
{
throw Throwables.propagate(io);
}
}
示例14: importControl
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public String importControl(byte[] zipFileData) throws IOException
{
StagingFile staging = stagingService.createStagingArea();
try
{
fileSystemService.unzipFile(staging, new ByteArrayInputStream(zipFileData), ArchiveType.ZIP);
Reader reader = new InputStreamReader(fileSystemService.read(staging, CONTROL_XML));
StringWriter writer = new StringWriter();
CharStreams.copy(reader, writer);
return writer.toString();
}
catch( IOException io )
{
Throwables.propagate(io);
return null; // unreachable
}
finally
{
stagingService.removeStagingArea(staging, true);
}
}
示例15: handleError
import com.google.common.io.CharStreams; //導入依賴的package包/類
@Override
public void handleError(ClientHttpResponse response) throws IOException {
String seyrenResponseBody;
LOGGER.debug("Response : {} {}", response.getStatusCode(), response.getStatusText());
if (response.getBody() != null) {
seyrenResponseBody = CharStreams.toString(new InputStreamReader(response.getBody(), "UTF-8"));
} else {
seyrenResponseBody = "Response whithout body";
}
CerebroException exception = new CerebroException(ErrorCode.SEYREN_ERROR, seyrenResponseBody);
throw exception;
}