Java 类java.nio.charset.Charset 实例源码

项目:safecharge-java    文件:SafechargeRequestExecutor.java   
/**
 * Sends a POST request to the service at {@code serviceUrl} with a payload of {@code request}.
 * The request type is determined by the {@code headers} param.
 *
 * @param request    A {@link String} representation of a request object. Can be JSON object, form data, etc...
 * @param serviceUrl The service URL to sent the request to
 * @param headers    An array of {@link Header} objects, used to determine the request type
 * @return {@link String} response from the service (representing JSON object)
 * @throws IOException if the connection is interrupted or the response is unparsable
 */
public String executeRequest(String request, String serviceUrl, Header[] headers) throws IOException {
    HttpPost httpPost = new HttpPost(serviceUrl);
    httpPost.setHeaders(headers);
    httpPost.setEntity(new StringEntity(request, Charset.forName("UTF-8")));

    if (logger.isDebugEnabled()) {
        logger.debug("Sent " + request);
    }

    HttpResponse response = httpClient.execute(httpPost);

    String responseJSON = EntityUtils.toString(response.getEntity(), UTF8_CHARSET);
    if (logger.isDebugEnabled()) {
        logger.debug("Received " + responseJSON);
    }
    return responseJSON;
}
项目:lams    文件:IOUtils.java   
/**
 * Writes the <code>toString()</code> value of each item in a collection to
 * an <code>OutputStream</code> line by line, using the specified character
 * encoding and the specified line ending.
 *
 * @param lines  the lines to write, null entries produce blank lines
 * @param lineEnding  the line separator to use, null is system default
 * @param output  the <code>OutputStream</code> to write to, not null, not closed
 * @param encoding  the encoding to use, null means platform default
 * @throws NullPointerException if the output is null
 * @throws IOException if an I/O error occurs
 * @since 2.3
 */
