本文整理汇总了Java中com.google.common.base.Charsets.UTF_8属性的典型用法代码示例。如果您正苦于以下问题:Java Charsets.UTF_8属性的具体用法?Java Charsets.UTF_8怎么用?Java Charsets.UTF_8使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.google.common.base.Charsets
的用法示例。
在下文中一共展示了Charsets.UTF_8属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractRequestEntity
static String extractRequestEntity(ContainerRequestContext request) {
if (request.hasEntity()) {
InputStream inputStreamOriginal = request.getEntityStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamOriginal, MAX_ENTITY_READ);
bufferedInputStream.mark(MAX_ENTITY_READ);
byte[] bytes = new byte[MAX_ENTITY_READ];
int read;
try {
read = bufferedInputStream.read(bytes, 0, MAX_ENTITY_READ);
bufferedInputStream.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
request.setEntityStream(bufferedInputStream);
return new String(bytes, Charsets.UTF_8);
}
return null;
}
示例2: readLine
private String readLine() throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
int i = 0;
int c;
while ((c = raf.read()) != -1) {
i++;
out.write((byte) c);
if (c == LINE_SEP.charAt(0)) {
break;
}
}
if (i == 0) {
return null;
}
return new String(out.toByteArray(), Charsets.UTF_8);
}
示例3: getConfResourceAsReader
/**
* Get a {@link Reader} attached to the configuration resource with the
* given <code>name</code>.
*
* @param name configuration resource name.
* @return a reader attached to the resource.
*/
public Reader getConfResourceAsReader(String name) {
try {
URL url= getResource(name);
if (url == null) {
LOG.info(name + " not found");
return null;
} else {
LOG.info("found resource " + name + " at " + url);
}
return new InputStreamReader(url.openStream(), Charsets.UTF_8);
} catch (Exception e) {
return null;
}
}
示例4: write
@Override
public void write(final ArgumentOutput output) throws IOException {
final File f = new File(directory, output.docId().toString());
final PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(f), Charsets.UTF_8)));
try {
for (final Response response : format.responseOrdering().sortedCopy(output.responses())) {
final String metadata = output.metadata(response);
if (!metadata.equals(ArgumentOutput.DEFAULT_METADATA)) {
out.print(METADATA_MARKER + metadata + "\n");
}
//out.print(response.responseID());
out.print(format.identifierField(response));
out.print("\t");
final double confidence = output.confidence(response);
out.print(argToString(response, confidence) + "\n");
}
} finally {
out.close();
}
}
示例5: testReconfigure
@Test
public void testReconfigure() throws InterruptedException, IOException {
final int NUM_RECONFIGS = 20;
for (int i = 0; i < NUM_RECONFIGS; i++) {
Context context = new Context();
File file = new File(tmpDir.getAbsolutePath() + "/file-" + i);
Files.write("File " + i, file, Charsets.UTF_8);
context.put(SpoolDirectorySourceConfigurationConstants.SPOOL_DIRECTORY,
tmpDir.getAbsolutePath());
Configurables.configure(source, context);
source.start();
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
Transaction txn = channel.getTransaction();
txn.begin();
try {
Event event = channel.take();
String content = new String(event.getBody(), Charsets.UTF_8);
Assert.assertEquals("File " + i, content);
txn.commit();
} catch (Throwable t) {
txn.rollback();
} finally {
txn.close();
}
source.stop();
Assert.assertFalse("Fatal error on iteration " + i, source.hasFatalError());
}
}
示例6: readBackupFromResource
private String readBackupFromResource(final String filename) throws IOException {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(filename), Charsets.UTF_8));
final StringBuilder backup = new StringBuilder();
Io.copy(reader, backup);
reader.close();
return backup.toString();
}
示例7: readFile
List<String> readFile(String filename) throws IOException {
List<String> result = new ArrayList<String>(10000);
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(filename), Charsets.UTF_8));
String line = in.readLine();
while (line != null) {
result.add(line);
line = in.readLine();
}
in.close();
return result;
}
示例8: writePropertiesFile
void writePropertiesFile(FileSystem fs, Path path) throws IOException {
Properties p = new Properties();
p.setProperty("table", tableName);
if (families != null) {
p.setProperty("columnFamilies", families);
}
p.setProperty("targetBatchSize", Long.toString(batchSize));
p.setProperty("numHashFiles", Integer.toString(numHashFiles));
if (!isTableStartRow(startRow)) {
p.setProperty("startRowHex", Bytes.toHex(startRow));
}
if (!isTableEndRow(stopRow)) {
p.setProperty("stopRowHex", Bytes.toHex(stopRow));
}
if (scanBatch > 0) {
p.setProperty("scanBatch", Integer.toString(scanBatch));
}
if (versions >= 0) {
p.setProperty("versions", Integer.toString(versions));
}
if (startTime != 0) {
p.setProperty("startTimestamp", Long.toString(startTime));
}
if (endTime != 0) {
p.setProperty("endTimestamp", Long.toString(endTime));
}
try (OutputStreamWriter osw = new OutputStreamWriter(fs.create(path), Charsets.UTF_8)) {
p.store(osw, null);
}
}
示例9: assertThat
public static ParserAssert assertThat(LexerlessGrammarBuilder b, GrammarRuleKey rule) {
return new ParserAssert(new ActionParser<Tree>(
Charsets.UTF_8,
b,
OneCGrammar.class,
new TreeFactory(),
new OneCNodeBuilder(),
rule));
}
示例10: init
@Override
public void init(Properties config, ServletContext servletContext,
long tokenValidity) throws Exception {
String signatureSecretFile = config.getProperty(
AuthenticationFilter.SIGNATURE_SECRET_FILE, null);
Reader reader = null;
if (signatureSecretFile != null) {
try {
StringBuilder sb = new StringBuilder();
reader = new InputStreamReader(
new FileInputStream(signatureSecretFile), Charsets.UTF_8);
int c = reader.read();
while (c > -1) {
sb.append((char) c);
c = reader.read();
}
secret = sb.toString().getBytes(Charset.forName("UTF-8"));
} catch (IOException ex) {
throw new RuntimeException("Could not read signature secret file: " +
signatureSecretFile);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// nothing to do
}
}
}
}
secrets = new byte[][]{secret};
}
示例11: readStringFromBuffer
/**
* Reads a string from this buffer. Expected parameter is maximum allowed string length. Will throw IOException if
* string length exceeds this value!
*/
public String readStringFromBuffer(int maxLength)
{
int i = this.readVarIntFromBuffer();
if (i > maxLength * 4)
{
throw new DecoderException("The received encoded string buffer length is longer than maximum allowed (" + i + " > " + maxLength * 4 + ")");
}
else if (i < 0)
{
throw new DecoderException("The received encoded string buffer length is less than zero! Weird string!");
}
else
{
String s = new String(this.readBytes(i).array(), Charsets.UTF_8);
if (s.length() > maxLength)
{
throw new DecoderException("The received string length is longer than maximum allowed (" + i + " > " + maxLength + ")");
}
else
{
return s;
}
}
}
示例12: testTempFiles
public void testTempFiles() throws Exception {
String fileName = storage.uploadTempFile("test\n".getBytes(Charsets.UTF_8));
BufferedReader reader = new BufferedReader(new InputStreamReader(storage.openTempFile(fileName),
Charsets.UTF_8));
assertTrue(reader.readLine().equals("test"));
storage.deleteTempFile(fileName);
try {
storage.deleteTempFile("frob"); // Should fail because doesn't start with __TEMP__
fail();
} catch (Exception e) {
assertTrue(e instanceof RuntimeException);
}
}
示例13: put
private EtcdResult put(String key, List<BasicNameValuePair> data, Map<String, String> params, int[] expectedHttpStatusCodes,
int... expectedErrorCodes) throws EtcdClientException {
URI uri = buildUriWithKeyAndParams(key, params);
HttpPut httpPut = new HttpPut(uri);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, Charsets.UTF_8);
httpPut.setEntity(entity);
return syncExecute(httpPut, expectedHttpStatusCodes, expectedErrorCodes);
}
示例14: assertOutputMatches
private void assertOutputMatches(String string) {
String errOutput = new String(out.toByteArray(), Charsets.UTF_8);
String output = new String(out.toByteArray(), Charsets.UTF_8);
if (!errOutput.matches(string) && !output.matches(string)) {
fail("Expected output to match '" + string +
"' but err_output was:\n" + errOutput +
"\n and output was: \n" + output);
}
out.reset();
err.reset();
}
示例15: onFileInit
@Override
public void onFileInit(Path file) {
if (logger.isTraceEnabled()) {
logger.trace("Loading script file : [{}]", file);
}
Tuple<String, String> scriptNameExt = scriptNameExt(file);
if (scriptNameExt != null) {
ScriptEngineService engineService = getScriptEngineServiceForFileExt(scriptNameExt.v2());
if (engineService == null) {
logger.warn("no script engine found for [{}]", scriptNameExt.v2());
} else {
try {
//we don't know yet what the script will be used for, but if all of the operations for this lang
// with file scripts are disabled, it makes no sense to even compile it and cache it.
if (isAnyScriptContextEnabled(engineService.types()[0], engineService, ScriptType.FILE)) {
logger.info("compiling script file [{}]", file.toAbsolutePath());
try(InputStreamReader reader = new InputStreamReader(Files.newInputStream(file), Charsets.UTF_8)) {
String script = Streams.copyToString(reader);
CacheKey cacheKey = new CacheKey(engineService, scriptNameExt.v1(), null, Collections.<String, String>emptyMap());
staticCache.put(cacheKey, new CompiledScript(ScriptType.FILE, scriptNameExt.v1(), engineService.types()[0], engineService.compile(script, Collections.<String, String>emptyMap())));
scriptMetrics.onCompilation();
}
} else {
logger.warn("skipping compile of script file [{}] as all scripted operations are disabled for file scripts", file.toAbsolutePath());
}
} catch (Throwable e) {
logger.warn("failed to load/compile script [{}]", e, scriptNameExt.v1());
}
}
}
}