Example usage for java.nio.charset StandardCharsets UTF_8

List of usage examples for java.nio.charset StandardCharsets UTF_8

Introduction

In this page you can find the example usage for java.nio.charset StandardCharsets UTF_8.

Prototype

Charset UTF_8

To view the source code for java.nio.charset StandardCharsets UTF_8.

Click Source Link

Document

Eight-bit UCS Transformation Format.

Usage

From source file:Main.java

public static Document toXmlDoc(String xml) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {/*ww w.  j a  v a2 s.  c om*/
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        return builder.parse(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateSketches() throws Exception {
    Path path = FileSystems.getDefault().getPath("array_of_doubles_sketch_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = new Random();
        int key = 0;
        for (int i = 0; i < 20; i++) {
            ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder()
                    .setNominalEntries(1024).build();
            sketch.update(key++, new double[] { 1 });
            sketch.update(key++, new double[] { 1 });
            out.write("2015010101");
            out.write('\t');
            out.write("product_" + (rand.nextInt(10) + 1));
            out.write('\t');
            out.write(Base64.encodeBase64String(sketch.compact().toByteArray()));
            out.newLine();//from   ww  w. j  a v  a2s  . c om
        }
    }
}

From source file:com.thoughtworks.go.server.newsecurity.utils.BasicAuthHeaderExtractor.java

public static UsernamePassword extractBasicAuthenticationCredentials(String authorizationHeader) {
    if (isBlank(authorizationHeader)) {
        return null;
    }//from  w ww. j a v  a 2 s  .c om

    final Matcher matcher = BASIC_AUTH_EXTRACTOR_PATTERN.matcher(authorizationHeader);
    if (matcher.matches()) {
        final String encodedCredentials = matcher.group(1);
        final byte[] decode = Base64.getDecoder().decode(encodedCredentials);
        String decodedCredentials = new String(decode, StandardCharsets.UTF_8);

        final int indexOfSeparator = decodedCredentials.indexOf(':');
        if (indexOfSeparator == -1) {
            throw new BadCredentialsException("Invalid basic authentication credentials specified in request.");
        }

        final String username = decodedCredentials.substring(0, indexOfSeparator);
        final String password = decodedCredentials.substring(indexOfSeparator + 1);

        return new UsernamePassword(username, password);
    }

    return null;
}

From source file:co.rsk.net.discovery.message.FindNodePeerMessage.java

public static FindNodePeerMessage create(byte[] nodeId, String check, ECKey privKey) {

    /* RLP Encode data */
    byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8));
    byte[] rlpNodeId = RLP.encodeElement(nodeId);

    byte[] type = new byte[] { (byte) DiscoveryMessageType.FIND_NODE.getTypeValue() };
    byte[] data = RLP.encodeList(rlpNodeId, rlpCheck);

    FindNodePeerMessage message = new FindNodePeerMessage();
    message.encode(type, data, privKey);

    message.messageId = check;/*from  w  ww . j a v  a  2s  .  co m*/
    message.nodeId = nodeId;

    return message;
}

From source file:name.martingeisse.common.util.HmacUtil.java

/**
 * Generates a HMAC, assuming UTF-8 encoding for all strings.
 * // w ww  .j  a va 2s  .c  om
 * @param payload the payload data
 * @param secret the secret to sign the HMAC
 * @param algorithm the HMAC algorithm to use
 * @return the HMAC
 */
public static byte[] generateHmac(String payload, String secret, String algorithm) {
    Charset utf8 = StandardCharsets.UTF_8;
    return generateHmac(payload.getBytes(utf8), secret.getBytes(utf8), algorithm);
}

From source file:net.mentalarray.doozie.PigSupport.PigInBlanket.java

private static BufferedReader stringToBufferedReader(String input) {
    return new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))));
}

From source file:costumetrade.common.wenqian.util.WQSignUtils.java

/**
 * ???//  w w  w  .j  av a  2  s . c om
 * 
 * @return
 */
public static String getToken() {

    List<BasicNameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("client_id", WQAPIConstants.CLIENT_ID));
    nvps.add(new BasicNameValuePair("client_secret", WQAPIConstants.CLIENT_SECRET));
    nvps.add(new BasicNameValuePair("grant_type", WQAPIConstants.GRANT_TYPE));
    nvps.add(new BasicNameValuePair("scope", WQAPIConstants.SCOPE));

    String response = HttpClientUtils.doPost(WQAPIConstants.token_api,
            new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
    JSONObject responseJSON = JSONObject.parseObject(response);
    if (responseJSON.containsKey("access_token")) {
        return responseJSON.get("access_token").toString();
    }
    throw new RuntimeException("???");
}

From source file:moacscoper.Parser.java

private List<CSVRecord> parse(String path) throws IOException {
    return CSVParser.parse(new File(path), StandardCharsets.UTF_8, CSVFormat.TDF).getRecords();
}

From source file:com.cognifide.aet.executor.xmlparser.xml.models.ModelConverterUtils.java

static List<ExtendedUrl> extendUrlsList(List<Url> urls) throws ParseException, UnsupportedEncodingException {
    List<ExtendedUrl> extendedUrls = Lists.newArrayList();
    List<ExtendedUrl> duplicatedUrls = Lists.newArrayList();
    Set<String> names = Sets.newHashSet();
    for (Url url : urls) {
        String urlName;/*from   w  w  w .  ja  v a  2 s  . c  o  m*/
        if (StringUtils.isBlank(url.getName())) {
            urlName = url.getHref().trim();
        } else {
            urlName = URLEncoder.encode(url.getName().trim(), StandardCharsets.UTF_8.displayName());
        }
        ExtendedUrl extendedUrl = new ExtendedUrl(url.getHref(), urlName, url.getDescription());
        if (!names.add(urlName)) {
            duplicatedUrls.add(extendedUrl);
        } else {
            extendedUrls.add(extendedUrl);
        }
    }
    if (!duplicatedUrls.isEmpty()) {
        StringBuilder builder = new StringBuilder("Duplicated urls:");
        for (ExtendedUrl url : duplicatedUrls) {
            builder.append(String.format("%n%s with name %s", url.getUrl(), url.getName()));
        }
        throw new ParseException(builder.toString());
    }
    return extendedUrls;
}

From source file:com.openshift.internal.util.URIUtils.java

public static Map<String, String> splitQuery(String q) {
    HashMap<String, String> params = new HashMap<String, String>();
    if (q != null) {
        try {// www  .  ja va  2  s .  co  m
            String decoded = URLDecoder.decode(q, StandardCharsets.UTF_8.toString());
            String[] split = decoded.split("&");
            for (String pair : split) {
                String[] keyValue = pair.split("=");
                if (keyValue.length >= 2) {
                    params.put(keyValue[0], keyValue[1]);
                }
            }
        } catch (UnsupportedEncodingException e) {
            LOG.error("Unable to decode " + q, e);
        }
    }
    return params;
}