public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
        throws IOException {
    if (lines == null) {
        return;
    }
    if (lineEnding == null) {
        lineEnding = LINE_SEPARATOR;
    }
    Charset cs = Charsets.toCharset(encoding);
    for (Object line : lines) {
        if (line != null) {
            output.write(line.toString().getBytes(cs));
        }
        output.write(lineEnding.getBytes(cs));
    }
}
项目:springboot-shiro-cas-mybatis    文件:ShibbolethCompatiblePersistentIdGenerator.java   
@Override
public String generate(final Principal principal, final Service service) {
    try {
        final MessageDigest md = MessageDigest.getInstance("SHA");
        final Charset charset = Charset.defaultCharset();
        md.update(service.getId().getBytes(charset));
        md.update(CONST_SEPARATOR);
        md.update(principal.getId().getBytes(charset));
        md.update(CONST_SEPARATOR);

        final String result = CompressionUtils.encodeBase64(md.digest(convertSaltToByteArray()));
        return result.replaceAll(System.getProperty("line.separator"), "");
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
项目:incubator-netbeans    文件:MessagesHandlerTest.java   
public void testMessageRepeat() throws Exception {
    clearWorkDir();
    System.setProperty("netbeans.user", getWorkDirPath());
    File logs = new File(getWorkDir(), "logs");
    logs.mkdirs();

    MessagesHandler mh = new MessagesHandler(logs, 2, 10000);

    mh.publish(new LogRecord(Level.INFO, "Hi"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.flush();
    File log = logs.listFiles()[0];
    List<String> allLines = Files.readAllLines(log.toPath(), Charset.defaultCharset());
    assertTrue(allLines.get(allLines.size() - 1), allLines.get(allLines.size() - 1).endsWith(MessagesHandler.getRepeatingMessage(1, 1)));
    assertTrue(allLines.get(allLines.size() - 2), allLines.get(allLines.size() - 2).endsWith("Hello"));
    assertTrue(allLines.get(allLines.size() - 3), allLines.get(allLines.size() - 3).endsWith("Hi"));

    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.publish(new LogRecord(Level.INFO, "Hello"));
    mh.flush();
    allLines = Files.readAllLines(log.toPath(), Charset.defaultCharset());
    assertTrue(allLines.get(allLines.size() - 1), allLines.get(allLines.size() - 1).endsWith(MessagesHandler.getRepeatingMessage(2, 2)));
}
项目:hadoop    文件:ApplicationCLI.java   
/**
 * Lists the containers matching the given application attempts
 * 
 * @param appAttemptId
 * @throws YarnException
 * @throws IOException
 */
private void listContainers(String appAttemptId) throws YarnException,
    IOException {
  PrintWriter writer = new PrintWriter(
      new OutputStreamWriter(sysout, Charset.forName("UTF-8")));

  List<ContainerReport> appsReport = client
      .getContainers(ConverterUtils.toApplicationAttemptId(appAttemptId));
  writer.println("Total number of containers " + ":" + appsReport.size());
  writer.printf(CONTAINER_PATTERN, "Container-Id", "Start Time",
      "Finish Time", "State", "Host", "Node Http Address", "LOG-URL");
  for (ContainerReport containerReport : appsReport) {
    writer.printf(
        CONTAINER_PATTERN,
        containerReport.getContainerId(),
        Times.format(containerReport.getCreationTime()),
        Times.format(containerReport.getFinishTime()),      
        containerReport.getContainerState(), containerReport
            .getAssignedNode(), containerReport.getNodeHttpAddress() == null
                ? "N/A" : containerReport.getNodeHttpAddress(),
        containerReport.getLogUrl());
  }
  writer.flush();
}
项目:springboot-shiro-cas-mybatis    文件:EncryptedMapDecorator.java   
/**
 * Decrypt the value.
 *
 * @param value the value
 * @param hashedKey the hashed key
 * @return the string
 */
protected String decrypt(final String value, final String hashedKey) {
    if (value == null) {
        return null;
    }

    try {
        final Cipher cipher = getCipherObject();
        final byte[] ivCiphertext = CompressionUtils.decodeBase64ToByteArray(value);
        final int ivSize = byte2int(Arrays.copyOfRange(ivCiphertext, 0, INTEGER_LEN));
        final byte[] ivValue = Arrays.copyOfRange(ivCiphertext, INTEGER_LEN, (INTEGER_LEN + ivSize));
        final byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, INTEGER_LEN + ivSize, ivCiphertext.length);
        final IvParameterSpec ivSpec = new IvParameterSpec(ivValue);

        cipher.init(Cipher.DECRYPT_MODE, this.key, ivSpec);

        final byte[] plaintext = cipher.doFinal(ciphertext);

        return new String(plaintext, Charset.defaultCharset());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
项目:openjdk-jdk10    文件:HttpInputStreamTest.java   
/**
 * Examine the response headers to figure out the charset used to
 * encode the body content.
 * If the content type is not textual, returns an empty Optional.
 * Otherwise, returns the body content's charset, defaulting to
 * ISO-8859-1 if none is explicitly specified.
 * @param headers The response headers.
 * @return The charset to use for decoding the response body, if
 *         the response body content is text/...
 */
public static Optional<Charset> getCharset(HttpHeaders headers) {
    Optional<String> contentType = headers.firstValue("Content-Type");
    Optional<Charset> charset = Optional.empty();
    if (contentType.isPresent()) {
        final String[] values = contentType.get().split(";");
        if (values[0].startsWith("text/")) {
            charset = Optional.of(Stream.of(values)
                .map(x -> x.toLowerCase(Locale.ROOT))
                .map(String::trim)
                .filter(x -> x.startsWith("charset="))
                .map(x -> x.substring("charset=".length()))
                .findFirst()
                .orElse("ISO-8859-1"))
                .map(Charset::forName);
        }
    }
    return charset;
}
项目:cornerstone    文件:LinuxInfoUtil.java   
public static Map<String,String> getMemInfo() throws IOException {

        Map<String,String> rtn = new HashMap<>();
        Path meminfoPath = Paths.get("/proc/meminfo");
        if (Files.exists(meminfoPath)) {
            List<String> lines = Files.readAllLines(meminfoPath, Charset.defaultCharset());
            for(String line :lines){
                String[] parts = line.split(":");
                if(parts.length>1) {
                    rtn.put(parts[0], parts[1].trim());
                }

            }


        }
        return rtn;
    }
项目:lighthouse    文件:EntityUtils.java   
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity,
 * or if the entity provided charset is invalid or not available.
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
 * this instance of the Java virtual machine and no defaultCharset is provided.
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    Args.notNull(entity, "Entity");
    ru.radiomayak.http.entity.ContentType contentType = null;
    try {
        contentType = ru.radiomayak.http.entity.ContentType.get(entity);
    } catch (final UnsupportedCharsetException ex) {
        if (defaultCharset == null) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
    }
    if (contentType != null) {
        if (contentType.getCharset() == null) {
            contentType = contentType.withCharset(defaultCharset);
        }
    } else {
        contentType = ru.radiomayak.http.entity.ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
    }
    return toString(entity, contentType);
}
项目:talchain    文件:RLPXTest.java   
@Test(expected = Exception.class)// failure on MDC
public void test5() {

    byte[] id = HashUtil.sha3("+++".getBytes(Charset.forName("UTF-8")));
    ECKey key = ECKey.fromPrivate(BigInteger.TEN);

    Message findNode = FindNodeMessage.create(id, key);
    logger.info("{}", findNode);

    byte[] wire = findNode.getPacket();
    wire[64]++;

    FindNodeMessage findNode2 = (FindNodeMessage) Message.decode(wire);
    logger.info("{}", findNode2);

    assertEquals(findNode.toString(), findNode2.toString());
}
项目:OpenJSharp    文件:Source.java   
private static char[] byteToCharArray(final byte[] bytes) {
    Charset cs = StandardCharsets.UTF_8;
    int start = 0;
    // BOM detection.
    if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
        start = 2;
        cs = StandardCharsets.UTF_16BE;
    } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
        start = 2;
        cs = StandardCharsets.UTF_16LE;
    } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
        start = 3;
        cs = StandardCharsets.UTF_8;
    } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) {
        start = 4;
        cs = Charset.forName("UTF-32LE");
    } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
        start = 4;
        cs = Charset.forName("UTF-32BE");
    }

    return new String(bytes, start, bytes.length - start, cs).toCharArray();
}
项目:log4j2-jsonevent-layout    文件:JSONLog4j2LayoutTest.java   
/**
 * Test filled multi line event.
 */
