本文整理汇总了Java中com.google.api.client.util.Charsets类的典型用法代码示例。如果您正苦于以下问题:Java Charsets类的具体用法?Java Charsets怎么用?Java Charsets使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Charsets类属于com.google.api.client.util包,在下文中一共展示了Charsets类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onEnable
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Override
public void onEnable() {
try {
uuid_map = new HashMap<String, UUID>();
getConfig().options().copyDefaults(true);
saveConfig();
initializeLocal();
registerEvents();
registerCommands();
if(uuidFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(uuidFile));
byte[] bytes = reader.readLine().getBytes(Charsets.UTF_8);
uuid_map = Serializer.<Map<String, UUID>>deserializeBase64(bytes);
reader.close();
} else {
uuidFile.createNewFile();
}
} catch (Exception | Error e) {
getLogger().log(Level.WARNING, e.getMessage(), e);
}
}
示例2: onDisable
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Override
public void onDisable() {
try {
logCat.close();
String dat = new String(Serializer.<Map<String, UUID>>serializeBase64(uuid_map), Charsets.UTF_8);
if(uuidFile.exists()) {
BufferedWriter bw = new BufferedWriter(new FileWriter(uuidFile));
bw.write(dat);
bw.flush();
bw.close();
} else {
}
} catch (Exception | Error e) {
getLogger().log(Level.SEVERE, e.getMessage(), e);
}
}
示例3: httpRequest
import com.google.api.client.util.Charsets; //导入依赖的package包/类
private static String httpRequest(String url, Object data, String method, int timeoutMilliseconds/*毫秒*/, int retryTimes) {
Preconditions.checkArgument(retryTimes <= 10 && retryTimes >= 0, "retryTimes should between 0(include) and 10(include)");
method = StringUtils.upperCase(method);
Preconditions.checkArgument(HttpMethod.resolve(method) != null, "http request method error");
try {
HttpRequest request = getHttpRequest(url, data, method);
long start = System.currentTimeMillis();
String uuid = StringUtils.left(UUID.randomUUID().toString(), 13);
logger.info("UUID:{}, Request URL:{} , method:{}, Request data:{}", uuid, url, method, JsonUtil.writeValueQuite(data));
request.setNumberOfRetries(retryTimes);
request.setConnectTimeout(timeoutMilliseconds);
request.setLoggingEnabled(LOGGING_ENABLED);
HttpResponse response = request.execute();
response.setLoggingEnabled(LOGGING_ENABLED);
InputStream in = new BufferedInputStream(response.getContent());
String res = StreamUtils.copyToString(in, Charsets.UTF_8);
logger.info("UUID:{}, Request cost [{}ms], Response data:{}", uuid, (System.currentTimeMillis() - start), res);
return res;
} catch (IOException e) {
logger.warn("Http request error", e);
}
return StringUtils.EMPTY;
}
示例4: testUpdateFile
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testUpdateFile() throws Exception {
// Given
File file = createTextFile(gdriveConnectionUser1, "myFile.txt", "the contents");
UpdateFileOperation updateFileOperation = new UpdateFileOperation(gdriveConnectionUser1.getDrive(), clock,
Duration.ofMinutes(10));
// When
// do not set etag. Otherwise it sometimes fails. Looks like the file is
// "modified" by gdrive after creation.
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "the new contents".getBytes(Charsets.UTF_8), null,
new NullProgressMonitor());
// Then
assertEquals("the new contents", new String(downloadFile(gdriveConnectionUser1, file.getId()), Charsets.UTF_8));
}
示例5: testUpdateFileDoesNotCreateNewRevisionIfModificationsAreCloseInTime
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testUpdateFileDoesNotCreateNewRevisionIfModificationsAreCloseInTime() throws Exception {
// Given
File file = createTextFile(gdriveConnectionUser1, "myFile.txt", "original contents");
UpdateFileOperation updateFileOperation = new UpdateFileOperation(gdriveConnectionUser1.getDrive(), clock,
Duration.ofMinutes(10));
// When
// do not set etag. Otherwise it sometimes fails. Looks like the file is
// "modified" by gdrive after creation.
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "first modification".getBytes(Charsets.UTF_8), null,
new NullProgressMonitor());
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "second modification".getBytes(Charsets.UTF_8),
null, new NullProgressMonitor());
// Then
assertEquals("second modification",
new String(downloadFile(gdriveConnectionUser1, file.getId()), Charsets.UTF_8));
assertEquals(1, getRevisionsCount(gdriveConnectionUser1, file.getId()));
}
示例6: testUpdateFileCreatesNewRevisionIfUserIsNotTheSameThanForPreviousModification
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testUpdateFileCreatesNewRevisionIfUserIsNotTheSameThanForPreviousModification() throws Exception {
// Given
File file = createTextFile(gdriveConnectionUser1, "myFile.txt", "original contents");
share(gdriveConnectionUser1, file.getId(), GDriveTestUser.USER2.getEmail());
UpdateFileOperation updateFileOperationUser1 = new UpdateFileOperation(gdriveConnectionUser1.getDrive(), clock,
Duration.ofMinutes(10));
UpdateFileOperation updateFileOperationUser2 = new UpdateFileOperation(gdriveConnectionUser2.getDrive(), clock,
Duration.ofMinutes(10));
// When
// do not set etag. Otherwise it sometimes fails. Looks like the file is
// "modified" by gdrive after creation.
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser2, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperationUser1.updateFile(file.getId(), TEXT_MIMETYPE, "first modification".getBytes(Charsets.UTF_8),
null, new NullProgressMonitor());
updateFileOperationUser2.updateFile(file.getId(), TEXT_MIMETYPE, "second modification".getBytes(Charsets.UTF_8),
null, new NullProgressMonitor());
// Then
assertEquals("second modification",
new String(downloadFile(gdriveConnectionUser1, file.getId()), Charsets.UTF_8));
assertEquals(2, getRevisionsCount(gdriveConnectionUser1, file.getId()));
}
示例7: testUpdateFileCreatesNewRevisionIfModificationsAreNotCloseInTime
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testUpdateFileCreatesNewRevisionIfModificationsAreNotCloseInTime() throws Exception {
// Given
File file = createTextFile(gdriveConnectionUser1, "myFile.txt", "original contents");
UpdateFileOperation updateFileOperation = new UpdateFileOperation(gdriveConnectionUser1.getDrive(), clock,
Duration.ofMinutes(10));
// When
// do not set etag. Otherwise it sometimes fails. Looks like the file is
// "modified" by gdrive after creation.
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "first modification".getBytes(Charsets.UTF_8), null,
new NullProgressMonitor());
when(clock.instant())
.thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(11, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "second modification".getBytes(Charsets.UTF_8),
null, new NullProgressMonitor());
// Then
assertEquals("second modification",
new String(downloadFile(gdriveConnectionUser1, file.getId()), Charsets.UTF_8));
assertEquals(2, getRevisionsCount(gdriveConnectionUser1, file.getId()));
}
示例8: testCannotUpdateFileThatHasBeenUpdatedSince
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testCannotUpdateFileThatHasBeenUpdatedSince() throws Exception {
// Given
File file = createTextFile(gdriveConnectionUser1, "myFile.txt", "the contents");
UpdateFileOperation updateFileOperation = new UpdateFileOperation(gdriveConnectionUser1.getDrive(), clock,
Duration.ofMinutes(10));
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "the new contents".getBytes(Charsets.UTF_8),
file.getEtag(), new NullProgressMonitor());
exception.expect(GoogleJsonResponseException.class);
exception.expectMessage("412 Precondition Failed");
// When
when(clock.instant()).thenReturn(lastModified(gdriveConnectionUser1, file.getId()).plus(4, ChronoUnit.MINUTES));
updateFileOperation.updateFile(file.getId(), TEXT_MIMETYPE, "the newest contents".getBytes(Charsets.UTF_8),
file.getEtag(), new NullProgressMonitor());
// Then
}
示例9: appendLink
import com.google.api.client.util.Charsets; //导入依赖的package包/类
static void appendLink(Path file, String url) throws IOException {
byte[] data = Files.readAllBytes(file);
byte[] alreadyThere = "<link rel=\"canonical\"".getBytes(Charsets.ISO_8859_1);
if (arrayIndexOf(data, alreadyThere, 0) != -1) {
System.out.printf("File %s has a link-rel-canonical already.%n", file);
} else {
byte[] endHead = "</head>".getBytes(Charsets.ISO_8859_1);
int endHeadIndex = arrayIndexOf(data, endHead, 0);
if (endHeadIndex < 0) {
System.out.printf("File %s has no </head>?!%n", file);
} else {
System.out.printf("Adding link %s to file %s.%n", url, file);
byte[] toInsert = ("<link rel=\"canonical\" href=\"" + url + "\"/>").getBytes(Charsets.ISO_8859_1);
byte[] newData = new byte[data.length + toInsert.length];
System.arraycopy(data, 0, newData, 0, endHeadIndex);
System.arraycopy(toInsert, 0, newData, endHeadIndex, toInsert.length);
System.arraycopy(data, endHeadIndex, newData, endHeadIndex + toInsert.length, data.length - endHeadIndex);
Files.write(file, newData);
}
}
}
示例10: decode
import com.google.api.client.util.Charsets; //导入依赖的package包/类
public SeriesKey decode(ByteString key, Transform<String, Series> transform) throws Exception {
final String string = key.toString(Charsets.UTF_8);
final List<String> parts = SPLITTER.splitToList(string);
if (parts.size() != 3) {
throw new IllegalArgumentException("Not a valid key: " + string);
}
final String category = parts.get(0);
if (!this.category.equals(category)) {
throw new IllegalArgumentException(
"Key is in the wrong category (expected " + this.category + "): " + string);
}
final LocalDate date = LocalDate.parse(parts.get(1));
final Series series = transform.transform(parts.get(2));
return new SeriesKey(date, series);
}
示例11: testKeyEncoding
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testKeyEncoding() throws Exception {
doReturn("series").when(toString).transform(series);
final ByteString bytes =
foo.encode(new SeriesKeyEncoding.SeriesKey(date, series), toString);
assertEquals(ByteString.copyFrom("foo/2016-01-31/series", Charsets.UTF_8), bytes);
doReturn(series).when(fromString).transform("series");
final SeriesKeyEncoding.SeriesKey k = foo.decode(bytes, fromString);
assertEquals(date, k.getDate());
assertEquals(series, k.getSeries());
assertEquals(ByteString.copyFrom("foo/2016-02-01", Charsets.UTF_8),
foo.rangeKey(date.plusDays(1)));
}
示例12: fromStreamToString
import com.google.api.client.util.Charsets; //导入依赖的package包/类
/**
* Reads an input stream line by line and converts it into String.
* @param inputStream
* @throws IOException
*/
public static String fromStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
String line;
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
return stringBuilder.toString();
}
示例13: fromStreamToString
import com.google.api.client.util.Charsets; //导入依赖的package包/类
/**
* Reads an input stream line by line and converts it into String.
* @param inputStream
* @throws java.io.IOException
*/
public static String fromStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
String line;
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
return stringBuilder.toString();
}
示例14: testEmptyProtoException
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testEmptyProtoException() {
Status statusProto = Status.newBuilder().build();
DatastoreException exception =
RemoteRpc.makeException(
"url",
METHOD_NAME,
new ByteArrayInputStream(statusProto.toByteArray()),
"application/x-protobuf",
Charsets.UTF_8,
new RuntimeException(),
404);
assertEquals(Code.INTERNAL, exception.getCode());
assertEquals(
"Unexpected OK error code with HTTP status code of 404. Message: .",
exception.getMessage());
assertEquals(METHOD_NAME, exception.getMethodName());
}
示例15: testEmptyProtoExceptionUnauthenticated
import com.google.api.client.util.Charsets; //导入依赖的package包/类
@Test
public void testEmptyProtoExceptionUnauthenticated() {
Status statusProto = Status.newBuilder().build();
DatastoreException exception =
RemoteRpc.makeException(
"url",
METHOD_NAME,
new ByteArrayInputStream(statusProto.toByteArray()),
"application/x-protobuf",
Charsets.UTF_8,
new RuntimeException(),
401);
assertEquals(Code.UNAUTHENTICATED, exception.getCode());
assertEquals("Unauthenticated.", exception.getMessage());
assertEquals(METHOD_NAME, exception.getMethodName());
}