Search in sources :

Example 46 with Charset

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();
}
Also used : Native2AsciiCharset(com.intellij.lang.properties.charset.Native2AsciiCharset) Charset(java.nio.charset.Charset)

Example 47 with Charset

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;
}
Also used : Charset(java.nio.charset.Charset)

Example 48 with 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));
}
Also used : Charset(java.nio.charset.Charset)

Example 49 with Charset

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;
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) Request(okhttp3.Request) Charset(java.nio.charset.Charset) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) BufferedSource(okio.BufferedSource)

Example 50 with Charset

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();
}
Also used : Charset(java.nio.charset.Charset) Computable(com.intellij.openapi.util.Computable)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48