@Test
public void testFilledMultiLineWithUserFieldsAndContextMapAsRootEvent(){
    UserField[] fields = new UserField[2];
    fields[0] = new UserField("A1","B1");
    fields[1] = new UserField("A2","B2");
    JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,true,fields,Charset.forName("UTF-8"));
    DummyFilledLogEvent event = new DummyFilledLogEvent(LONG_STRING);
    event.getContextMap().put("CT1", "VALUE");
    event.getContextMap().put("CT2", "VALUE");
    System.out.println(layout.toSerializable(event));
}
项目:jdk8u-jdk    文件:Exceptions.java   
private static void getBytes() {
    System.out.println("getChars.(int srcBegin, int srcEnd, char dst[], "
                       + " int dstBegin");
    tryCatch("  1, 2, null, 1", NullPointerException.class, new Runnable() {
            public void run() {
                "foo".getBytes(1, 2, null, 1);
            }});

    System.out.println("getBytes.(String charsetName)"
                       + " throws UnsupportedEncodingException");
    tryCatch("  null", NullPointerException.class, new Runnable() {
            public void run() {
                try {
                    "foo".getBytes((String)null);
                } catch (UnsupportedEncodingException x) {
                    throw new RuntimeException(x);
                }
            }});

    System.out.println("getBytes.(Charset charset)");
    tryCatch("  null", NullPointerException.class, new Runnable() {
            public void run() {
                "foo".getBytes((Charset)null);
            }});
}
项目:rapidminer    文件:SimpleExampleSource.java   
public static ExampleSet createExampleSet(File file, boolean firstRowAsColumnNames, double sampleRatio, int maxLines,
        String separatorRegExpr, char[] comments, int dataRowType, boolean useQuotes, boolean trimLines,
        boolean skipErrorLines, char decimalPointCharacter, Charset encoding, String labelName, int labelColumn,
        String idName, int idColumn, String weightName, int weightColumn) throws IOException, UserError,
        IndexOutOfBoundsException {
    // create attribute data sources and guess value types (performs a data scan)
    AttributeDataSourceCreator adsCreator = new AttributeDataSourceCreator();

    adsCreator.loadData(file, comments, separatorRegExpr, decimalPointCharacter, useQuotes, '"', '\\', trimLines,
            firstRowAsColumnNames, -1, skipErrorLines, encoding, null);

    List<AttributeDataSource> attributeDataSources = adsCreator.getAttributeDataSources();

    // set special attributes
    resetAttributeType(attributeDataSources, labelName, labelColumn, Attributes.LABEL_NAME);
    resetAttributeType(attributeDataSources, idName, idColumn, Attributes.ID_NAME);
    resetAttributeType(attributeDataSources, weightName, weightColumn, Attributes.WEIGHT_NAME);

    // read data
    FileDataRowReader reader = new FileDataRowReader(new DataRowFactory(dataRowType, decimalPointCharacter),
            attributeDataSources, sampleRatio, maxLines, separatorRegExpr, comments, useQuotes, '"', '\\', trimLines,
            skipErrorLines, encoding, RandomGenerator.getGlobalRandomGenerator());
    if (firstRowAsColumnNames) {
        reader.skipLine();
    }

    AttributeSet attributeSet = new AttributeSet(new AttributeDataSources(attributeDataSources, file, encoding));

    // create table and example set
    ExampleTable table = new MemoryExampleTable(attributeSet.getAllAttributes(), reader);
    ExampleSet result = table.createExampleSet(attributeSet);

    return result;
}
项目:sample-zuul-filters    文件:ModifyResponseDataStreamFilter.java   
public Object run() {
    try {
        RequestContext context = getCurrentContext();
        InputStream stream = context.getResponseDataStream();
        String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
        body = "Modified via setResponseDataStream(): " + body;
        context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8")));
    }
    catch (IOException e) {
        rethrowRuntimeException(e);
    }
    return null;
}
项目:boohee_v5.6    文件:RequestBody.java   
public static RequestBody create(MediaType contentType, String content) {
    Charset charset = Util.UTF_8;
    if (contentType != null) {
        charset = contentType.charset();
        if (charset == null) {
            charset = Util.UTF_8;
            contentType = MediaType.parse(contentType + "; charset=utf-8");
        }
    }
    return create(contentType, content.getBytes(charset));
}
项目:jdk8u-jdk    文件:MimeTypesFileTypeDetector.java   
/**
 * Parse the mime types file, and store the type-extension mappings into
 * mimeTypeMap. The mime types file is not loaded until the first probe
 * to achieve the lazy initialization. It adopts double-checked locking
 * optimization to reduce the locking overhead.
 */
