use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class PropertiesFileType method getCharset.
@Override
public String getCharset(@NotNull VirtualFile file, @NotNull final byte[] content) {
Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessed = LoadTextUtil.guessFromContent(file, content, content.length);
Charset charset = guessed == null || guessed.first == null ? EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(file) : guessed.first;
if (charset == null) {
charset = CharsetToolkit.getDefaultSystemCharset();
}
if (EncodingRegistry.getInstance().isNative2Ascii(file)) {
charset = Native2AsciiCharset.wrap(charset);
}
return charset.name();
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class XmlLikeFileType method extractCharsetFromFileContent.
@Override
public Charset extractCharsetFromFileContent(final Project project, @Nullable final VirtualFile file, @NotNull final CharSequence content) {
String name = XmlCharsetDetector.extractXmlEncodingFromProlog(content);
Charset charset = CharsetToolkit.forName(name);
return charset == null ? CharsetToolkit.UTF8_CHARSET : charset;
}
use of java.nio.charset.Charset in project jackson-databind by FasterXML.
the class JDKStringLikeTypesTest method testCharset.
public void testCharset() throws Exception {
Charset UTF8 = Charset.forName("UTF-8");
assertSame(UTF8, MAPPER.readValue(quote("UTF-8"), Charset.class));
}
use of java.nio.charset.Charset in project MVPArms by JessYanCoding.
the class RequestIntercept method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (//在请求服务器之前可以拿到request,做一些操作比如给request添加header,如果不做操作则返回参数中的request
mHandler != null)
request = mHandler.onHttpRequestBefore(chain, request);
Buffer requestbuffer = new Buffer();
if (request.body() != null) {
request.body().writeTo(requestbuffer);
} else {
Timber.tag("Request").w("request.body() == null");
}
//打印url信息
Timber.tag("Request").w("Sending Request %s on %n Params ---> %s%n Connection ---> %s%n Headers ---> %s", request.url(), request.body() != null ? parseParams(request.body(), requestbuffer) : "null", chain.connection(), request.headers());
long t1 = System.nanoTime();
Response originalResponse = chain.proceed(request);
long t2 = System.nanoTime();
//打印响应时间
Timber.tag("Response").w("Received response in %.1fms%n%s", (t2 - t1) / 1e6d, originalResponse.headers());
//读取服务器返回的结果
ResponseBody responseBody = originalResponse.body();
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
//获取content的压缩类型
String encoding = originalResponse.headers().get("Content-Encoding");
Buffer clone = buffer.clone();
String bodyString;
//解析response content
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
//content使用gzip压缩
//解压
bodyString = ZipHelper.decompressForGzip(clone.readByteArray());
} else if (encoding != null && encoding.equalsIgnoreCase("zlib")) {
//content使用zlib压缩
//解压
bodyString = ZipHelper.decompressToStringForZlib(clone.readByteArray());
} else {
//content没有被压缩
Charset charset = Charset.forName("UTF-8");
MediaType contentType = responseBody.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
bodyString = clone.readString(charset);
}
Timber.tag("Result").w(jsonFormat(bodyString));
if (//这里可以比客户端提前一步拿到服务器返回的结果,可以做一些操作,比如token超时,重新获取
mHandler != null)
return mHandler.onHttpResultResponse(bodyString, chain, originalResponse);
return originalResponse;
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class ModuleChunk method getChunkSpecificCompileOptions.
public String getChunkSpecificCompileOptions() {
final StringBuilder options = new StringBuilder();
final Charset encoding = CompilerEncodingService.getInstance(getProject()).getPreferredModuleEncoding(myMainModule);
if (encoding != null) {
appendOption(options, "-encoding", encoding.name());
}
final String languageLevel = getLanguageLevelOption(ApplicationManager.getApplication().runReadAction(new Computable<LanguageLevel>() {
@Override
public LanguageLevel compute() {
return EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(myMainModule);
}
}));
appendOption(options, "-source", languageLevel);
String bytecodeTarget = CompilerConfiguration.getInstance(getProject()).getBytecodeTargetLevel(myMainModule);
if (StringUtil.isEmpty(bytecodeTarget)) {
// according to IDEA rule: if not specified explicitly, set target to be the same as source language level
bytecodeTarget = languageLevel;
}
appendOption(options, "-target", bytecodeTarget);
return options.toString();
}
Aggregations