private void loadMimeTypes() {
    if (!loaded) {
        synchronized (this) {
            if (!loaded) {
                List<String> lines = AccessController.doPrivileged(
                    new PrivilegedAction<List<String>>() {
                        @Override
                        public List<String> run() {
                            try {
                                return Files.readAllLines(mimeTypesFile,
                                                          Charset.defaultCharset());
                            } catch (IOException ignore) {
                                return Collections.emptyList();
                            }
                        }
                    });

                mimeTypeMap = new HashMap<>(lines.size());
                String entry = "";
                for (String line : lines) {
                    entry += line;
                    if (entry.endsWith("\\")) {
                        entry = entry.substring(0, entry.length() - 1);
                        continue;
                    }
                    parseMimeEntry(entry);
                    entry = "";
                }
                if (!entry.isEmpty()) {
                    parseMimeEntry(entry);
                }
                loaded = true;
            }
        }
    }
}
项目:stroom-query    文件:StreamUtil.java   
/**
 * Takes a string and writes it to a file.
 */
public static void stringToFile(final String string, final Path path, final Charset charset) {
    try {
        if (Files.isRegularFile(path)) {
            Files.delete(path);
        }
        Files.createDirectories(path.getParent());

        try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
            writer.write(string);
        }
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
}
项目:lams    文件:BlockingWriterSenderImpl.java   
@Override
public void send(final String data, final Charset charset, final IoCallback callback) {
    if (inCall) {
        queue(new ByteBuffer[]{ByteBuffer.wrap(data.getBytes(charset))}, callback);
        return;
    }
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
    } else {
        invokeOnComplete(callback);
    }
}
项目:Open-DM    文件:ConfigurationHelper.java   
private static void testUTF8Support() {
    // Test for UTF8 Support. Add warning to the logs if it is not supported
    try {
        if (!Charset.isSupported(HCPMoverConstants.SUPPORTED_CHARSET)) {
            LOG.log(Level.WARNING, HCPMoverConstants.SUPPORTED_CHARSET
                    + " Encoder not available.  Please consult the Java internationalization documentation.");
        }
    } catch (IllegalCharsetNameException e) {
        LOG.log(Level.WARNING,
                HCPMoverConstants.SUPPORTED_CHARSET
                        + " Encoder not available.  Please consult the Java internationalization documentation.",
                e);
    }
}
项目:incubator-netbeans    文件:PropertiesEditorSupport.java   
/**
 * Reads the file from the stream, filter the guarded section
 * comments, and mark the sections in the editor. Overrides superclass method. 
 * @param document the document to read into
 * @param inputStream the open stream to read from
 * @param editorKit the associated editor kit
 * @throws <code>IOException</code> if there was a problem reading the file
 * @throws <code>BadLocationException</code> should not normally be thrown
 * @see #saveFromKitToStream
 */
@Override
protected void loadFromStreamToKit(StyledDocument document, InputStream inputStream, EditorKit editorKit) throws IOException, BadLocationException {
    final Charset c = getCharset();
    final Reader reader = new BufferedReader(new InputStreamReader(inputStream, c));

    try {
        editorKit.read(reader, document, 0);
    } finally {
        reader.close();
    }
}
项目:lams    文件:URLEncodedUtils.java   
/**
 * Emcode/escape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * 
 * @param content the portion to decode
 * @param charset the charset to use
 * @param blankAsPlus if {@code true}, then convert space to '+' (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urlencode(
        final String content,
        final Charset charset,
        final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}
项目:openjdk-jdk10    文件:DataFlavorUtil.java   
/**
 * Converts an arbitrary text encoding to its canonical name.
 */
public static String canonicalName(String encoding) {
    if (encoding == null) {
        return null;
    }
    try {
        return Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException icne) {
        return encoding;
    } catch (UnsupportedCharsetException uce) {
        return encoding;
    }
}
项目:apache-tomcat-7.0.73-with-comment    文件:InternalAprInputBuffer.java   
private void skipLine(int start) throws IOException {
    boolean eol = false;
    int lastRealByte = start;
    if (pos - 1 > start) {
        lastRealByte = pos - 1;
    }

    while (!eol) {

        // Read new bytes if needed
        if (pos >= lastValid) {
            if (!fill())
                throw new EOFException(sm.getString("iib.eof.error"));
        }

        if (buf[pos] == Constants.CR) {
            // Skip
        } else if (buf[pos] == Constants.LF) {
            eol = true;
        } else {
            lastRealByte = pos;
        }
        pos++;
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("iib.invalidheader", new String(buf, start,
                lastRealByte - start + 1, Charset.forName("ISO-8859-1"))));
    }
}
项目:gitplex-mit    文件:ContentDetector.java   
/**
 * Get text from specified content bytes, optionally with help of file name.
 * 
 * @param contentBytes
 *          content bytes to construct text from
 * @param fileName
 *          file name to help deciding if supplied content bytes represents text
 * @return
 *          text representation of content bytes, or <tt>null</tt> if content 
 *          can not be converted to text
 */
@Nullable
public static String convertToText(byte[] contentBytes, @Nullable String fileName) {
    if (!isBinary(contentBytes, fileName)) {
        Charset charset = detectCharset(contentBytes);
        if (charset != null)
            return new String(contentBytes, charset);
        else
            return new String(contentBytes);
    } else {
        return null;
    }
}
项目:GitHub    文件:ResponseBody.java   
/**
 * Returns a new response body that transmits {@code content}. If {@code contentType} is non-null
 * and lacks a charset, this will use UTF-8.
 */
public static ResponseBody create(MediaType contentType, String content) {
  Charset charset = UTF_8;
  if (contentType != null) {
    charset = contentType.charset();
    if (charset == null) {
      charset = UTF_8;
      contentType = MediaType.parse(contentType + "; charset=utf-8");
    }
  }
  Buffer buffer = new Buffer().writeString(content, charset);
  return create(contentType, buffer.size(), buffer);
}
项目:mpd-2017-i41d    文件:HttpServer.java   
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, IOException {
    Charset utf8 = Charset.forName("utf-8");
    resp.setContentType(String.format("text/html; charset=%s", utf8.name()));

    String respBody = handler.apply(req);

    byte[] respBodyBytes = respBody.getBytes(utf8);
    resp.setStatus(200);
    resp.setContentLength(respBodyBytes.length);
    OutputStream os = resp.getOutputStream();
    os.write(respBodyBytes);
    os.close();
}
项目:OpenJSharp    文件:IndentingWriter.java   
/**
 * Check if encode can handle the chars in this string.
 *
 */
protected boolean canEncode(String s) {
    final CharsetEncoder encoder =
        Charset.forName(System.getProperty("file.encoding")).newEncoder();
    char[] chars = s.toCharArray();
    for (int i=0; i<chars.length; i++) {
        if(!encoder.canEncode(chars[i])) {
            return false;
        }
    }
    return true;
}
项目:NeiHanDuanZiTV    文件:RequestInterceptor.java   
/**
 * 解析服务器响应的内容
 *
 * @param responseBody
 * @param encoding
 * @param clone
 * @return
 */
private String parseContent(ResponseBody responseBody, String encoding, Buffer clone) {
    Charset charset = Charset.forName("UTF-8");
    MediaType contentType = responseBody.contentType();
    if (contentType != null) {
        charset = contentType.charset(charset);
    }
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {//content使用gzip压缩
        return ZipHelper.decompressForGzip(clone.readByteArray(), convertCharset(charset));//解压
    } else if (encoding != null && encoding.equalsIgnoreCase("zlib")) {//content使用zlib压缩
        return ZipHelper.decompressToStringForZlib(clone.readByteArray(), convertCharset(charset));//解压
    } else {//content没有被压缩
        return clone.readString(charset);
    }
}
项目:convertigo-engine    文件:XMLUtils.java   
public static Charset getEncoding(byte[] bytes, Charset charset) {
    try {
        String encoding = new String(bytes, 0, Math.min(bytes.length, 128), "ASCII").replaceFirst("[\\d\\D]*encoding=\"(.*?)\"[\\d\\D]*", "$1");
        charset = Charset.forName(encoding);
    } catch (Exception e) { }

    return charset == null ? Charset.defaultCharset() : charset;
}
项目:ibm-cos-sdk-java    文件:DateUtilsTest.java   
@Test
public void testNumericNoQuote() {
    StructuredJsonGenerator jw = new SdkJsonGenerator(new JsonFactory(), null);
    jw.writeStartObject();
    jw.writeFieldName("foo").writeValue(new Date());
    jw.writeEndObject();
    String s = new String(jw.getBytes(), Charset.forName("UTF-8"));
    // Something like: {"foo":1408378076.135}.
    // Note prior to the changes, it was {"foo":1408414571}
    // (with no decimal point nor places.)
    System.out.println(s);
    final String prefix = "{\"foo\":";
    assertTrue(s, s.startsWith(prefix));
    final int startPos = prefix.length();
    // verify no starting quote for the value
    assertFalse(s, s.startsWith("{\"foo\":\""));
    assertTrue(s, s.endsWith("}"));
    // Not: {"foo":"1408378076.135"}.
    // verify no ending quote for the value
    assertFalse(s, s.endsWith("\"}"));
    final int endPos = s.indexOf("}");
    final int dotPos = s.length()-5;
    assertTrue(s, s.charAt(dotPos) == '.');
    // verify all numeric before '.'
    char[] a = s.toCharArray();
    for (int i=startPos; i < dotPos; i++)
        assertTrue(a[i] <= '9' && a[i] >= '0' );
    int j=0;
    // verify all numeric after '.'
    for (int i=dotPos+1; i < endPos; i++) {
        assertTrue(a[i] <= '9' && a[i] >= '0' );
        j++;
    }
    // verify decimal precision of exactly 3
    assertTrue(j == 3);
}
项目:phone-simulator    文件:SmsSignalInfoImpl.java   
public SmsSignalInfoImpl(SmsTpdu tpdu, Charset gsm8Charset) throws MAPException {
    if (tpdu == null)
        throw new MAPException("SmsTpdu must not be null");

    this.setGsm8Charset(gsm8Charset);
    this.data = tpdu.encodeData();
}
项目:sonar-css-plugin    文件:CssCheckVerifier.java   
/**
 * See {@link CssCheckVerifier#issuesOnScssFile(CssCheck, File)}
 *
 * @param charset Charset of the file to test.
 */
public static CheckMessagesVerifier issuesOnScssFile(CssCheck check, File file, Charset charset) {
  if (check instanceof CharsetAwareVisitor) {
    ((CharsetAwareVisitor) check).setCharset(charset);
  }
  return CheckMessagesVerifier.verify(TreeCheckTest.getIssues(file.getAbsolutePath(), check, ScssParser.createParser(charset)));
}
项目:jdk8u-jdk    文件:Johab_OLD.java   
public Decoder(Charset cs) {
    super(cs,
          index1,
          index2,
          0x20,
          0xFE);
}
项目:mv-android-client    文件:WebPlayerActivity.java   
private Bootstrapper(Player player) {
    Context context = player.getContext();
    player.addJavascriptInterface(this, Bootstrapper.INTERFACE);

    mPlayer = player;
    mURIBuilder = Uri.fromFile(new File(context.getString(R.string.mv_project_index))).buildUpon();
    mPlayer.loadData(new String(Base64.decode(context.getString(R.string.webview_default_page), Base64.DEFAULT), Charset.forName("UTF-8")));
}
项目:CryptoKnight    文件:HashFactory.java   
public static String CommonHash(String p, String alg) throws NoSuchAlgorithmException
{
    MessageDigest md = MessageDigest.getInstance(alg);

    md.reset();

    md.update(p.getBytes(Charset.forName("UTF-8")));
    byte[] resBytes = md.digest();
    String res = new String(Hex.encodeHex(resBytes));

    return res;
}
项目:BibliotecaPS    文件:CharsetMapping.java   
/**
 * Constructs MysqlCharset object
 * 
 * @param charsetName
 *            MySQL charset name
 * @param mblen
 *            Max number of bytes per character
 * @param priority
 *            MysqlCharset with highest lever of this param will be used for Java encoding --> Mysql charsets conversion.
 * @param javaEncodings
 *            List of Java encodings corresponding to this MySQL charset; the first name in list is the default for mysql --> java data conversion
 */
public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEncodings) {
    this.charsetName = charsetName;
    this.mblen = mblen;
    this.priority = priority;

    for (int i = 0; i < javaEncodings.length; i++) {
        String encoding = javaEncodings[i];
        try {
            Charset cs = Charset.forName(encoding);
            addEncodingMapping(cs.name());

            Set<String> als = cs.aliases();
            Iterator<String> ali = als.iterator();
            while (ali.hasNext()) {
                addEncodingMapping(ali.next());
            }
        } catch (Exception e) {
            // if there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets
            if (mblen == 1) {
                addEncodingMapping(encoding);
            }
        }
    }

    if (this.javaEncodingsUc.size() == 0) {
        if (mblen > 1) {
            addEncodingMapping("UTF-8");
        } else {
            addEncodingMapping("Cp1252");
        }
    }
}
项目:openjdk-jdk10    文件:IBM942_OLD.java   
public Encoder(Charset cs) {
    super(cs);
    super.mask1 = 0xFFE0;
    super.mask2 = 0x001F;
    super.shift = 5;
    super.index1 = index1;
    super.index2 = index2;
    super.index2a = index2a;
}
项目:incubator-netbeans    文件:InputReaders.java   
/**
 * Returns the input reader for the given file. To convert read bytes
 * to characters specified charset is used.
 * <p>
 * Returned reader will never call reset on {@link InputProcessor} while
 * reading.
 * <p>
 * Returned reader is <i>not thread safe</i> so it can't be used in
 * multiple instances of {@link InputReaderTask}.
 *
 * @param file file to read from
 * @param charset bytes to characters conversion charset
 * @return input reader for the given file
 */
@NonNull
public static InputReader forFile(@NonNull File file, @NonNull Charset charset) {
    Parameters.notNull("file", file);
    Parameters.notNull("charset", charset);

    final FileInput fileInput = new FileInput(file, charset);
    return forFileInputProvider(new FileInput.Provider() {

        @Override
        public FileInput getFileInput() {
            return fileInput;
        }
    });
}
项目:ditb    文件:SaslClientHandler.java   
/**
 * Get the read status
 *
 * @param inStream to read
 * @throws org.apache.hadoop.ipc.RemoteException if status was not success
 */
private static void readStatus(ByteBuf inStream) throws RemoteException {
  int status = inStream.readInt(); // read status
  if (status != SaslStatus.SUCCESS.state) {
    throw new RemoteException(inStream.toString(Charset.forName("UTF-8")),
        inStream.toString(Charset.forName("UTF-8")));
  }
}